axios 0.27.2 → 1.0.0
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.
Potentially problematic release.
This version of axios might be problematic. Click here for more details.
- package/CHANGELOG.md +178 -940
- package/LICENSE +4 -16
- package/README.md +292 -93
- package/SECURITY.md +4 -3
- package/UPGRADE_GUIDE.md +1 -166
- package/bin/ssl_hotfix.js +22 -0
- package/dist/axios.js +2537 -2211
- package/dist/axios.js.map +1 -0
- package/dist/axios.min.js +2 -3
- package/dist/axios.min.js.map +1 -0
- package/dist/esm/axios.js +2942 -0
- package/dist/esm/axios.js.map +1 -0
- package/dist/esm/axios.min.js +2 -0
- package/dist/esm/axios.min.js.map +1 -0
- package/dist/node/axios.cjs +3750 -0
- package/dist/node/axios.cjs.map +1 -0
- package/gulpfile.js +88 -0
- package/index.d.ts +293 -70
- package/index.js +2 -1
- package/karma.conf.cjs +250 -0
- package/lib/adapters/http.js +371 -212
- package/lib/adapters/index.js +33 -0
- package/lib/adapters/xhr.js +81 -57
- package/lib/axios.js +34 -22
- package/lib/cancel/CancelToken.js +91 -89
- package/lib/cancel/CanceledError.js +9 -6
- package/lib/cancel/isCancel.js +2 -2
- package/lib/core/Axios.js +127 -99
- package/lib/core/AxiosError.js +22 -8
- package/lib/core/AxiosHeaders.js +274 -0
- package/lib/core/InterceptorManager.js +62 -45
- package/lib/core/buildFullPath.js +5 -4
- package/lib/core/dispatchRequest.js +21 -32
- package/lib/core/mergeConfig.js +8 -7
- package/lib/core/settle.js +6 -4
- package/lib/core/transformData.js +15 -9
- package/lib/defaults/index.js +77 -38
- package/lib/defaults/transitional.js +1 -1
- package/lib/env/classes/FormData.js +2 -0
- package/lib/env/data.js +1 -3
- package/lib/helpers/AxiosTransformStream.js +191 -0
- package/lib/helpers/AxiosURLSearchParams.js +58 -0
- package/lib/helpers/bind.js +3 -7
- package/lib/helpers/buildURL.js +24 -38
- package/lib/helpers/combineURLs.js +3 -2
- package/lib/helpers/cookies.js +43 -44
- package/lib/helpers/deprecatedMethod.js +4 -2
- package/lib/helpers/formDataToJSON.js +92 -0
- package/lib/helpers/fromDataURI.js +53 -0
- package/lib/helpers/isAbsoluteURL.js +3 -2
- package/lib/helpers/isAxiosError.js +4 -3
- package/lib/helpers/isURLSameOrigin.js +44 -45
- package/lib/helpers/null.js +1 -1
- package/lib/helpers/parseHeaders.js +24 -22
- package/lib/helpers/parseProtocol.js +3 -3
- package/lib/helpers/speedometer.js +55 -0
- package/lib/helpers/spread.js +3 -2
- package/lib/helpers/throttle.js +33 -0
- package/lib/helpers/toFormData.js +193 -36
- package/lib/helpers/toURLEncodedForm.js +18 -0
- package/lib/helpers/validator.js +20 -15
- package/lib/platform/browser/classes/FormData.js +3 -0
- package/lib/platform/browser/classes/URLSearchParams.js +4 -0
- package/lib/platform/browser/index.js +43 -0
- package/lib/platform/index.js +3 -0
- package/lib/platform/node/classes/FormData.js +3 -0
- package/lib/platform/node/classes/URLSearchParams.js +4 -0
- package/lib/platform/node/index.js +12 -0
- package/lib/utils.js +320 -177
- package/package.json +69 -23
- package/rollup.config.js +90 -0
- package/dist/axios.map +0 -1
- package/dist/axios.min.map +0 -1
- package/lib/defaults/env/FormData.js +0 -2
- package/lib/helpers/normalizeHeaderName.js +0 -12
@@ -0,0 +1,33 @@
|
|
1
|
+
import utils from '../utils.js';
|
2
|
+
import httpAdapter from './http.js';
|
3
|
+
import xhrAdapter from './xhr.js';
|
4
|
+
|
5
|
+
const adapters = {
|
6
|
+
http: httpAdapter,
|
7
|
+
xhr: xhrAdapter
|
8
|
+
}
|
9
|
+
|
10
|
+
export default {
|
11
|
+
getAdapter: (nameOrAdapter) => {
|
12
|
+
if(utils.isString(nameOrAdapter)){
|
13
|
+
const adapter = adapters[nameOrAdapter];
|
14
|
+
|
15
|
+
if (!nameOrAdapter) {
|
16
|
+
throw Error(
|
17
|
+
utils.hasOwnProp(nameOrAdapter) ?
|
18
|
+
`Adapter '${nameOrAdapter}' is not available in the build` :
|
19
|
+
`Can not resolve adapter '${nameOrAdapter}'`
|
20
|
+
);
|
21
|
+
}
|
22
|
+
|
23
|
+
return adapter
|
24
|
+
}
|
25
|
+
|
26
|
+
if (!utils.isFunction(nameOrAdapter)) {
|
27
|
+
throw new TypeError('adapter is not a function');
|
28
|
+
}
|
29
|
+
|
30
|
+
return nameOrAdapter;
|
31
|
+
},
|
32
|
+
adapters
|
33
|
+
}
|
package/lib/adapters/xhr.js
CHANGED
@@ -1,23 +1,53 @@
|
|
1
1
|
'use strict';
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
3
|
+
import utils from './../utils.js';
|
4
|
+
import settle from './../core/settle.js';
|
5
|
+
import cookies from './../helpers/cookies.js';
|
6
|
+
import buildURL from './../helpers/buildURL.js';
|
7
|
+
import buildFullPath from '../core/buildFullPath.js';
|
8
|
+
import isURLSameOrigin from './../helpers/isURLSameOrigin.js';
|
9
|
+
import transitionalDefaults from '../defaults/transitional.js';
|
10
|
+
import AxiosError from '../core/AxiosError.js';
|
11
|
+
import CanceledError from '../cancel/CanceledError.js';
|
12
|
+
import parseProtocol from '../helpers/parseProtocol.js';
|
13
|
+
import platform from '../platform/index.js';
|
14
|
+
import AxiosHeaders from '../core/AxiosHeaders.js';
|
15
|
+
import speedometer from '../helpers/speedometer.js';
|
16
|
+
|
17
|
+
function progressEventReducer(listener, isDownloadStream) {
|
18
|
+
let bytesNotified = 0;
|
19
|
+
const _speedometer = speedometer(50, 250);
|
20
|
+
|
21
|
+
return e => {
|
22
|
+
const loaded = e.loaded;
|
23
|
+
const total = e.lengthComputable ? e.total : undefined;
|
24
|
+
const progressBytes = loaded - bytesNotified;
|
25
|
+
const rate = _speedometer(progressBytes);
|
26
|
+
const inRange = loaded <= total;
|
27
|
+
|
28
|
+
bytesNotified = loaded;
|
29
|
+
|
30
|
+
const data = {
|
31
|
+
loaded,
|
32
|
+
total,
|
33
|
+
progress: total ? (loaded / total) : undefined,
|
34
|
+
bytes: progressBytes,
|
35
|
+
rate: rate ? rate : undefined,
|
36
|
+
estimated: rate && total && inRange ? (total - loaded) / rate : undefined
|
37
|
+
};
|
38
|
+
|
39
|
+
data[isDownloadStream ? 'download' : 'upload'] = true;
|
40
|
+
|
41
|
+
listener(data);
|
42
|
+
};
|
43
|
+
}
|
44
|
+
|
45
|
+
export default function xhrAdapter(config) {
|
16
46
|
return new Promise(function dispatchXhrRequest(resolve, reject) {
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
47
|
+
let requestData = config.data;
|
48
|
+
const requestHeaders = AxiosHeaders.from(config.headers).normalize();
|
49
|
+
const responseType = config.responseType;
|
50
|
+
let onCanceled;
|
21
51
|
function done() {
|
22
52
|
if (config.cancelToken) {
|
23
53
|
config.cancelToken.unsubscribe(onCanceled);
|
@@ -28,20 +58,20 @@ module.exports = function xhrAdapter(config) {
|
|
28
58
|
}
|
29
59
|
}
|
30
60
|
|
31
|
-
if (utils.isFormData(requestData) &&
|
32
|
-
|
61
|
+
if (utils.isFormData(requestData) && platform.isStandardBrowserEnv) {
|
62
|
+
requestHeaders.setContentType(false); // Let the browser set it
|
33
63
|
}
|
34
64
|
|
35
|
-
|
65
|
+
let request = new XMLHttpRequest();
|
36
66
|
|
37
67
|
// HTTP basic authentication
|
38
68
|
if (config.auth) {
|
39
|
-
|
40
|
-
|
41
|
-
requestHeaders.Authorization
|
69
|
+
const username = config.auth.username || '';
|
70
|
+
const password = config.auth.password ? unescape(encodeURIComponent(config.auth.password)) : '';
|
71
|
+
requestHeaders.set('Authorization', 'Basic ' + btoa(username + ':' + password));
|
42
72
|
}
|
43
73
|
|
44
|
-
|
74
|
+
const fullPath = buildFullPath(config.baseURL, config.url);
|
45
75
|
|
46
76
|
request.open(config.method.toUpperCase(), buildURL(fullPath, config.params, config.paramsSerializer), true);
|
47
77
|
|
@@ -53,16 +83,18 @@ module.exports = function xhrAdapter(config) {
|
|
53
83
|
return;
|
54
84
|
}
|
55
85
|
// Prepare the response
|
56
|
-
|
57
|
-
|
86
|
+
const responseHeaders = AxiosHeaders.from(
|
87
|
+
'getAllResponseHeaders' in request && request.getAllResponseHeaders()
|
88
|
+
);
|
89
|
+
const responseData = !responseType || responseType === 'text' || responseType === 'json' ?
|
58
90
|
request.responseText : request.response;
|
59
|
-
|
91
|
+
const response = {
|
60
92
|
data: responseData,
|
61
93
|
status: request.status,
|
62
94
|
statusText: request.statusText,
|
63
95
|
headers: responseHeaders,
|
64
|
-
config
|
65
|
-
request
|
96
|
+
config,
|
97
|
+
request
|
66
98
|
};
|
67
99
|
|
68
100
|
settle(function _resolve(value) {
|
@@ -116,7 +148,7 @@ module.exports = function xhrAdapter(config) {
|
|
116
148
|
request.onerror = function handleError() {
|
117
149
|
// Real errors are hidden from us by the browser
|
118
150
|
// onerror should only fire if it's a network error
|
119
|
-
reject(new AxiosError('Network Error', AxiosError.ERR_NETWORK, config, request
|
151
|
+
reject(new AxiosError('Network Error', AxiosError.ERR_NETWORK, config, request));
|
120
152
|
|
121
153
|
// Clean up request
|
122
154
|
request = null;
|
@@ -124,8 +156,8 @@ module.exports = function xhrAdapter(config) {
|
|
124
156
|
|
125
157
|
// Handle timeout
|
126
158
|
request.ontimeout = function handleTimeout() {
|
127
|
-
|
128
|
-
|
159
|
+
let timeoutErrorMessage = config.timeout ? 'timeout of ' + config.timeout + 'ms exceeded' : 'timeout exceeded';
|
160
|
+
const transitional = config.transitional || transitionalDefaults;
|
129
161
|
if (config.timeoutErrorMessage) {
|
130
162
|
timeoutErrorMessage = config.timeoutErrorMessage;
|
131
163
|
}
|
@@ -142,27 +174,23 @@ module.exports = function xhrAdapter(config) {
|
|
142
174
|
// Add xsrf header
|
143
175
|
// This is only done if running in a standard browser environment.
|
144
176
|
// Specifically not if we're in a web worker, or react-native.
|
145
|
-
if (
|
177
|
+
if (platform.isStandardBrowserEnv) {
|
146
178
|
// Add xsrf header
|
147
|
-
|
148
|
-
cookies.read(config.xsrfCookieName)
|
149
|
-
undefined;
|
179
|
+
const xsrfValue = (config.withCredentials || isURLSameOrigin(fullPath))
|
180
|
+
&& config.xsrfCookieName && cookies.read(config.xsrfCookieName);
|
150
181
|
|
151
182
|
if (xsrfValue) {
|
152
|
-
requestHeaders
|
183
|
+
requestHeaders.set(config.xsrfHeaderName, xsrfValue);
|
153
184
|
}
|
154
185
|
}
|
155
186
|
|
187
|
+
// Remove Content-Type if data is undefined
|
188
|
+
requestData === undefined && requestHeaders.setContentType(null);
|
189
|
+
|
156
190
|
// Add headers to the request
|
157
191
|
if ('setRequestHeader' in request) {
|
158
|
-
utils.forEach(requestHeaders, function setRequestHeader(val, key) {
|
159
|
-
|
160
|
-
// Remove Content-Type if data is undefined
|
161
|
-
delete requestHeaders[key];
|
162
|
-
} else {
|
163
|
-
// Otherwise add header to the request
|
164
|
-
request.setRequestHeader(key, val);
|
165
|
-
}
|
192
|
+
utils.forEach(requestHeaders.toJSON(), function setRequestHeader(val, key) {
|
193
|
+
request.setRequestHeader(key, val);
|
166
194
|
});
|
167
195
|
}
|
168
196
|
|
@@ -178,22 +206,22 @@ module.exports = function xhrAdapter(config) {
|
|
178
206
|
|
179
207
|
// Handle progress if needed
|
180
208
|
if (typeof config.onDownloadProgress === 'function') {
|
181
|
-
request.addEventListener('progress', config.onDownloadProgress);
|
209
|
+
request.addEventListener('progress', progressEventReducer(config.onDownloadProgress, true));
|
182
210
|
}
|
183
211
|
|
184
212
|
// Not all browsers support upload events
|
185
213
|
if (typeof config.onUploadProgress === 'function' && request.upload) {
|
186
|
-
request.upload.addEventListener('progress', config.onUploadProgress);
|
214
|
+
request.upload.addEventListener('progress', progressEventReducer(config.onUploadProgress));
|
187
215
|
}
|
188
216
|
|
189
217
|
if (config.cancelToken || config.signal) {
|
190
218
|
// Handle cancellation
|
191
219
|
// eslint-disable-next-line func-names
|
192
|
-
onCanceled =
|
220
|
+
onCanceled = cancel => {
|
193
221
|
if (!request) {
|
194
222
|
return;
|
195
223
|
}
|
196
|
-
reject(!cancel ||
|
224
|
+
reject(!cancel || cancel.type ? new CanceledError(null, config, request) : cancel);
|
197
225
|
request.abort();
|
198
226
|
request = null;
|
199
227
|
};
|
@@ -204,19 +232,15 @@ module.exports = function xhrAdapter(config) {
|
|
204
232
|
}
|
205
233
|
}
|
206
234
|
|
207
|
-
|
208
|
-
requestData = null;
|
209
|
-
}
|
210
|
-
|
211
|
-
var protocol = parseProtocol(fullPath);
|
235
|
+
const protocol = parseProtocol(fullPath);
|
212
236
|
|
213
|
-
if (protocol &&
|
237
|
+
if (protocol && platform.protocols.indexOf(protocol) === -1) {
|
214
238
|
reject(new AxiosError('Unsupported protocol ' + protocol + ':', AxiosError.ERR_BAD_REQUEST, config));
|
215
239
|
return;
|
216
240
|
}
|
217
241
|
|
218
242
|
|
219
243
|
// Send the request
|
220
|
-
request.send(requestData);
|
244
|
+
request.send(requestData || null);
|
221
245
|
});
|
222
|
-
}
|
246
|
+
}
|
package/lib/axios.js
CHANGED
@@ -1,26 +1,36 @@
|
|
1
1
|
'use strict';
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
3
|
+
import utils from './utils.js';
|
4
|
+
import bind from './helpers/bind.js';
|
5
|
+
import Axios from './core/Axios.js';
|
6
|
+
import mergeConfig from './core/mergeConfig.js';
|
7
|
+
import defaults from './defaults/index.js';
|
8
|
+
import formDataToJSON from './helpers/formDataToJSON.js';
|
9
|
+
import CanceledError from './cancel/CanceledError.js';
|
10
|
+
import CancelToken from'./cancel/CancelToken.js';
|
11
|
+
import isCancel from'./cancel/isCancel.js';
|
12
|
+
import {VERSION} from './env/data.js';
|
13
|
+
import toFormData from './helpers/toFormData.js';
|
14
|
+
import AxiosError from './core/AxiosError.js';
|
15
|
+
import spread from './helpers/spread.js';
|
16
|
+
import isAxiosError from './helpers/isAxiosError.js';
|
8
17
|
|
9
18
|
/**
|
10
19
|
* Create an instance of Axios
|
11
20
|
*
|
12
21
|
* @param {Object} defaultConfig The default config for the instance
|
13
|
-
*
|
22
|
+
*
|
23
|
+
* @returns {Axios} A new instance of Axios
|
14
24
|
*/
|
15
25
|
function createInstance(defaultConfig) {
|
16
|
-
|
17
|
-
|
26
|
+
const context = new Axios(defaultConfig);
|
27
|
+
const instance = bind(Axios.prototype.request, context);
|
18
28
|
|
19
29
|
// Copy axios.prototype to instance
|
20
|
-
utils.extend(instance, Axios.prototype, context);
|
30
|
+
utils.extend(instance, Axios.prototype, context, {allOwnKeys: true});
|
21
31
|
|
22
32
|
// Copy context to instance
|
23
|
-
utils.extend(instance, context);
|
33
|
+
utils.extend(instance, context, null, {allOwnKeys: true});
|
24
34
|
|
25
35
|
// Factory for creating new instances
|
26
36
|
instance.create = function create(instanceConfig) {
|
@@ -31,20 +41,20 @@ function createInstance(defaultConfig) {
|
|
31
41
|
}
|
32
42
|
|
33
43
|
// Create the default instance to be exported
|
34
|
-
|
44
|
+
const axios = createInstance(defaults);
|
35
45
|
|
36
46
|
// Expose Axios class to allow class inheritance
|
37
47
|
axios.Axios = Axios;
|
38
48
|
|
39
49
|
// Expose Cancel & CancelToken
|
40
|
-
axios.CanceledError =
|
41
|
-
axios.CancelToken =
|
42
|
-
axios.isCancel =
|
43
|
-
axios.VERSION =
|
44
|
-
axios.toFormData =
|
50
|
+
axios.CanceledError = CanceledError;
|
51
|
+
axios.CancelToken = CancelToken;
|
52
|
+
axios.isCancel = isCancel;
|
53
|
+
axios.VERSION = VERSION;
|
54
|
+
axios.toFormData = toFormData;
|
45
55
|
|
46
56
|
// Expose AxiosError class
|
47
|
-
axios.AxiosError =
|
57
|
+
axios.AxiosError = AxiosError;
|
48
58
|
|
49
59
|
// alias for CanceledError for backward compatibility
|
50
60
|
axios.Cancel = axios.CanceledError;
|
@@ -53,12 +63,14 @@ axios.Cancel = axios.CanceledError;
|
|
53
63
|
axios.all = function all(promises) {
|
54
64
|
return Promise.all(promises);
|
55
65
|
};
|
56
|
-
|
66
|
+
|
67
|
+
axios.spread = spread;
|
57
68
|
|
58
69
|
// Expose isAxiosError
|
59
|
-
axios.isAxiosError =
|
70
|
+
axios.isAxiosError = isAxiosError;
|
60
71
|
|
61
|
-
|
72
|
+
axios.formToJSON = thing => {
|
73
|
+
return formDataToJSON(utils.isHTMLForm(thing) ? new FormData(thing) : thing);
|
74
|
+
};
|
62
75
|
|
63
|
-
|
64
|
-
module.exports.default = axios;
|
76
|
+
export default axios;
|
@@ -1,119 +1,121 @@
|
|
1
1
|
'use strict';
|
2
2
|
|
3
|
-
|
3
|
+
import CanceledError from './CanceledError.js';
|
4
4
|
|
5
5
|
/**
|
6
6
|
* A `CancelToken` is an object that can be used to request cancellation of an operation.
|
7
7
|
*
|
8
|
-
* @class
|
9
8
|
* @param {Function} executor The executor function.
|
9
|
+
*
|
10
|
+
* @returns {CancelToken}
|
10
11
|
*/
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
12
|
+
class CancelToken {
|
13
|
+
constructor(executor) {
|
14
|
+
if (typeof executor !== 'function') {
|
15
|
+
throw new TypeError('executor must be a function.');
|
16
|
+
}
|
15
17
|
|
16
|
-
|
18
|
+
let resolvePromise;
|
17
19
|
|
18
|
-
|
19
|
-
|
20
|
-
|
20
|
+
this.promise = new Promise(function promiseExecutor(resolve) {
|
21
|
+
resolvePromise = resolve;
|
22
|
+
});
|
21
23
|
|
22
|
-
|
24
|
+
const token = this;
|
23
25
|
|
24
|
-
|
25
|
-
|
26
|
-
|
26
|
+
// eslint-disable-next-line func-names
|
27
|
+
this.promise.then(cancel => {
|
28
|
+
if (!token._listeners) return;
|
27
29
|
|
28
|
-
|
29
|
-
var l = token._listeners.length;
|
30
|
+
let i = token._listeners.length;
|
30
31
|
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
32
|
+
while (i-- > 0) {
|
33
|
+
token._listeners[i](cancel);
|
34
|
+
}
|
35
|
+
token._listeners = null;
|
36
|
+
});
|
36
37
|
|
37
|
-
// eslint-disable-next-line func-names
|
38
|
-
this.promise.then = function(onfulfilled) {
|
39
|
-
var _resolve;
|
40
38
|
// eslint-disable-next-line func-names
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
39
|
+
this.promise.then = onfulfilled => {
|
40
|
+
let _resolve;
|
41
|
+
// eslint-disable-next-line func-names
|
42
|
+
const promise = new Promise(resolve => {
|
43
|
+
token.subscribe(resolve);
|
44
|
+
_resolve = resolve;
|
45
|
+
}).then(onfulfilled);
|
46
|
+
|
47
|
+
promise.cancel = function reject() {
|
48
|
+
token.unsubscribe(_resolve);
|
49
|
+
};
|
50
|
+
|
51
|
+
return promise;
|
48
52
|
};
|
49
53
|
|
50
|
-
|
51
|
-
|
54
|
+
executor(function cancel(message, config, request) {
|
55
|
+
if (token.reason) {
|
56
|
+
// Cancellation has already been requested
|
57
|
+
return;
|
58
|
+
}
|
52
59
|
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
}
|
58
|
-
|
59
|
-
token.reason = new CanceledError(message);
|
60
|
-
resolvePromise(token.reason);
|
61
|
-
});
|
62
|
-
}
|
60
|
+
token.reason = new CanceledError(message, config, request);
|
61
|
+
resolvePromise(token.reason);
|
62
|
+
});
|
63
|
+
}
|
63
64
|
|
64
|
-
/**
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
65
|
+
/**
|
66
|
+
* Throws a `CanceledError` if cancellation has been requested.
|
67
|
+
*/
|
68
|
+
throwIfRequested() {
|
69
|
+
if (this.reason) {
|
70
|
+
throw this.reason;
|
71
|
+
}
|
70
72
|
}
|
71
|
-
};
|
72
73
|
|
73
|
-
/**
|
74
|
-
|
75
|
-
|
74
|
+
/**
|
75
|
+
* Subscribe to the cancel signal
|
76
|
+
*/
|
76
77
|
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
78
|
+
subscribe(listener) {
|
79
|
+
if (this.reason) {
|
80
|
+
listener(this.reason);
|
81
|
+
return;
|
82
|
+
}
|
82
83
|
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
84
|
+
if (this._listeners) {
|
85
|
+
this._listeners.push(listener);
|
86
|
+
} else {
|
87
|
+
this._listeners = [listener];
|
88
|
+
}
|
87
89
|
}
|
88
|
-
};
|
89
90
|
|
90
|
-
/**
|
91
|
-
|
92
|
-
|
91
|
+
/**
|
92
|
+
* Unsubscribe from the cancel signal
|
93
|
+
*/
|
93
94
|
|
94
|
-
|
95
|
-
|
96
|
-
|
95
|
+
unsubscribe(listener) {
|
96
|
+
if (!this._listeners) {
|
97
|
+
return;
|
98
|
+
}
|
99
|
+
const index = this._listeners.indexOf(listener);
|
100
|
+
if (index !== -1) {
|
101
|
+
this._listeners.splice(index, 1);
|
102
|
+
}
|
97
103
|
}
|
98
|
-
|
99
|
-
|
100
|
-
|
104
|
+
|
105
|
+
/**
|
106
|
+
* Returns an object that contains a new `CancelToken` and a function that, when called,
|
107
|
+
* cancels the `CancelToken`.
|
108
|
+
*/
|
109
|
+
static source() {
|
110
|
+
let cancel;
|
111
|
+
const token = new CancelToken(function executor(c) {
|
112
|
+
cancel = c;
|
113
|
+
});
|
114
|
+
return {
|
115
|
+
token,
|
116
|
+
cancel
|
117
|
+
};
|
101
118
|
}
|
102
|
-
}
|
119
|
+
}
|
103
120
|
|
104
|
-
|
105
|
-
* Returns an object that contains a new `CancelToken` and a function that, when called,
|
106
|
-
* cancels the `CancelToken`.
|
107
|
-
*/
|
108
|
-
CancelToken.source = function source() {
|
109
|
-
var cancel;
|
110
|
-
var token = new CancelToken(function executor(c) {
|
111
|
-
cancel = c;
|
112
|
-
});
|
113
|
-
return {
|
114
|
-
token: token,
|
115
|
-
cancel: cancel
|
116
|
-
};
|
117
|
-
};
|
118
|
-
|
119
|
-
module.exports = CancelToken;
|
121
|
+
export default CancelToken;
|
@@ -1,17 +1,20 @@
|
|
1
1
|
'use strict';
|
2
2
|
|
3
|
-
|
4
|
-
|
3
|
+
import AxiosError from '../core/AxiosError.js';
|
4
|
+
import utils from '../utils.js';
|
5
5
|
|
6
6
|
/**
|
7
7
|
* A `CanceledError` is an object that is thrown when an operation is canceled.
|
8
8
|
*
|
9
|
-
* @class
|
10
9
|
* @param {string=} message The message.
|
10
|
+
* @param {Object=} config The config.
|
11
|
+
* @param {Object=} request The request.
|
12
|
+
*
|
13
|
+
* @returns {CanceledError} The created error.
|
11
14
|
*/
|
12
|
-
function CanceledError(message) {
|
15
|
+
function CanceledError(message, config, request) {
|
13
16
|
// eslint-disable-next-line no-eq-null,eqeqeq
|
14
|
-
AxiosError.call(this, message == null ? 'canceled' : message, AxiosError.ERR_CANCELED);
|
17
|
+
AxiosError.call(this, message == null ? 'canceled' : message, AxiosError.ERR_CANCELED, config, request);
|
15
18
|
this.name = 'CanceledError';
|
16
19
|
}
|
17
20
|
|
@@ -19,4 +22,4 @@ utils.inherits(CanceledError, AxiosError, {
|
|
19
22
|
__CANCEL__: true
|
20
23
|
});
|
21
24
|
|
22
|
-
|
25
|
+
export default CanceledError;
|
package/lib/cancel/isCancel.js
CHANGED