@wiajs/req 1.7.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +1023 -0
- package/LICENSE +7 -0
- package/MIGRATION_GUIDE.md +3 -0
- package/README.md +1645 -0
- package/SECURITY.md +6 -0
- package/dist/node/req.cjs +4397 -0
- package/dist/node/req.mjs +3551 -0
- package/dist/web/req.mjs +2609 -0
- package/index.d.cts +545 -0
- package/index.d.ts +565 -0
- package/index.js +43 -0
- package/lib/adapters/README.md +37 -0
- package/lib/adapters/adapters.js +57 -0
- package/lib/adapters/fetch.js +229 -0
- package/lib/adapters/http.js +552 -0
- package/lib/adapters/xhr.js +200 -0
- package/lib/axios.js +89 -0
- package/lib/cancel/CancelToken.js +106 -0
- package/lib/cancel/CanceledError.js +20 -0
- package/lib/cancel/isCancel.js +4 -0
- package/lib/core/Axios.js +359 -0
- package/lib/core/AxiosError.js +89 -0
- package/lib/core/AxiosHeaders.js +243 -0
- package/lib/core/InterceptorManager.js +59 -0
- package/lib/core/README.md +8 -0
- package/lib/core/buildFullPath.js +18 -0
- package/lib/core/dispatchRequest.js +72 -0
- package/lib/core/mergeConfig.js +98 -0
- package/lib/core/settle.js +21 -0
- package/lib/core/transformData.js +22 -0
- package/lib/defaults/index.js +136 -0
- package/lib/defaults/transitional.js +6 -0
- package/lib/env/README.md +3 -0
- package/lib/env/classes/FormData.js +2 -0
- package/lib/env/data.js +1 -0
- package/lib/helpers/AxiosTransformStream.js +116 -0
- package/lib/helpers/AxiosURLSearchParams.js +50 -0
- package/lib/helpers/HttpStatusCode.js +69 -0
- package/lib/helpers/README.md +7 -0
- package/lib/helpers/ZlibHeaderTransformStream.js +22 -0
- package/lib/helpers/bind.js +6 -0
- package/lib/helpers/buildURL.js +42 -0
- package/lib/helpers/callbackify.js +14 -0
- package/lib/helpers/combineURLs.js +11 -0
- package/lib/helpers/composeSignals.js +37 -0
- package/lib/helpers/cookies.js +29 -0
- package/lib/helpers/deprecatedMethod.js +18 -0
- package/lib/helpers/formDataToJSON.js +78 -0
- package/lib/helpers/formDataToStream.js +77 -0
- package/lib/helpers/fromDataURI.js +44 -0
- package/lib/helpers/isAbsoluteURL.js +13 -0
- package/lib/helpers/isAxiosError.js +11 -0
- package/lib/helpers/isURLSameOrigin.js +50 -0
- package/lib/helpers/null.js +2 -0
- package/lib/helpers/parseHeaders.js +61 -0
- package/lib/helpers/parseProtocol.js +5 -0
- package/lib/helpers/progressEventReducer.js +54 -0
- package/lib/helpers/readBlob.js +13 -0
- package/lib/helpers/resolveConfig.js +45 -0
- package/lib/helpers/speedometer.js +40 -0
- package/lib/helpers/spread.js +26 -0
- package/lib/helpers/throttle.js +41 -0
- package/lib/helpers/toFormData.js +175 -0
- package/lib/helpers/toURLEncodedForm.js +15 -0
- package/lib/helpers/trackStream.js +75 -0
- package/lib/helpers/validator.js +84 -0
- package/lib/platform/browser/classes/Blob.js +2 -0
- package/lib/platform/browser/classes/FormData.js +2 -0
- package/lib/platform/browser/classes/URLSearchParams.js +3 -0
- package/lib/platform/browser/index.js +19 -0
- package/lib/platform/common/utils.js +37 -0
- package/lib/platform/index.js +6 -0
- package/lib/platform/node/classes/FormData.js +2 -0
- package/lib/platform/node/classes/URLSearchParams.js +3 -0
- package/lib/platform/node/index.js +16 -0
- package/lib/req.js +67 -0
- package/lib/utils.js +635 -0
- package/package.json +214 -0
|
@@ -0,0 +1,359 @@
|
|
|
1
|
+
import utils from '../utils.js';
|
|
2
|
+
import buildURL from '../helpers/buildURL.js';
|
|
3
|
+
import InterceptorManager from './InterceptorManager.js';
|
|
4
|
+
import dispatchRequest from './dispatchRequest.js';
|
|
5
|
+
import mergeConfig from './mergeConfig.js';
|
|
6
|
+
import buildFullPath from './buildFullPath.js';
|
|
7
|
+
import validator from '../helpers/validator.js';
|
|
8
|
+
import AxiosHeaders from './AxiosHeaders.js';
|
|
9
|
+
const { validators } = validator;
|
|
10
|
+
/**
|
|
11
|
+
* Create a new instance of Axios
|
|
12
|
+
*
|
|
13
|
+
* @param {Object} instanceConfig The default config for the instance
|
|
14
|
+
*
|
|
15
|
+
* @return {Axios} A new instance of Axios
|
|
16
|
+
*/ let Axios = class Axios {
|
|
17
|
+
constructor(instanceConfig){
|
|
18
|
+
this.defaults = instanceConfig;
|
|
19
|
+
this.config = this.defaults // !+++
|
|
20
|
+
;
|
|
21
|
+
this.interceptors = {
|
|
22
|
+
request: new InterceptorManager(),
|
|
23
|
+
response: new InterceptorManager()
|
|
24
|
+
};
|
|
25
|
+
this.init() // !+++
|
|
26
|
+
;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* !+++
|
|
30
|
+
* config 属性直接挂在到实例上,方便读取、设置、修改
|
|
31
|
+
* 需注意,不要与其属性、方法冲突!!!
|
|
32
|
+
request(config)
|
|
33
|
+
get(url[, config])
|
|
34
|
+
delete(url[, config])
|
|
35
|
+
head(url[, config])
|
|
36
|
+
options(url[, config])
|
|
37
|
+
post(url[, data[, config]])
|
|
38
|
+
put(url[, data[, config]])
|
|
39
|
+
patch(url[, data[, config]])
|
|
40
|
+
getUri([config])
|
|
41
|
+
*/ init() {
|
|
42
|
+
const m = this;
|
|
43
|
+
[
|
|
44
|
+
'url',
|
|
45
|
+
'method',
|
|
46
|
+
'baseURL',
|
|
47
|
+
'transformRequest',
|
|
48
|
+
'transformResponse',
|
|
49
|
+
'headers',
|
|
50
|
+
'params',
|
|
51
|
+
'paramsSerializer',
|
|
52
|
+
'body',
|
|
53
|
+
'data',
|
|
54
|
+
'timeout',
|
|
55
|
+
'withCredentials',
|
|
56
|
+
'adapter',
|
|
57
|
+
'auth',
|
|
58
|
+
'responseType',
|
|
59
|
+
'responseEncoding',
|
|
60
|
+
'xsrfCookieName',
|
|
61
|
+
'xsrfHeaderName',
|
|
62
|
+
'onUploadProgress',
|
|
63
|
+
'onDownloadProgress',
|
|
64
|
+
'maxContentLength',
|
|
65
|
+
'maxBodyLength',
|
|
66
|
+
'validateStatus',
|
|
67
|
+
'maxRedirects',
|
|
68
|
+
'beforeRedirect',
|
|
69
|
+
'socketPath',
|
|
70
|
+
'httpAgent',
|
|
71
|
+
'httpsAgent',
|
|
72
|
+
'agent',
|
|
73
|
+
'cancelToken',
|
|
74
|
+
'signal',
|
|
75
|
+
'decompress',
|
|
76
|
+
'insecureHTTPParser',
|
|
77
|
+
'transitional',
|
|
78
|
+
'env',
|
|
79
|
+
'formSerializer',
|
|
80
|
+
'maxRate'
|
|
81
|
+
].forEach((p)=>Object.defineProperty(m, p, {
|
|
82
|
+
enumerable: true,
|
|
83
|
+
get () {
|
|
84
|
+
return m.config[p];
|
|
85
|
+
},
|
|
86
|
+
set (value) {
|
|
87
|
+
m.config[p] = value;
|
|
88
|
+
}
|
|
89
|
+
}));
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Dispatch a request
|
|
93
|
+
* 启动执行请求,返回 Promise 实例
|
|
94
|
+
* @param {String|Object} configOrUrl The config specific for this request (merged with this.defaults)
|
|
95
|
+
* @param {?Object} config
|
|
96
|
+
* @returns {Promise} The Promise to be fulfilled
|
|
97
|
+
*/ async request(configOrUrl, config) {
|
|
98
|
+
try {
|
|
99
|
+
return await this._request(configOrUrl, config);
|
|
100
|
+
} catch (err) {
|
|
101
|
+
if (err instanceof Error) {
|
|
102
|
+
let dummy = {};
|
|
103
|
+
Error.captureStackTrace ? Error.captureStackTrace(dummy) : dummy = new Error();
|
|
104
|
+
// slice off the Error: ... line
|
|
105
|
+
const stack = dummy.stack ? dummy.stack.replace(/^.+\n/, '') : '';
|
|
106
|
+
try {
|
|
107
|
+
if (!err.stack) {
|
|
108
|
+
err.stack = stack;
|
|
109
|
+
// match without the 2 top stack lines
|
|
110
|
+
} else if (stack && !String(err.stack).endsWith(stack.replace(/^.+\n.+\n/, ''))) {
|
|
111
|
+
err.stack += '\n' + stack;
|
|
112
|
+
}
|
|
113
|
+
} catch (e) {
|
|
114
|
+
// ignore the case where "stack" is an un-writable property
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
throw err // 抛出异常
|
|
118
|
+
;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* 执行请求
|
|
123
|
+
* @param {*} configOrUrl
|
|
124
|
+
* @param {*} config
|
|
125
|
+
* @param {boolean} [stream = false] - 是否返回 stream
|
|
126
|
+
* @returns
|
|
127
|
+
*/ _request(configOrUrl, config, stream = false) {
|
|
128
|
+
let R = null;
|
|
129
|
+
const m = this;
|
|
130
|
+
/* eslint no-param-reassign:0 */ // Allow for axios('example/url'[, config]) a la fetch API
|
|
131
|
+
if (typeof configOrUrl === 'string') {
|
|
132
|
+
config = config || {};
|
|
133
|
+
config.url = configOrUrl;
|
|
134
|
+
} else {
|
|
135
|
+
config = configOrUrl || {};
|
|
136
|
+
}
|
|
137
|
+
// ! body as data alias, body ==> data,内部保持data不变
|
|
138
|
+
if (!config.data && config.body) {
|
|
139
|
+
config.data = config.body;
|
|
140
|
+
// config.body = undefined;
|
|
141
|
+
}
|
|
142
|
+
config = mergeConfig(this.defaults, config);
|
|
143
|
+
const { transitional, paramsSerializer, headers } = config;
|
|
144
|
+
if (transitional !== undefined) {
|
|
145
|
+
validator.assertOptions(transitional, {
|
|
146
|
+
silentJSONParsing: validators.transitional(validators.boolean),
|
|
147
|
+
forcedJSONParsing: validators.transitional(validators.boolean),
|
|
148
|
+
clarifyTimeoutError: validators.transitional(validators.boolean)
|
|
149
|
+
}, false);
|
|
150
|
+
}
|
|
151
|
+
if (paramsSerializer) {
|
|
152
|
+
if (utils.isFunction(paramsSerializer)) {
|
|
153
|
+
config.paramsSerializer = {
|
|
154
|
+
serialize: paramsSerializer
|
|
155
|
+
};
|
|
156
|
+
} else {
|
|
157
|
+
validator.assertOptions(paramsSerializer, {
|
|
158
|
+
encode: validators.function,
|
|
159
|
+
serialize: validators.function
|
|
160
|
+
}, true);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
validator.assertOptions(config, {
|
|
164
|
+
baseUrl: validators.spelling('baseURL'),
|
|
165
|
+
withXsrfToken: validators.spelling('withXSRFToken')
|
|
166
|
+
}, true);
|
|
167
|
+
// Set config.method
|
|
168
|
+
config.method = (config.method || this.defaults.method || 'get').toLowerCase();
|
|
169
|
+
// Flatten headers,方法头覆盖通用头
|
|
170
|
+
const contextHeaders = headers && utils.merge(headers.common, headers[config.method]);
|
|
171
|
+
headers && utils.forEach([
|
|
172
|
+
'delete',
|
|
173
|
+
'get',
|
|
174
|
+
'head',
|
|
175
|
+
'post',
|
|
176
|
+
'put',
|
|
177
|
+
'patch',
|
|
178
|
+
'common'
|
|
179
|
+
], (method)=>{
|
|
180
|
+
delete headers[method];
|
|
181
|
+
});
|
|
182
|
+
// 源值存在,则不覆盖,contextHeaders 优先于 headers
|
|
183
|
+
config.headers = AxiosHeaders.concat(contextHeaders, headers);
|
|
184
|
+
// filter out skipped interceptors
|
|
185
|
+
const requestInterceptorChain = [] // 请求拦截器,hook
|
|
186
|
+
;
|
|
187
|
+
let synchronousRequestInterceptors = true;
|
|
188
|
+
this.interceptors.request.forEach(function unshiftRequestInterceptors(interceptor) {
|
|
189
|
+
if (typeof interceptor.runWhen === 'function' && interceptor.runWhen(config) === false) {
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
synchronousRequestInterceptors = synchronousRequestInterceptors && interceptor.synchronous;
|
|
193
|
+
requestInterceptorChain.unshift(interceptor.fulfilled, interceptor.rejected);
|
|
194
|
+
});
|
|
195
|
+
const responseInterceptorChain = [] // 响应拦截器,hook
|
|
196
|
+
;
|
|
197
|
+
this.interceptors.response.forEach(function pushResponseInterceptors(interceptor) {
|
|
198
|
+
responseInterceptorChain.push(interceptor.fulfilled, interceptor.rejected);
|
|
199
|
+
});
|
|
200
|
+
let promise;
|
|
201
|
+
let i = 0;
|
|
202
|
+
let len;
|
|
203
|
+
debugger;
|
|
204
|
+
// 执行dispatchRequest
|
|
205
|
+
// !+++ stream
|
|
206
|
+
if (stream) {
|
|
207
|
+
config.stream = true;
|
|
208
|
+
R = dispatchRequest.call(this, config) // not promise
|
|
209
|
+
;
|
|
210
|
+
} else if (!synchronousRequestInterceptors) {
|
|
211
|
+
// 异步拦截器
|
|
212
|
+
const chain = [
|
|
213
|
+
dispatchRequest.bind(this),
|
|
214
|
+
undefined
|
|
215
|
+
] // dispatchRequest 放入运行链
|
|
216
|
+
;
|
|
217
|
+
chain.unshift(...requestInterceptorChain) // !*** 插入头
|
|
218
|
+
;
|
|
219
|
+
chain.push(...responseInterceptorChain) // !*** 加入尾
|
|
220
|
+
;
|
|
221
|
+
len = chain.length;
|
|
222
|
+
promise = Promise.resolve(config) // promise 对象
|
|
223
|
+
;
|
|
224
|
+
// 传入config配置,按顺序执行
|
|
225
|
+
while(i < len)promise = promise.then(chain[i++], chain[i++]);
|
|
226
|
+
R = promise;
|
|
227
|
+
} else {
|
|
228
|
+
// 同步拦截器
|
|
229
|
+
len = requestInterceptorChain.length;
|
|
230
|
+
let newConfig = config;
|
|
231
|
+
i = 0;
|
|
232
|
+
// 按加入顺序运行 request 拦截器
|
|
233
|
+
while(i < len){
|
|
234
|
+
const onFulfilled = requestInterceptorChain[i++];
|
|
235
|
+
const onRejected = requestInterceptorChain[i++];
|
|
236
|
+
try {
|
|
237
|
+
newConfig = onFulfilled(newConfig) // 执行
|
|
238
|
+
;
|
|
239
|
+
} catch (error) {
|
|
240
|
+
onRejected.call(this, error);
|
|
241
|
+
break;
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
try {
|
|
245
|
+
promise = dispatchRequest.call(this, newConfig);
|
|
246
|
+
i = 0;
|
|
247
|
+
len = responseInterceptorChain.length;
|
|
248
|
+
// 按顺序执行响应 hook
|
|
249
|
+
while(i < len)promise = promise.then(responseInterceptorChain[i++], responseInterceptorChain[i++]);
|
|
250
|
+
R = promise;
|
|
251
|
+
} catch (error) {
|
|
252
|
+
R = Promise.reject(error);
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
return R;
|
|
256
|
+
}
|
|
257
|
+
/**
|
|
258
|
+
* !+++
|
|
259
|
+
* 类似 request 库,返回 stream
|
|
260
|
+
* stream 模式下,拦截器无效
|
|
261
|
+
* 如需拦截器,请使用 responseType: 'stream',data 为 stream 传出
|
|
262
|
+
* 或者 将 stream 作为 data 传入
|
|
263
|
+
* @param {*} configOrUrl
|
|
264
|
+
* @param {*} config
|
|
265
|
+
* @returns
|
|
266
|
+
*/ stream(configOrUrl, config) {
|
|
267
|
+
return this._request(configOrUrl, config, true);
|
|
268
|
+
}
|
|
269
|
+
/**
|
|
270
|
+
*
|
|
271
|
+
* @param {*} config
|
|
272
|
+
* @returns
|
|
273
|
+
*/ getUri(config) {
|
|
274
|
+
config = mergeConfig(this.defaults, config);
|
|
275
|
+
const fullPath = buildFullPath(config.baseURL, config.url);
|
|
276
|
+
return buildURL(fullPath, config.params, config.paramsSerializer);
|
|
277
|
+
}
|
|
278
|
+
};
|
|
279
|
+
// Provide aliases for supported request methods
|
|
280
|
+
utils.forEach([
|
|
281
|
+
'head',
|
|
282
|
+
'options'
|
|
283
|
+
], function forEachMethodNoData(method) {
|
|
284
|
+
/* eslint func-names:0 */ Axios.prototype[method] = function(url, config) {
|
|
285
|
+
return this.request(mergeConfig(config || {}, {
|
|
286
|
+
method,
|
|
287
|
+
url,
|
|
288
|
+
data: (config || {}).data
|
|
289
|
+
}));
|
|
290
|
+
};
|
|
291
|
+
});
|
|
292
|
+
// delete、get, 与 axios不同,第二个参数为 params,而不是 data
|
|
293
|
+
utils.forEach([
|
|
294
|
+
'delete',
|
|
295
|
+
'get'
|
|
296
|
+
], function forEachMethodNoData(method) {
|
|
297
|
+
Axios.prototype[method] = function(url, params, config) {
|
|
298
|
+
return this.request(mergeConfig(config || {}, {
|
|
299
|
+
method,
|
|
300
|
+
url,
|
|
301
|
+
params
|
|
302
|
+
}));
|
|
303
|
+
};
|
|
304
|
+
});
|
|
305
|
+
utils.forEach([
|
|
306
|
+
'post',
|
|
307
|
+
'put',
|
|
308
|
+
'patch'
|
|
309
|
+
], function forEachMethodWithData(method) {
|
|
310
|
+
function generateHTTPMethod(isForm) {
|
|
311
|
+
return function httpMethod(url, data, config) {
|
|
312
|
+
return this.request(mergeConfig(config || {}, {
|
|
313
|
+
method,
|
|
314
|
+
headers: isForm ? {
|
|
315
|
+
'Content-Type': 'multipart/form-data'
|
|
316
|
+
} : {},
|
|
317
|
+
url,
|
|
318
|
+
data
|
|
319
|
+
}));
|
|
320
|
+
};
|
|
321
|
+
}
|
|
322
|
+
Axios.prototype[method] = generateHTTPMethod();
|
|
323
|
+
Axios.prototype[`${method}Form`] = generateHTTPMethod(true);
|
|
324
|
+
});
|
|
325
|
+
// stream get, 与 axios不同,第二个参数为 params,而不是 data
|
|
326
|
+
utils.forEach([
|
|
327
|
+
'gets'
|
|
328
|
+
], function forEachMethodNoData(method) {
|
|
329
|
+
Axios.prototype[method] = function(url, params, config) {
|
|
330
|
+
return this.stream(mergeConfig(config || {}, {
|
|
331
|
+
method,
|
|
332
|
+
url,
|
|
333
|
+
params,
|
|
334
|
+
data: (config || {}).data
|
|
335
|
+
}));
|
|
336
|
+
};
|
|
337
|
+
});
|
|
338
|
+
// stream post put patch
|
|
339
|
+
utils.forEach([
|
|
340
|
+
'posts',
|
|
341
|
+
'puts',
|
|
342
|
+
'patchs'
|
|
343
|
+
], function forEachMethodWithData(method) {
|
|
344
|
+
function generateStreamMethod(isForm) {
|
|
345
|
+
return function httpMethod(url, data, config) {
|
|
346
|
+
return this.stream(mergeConfig(config || {}, {
|
|
347
|
+
method,
|
|
348
|
+
headers: isForm ? {
|
|
349
|
+
'Content-Type': 'multipart/form-data'
|
|
350
|
+
} : {},
|
|
351
|
+
url,
|
|
352
|
+
data
|
|
353
|
+
}));
|
|
354
|
+
};
|
|
355
|
+
}
|
|
356
|
+
Axios.prototype[method] = generateStreamMethod();
|
|
357
|
+
Axios.prototype[`${method}Forms`] = generateStreamMethod(true);
|
|
358
|
+
});
|
|
359
|
+
export default Axios;
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
import utils from '../utils.js';
|
|
3
|
+
/**
|
|
4
|
+
* Create an Error with the specified message, config, error code, request and response.
|
|
5
|
+
*
|
|
6
|
+
* @param {string} message The error message.
|
|
7
|
+
* @param {string} [code] The error code (for example, 'ECONNABORTED').
|
|
8
|
+
* @param {Object} [config] The config.
|
|
9
|
+
* @param {Object} [request] The request.
|
|
10
|
+
* @param {Object} [response] The response.
|
|
11
|
+
*
|
|
12
|
+
* @returns {Error} The created error.
|
|
13
|
+
*/ function AxiosError(message, code, config, request, response) {
|
|
14
|
+
Error.call(this);
|
|
15
|
+
if (Error.captureStackTrace) {
|
|
16
|
+
Error.captureStackTrace(this, this.constructor);
|
|
17
|
+
} else {
|
|
18
|
+
this.stack = new Error().stack;
|
|
19
|
+
}
|
|
20
|
+
this.message = message;
|
|
21
|
+
this.name = 'AxiosError';
|
|
22
|
+
code && (this.code = code);
|
|
23
|
+
config && (this.config = config);
|
|
24
|
+
request && (this.request = request);
|
|
25
|
+
if (response) {
|
|
26
|
+
this.response = response;
|
|
27
|
+
this.status = response.status ? response.status : null;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
utils.inherits(AxiosError, Error, {
|
|
31
|
+
toJSON: function toJSON() {
|
|
32
|
+
return {
|
|
33
|
+
// Standard
|
|
34
|
+
message: this.message,
|
|
35
|
+
name: this.name,
|
|
36
|
+
// Microsoft
|
|
37
|
+
description: this.description,
|
|
38
|
+
number: this.number,
|
|
39
|
+
// Mozilla
|
|
40
|
+
fileName: this.fileName,
|
|
41
|
+
lineNumber: this.lineNumber,
|
|
42
|
+
columnNumber: this.columnNumber,
|
|
43
|
+
stack: this.stack,
|
|
44
|
+
// Axios
|
|
45
|
+
config: utils.toJSONObject(this.config),
|
|
46
|
+
code: this.code,
|
|
47
|
+
status: this.status
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
const prototype = AxiosError.prototype;
|
|
52
|
+
const descriptors = {};
|
|
53
|
+
[
|
|
54
|
+
'ERR_BAD_OPTION_VALUE',
|
|
55
|
+
'ERR_BAD_OPTION',
|
|
56
|
+
'ECONNABORTED',
|
|
57
|
+
'ETIMEDOUT',
|
|
58
|
+
'ERR_NETWORK',
|
|
59
|
+
'ERR_FR_TOO_MANY_REDIRECTS',
|
|
60
|
+
'ERR_DEPRECATED',
|
|
61
|
+
'ERR_BAD_RESPONSE',
|
|
62
|
+
'ERR_BAD_REQUEST',
|
|
63
|
+
'ERR_CANCELED',
|
|
64
|
+
'ERR_NOT_SUPPORT',
|
|
65
|
+
'ERR_INVALID_URL'
|
|
66
|
+
].forEach((code)=>{
|
|
67
|
+
descriptors[code] = {
|
|
68
|
+
value: code
|
|
69
|
+
};
|
|
70
|
+
});
|
|
71
|
+
Object.defineProperties(AxiosError, descriptors);
|
|
72
|
+
Object.defineProperty(prototype, 'isAxiosError', {
|
|
73
|
+
value: true
|
|
74
|
+
});
|
|
75
|
+
// eslint-disable-next-line func-names
|
|
76
|
+
AxiosError.from = (error, code, config, request, response, customProps)=>{
|
|
77
|
+
const axiosError = Object.create(prototype);
|
|
78
|
+
utils.toFlatObject(error, axiosError, function filter(obj) {
|
|
79
|
+
return obj !== Error.prototype;
|
|
80
|
+
}, (prop)=>{
|
|
81
|
+
return prop !== 'isAxiosError';
|
|
82
|
+
});
|
|
83
|
+
AxiosError.call(axiosError, error.message, code, config, request, response);
|
|
84
|
+
axiosError.cause = error;
|
|
85
|
+
axiosError.name = error.name;
|
|
86
|
+
customProps && Object.assign(axiosError, customProps);
|
|
87
|
+
return axiosError;
|
|
88
|
+
};
|
|
89
|
+
export default AxiosError;
|
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
var _computedKey, _computedKey1;
|
|
3
|
+
import utils from '../utils.js';
|
|
4
|
+
import parseHeaders from '../helpers/parseHeaders.js';
|
|
5
|
+
const $internals = Symbol('internals');
|
|
6
|
+
function normalizeHeader(header) {
|
|
7
|
+
return header && String(header).trim().toLowerCase();
|
|
8
|
+
}
|
|
9
|
+
function normalizeValue(value) {
|
|
10
|
+
if (value === false || value == null) {
|
|
11
|
+
return value;
|
|
12
|
+
}
|
|
13
|
+
return utils.isArray(value) ? value.map(normalizeValue) : String(value);
|
|
14
|
+
}
|
|
15
|
+
function parseTokens(str) {
|
|
16
|
+
const tokens = Object.create(null);
|
|
17
|
+
const tokensRE = /([^\s,;=]+)\s*(?:=\s*([^,;]+))?/g;
|
|
18
|
+
let match;
|
|
19
|
+
while(match = tokensRE.exec(str)){
|
|
20
|
+
tokens[match[1]] = match[2];
|
|
21
|
+
}
|
|
22
|
+
return tokens;
|
|
23
|
+
}
|
|
24
|
+
const isValidHeaderName = (str)=>/^[-_a-zA-Z0-9^`|~,!#$%&'*+.]+$/.test(str.trim());
|
|
25
|
+
function matchHeaderValue(context, value, header, filter, isHeaderNameFilter) {
|
|
26
|
+
if (utils.isFunction(filter)) {
|
|
27
|
+
return filter.call(this, value, header);
|
|
28
|
+
}
|
|
29
|
+
if (isHeaderNameFilter) {
|
|
30
|
+
value = header;
|
|
31
|
+
}
|
|
32
|
+
if (!utils.isString(value)) return;
|
|
33
|
+
if (utils.isString(filter)) {
|
|
34
|
+
return value.indexOf(filter) !== -1;
|
|
35
|
+
}
|
|
36
|
+
if (utils.isRegExp(filter)) {
|
|
37
|
+
return filter.test(value);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
function formatHeader(header) {
|
|
41
|
+
return header.trim().toLowerCase().replace(/([a-z\d])(\w*)/g, (w, char, str)=>{
|
|
42
|
+
return char.toUpperCase() + str;
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
function buildAccessors(obj, header) {
|
|
46
|
+
const accessorName = utils.toCamelCase(' ' + header);
|
|
47
|
+
[
|
|
48
|
+
'get',
|
|
49
|
+
'set',
|
|
50
|
+
'has'
|
|
51
|
+
].forEach((methodName)=>{
|
|
52
|
+
Object.defineProperty(obj, methodName + accessorName, {
|
|
53
|
+
value: function(arg1, arg2, arg3) {
|
|
54
|
+
return this[methodName].call(this, header, arg1, arg2, arg3);
|
|
55
|
+
},
|
|
56
|
+
configurable: true
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
_computedKey = Symbol.iterator, _computedKey1 = Symbol.toStringTag;
|
|
61
|
+
let AxiosHeaders = class AxiosHeaders {
|
|
62
|
+
constructor(headers){
|
|
63
|
+
headers && this.set(headers);
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
*
|
|
67
|
+
* @param {*} header
|
|
68
|
+
* @param {*} valueOrRewrite
|
|
69
|
+
* @param {*} rewrite true:无论源值是否存在均赋值,false:源值如存在不覆盖,
|
|
70
|
+
* undefined:源值不为 false 直接赋值,false 不赋值
|
|
71
|
+
* @returns
|
|
72
|
+
*/ set(header, valueOrRewrite, rewrite) {
|
|
73
|
+
const self = this;
|
|
74
|
+
function setHeader(_value, _header, _rewrite) {
|
|
75
|
+
const lHeader = normalizeHeader(_header);
|
|
76
|
+
if (!lHeader) {
|
|
77
|
+
throw new Error('header name must be a non-empty string');
|
|
78
|
+
}
|
|
79
|
+
const key = utils.findKey(self, lHeader);
|
|
80
|
+
if (!key || self[key] === undefined || _rewrite === true || _rewrite === undefined && self[key] !== false) {
|
|
81
|
+
self[key || _header] = normalizeValue(_value);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
const setHeaders = (headers, _rewrite)=>utils.forEach(headers, (_value, _header)=>setHeader(_value, _header, _rewrite));
|
|
85
|
+
if (utils.isPlainObject(header) || header instanceof this.constructor) {
|
|
86
|
+
setHeaders(header, valueOrRewrite);
|
|
87
|
+
} else if (utils.isString(header) && (header = header.trim()) && !isValidHeaderName(header)) {
|
|
88
|
+
setHeaders(parseHeaders(header), valueOrRewrite);
|
|
89
|
+
} else if (utils.isHeaders(header)) {
|
|
90
|
+
for (const [key, value] of header.entries()){
|
|
91
|
+
setHeader(value, key, rewrite);
|
|
92
|
+
}
|
|
93
|
+
} else {
|
|
94
|
+
header != null && setHeader(valueOrRewrite, header, rewrite);
|
|
95
|
+
}
|
|
96
|
+
return this;
|
|
97
|
+
}
|
|
98
|
+
get(header, parser) {
|
|
99
|
+
header = normalizeHeader(header);
|
|
100
|
+
if (header) {
|
|
101
|
+
const key = utils.findKey(this, header);
|
|
102
|
+
if (key) {
|
|
103
|
+
const value = this[key];
|
|
104
|
+
if (!parser) {
|
|
105
|
+
return value;
|
|
106
|
+
}
|
|
107
|
+
if (parser === true) {
|
|
108
|
+
return parseTokens(value);
|
|
109
|
+
}
|
|
110
|
+
if (utils.isFunction(parser)) {
|
|
111
|
+
return parser.call(this, value, key);
|
|
112
|
+
}
|
|
113
|
+
if (utils.isRegExp(parser)) {
|
|
114
|
+
return parser.exec(value);
|
|
115
|
+
}
|
|
116
|
+
throw new TypeError('parser must be boolean|regexp|function');
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
has(header, matcher) {
|
|
121
|
+
header = normalizeHeader(header);
|
|
122
|
+
if (header) {
|
|
123
|
+
const key = utils.findKey(this, header);
|
|
124
|
+
return !!(key && this[key] !== undefined && (!matcher || matchHeaderValue(this, this[key], key, matcher)));
|
|
125
|
+
}
|
|
126
|
+
return false;
|
|
127
|
+
}
|
|
128
|
+
delete(header, matcher) {
|
|
129
|
+
const self = this;
|
|
130
|
+
let deleted = false;
|
|
131
|
+
function deleteHeader(_header) {
|
|
132
|
+
_header = normalizeHeader(_header);
|
|
133
|
+
if (_header) {
|
|
134
|
+
const key = utils.findKey(self, _header);
|
|
135
|
+
if (key && (!matcher || matchHeaderValue(self, self[key], key, matcher))) {
|
|
136
|
+
delete self[key];
|
|
137
|
+
deleted = true;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
if (utils.isArray(header)) {
|
|
142
|
+
header.forEach(deleteHeader);
|
|
143
|
+
} else {
|
|
144
|
+
deleteHeader(header);
|
|
145
|
+
}
|
|
146
|
+
return deleted;
|
|
147
|
+
}
|
|
148
|
+
clear(matcher) {
|
|
149
|
+
const keys = Object.keys(this);
|
|
150
|
+
let i = keys.length;
|
|
151
|
+
let deleted = false;
|
|
152
|
+
while(i--){
|
|
153
|
+
const key = keys[i];
|
|
154
|
+
if (!matcher || matchHeaderValue(this, this[key], key, matcher, true)) {
|
|
155
|
+
delete this[key];
|
|
156
|
+
deleted = true;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
return deleted;
|
|
160
|
+
}
|
|
161
|
+
normalize(format) {
|
|
162
|
+
const self = this;
|
|
163
|
+
const headers = {};
|
|
164
|
+
utils.forEach(this, (value, header)=>{
|
|
165
|
+
const key = utils.findKey(headers, header);
|
|
166
|
+
if (key) {
|
|
167
|
+
self[key] = normalizeValue(value);
|
|
168
|
+
delete self[header];
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
const normalized = format ? formatHeader(header) : String(header).trim();
|
|
172
|
+
if (normalized !== header) {
|
|
173
|
+
delete self[header];
|
|
174
|
+
}
|
|
175
|
+
self[normalized] = normalizeValue(value);
|
|
176
|
+
headers[normalized] = true;
|
|
177
|
+
});
|
|
178
|
+
return this;
|
|
179
|
+
}
|
|
180
|
+
concat(...targets) {
|
|
181
|
+
return this.constructor.concat(this, ...targets);
|
|
182
|
+
}
|
|
183
|
+
toJSON(asStrings) {
|
|
184
|
+
const obj = Object.create(null);
|
|
185
|
+
utils.forEach(this, (value, header)=>{
|
|
186
|
+
value != null && value !== false && (obj[header] = asStrings && utils.isArray(value) ? value.join(', ') : value);
|
|
187
|
+
});
|
|
188
|
+
return obj;
|
|
189
|
+
}
|
|
190
|
+
[_computedKey]() {
|
|
191
|
+
return Object.entries(this.toJSON())[Symbol.iterator]();
|
|
192
|
+
}
|
|
193
|
+
toString() {
|
|
194
|
+
return Object.entries(this.toJSON()).map(([header, value])=>header + ': ' + value).join('\n');
|
|
195
|
+
}
|
|
196
|
+
get [_computedKey1]() {
|
|
197
|
+
return 'AxiosHeaders';
|
|
198
|
+
}
|
|
199
|
+
static from(thing) {
|
|
200
|
+
return thing instanceof this ? thing : new this(thing);
|
|
201
|
+
}
|
|
202
|
+
static concat(first, ...targets) {
|
|
203
|
+
const computed = new this(first);
|
|
204
|
+
targets.forEach((target)=>computed.set(target));
|
|
205
|
+
return computed;
|
|
206
|
+
}
|
|
207
|
+
static accessor(header) {
|
|
208
|
+
const internals = this[$internals] = this[$internals] = {
|
|
209
|
+
accessors: {}
|
|
210
|
+
};
|
|
211
|
+
const accessors = internals.accessors;
|
|
212
|
+
const prototype = this.prototype;
|
|
213
|
+
function defineAccessor(_header) {
|
|
214
|
+
const lHeader = normalizeHeader(_header);
|
|
215
|
+
if (!accessors[lHeader]) {
|
|
216
|
+
buildAccessors(prototype, _header);
|
|
217
|
+
accessors[lHeader] = true;
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
utils.isArray(header) ? header.forEach(defineAccessor) : defineAccessor(header);
|
|
221
|
+
return this;
|
|
222
|
+
}
|
|
223
|
+
};
|
|
224
|
+
AxiosHeaders.accessor([
|
|
225
|
+
'Content-Type',
|
|
226
|
+
'Content-Length',
|
|
227
|
+
'Accept',
|
|
228
|
+
'Accept-Encoding',
|
|
229
|
+
'User-Agent',
|
|
230
|
+
'Authorization'
|
|
231
|
+
]);
|
|
232
|
+
// reserved names hotfix
|
|
233
|
+
utils.reduceDescriptors(AxiosHeaders.prototype, ({ value }, key)=>{
|
|
234
|
+
let mapped = key[0].toUpperCase() + key.slice(1); // map `set` => `Set`
|
|
235
|
+
return {
|
|
236
|
+
get: ()=>value,
|
|
237
|
+
set (headerValue) {
|
|
238
|
+
this[mapped] = headerValue;
|
|
239
|
+
}
|
|
240
|
+
};
|
|
241
|
+
});
|
|
242
|
+
utils.freezeMethods(AxiosHeaders);
|
|
243
|
+
export default AxiosHeaders;
|