Skip to content

Commit c6161a9

Browse files
committed
interfaceを個別のファイルに分割
1 parent a49e6fb commit c6161a9

File tree

15 files changed

+340
-0
lines changed

15 files changed

+340
-0
lines changed

src/client/Gateway.ts

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
import nodeFetch from 'node-fetch';
2+
import * as querystring from 'querystring';
3+
4+
export class Gateway {
5+
6+
/** ユーザーのアクセストークンです */
7+
private token = '';
8+
9+
/** Qiitaのホストです */
10+
private endpoint = 'https://qiita.com';
11+
12+
/** APIバージョンを示すロケーションです */
13+
private version = '/api/v2';
14+
15+
/**
16+
* @param options Optional params
17+
* @param options.url Rest API URL of the instance
18+
* @param options.streamingUrl Streaming API URL of the instance
19+
* @param options.token API token of the user
20+
*/
21+
constructor (options: { url?: string, streamingUrl?: string, token?: string }) {
22+
if (options) {
23+
this.url = options.url || '';
24+
this.streamingUrl = options.streamingUrl || '';
25+
26+
if (options.token) {
27+
this.token = options.token;
28+
}
29+
}
30+
}
31+
32+
/**
33+
* Fetch API wrapper function
34+
* @param url URL to request
35+
* @param options Fetch API options
36+
* @param parse Whether parse response before return
37+
* @return Parsed response object
38+
*/
39+
protected async request (url: string, options: { [key: string]: any } = {}, parse = true): Promise<any> {
40+
if ( !options.headers ) {
41+
options.headers = {};
42+
}
43+
44+
options.headers['Content-Type'] = 'application/json';
45+
46+
if ( !this.url ) {
47+
throw new MastodonURLResolveError('REST API URL has not been specified, Use Mastodon.setUrl to set your instance\'s URL');
48+
}
49+
50+
if ( this.token ) {
51+
options.headers.Authorization = `Bearer ${this.token}`;
52+
}
53+
54+
const response = typeof window === 'undefined'
55+
? await nodeFetch(url, options)
56+
: await fetch(url, options);
57+
58+
if ( !parse ) {
59+
return response;
60+
}
61+
62+
const data = await response.json();
63+
64+
if (response.ok) {
65+
return data;
66+
} else {
67+
switch (response.status) {
68+
case 401:
69+
throw new MastodonUnauthorizedError(data.error);
70+
case 404:
71+
throw new MastodonNotFoundError(data.error);
72+
case 429:
73+
throw new MastodonRatelimitError(data.error);
74+
default:
75+
throw new MastodonError('MastodonError', data.error || 'Unexpected error occurred');
76+
}
77+
}
78+
}
79+
80+
/**
81+
* HTTP GET
82+
* @param url URL to request
83+
* @param params Query strings
84+
* @param options Fetch API options
85+
* @param parse Whether parse response before return
86+
*/
87+
protected get <T> (url: string, params = {}, options = {}, parse = true): Promise<T> {
88+
return this.request(url + (Object.keys(params).length ? '?' + querystring.stringify(params) : ''), { method: 'GET', ...options }, parse);
89+
}
90+
91+
/**
92+
* HTTP POST
93+
* @param url URL to request
94+
* @param body Payload
95+
* @param options Fetch API options
96+
* @param parse Whether parse response before return
97+
*/
98+
protected post <T> (url: string, body = {}, options = {}, parse = true): Promise<T> {
99+
return this.request(url, { method: 'POST', body: JSON.stringify(body), ...options }, parse);
100+
}
101+
102+
/**
103+
* HTTP PUT
104+
* @param url URL to request
105+
* @param body Payload
106+
* @param options Fetch API options
107+
* @param parse Whether parse response before return
108+
*/
109+
protected put <T> (url: string, body = {}, options = {}, parse = true): Promise<T> {
110+
return this.request(url, { method: 'PUT', body: JSON.stringify(body), ...options }, parse);
111+
}
112+
113+
/**
114+
* HTTP DELETE
115+
* @param url URL to request
116+
* @param body Payload
117+
* @param options Fetch API options
118+
* @param parse Whether parse response before return
119+
*/
120+
protected delete <T> (url: string, body = {}, options = {}, parse = true): Promise<T> {
121+
return this.request(url, { method: 'DELETE', body: JSON.stringify(body), ...options }, parse);
122+
}
123+
124+
/**
125+
* HTTP PATCH
126+
* @param url URL to request
127+
* @param body Payload
128+
* @param options Fetch API options
129+
* @param parse Whether parse response before return
130+
*/
131+
protected patch <T> (url: string, body = {}, options = {}, parse = true): Promise<T> {
132+
return this.request(url, { method: 'PATCH', body: JSON.stringify(body), ...options }, parse);
133+
}
134+
}

