Skip to content

Commit 22ed986

Browse files
committed
HTTPライブラリをaxiosに変更
1 parent ec66de9 commit 22ed986

File tree

3 files changed

+85
-40
lines changed

3 files changed

+85
-40
lines changed

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@
2424
},
2525
"homepage": "https://github.com/neet/qiita-js-2#readme",
2626
"dependencies": {
27-
"node-fetch": "^2.2.0"
27+
"axios": "^0.18.0",
28+
"http-link-header": "^0.8.0"
2829
},
2930
"devDependencies": {
31+
"@types/http-link-header": "^0.6.2",
3032
"@types/node-fetch": "^2.1.2",
3133
"tslint": "^5.11.0",
3234
"typedoc": "^0.12.0",

src/client/Gateway.ts

Lines changed: 52 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import nodeFetch from 'node-fetch';
1+
import axios, { AxiosRequestConfig, AxiosResponse } from 'axios';
22
import * as querystring from 'querystring';
33

44
import { QiitaError } from '../errors/QiitaError';
@@ -95,7 +95,7 @@ export abstract class Gateway {
9595
* @param options Fetch APIの第二引数に渡されるオプションです
9696
* @return パースされたJSONオブジェクトを解決するPromiseです
9797
*/
98-
protected async request <T> (url: string, options: { [key: string]: any } = {}): Promise<T> {
98+
protected async request <T> (options: AxiosRequestConfig): Promise<AxiosResponse<T>> {
9999
if ( !options.headers ) {
100100
options.headers = {};
101101
}
@@ -112,38 +112,30 @@ export abstract class Gateway {
112112
options.headers.Authorization = `Bearer ${this.token}`;
113113
}
114114

115-
const response = typeof window === 'undefined'
116-
? await nodeFetch(url, options)
117-
: await fetch(url, options);
118-
119-
let data;
115+
options.transformResponse = [(data) => {
116+
try {
117+
return JSON.parse(data);
118+
} catch {
119+
return data;
120+
}
121+
}];
120122

121123
try {
122-
data = await response.json();
123-
} catch {
124-
// JSONでパースできないレスポンスボディのときはundefinedを返す
125-
data = undefined;
126-
}
127-
128-
if (response.ok) {
129-
return data as T;
130-
} else {
131-
// Qiitaがエラーの際に返すステータスコードが明記されていなかったので
132-
// ありそうなものをハンドルしています。
133-
// ref: https://qiita.com/api/v2/docs#%E3%82%B9%E3%83%86%E3%83%BC%E3%82%BF%E3%82%B9%E3%82%B3%E3%83%BC%E3%83%89
134-
switch (response.status) {
124+
return await axios.request<T>(options);
125+
} catch (error) {
126+
switch (error.status) {
135127
case 401:
136-
throw new QiitaUnauthorizedError(data.message || 'リクエストに必要な権限が不足しています。');
128+
throw new QiitaUnauthorizedError(error.data.message || 'リクエストに必要な権限が不足しています。');
137129
case 403:
138-
throw new QiitaForbiddenError(data.message || 'このリクエストは禁止されています。');
130+
throw new QiitaForbiddenError(error.data.message || 'このリクエストは禁止されています。');
139131
case 404:
140-
throw new QiitaNotFoundError(data.message || '指定したエンドポイントが見つかりませんでした');
132+
throw new QiitaNotFoundError(error.data.message || '指定したエンドポイントが見つかりませんでした');
141133
case 429:
142-
throw new QiitaRateLimitError(data.message || 'APIのレートリミットに到達しました。時間をおいてもう一度お試しください。');
134+
throw new QiitaRateLimitError(error.data.message || 'APIのレートリミットに到達しました。時間をおいてもう一度お試しください。');
143135
case 500:
144-
throw new QiitaInternalServerError(data.message || 'Qiitaのサーバーが internal server error を返しました。ホストが混雑している可能性がありますので、時間をおいてもう一度お試しください。');
136+
throw new QiitaInternalServerError(error.data.message || 'Qiitaのサーバーが internal server error を返しました。ホストが混雑している可能性がありますので、時間をおいてもう一度お試しください。');
145137
default:
146-
throw new QiitaError('QiitaError', data.message || 'Qiita APIのリクエスト中に予期せぬエラーが発生しました');
138+
throw new QiitaError('QiitaError', error.data.message || 'Qiita APIのリクエスト中に予期せぬエラーが発生しました');
147139
}
148140
}
149141
}
@@ -154,8 +146,12 @@ export abstract class Gateway {
154146
* @param params クエリ文字列
155147
* @param options Fetch APIの第二引数になるオブジェクト
156148
*/
157-
protected get <T> (url: string, params = {}, options = {}): Promise<T> {
158-
return this.request(url + (Object.keys(params).length ? '?' + querystring.stringify(params) : ''), { method: 'GET', ...options });
149+
protected get <T> (url: string, params = {}, options = {}) {
150+
return this.request<T>({
151+
method: 'GET',
152+
url: url + (Object.keys(params).length ? '?' + querystring.stringify(params) : ''),
153+
...options,
154+
});
159155
}
160156

161157
/**
@@ -164,8 +160,13 @@ export abstract class Gateway {
164160
* @param body リクエストボディ
165161
* @param options Fetch APIの第二引数になるオブジェクト
166162
*/
167-
protected post <T> (url: string, body = {}, options = {}): Promise<T> {
168-
return this.request(url, { method: 'POST', body: JSON.stringify(body), ...options });
163+
protected post <T> (url: string, body = {}, options = {}) {
164+
return this.request<T>({
165+
method: 'POST',
166+
url,
167+
data: JSON.stringify(body),
168+
...options,
169+
});
169170
}
170171

171172
/**
@@ -174,8 +175,13 @@ export abstract class Gateway {
174175
* @param body リクエストボディ
175176
* @param options Fetch APIの第二引数になるオブジェクト
176177
*/
177-
protected put <T> (url: string, body = {}, options = {}): Promise<T> {
178-
return this.request(url, { method: 'PUT', body: JSON.stringify(body), ...options });
178+
protected put <T> (url: string, body = {}, options = {}) {
179+
return this.request<T>({
180+
method: 'PUT',
181+
url,
182+
data: JSON.stringify(body),
183+
...options,
184+
});
179185
}
180186

181187
/**
@@ -184,8 +190,13 @@ export abstract class Gateway {
184190
* @param body リクエストボディ
185191
* @param options Fetch APIの第二引数になるオブジェクト
186192
*/
187-
protected delete <T> (url: string, body = {}, options = {}): Promise<T> {
188-
return this.request(url, { method: 'DELETE', body: JSON.stringify(body), ...options });
193+
protected delete <T> (url: string, body = {}, options = {}) {
194+
return this.request<T>({
195+
method: 'DELETE',
196+
url,
197+
data: JSON.stringify(body),
198+
...options,
199+
});
189200
}
190201

191202
/**
@@ -194,7 +205,12 @@ export abstract class Gateway {
194205
* @param body リクエストボディ
195206
* @param options Fetch APIの第二引数になるオブジェクト
196207
*/
197-
protected patch <T> (url: string, body = {}, options = {}): Promise<T> {
198-
return this.request(url, { method: 'PATCH', body: JSON.stringify(body), ...options });
208+
protected patch <T> (url: string, body = {}, options = {}) {
209+
return this.request<T>({
210+
method: 'PATCH',
211+
url,
212+
data: JSON.stringify(body),
213+
...options,
214+
});
199215
}
200216
}

yarn.lock

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@
2828
version "9.12.3"
2929
resolved "https://registry.yarnpkg.com/@types/highlight.js/-/highlight.js-9.12.3.tgz#b672cfaac25cbbc634a0fd92c515f66faa18dbca"
3030

31+
"@types/http-link-header@^0.6.2":
32+
version "0.6.2"
33+
resolved "https://registry.yarnpkg.com/@types/http-link-header/-/http-link-header-0.6.2.tgz#ca4d3064a6f6e016caea6c3cc5a1c6b3854d5af6"
34+
3135
"@types/lodash@^4.14.110":
3236
version "4.14.116"
3337
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.116.tgz#5ccf215653e3e8c786a58390751033a9adca0eb9"
@@ -93,6 +97,13 @@ async@^1.4.0:
9397
version "1.5.2"
9498
resolved "https://registry.yarnpkg.com/async/-/async-1.5.2.tgz#ec6a61ae56480c0c3cb241c95618e20892f9672a"
9599

100+
axios@^0.18.0:
101+
version "0.18.0"
102+
resolved "http://registry.npmjs.org/axios/-/axios-0.18.0.tgz#32d53e4851efdc0a11993b6cd000789d70c05102"
103+
dependencies:
104+
follow-redirects "^1.3.0"
105+
is-buffer "^1.1.5"
106+
96107
babel-code-frame@^6.22.0:
97108
version "6.26.0"
98109
resolved "https://registry.yarnpkg.com/babel-code-frame/-/babel-code-frame-6.26.0.tgz#63fd43f7dc1e3bb7ce35947db8fe369a3f58c74b"
@@ -171,6 +182,12 @@ concat-map@0.0.1:
171182
version "0.0.1"
172183
resolved "https://registry.yarnpkg.com/concat-map/-/concat-map-0.0.1.tgz#d8a96bd77fd68df7793a73036a3ba0d5405d477b"
173184

185+
debug@=3.1.0:
186+
version "3.1.0"
187+
resolved "https://registry.yarnpkg.com/debug/-/debug-3.1.0.tgz#5bb5a0672628b64149566ba16819e61518c67261"
188+
dependencies:
189+
ms "2.0.0"
190+
174191
decamelize@^1.0.0:
175192
version "1.2.0"
176193
resolved "https://registry.yarnpkg.com/decamelize/-/decamelize-1.2.0.tgz#f6534d15148269b20352e7bee26f501f9a191290"
@@ -191,6 +208,12 @@ esutils@^2.0.2:
191208
version "2.0.2"
192209
resolved "https://registry.yarnpkg.com/esutils/-/esutils-2.0.2.tgz#0abf4f1caa5bcb1f7a9d8acc6dea4faaa04bac9b"
193210

211+
follow-redirects@^1.3.0:
212+
version "1.5.8"
213+
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.5.8.tgz#1dbfe13e45ad969f813e86c00e5296f525c885a1"
214+
dependencies:
215+
debug "=3.1.0"
216+
194217
fs-extra@^7.0.0:
195218
version "7.0.0"
196219
resolved "https://registry.yarnpkg.com/fs-extra/-/fs-extra-7.0.0.tgz#8cc3f47ce07ef7b3593a11b9fb245f7e34c041d6"
@@ -242,6 +265,10 @@ highlight.js@^9.0.0:
242265
version "9.12.0"
243266
resolved "https://registry.yarnpkg.com/highlight.js/-/highlight.js-9.12.0.tgz#e6d9dbe57cbefe60751f02af336195870c90c01e"
244267

268+
http-link-header@^0.8.0:
269+
version "0.8.0"
270+
resolved "https://registry.yarnpkg.com/http-link-header/-/http-link-header-0.8.0.tgz#a22b41a0c9b1e2d8fac1bf1b697c6bd532d5f5e4"
271+
245272
inflight@^1.0.4:
246273
version "1.0.6"
247274
resolved "https://registry.yarnpkg.com/inflight/-/inflight-1.0.6.tgz#49bd6331d7d02d0c09bc910a1075ba8165b56df9"
@@ -310,9 +337,9 @@ minimist@~0.0.1:
310337
version "0.0.10"
311338
resolved "https://registry.yarnpkg.com/minimist/-/minimist-0.0.10.tgz#de3f98543dbf96082be48ad1a0c7cda836301dcf"
312339

313-
node-fetch@^2.2.0:
314-
version "2.2.0"
315-
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.2.0.tgz#4ee79bde909262f9775f731e3656d0db55ced5b5"
340+
ms@2.0.0:
341+
version "2.0.0"
342+
resolved "https://registry.yarnpkg.com/ms/-/ms-2.0.0.tgz#5608aeadfc00be6c2901df5f9861788de0d597c8"
316343

317344
once@^1.3.0:
318345
version "1.4.0"

0 commit comments

Comments
 (0)