@lcap/axios-fixed 1.13.2
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/.husky/commit-msg +4 -0
- package/CHANGELOG.md +1359 -0
- package/LICENSE +7 -0
- package/MIGRATION_GUIDE.md +3 -0
- package/README.md +1784 -0
- package/dist/axios.js +4471 -0
- package/dist/axios.js.map +1 -0
- package/dist/axios.min.js +3 -0
- package/dist/axios.min.js.map +1 -0
- package/dist/browser/axios.cjs +3909 -0
- package/dist/browser/axios.cjs.map +1 -0
- package/dist/esm/axios.js +3932 -0
- package/dist/esm/axios.js.map +1 -0
- package/dist/esm/axios.min.js +3 -0
- package/dist/esm/axios.min.js.map +1 -0
- package/dist/node/axios.cjs +5242 -0
- package/dist/node/axios.cjs.map +1 -0
- package/index.d.cts +572 -0
- package/index.d.ts +585 -0
- package/index.js +43 -0
- package/lib/adapters/README.md +37 -0
- package/lib/adapters/adapters.js +126 -0
- package/lib/adapters/fetch.js +288 -0
- package/lib/adapters/http.js +895 -0
- package/lib/adapters/xhr.js +200 -0
- package/lib/axios.js +89 -0
- package/lib/cancel/CancelToken.js +135 -0
- package/lib/cancel/CanceledError.js +25 -0
- package/lib/cancel/isCancel.js +5 -0
- package/lib/core/Axios.js +240 -0
- package/lib/core/AxiosError.js +110 -0
- package/lib/core/AxiosHeaders.js +314 -0
- package/lib/core/InterceptorManager.js +71 -0
- package/lib/core/README.md +8 -0
- package/lib/core/buildFullPath.js +22 -0
- package/lib/core/dispatchRequest.js +81 -0
- package/lib/core/mergeConfig.js +106 -0
- package/lib/core/settle.js +27 -0
- package/lib/core/transformData.js +28 -0
- package/lib/defaults/index.js +161 -0
- package/lib/defaults/transitional.js +7 -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 +143 -0
- package/lib/helpers/AxiosURLSearchParams.js +58 -0
- package/lib/helpers/HttpStatusCode.js +77 -0
- package/lib/helpers/README.md +7 -0
- package/lib/helpers/ZlibHeaderTransformStream.js +28 -0
- package/lib/helpers/bind.js +14 -0
- package/lib/helpers/buildURL.js +67 -0
- package/lib/helpers/callbackify.js +16 -0
- package/lib/helpers/combineURLs.js +15 -0
- package/lib/helpers/composeSignals.js +48 -0
- package/lib/helpers/cookies.js +53 -0
- package/lib/helpers/deprecatedMethod.js +26 -0
- package/lib/helpers/estimateDataURLDecodedBytes.js +73 -0
- package/lib/helpers/formDataToJSON.js +95 -0
- package/lib/helpers/formDataToStream.js +112 -0
- package/lib/helpers/fromDataURI.js +53 -0
- package/lib/helpers/isAbsoluteURL.js +15 -0
- package/lib/helpers/isAxiosError.js +14 -0
- package/lib/helpers/isURLSameOrigin.js +14 -0
- package/lib/helpers/null.js +2 -0
- package/lib/helpers/parseHeaders.js +55 -0
- package/lib/helpers/parseProtocol.js +6 -0
- package/lib/helpers/progressEventReducer.js +44 -0
- package/lib/helpers/readBlob.js +15 -0
- package/lib/helpers/resolveConfig.js +61 -0
- package/lib/helpers/speedometer.js +55 -0
- package/lib/helpers/spread.js +28 -0
- package/lib/helpers/throttle.js +44 -0
- package/lib/helpers/toFormData.js +223 -0
- package/lib/helpers/toURLEncodedForm.js +19 -0
- package/lib/helpers/trackStream.js +87 -0
- package/lib/helpers/validator.js +99 -0
- package/lib/platform/browser/classes/Blob.js +3 -0
- package/lib/platform/browser/classes/FormData.js +3 -0
- package/lib/platform/browser/classes/URLSearchParams.js +4 -0
- package/lib/platform/browser/index.js +13 -0
- package/lib/platform/common/utils.js +51 -0
- package/lib/platform/index.js +7 -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 +38 -0
- package/lib/utils.js +782 -0
- package/package.json +237 -0
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import platform from "../platform/index.js";
|
|
2
|
+
import utils from "../utils.js";
|
|
3
|
+
import isURLSameOrigin from "./isURLSameOrigin.js";
|
|
4
|
+
import cookies from "./cookies.js";
|
|
5
|
+
import buildFullPath from "../core/buildFullPath.js";
|
|
6
|
+
import mergeConfig from "../core/mergeConfig.js";
|
|
7
|
+
import AxiosHeaders from "../core/AxiosHeaders.js";
|
|
8
|
+
import buildURL from "./buildURL.js";
|
|
9
|
+
|
|
10
|
+
export default (config) => {
|
|
11
|
+
const newConfig = mergeConfig({}, config);
|
|
12
|
+
|
|
13
|
+
let { data, withXSRFToken, xsrfHeaderName, xsrfCookieName, headers, auth } = newConfig;
|
|
14
|
+
|
|
15
|
+
newConfig.headers = headers = AxiosHeaders.from(headers);
|
|
16
|
+
|
|
17
|
+
newConfig.url = buildURL(buildFullPath(newConfig.baseURL, newConfig.url, newConfig.allowAbsoluteUrls), config.params, config.paramsSerializer);
|
|
18
|
+
|
|
19
|
+
// HTTP basic authentication
|
|
20
|
+
if (auth) {
|
|
21
|
+
headers.set('Authorization', 'Basic ' +
|
|
22
|
+
btoa((auth.username || '') + ':' + (auth.password ? unescape(encodeURIComponent(auth.password)) : ''))
|
|
23
|
+
);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
if (utils.isFormData(data)) {
|
|
27
|
+
if (platform.hasStandardBrowserEnv || platform.hasStandardBrowserWebWorkerEnv) {
|
|
28
|
+
headers.setContentType(undefined); // browser handles it
|
|
29
|
+
} else if (utils.isFunction(data.getHeaders)) {
|
|
30
|
+
// Node.js FormData (like form-data package)
|
|
31
|
+
const formHeaders = data.getHeaders();
|
|
32
|
+
// Only set safe headers to avoid overwriting security headers
|
|
33
|
+
const allowedHeaders = ['content-type', 'content-length'];
|
|
34
|
+
Object.entries(formHeaders).forEach(([key, val]) => {
|
|
35
|
+
if (allowedHeaders.includes(key.toLowerCase())) {
|
|
36
|
+
headers.set(key, val);
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// Add xsrf header
|
|
43
|
+
// This is only done if running in a standard browser environment.
|
|
44
|
+
// Specifically not if we're in a web worker, or react-native.
|
|
45
|
+
|
|
46
|
+
if (platform.hasStandardBrowserEnv) {
|
|
47
|
+
withXSRFToken && utils.isFunction(withXSRFToken) && (withXSRFToken = withXSRFToken(newConfig));
|
|
48
|
+
|
|
49
|
+
if (withXSRFToken || (withXSRFToken !== false && isURLSameOrigin(newConfig.url))) {
|
|
50
|
+
// Add xsrf header
|
|
51
|
+
const xsrfValue = xsrfHeaderName && xsrfCookieName && cookies.read(xsrfCookieName);
|
|
52
|
+
|
|
53
|
+
if (xsrfValue) {
|
|
54
|
+
headers.set(xsrfHeaderName, xsrfValue);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return newConfig;
|
|
60
|
+
}
|
|
61
|
+
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Calculate data maxRate
|
|
5
|
+
* @param {Number} [samplesCount= 10]
|
|
6
|
+
* @param {Number} [min= 1000]
|
|
7
|
+
* @returns {Function}
|
|
8
|
+
*/
|
|
9
|
+
function speedometer(samplesCount, min) {
|
|
10
|
+
samplesCount = samplesCount || 10;
|
|
11
|
+
const bytes = new Array(samplesCount);
|
|
12
|
+
const timestamps = new Array(samplesCount);
|
|
13
|
+
let head = 0;
|
|
14
|
+
let tail = 0;
|
|
15
|
+
let firstSampleTS;
|
|
16
|
+
|
|
17
|
+
min = min !== undefined ? min : 1000;
|
|
18
|
+
|
|
19
|
+
return function push(chunkLength) {
|
|
20
|
+
const now = Date.now();
|
|
21
|
+
|
|
22
|
+
const startedAt = timestamps[tail];
|
|
23
|
+
|
|
24
|
+
if (!firstSampleTS) {
|
|
25
|
+
firstSampleTS = now;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
bytes[head] = chunkLength;
|
|
29
|
+
timestamps[head] = now;
|
|
30
|
+
|
|
31
|
+
let i = tail;
|
|
32
|
+
let bytesCount = 0;
|
|
33
|
+
|
|
34
|
+
while (i !== head) {
|
|
35
|
+
bytesCount += bytes[i++];
|
|
36
|
+
i = i % samplesCount;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
head = (head + 1) % samplesCount;
|
|
40
|
+
|
|
41
|
+
if (head === tail) {
|
|
42
|
+
tail = (tail + 1) % samplesCount;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if (now - firstSampleTS < min) {
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const passed = startedAt && now - startedAt;
|
|
50
|
+
|
|
51
|
+
return passed ? Math.round(bytesCount * 1000 / passed) : undefined;
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export default speedometer;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Syntactic sugar for invoking a function and expanding an array for arguments.
|
|
5
|
+
*
|
|
6
|
+
* Common use case would be to use `Function.prototype.apply`.
|
|
7
|
+
*
|
|
8
|
+
* ```js
|
|
9
|
+
* function f(x, y, z) {}
|
|
10
|
+
* var args = [1, 2, 3];
|
|
11
|
+
* f.apply(null, args);
|
|
12
|
+
* ```
|
|
13
|
+
*
|
|
14
|
+
* With `spread` this example can be re-written.
|
|
15
|
+
*
|
|
16
|
+
* ```js
|
|
17
|
+
* spread(function(x, y, z) {})([1, 2, 3]);
|
|
18
|
+
* ```
|
|
19
|
+
*
|
|
20
|
+
* @param {Function} callback
|
|
21
|
+
*
|
|
22
|
+
* @returns {Function}
|
|
23
|
+
*/
|
|
24
|
+
export default function spread(callback) {
|
|
25
|
+
return function wrap(arr) {
|
|
26
|
+
return callback.apply(null, arr);
|
|
27
|
+
};
|
|
28
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Throttle decorator
|
|
3
|
+
* @param {Function} fn
|
|
4
|
+
* @param {Number} freq
|
|
5
|
+
* @return {Function}
|
|
6
|
+
*/
|
|
7
|
+
function throttle(fn, freq) {
|
|
8
|
+
let timestamp = 0;
|
|
9
|
+
let threshold = 1000 / freq;
|
|
10
|
+
let lastArgs;
|
|
11
|
+
let timer;
|
|
12
|
+
|
|
13
|
+
const invoke = (args, now = Date.now()) => {
|
|
14
|
+
timestamp = now;
|
|
15
|
+
lastArgs = null;
|
|
16
|
+
if (timer) {
|
|
17
|
+
clearTimeout(timer);
|
|
18
|
+
timer = null;
|
|
19
|
+
}
|
|
20
|
+
fn(...args);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const throttled = (...args) => {
|
|
24
|
+
const now = Date.now();
|
|
25
|
+
const passed = now - timestamp;
|
|
26
|
+
if ( passed >= threshold) {
|
|
27
|
+
invoke(args, now);
|
|
28
|
+
} else {
|
|
29
|
+
lastArgs = args;
|
|
30
|
+
if (!timer) {
|
|
31
|
+
timer = setTimeout(() => {
|
|
32
|
+
timer = null;
|
|
33
|
+
invoke(lastArgs)
|
|
34
|
+
}, threshold - passed);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const flush = () => lastArgs && invoke(lastArgs);
|
|
40
|
+
|
|
41
|
+
return [throttled, flush];
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export default throttle;
|
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
import utils from '../utils.js';
|
|
4
|
+
import AxiosError from '../core/AxiosError.js';
|
|
5
|
+
// temporary hotfix to avoid circular references until AxiosURLSearchParams is refactored
|
|
6
|
+
import PlatformFormData from '../platform/node/classes/FormData.js';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Determines if the given thing is a array or js object.
|
|
10
|
+
*
|
|
11
|
+
* @param {string} thing - The object or array to be visited.
|
|
12
|
+
*
|
|
13
|
+
* @returns {boolean}
|
|
14
|
+
*/
|
|
15
|
+
function isVisitable(thing) {
|
|
16
|
+
return utils.isPlainObject(thing) || utils.isArray(thing);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* It removes the brackets from the end of a string
|
|
21
|
+
*
|
|
22
|
+
* @param {string} key - The key of the parameter.
|
|
23
|
+
*
|
|
24
|
+
* @returns {string} the key without the brackets.
|
|
25
|
+
*/
|
|
26
|
+
function removeBrackets(key) {
|
|
27
|
+
return utils.endsWith(key, '[]') ? key.slice(0, -2) : key;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* It takes a path, a key, and a boolean, and returns a string
|
|
32
|
+
*
|
|
33
|
+
* @param {string} path - The path to the current key.
|
|
34
|
+
* @param {string} key - The key of the current object being iterated over.
|
|
35
|
+
* @param {string} dots - If true, the key will be rendered with dots instead of brackets.
|
|
36
|
+
*
|
|
37
|
+
* @returns {string} The path to the current key.
|
|
38
|
+
*/
|
|
39
|
+
function renderKey(path, key, dots) {
|
|
40
|
+
if (!path) return key;
|
|
41
|
+
return path.concat(key).map(function each(token, i) {
|
|
42
|
+
// eslint-disable-next-line no-param-reassign
|
|
43
|
+
token = removeBrackets(token);
|
|
44
|
+
return !dots && i ? '[' + token + ']' : token;
|
|
45
|
+
}).join(dots ? '.' : '');
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* If the array is an array and none of its elements are visitable, then it's a flat array.
|
|
50
|
+
*
|
|
51
|
+
* @param {Array<any>} arr - The array to check
|
|
52
|
+
*
|
|
53
|
+
* @returns {boolean}
|
|
54
|
+
*/
|
|
55
|
+
function isFlatArray(arr) {
|
|
56
|
+
return utils.isArray(arr) && !arr.some(isVisitable);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const predicates = utils.toFlatObject(utils, {}, null, function filter(prop) {
|
|
60
|
+
return /^is[A-Z]/.test(prop);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Convert a data object to FormData
|
|
65
|
+
*
|
|
66
|
+
* @param {Object} obj
|
|
67
|
+
* @param {?Object} [formData]
|
|
68
|
+
* @param {?Object} [options]
|
|
69
|
+
* @param {Function} [options.visitor]
|
|
70
|
+
* @param {Boolean} [options.metaTokens = true]
|
|
71
|
+
* @param {Boolean} [options.dots = false]
|
|
72
|
+
* @param {?Boolean} [options.indexes = false]
|
|
73
|
+
*
|
|
74
|
+
* @returns {Object}
|
|
75
|
+
**/
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* It converts an object into a FormData object
|
|
79
|
+
*
|
|
80
|
+
* @param {Object<any, any>} obj - The object to convert to form data.
|
|
81
|
+
* @param {string} formData - The FormData object to append to.
|
|
82
|
+
* @param {Object<string, any>} options
|
|
83
|
+
*
|
|
84
|
+
* @returns
|
|
85
|
+
*/
|
|
86
|
+
function toFormData(obj, formData, options) {
|
|
87
|
+
if (!utils.isObject(obj)) {
|
|
88
|
+
throw new TypeError('target must be an object');
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// eslint-disable-next-line no-param-reassign
|
|
92
|
+
formData = formData || new (PlatformFormData || FormData)();
|
|
93
|
+
|
|
94
|
+
// eslint-disable-next-line no-param-reassign
|
|
95
|
+
options = utils.toFlatObject(options, {
|
|
96
|
+
metaTokens: true,
|
|
97
|
+
dots: false,
|
|
98
|
+
indexes: false
|
|
99
|
+
}, false, function defined(option, source) {
|
|
100
|
+
// eslint-disable-next-line no-eq-null,eqeqeq
|
|
101
|
+
return !utils.isUndefined(source[option]);
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
const metaTokens = options.metaTokens;
|
|
105
|
+
// eslint-disable-next-line no-use-before-define
|
|
106
|
+
const visitor = options.visitor || defaultVisitor;
|
|
107
|
+
const dots = options.dots;
|
|
108
|
+
const indexes = options.indexes;
|
|
109
|
+
const _Blob = options.Blob || typeof Blob !== 'undefined' && Blob;
|
|
110
|
+
const useBlob = _Blob && utils.isSpecCompliantForm(formData);
|
|
111
|
+
|
|
112
|
+
if (!utils.isFunction(visitor)) {
|
|
113
|
+
throw new TypeError('visitor must be a function');
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function convertValue(value) {
|
|
117
|
+
if (value === null) return '';
|
|
118
|
+
|
|
119
|
+
if (utils.isDate(value)) {
|
|
120
|
+
return value.toISOString();
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
if (utils.isBoolean(value)) {
|
|
124
|
+
return value.toString();
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
if (!useBlob && utils.isBlob(value)) {
|
|
128
|
+
throw new AxiosError('Blob is not supported. Use a Buffer instead.');
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
if (utils.isArrayBuffer(value) || utils.isTypedArray(value)) {
|
|
132
|
+
return useBlob && typeof Blob === 'function' ? new Blob([value]) : Buffer.from(value);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
return value;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Default visitor.
|
|
140
|
+
*
|
|
141
|
+
* @param {*} value
|
|
142
|
+
* @param {String|Number} key
|
|
143
|
+
* @param {Array<String|Number>} path
|
|
144
|
+
* @this {FormData}
|
|
145
|
+
*
|
|
146
|
+
* @returns {boolean} return true to visit the each prop of the value recursively
|
|
147
|
+
*/
|
|
148
|
+
function defaultVisitor(value, key, path) {
|
|
149
|
+
let arr = value;
|
|
150
|
+
|
|
151
|
+
if (value && !path && typeof value === 'object') {
|
|
152
|
+
if (utils.endsWith(key, '{}')) {
|
|
153
|
+
// eslint-disable-next-line no-param-reassign
|
|
154
|
+
key = metaTokens ? key : key.slice(0, -2);
|
|
155
|
+
// eslint-disable-next-line no-param-reassign
|
|
156
|
+
value = JSON.stringify(value);
|
|
157
|
+
} else if (
|
|
158
|
+
(utils.isArray(value) && isFlatArray(value)) ||
|
|
159
|
+
((utils.isFileList(value) || utils.endsWith(key, '[]')) && (arr = utils.toArray(value))
|
|
160
|
+
)) {
|
|
161
|
+
// eslint-disable-next-line no-param-reassign
|
|
162
|
+
key = removeBrackets(key);
|
|
163
|
+
|
|
164
|
+
arr.forEach(function each(el, index) {
|
|
165
|
+
!(utils.isUndefined(el) || el === null) && formData.append(
|
|
166
|
+
// eslint-disable-next-line no-nested-ternary
|
|
167
|
+
indexes === true ? renderKey([key], index, dots) : (indexes === null ? key : key + '[]'),
|
|
168
|
+
convertValue(el)
|
|
169
|
+
);
|
|
170
|
+
});
|
|
171
|
+
return false;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
if (isVisitable(value)) {
|
|
176
|
+
return true;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
formData.append(renderKey(path, key, dots), convertValue(value));
|
|
180
|
+
|
|
181
|
+
return false;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
const stack = [];
|
|
185
|
+
|
|
186
|
+
const exposedHelpers = Object.assign(predicates, {
|
|
187
|
+
defaultVisitor,
|
|
188
|
+
convertValue,
|
|
189
|
+
isVisitable
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
function build(value, path) {
|
|
193
|
+
if (utils.isUndefined(value)) return;
|
|
194
|
+
|
|
195
|
+
if (stack.indexOf(value) !== -1) {
|
|
196
|
+
throw Error('Circular reference detected in ' + path.join('.'));
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
stack.push(value);
|
|
200
|
+
|
|
201
|
+
utils.forEach(value, function each(el, key) {
|
|
202
|
+
const result = !(utils.isUndefined(el) || el === null) && visitor.call(
|
|
203
|
+
formData, el, utils.isString(key) ? key.trim() : key, path, exposedHelpers
|
|
204
|
+
);
|
|
205
|
+
|
|
206
|
+
if (result === true) {
|
|
207
|
+
build(el, path ? path.concat(key) : [key]);
|
|
208
|
+
}
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
stack.pop();
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
if (!utils.isObject(obj)) {
|
|
215
|
+
throw new TypeError('data must be an object');
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
build(obj);
|
|
219
|
+
|
|
220
|
+
return formData;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
export default toFormData;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
import utils from '../utils.js';
|
|
4
|
+
import toFormData from './toFormData.js';
|
|
5
|
+
import platform from '../platform/index.js';
|
|
6
|
+
|
|
7
|
+
export default function toURLEncodedForm(data, options) {
|
|
8
|
+
return toFormData(data, new platform.classes.URLSearchParams(), {
|
|
9
|
+
visitor: function(value, key, path, helpers) {
|
|
10
|
+
if (platform.isNode && utils.isBuffer(value)) {
|
|
11
|
+
this.append(key, value.toString('base64'));
|
|
12
|
+
return false;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
return helpers.defaultVisitor.apply(this, arguments);
|
|
16
|
+
},
|
|
17
|
+
...options
|
|
18
|
+
});
|
|
19
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
|
|
2
|
+
export const streamChunk = function* (chunk, chunkSize) {
|
|
3
|
+
let len = chunk.byteLength;
|
|
4
|
+
|
|
5
|
+
if (!chunkSize || len < chunkSize) {
|
|
6
|
+
yield chunk;
|
|
7
|
+
return;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
let pos = 0;
|
|
11
|
+
let end;
|
|
12
|
+
|
|
13
|
+
while (pos < len) {
|
|
14
|
+
end = pos + chunkSize;
|
|
15
|
+
yield chunk.slice(pos, end);
|
|
16
|
+
pos = end;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export const readBytes = async function* (iterable, chunkSize) {
|
|
21
|
+
for await (const chunk of readStream(iterable)) {
|
|
22
|
+
yield* streamChunk(chunk, chunkSize);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const readStream = async function* (stream) {
|
|
27
|
+
if (stream[Symbol.asyncIterator]) {
|
|
28
|
+
yield* stream;
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const reader = stream.getReader();
|
|
33
|
+
try {
|
|
34
|
+
for (;;) {
|
|
35
|
+
const {done, value} = await reader.read();
|
|
36
|
+
if (done) {
|
|
37
|
+
break;
|
|
38
|
+
}
|
|
39
|
+
yield value;
|
|
40
|
+
}
|
|
41
|
+
} finally {
|
|
42
|
+
await reader.cancel();
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export const trackStream = (stream, chunkSize, onProgress, onFinish) => {
|
|
47
|
+
const iterator = readBytes(stream, chunkSize);
|
|
48
|
+
|
|
49
|
+
let bytes = 0;
|
|
50
|
+
let done;
|
|
51
|
+
let _onFinish = (e) => {
|
|
52
|
+
if (!done) {
|
|
53
|
+
done = true;
|
|
54
|
+
onFinish && onFinish(e);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
return new ReadableStream({
|
|
59
|
+
async pull(controller) {
|
|
60
|
+
try {
|
|
61
|
+
const {done, value} = await iterator.next();
|
|
62
|
+
|
|
63
|
+
if (done) {
|
|
64
|
+
_onFinish();
|
|
65
|
+
controller.close();
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
let len = value.byteLength;
|
|
70
|
+
if (onProgress) {
|
|
71
|
+
let loadedBytes = bytes += len;
|
|
72
|
+
onProgress(loadedBytes);
|
|
73
|
+
}
|
|
74
|
+
controller.enqueue(new Uint8Array(value));
|
|
75
|
+
} catch (err) {
|
|
76
|
+
_onFinish(err);
|
|
77
|
+
throw err;
|
|
78
|
+
}
|
|
79
|
+
},
|
|
80
|
+
cancel(reason) {
|
|
81
|
+
_onFinish(reason);
|
|
82
|
+
return iterator.return();
|
|
83
|
+
}
|
|
84
|
+
}, {
|
|
85
|
+
highWaterMark: 2
|
|
86
|
+
})
|
|
87
|
+
}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
import {VERSION} from '../env/data.js';
|
|
4
|
+
import AxiosError from '../core/AxiosError.js';
|
|
5
|
+
|
|
6
|
+
const validators = {};
|
|
7
|
+
|
|
8
|
+
// eslint-disable-next-line func-names
|
|
9
|
+
['object', 'boolean', 'number', 'function', 'string', 'symbol'].forEach((type, i) => {
|
|
10
|
+
validators[type] = function validator(thing) {
|
|
11
|
+
return typeof thing === type || 'a' + (i < 1 ? 'n ' : ' ') + type;
|
|
12
|
+
};
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
const deprecatedWarnings = {};
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Transitional option validator
|
|
19
|
+
*
|
|
20
|
+
* @param {function|boolean?} validator - set to false if the transitional option has been removed
|
|
21
|
+
* @param {string?} version - deprecated version / removed since version
|
|
22
|
+
* @param {string?} message - some message with additional info
|
|
23
|
+
*
|
|
24
|
+
* @returns {function}
|
|
25
|
+
*/
|
|
26
|
+
validators.transitional = function transitional(validator, version, message) {
|
|
27
|
+
function formatMessage(opt, desc) {
|
|
28
|
+
return '[Axios v' + VERSION + '] Transitional option \'' + opt + '\'' + desc + (message ? '. ' + message : '');
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// eslint-disable-next-line func-names
|
|
32
|
+
return (value, opt, opts) => {
|
|
33
|
+
if (validator === false) {
|
|
34
|
+
throw new AxiosError(
|
|
35
|
+
formatMessage(opt, ' has been removed' + (version ? ' in ' + version : '')),
|
|
36
|
+
AxiosError.ERR_DEPRECATED
|
|
37
|
+
);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (version && !deprecatedWarnings[opt]) {
|
|
41
|
+
deprecatedWarnings[opt] = true;
|
|
42
|
+
// eslint-disable-next-line no-console
|
|
43
|
+
console.warn(
|
|
44
|
+
formatMessage(
|
|
45
|
+
opt,
|
|
46
|
+
' has been deprecated since v' + version + ' and will be removed in the near future'
|
|
47
|
+
)
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return validator ? validator(value, opt, opts) : true;
|
|
52
|
+
};
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
validators.spelling = function spelling(correctSpelling) {
|
|
56
|
+
return (value, opt) => {
|
|
57
|
+
// eslint-disable-next-line no-console
|
|
58
|
+
console.warn(`${opt} is likely a misspelling of ${correctSpelling}`);
|
|
59
|
+
return true;
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Assert object's properties type
|
|
65
|
+
*
|
|
66
|
+
* @param {object} options
|
|
67
|
+
* @param {object} schema
|
|
68
|
+
* @param {boolean?} allowUnknown
|
|
69
|
+
*
|
|
70
|
+
* @returns {object}
|
|
71
|
+
*/
|
|
72
|
+
|
|
73
|
+
function assertOptions(options, schema, allowUnknown) {
|
|
74
|
+
if (typeof options !== 'object') {
|
|
75
|
+
throw new AxiosError('options must be an object', AxiosError.ERR_BAD_OPTION_VALUE);
|
|
76
|
+
}
|
|
77
|
+
const keys = Object.keys(options);
|
|
78
|
+
let i = keys.length;
|
|
79
|
+
while (i-- > 0) {
|
|
80
|
+
const opt = keys[i];
|
|
81
|
+
const validator = schema[opt];
|
|
82
|
+
if (validator) {
|
|
83
|
+
const value = options[opt];
|
|
84
|
+
const result = value === undefined || validator(value, opt, options);
|
|
85
|
+
if (result !== true) {
|
|
86
|
+
throw new AxiosError('option ' + opt + ' must be ' + result, AxiosError.ERR_BAD_OPTION_VALUE);
|
|
87
|
+
}
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
if (allowUnknown !== true) {
|
|
91
|
+
throw new AxiosError('Unknown option ' + opt, AxiosError.ERR_BAD_OPTION);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export default {
|
|
97
|
+
assertOptions,
|
|
98
|
+
validators
|
|
99
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import URLSearchParams from './classes/URLSearchParams.js'
|
|
2
|
+
import FormData from './classes/FormData.js'
|
|
3
|
+
import Blob from './classes/Blob.js'
|
|
4
|
+
|
|
5
|
+
export default {
|
|
6
|
+
isBrowser: true,
|
|
7
|
+
classes: {
|
|
8
|
+
URLSearchParams,
|
|
9
|
+
FormData,
|
|
10
|
+
Blob
|
|
11
|
+
},
|
|
12
|
+
protocols: ['http', 'https', 'file', 'blob', 'url', 'data']
|
|
13
|
+
};
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
const hasBrowserEnv = typeof window !== 'undefined' && typeof document !== 'undefined';
|
|
2
|
+
|
|
3
|
+
const _navigator = typeof navigator === 'object' && navigator || undefined;
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Determine if we're running in a standard browser environment
|
|
7
|
+
*
|
|
8
|
+
* This allows axios to run in a web worker, and react-native.
|
|
9
|
+
* Both environments support XMLHttpRequest, but not fully standard globals.
|
|
10
|
+
*
|
|
11
|
+
* web workers:
|
|
12
|
+
* typeof window -> undefined
|
|
13
|
+
* typeof document -> undefined
|
|
14
|
+
*
|
|
15
|
+
* react-native:
|
|
16
|
+
* navigator.product -> 'ReactNative'
|
|
17
|
+
* nativescript
|
|
18
|
+
* navigator.product -> 'NativeScript' or 'NS'
|
|
19
|
+
*
|
|
20
|
+
* @returns {boolean}
|
|
21
|
+
*/
|
|
22
|
+
const hasStandardBrowserEnv = hasBrowserEnv &&
|
|
23
|
+
(!_navigator || ['ReactNative', 'NativeScript', 'NS'].indexOf(_navigator.product) < 0);
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Determine if we're running in a standard browser webWorker environment
|
|
27
|
+
*
|
|
28
|
+
* Although the `isStandardBrowserEnv` method indicates that
|
|
29
|
+
* `allows axios to run in a web worker`, the WebWorker will still be
|
|
30
|
+
* filtered out due to its judgment standard
|
|
31
|
+
* `typeof window !== 'undefined' && typeof document !== 'undefined'`.
|
|
32
|
+
* This leads to a problem when axios post `FormData` in webWorker
|
|
33
|
+
*/
|
|
34
|
+
const hasStandardBrowserWebWorkerEnv = (() => {
|
|
35
|
+
return (
|
|
36
|
+
typeof WorkerGlobalScope !== 'undefined' &&
|
|
37
|
+
// eslint-disable-next-line no-undef
|
|
38
|
+
self instanceof WorkerGlobalScope &&
|
|
39
|
+
typeof self.importScripts === 'function'
|
|
40
|
+
);
|
|
41
|
+
})();
|
|
42
|
+
|
|
43
|
+
const origin = hasBrowserEnv && window.location.href || 'http://localhost';
|
|
44
|
+
|
|
45
|
+
export {
|
|
46
|
+
hasBrowserEnv,
|
|
47
|
+
hasStandardBrowserWebWorkerEnv,
|
|
48
|
+
hasStandardBrowserEnv,
|
|
49
|
+
_navigator as navigator,
|
|
50
|
+
origin
|
|
51
|
+
}
|