src/entities/AuthenticatedUser.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { User } from './User';
2+
3+
export interface AuthenticatedUser extends User {
4+
/** 1ヶ月あたりにQiitaにアップロードできる画像の総容量 */
5+
image_monthly_upload_limit: number;
6+
/** その月にQiitaにアップロードできる画像の残りの容量 */
7+
image_monthly_upload_remaining: number;
8+
/** Qiita:Team専用モードに設定されているかどうか */
9+
team_only: boolean;
10+
}

src/entities/Comment.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { User } from './User';
2+
3+
export interface Comment {
4+
/** コメントの内容を表すMarkdown形式の文字列 */
5+
body: string;
6+
/** データが作成された日時 */
7+
created_at: string;
8+
/** コメントの一意なID */
9+
id: string;
10+
/** コメントの内容を表すHTML形式の文字列 */
11+
rendered_body: string;
12+
/** データが最後に更新された日時 */
13+
updated_at: string;
14+
/** Qiita上のユーザを表します。 */
15+
user: User;
16+
}

src/entities/ExpandedTemplate.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { Tagging } from './Tagging';
2+
3+
export interface ExpandedTemplate {
4+
/** 変数を展開した状態の本文 */
5+
expanded_body: string;
6+
/** 変数を展開した状態のタグ一覧 */
7+
expanded_tags: Tagging[];
8+
/** 変数を展開した状態のタイトル */
9+
expanded_title: string;
10+
}

src/entities/Group.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export interface Group {
2+
/** データが作成された日時 */
3+
created_at: string;
4+
/** グループの一意なIDを表します。 */
5+
id: number;
6+
/** グループに付けられた表示用の名前を表します。 */
7+
name: string;
8+
/** 非公開グループかどうかを表します。 */
9+
private: boolean;
10+
/** データが最後に更新された日時 */
11+
updated_at: string;
12+
/** グループのチーム上での一意な名前を表します。 */
13+
url_name: string;
14+
}

src/entities/Item.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { Group } from './Group';
2+
import { Tagging } from './Tagging';
3+
import { User } from './User';
4+
5+
export interface Item {
6+
/** HTML形式の本文 */
7+
rendered_body: string;
8+
/** Markdown形式の本文 */
9+
body: string;
10+
/** この投稿が共同更新状態かどうか (Qiita:Teamでのみ有効) */
11+
coediting: boolean;
12+
/** この投稿へのコメントの数 */
13+
comments_count: number;
14+
/** データが作成された日時 */
15+
created_at: string;
16+
/** Qiita:Teamのグループを表します。 */
17+
group: Group;
18+
/** 投稿の一意なID */
19+
id: string;
20+
/** この投稿への「いいね!」の数(Qiitaでのみ有効) */
21+
likes_count: number;
22+
/** 限定共有状態かどうかを表すフラグ (Qiita:Teamでは無効) */
23+
private: boolean;
24+
/** 投稿に付いたタグ一覧 */
25+
tags: Tagging[];
26+
/** 投稿のタイトル */
27+
title: string;
28+
/** データが最後に更新された日時 */
29+
updated_at: string;
30+
/** 投稿のURL */
31+
url: string;
32+
/** Qiita上のユーザを表します。 */
33+
user: User;
34+
/** 閲覧数 */
35+
page_views_count: number;
36+
}

src/entities/Like.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { User } from './User';
2+
3+
export interface Like {
4+
/** データが作成された日時 */
5+
created_at: string;
6+
/** Qiita上のユーザを表します。 */
7+
user: User;
8+
}

src/entities/Project.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
export interface Project {
2+
/** HTML形式の本文 */
3+
rendered_body: string;
4+
/** このプロジェクトが進行中かどうか */
5+
archived: boolean;
6+
/** Markdown形式の本文 */
7+
body: string;
8+
/** データが作成された日時 */
9+
created_at: string;
10+
/** プロジェクトのチーム上での一意なID */
11+
id: number;
12+
/** プロジェクト名 */
13+
name: string;
14+
/** 絵文字リアクション数 */
15+
reactions_count: number;
16+
/** データが最後に更新された日時 */
17+
updated_at: string;
18+
}

src/entities/Reaction.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { User } from './User';
2+
3+
export interface Reaction {
4+
/** データが作成された日時 */
5+
created_at: string;
6+
/** 絵文字画像のURL */
7+
image_url: string;
8+
/** 絵文字の識別子 */
9+
name: string;
10+
/** Qiita上のユーザを表します。 */
11+
user: User;
12+
}

src/entities/Tag.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export interface Tag {
2+
/** このタグをフォローしているユーザの数 */
3+
followers_count: number;
4+
/** このタグに設定されたアイコン画像のURL */
5+
icon_url?: string;
6+
/** タグを特定するための一意な名前 */
7+
id: string;
8+
/** このタグが付けられた投稿の数 */
9+
items_count: number;
10+
}

0 commit comments

Comments
 (0)