11import nodeFetch from 'node-fetch' ;
22import * as querystring from 'querystring' ;
33
4- export class Gateway {
4+ import { QiitaError } from '../errors/QiitaError' ;
5+ import { QiitaNotFoundError } from '../errors/QiitaNotFoundError' ;
6+ import { QiitaUnauthorizedError } from '../errors/QiitaUnauthorizedError' ;
7+ import { QiitaURLResolveError } from '../errors/QiitaURLResolveError' ;
58
6- /** ユーザーのアクセストークンです */
7- private token = '' ;
9+ export abstract class Gateway {
810
911 /** Qiitaのホストです */
10- private endpoint = 'https://qiita.com' ;
12+ protected url = 'https://qiita.com' ;
13+
14+ /** ユーザーのアクセストークンです */
15+ protected token = '' ;
1116
1217 /** APIバージョンを示すロケーションです */
13- private version = '/api/v2' ;
18+ protected version = '/api/v2' ;
1419
1520 /**
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
21+ * @param options オプショナルなパラメーター
22+ * @param options.url Qiitaのホストです
23+ * @param options.version APIバージョンを示すロケーションです
24+ * @param options.token ユーザーのアクセストークンです
2025 */
21- constructor ( options : { url ?: string , streamingUrl ?: string , token ?: string } ) {
22- if ( options ) {
23- this . url = options . url || '' ;
24- this . streamingUrl = options . streamingUrl || '' ;
26+ constructor ( options ? : { url ?: string , token ?: string , version ?: string } ) {
27+ if ( options && options . url ) {
28+ this . url = options . url ;
29+ }
2530
26- if ( options . token ) {
27- this . token = options . token ;
28- }
31+ if ( options && options . token ) {
32+ this . version = options . version || '' ;
33+ }
34+
35+ if ( options && options . token ) {
36+ this . token = options . token ;
2937 }
3038 }
3139
3240 /**
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
41+ * Qiita APIにアクセスするためのトークンを設定します
42+ * @param token トークン文字列
43+ * @return 何も返しません
44+ */
45+ public setToken ( token : string ) : void {
46+ this . token = token ;
47+ }
48+
49+ /**
50+ * Qiita Teamへのエンドポイントを設定します
51+ * @param url Qiitaのホスト
52+ * @return 何も返しません
53+ */
54+ public setURL ( url : string ) : void {
55+ this . url = url ;
56+ }
57+
58+ /**
59+ * Qiita APIへのパスを指定します.
60+ * @param version APIへのパスの文字列 (e.g. `/api/v2`)
61+ * @return 何も返しません
62+ */
63+ public setVersion ( version : string ) : void {
64+ this . version = version ;
65+ }
66+
67+ /**
68+ * Fetch APIのラッパー関数です
69+ * @param url リクエストするURLです
70+ * @param options Fetch APIの第二引数に渡されるオプションです
71+ * @return パースされたJSONオブジェクトを解決するPromiseです
3872 */
39- protected async request ( url : string , options : { [ key : string ] : any } = { } , parse = true ) : Promise < any > {
73+ protected async request < T > ( url : string , options : { [ key : string ] : any } = { } ) : Promise < T > {
4074 if ( ! options . headers ) {
4175 options . headers = { } ;
4276 }
4377
44- options . headers [ 'Content-Type' ] = 'application/json' ;
78+ if ( ! options . headers [ 'Content-Type' ] ) {
79+ options . headers [ 'Content-Type' ] = 'application/json' ;
80+ }
4581
4682 if ( ! this . url ) {
47- throw new MastodonURLResolveError ( 'REST API URL has not been specified, Use Mastodon.setUrl to set your instance\'s URL ') ;
83+ throw new QiitaURLResolveError ( 'Qiitaのホストが指定されていません。`Qiita.setURL` でAPIのホストを指定してからメソッドを呼び出してください。 ') ;
4884 }
4985
5086 if ( this . token ) {
@@ -55,24 +91,18 @@ export class Gateway {
5591 ? await nodeFetch ( url , options )
5692 : await fetch ( url , options ) ;
5793
58- if ( ! parse ) {
59- return response ;
60- }
61-
6294 const data = await response . json ( ) ;
6395
6496 if ( response . ok ) {
65- return data ;
97+ return data as T ;
6698 } else {
6799 switch ( response . status ) {
68100 case 401 :
69- throw new MastodonUnauthorizedError ( data . error ) ;
101+ throw new QiitaUnauthorizedError ( data . error ) ;
70102 case 404 :
71- throw new MastodonNotFoundError ( data . error ) ;
72- case 429 :
73- throw new MastodonRatelimitError ( data . error ) ;
103+ throw new QiitaNotFoundError ( data . error ) ;
74104 default :
75- throw new MastodonError ( 'MastodonError ', data . error || 'Unexpected error occurred ' ) ;
105+ throw new QiitaError ( 'QiitaError ', data . error || 'Qiita APIのリクエスト中に予期せぬエラーが発生しました ' ) ;
76106 }
77107 }
78108 }
@@ -82,53 +112,48 @@ export class Gateway {
82112 * @param url URL to request
83113 * @param params Query strings
84114 * @param options Fetch API options
85- * @param parse Whether parse response before return
86115 */
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 ) ;
116+ protected get < T > ( url : string , params = { } , options = { } ) : Promise < T > {
117+ return this . request ( url + ( Object . keys ( params ) . length ? '?' + querystring . stringify ( params ) : '' ) , { method : 'GET' , ...options } ) ;
89118 }
90119
91120 /**
92121 * HTTP POST
93122 * @param url URL to request
94123 * @param body Payload
95124 * @param options Fetch API options
96- * @param parse Whether parse response before return
97125 */
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 ) ;
126+ protected post < T > ( url : string , body = { } , options = { } ) : Promise < T > {
127+ return this . request ( url , { method : 'POST' , body : JSON . stringify ( body ) , ...options } ) ;
100128 }
101129
102130 /**
103131 * HTTP PUT
104132 * @param url URL to request
105133 * @param body Payload
106134 * @param options Fetch API options
107- * @param parse Whether parse response before return
108135 */
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 ) ;
136+ protected put < T > ( url : string , body = { } , options = { } ) : Promise < T > {
137+ return this . request ( url , { method : 'PUT' , body : JSON . stringify ( body ) , ...options } ) ;
111138 }
112139
113140 /**
114141 * HTTP DELETE
115142 * @param url URL to request
116143 * @param body Payload
117144 * @param options Fetch API options
118- * @param parse Whether parse response before return
119145 */
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 ) ;
146+ protected delete < T > ( url : string , body = { } , options = { } ) : Promise < T > {
147+ return this . request ( url , { method : 'DELETE' , body : JSON . stringify ( body ) , ...options } ) ;
122148 }
123149
124150 /**
125151 * HTTP PATCH
126152 * @param url URL to request
127153 * @param body Payload
128154 * @param options Fetch API options
129- * @param parse Whether parse response before return
130155 */
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 ) ;
156+ protected patch < T > ( url : string , body = { } , options = { } ) : Promise < T > {
157+ return this . request ( url , { method : 'PATCH' , body : JSON . stringify ( body ) , ...options } ) ;
133158 }
134159}
0 commit comments