axios 1.0.0-alpha.1 → 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 +55 -1
- package/README.md +59 -48
- package/SECURITY.md +3 -2
- package/bin/ssl_hotfix.js +1 -1
- package/dist/axios.js +1552 -975
- package/dist/axios.js.map +1 -1
- package/dist/axios.min.js +1 -1
- package/dist/axios.min.js.map +1 -1
- package/dist/esm/axios.js +1471 -865
- package/dist/esm/axios.js.map +1 -1
- package/dist/esm/axios.min.js +1 -1
- package/dist/esm/axios.min.js.map +1 -1
- package/dist/node/axios.cjs +3750 -0
- package/dist/node/axios.cjs.map +1 -0
- package/gulpfile.js +88 -0
- package/index.d.ts +208 -63
- package/index.js +2 -1
- package/karma.conf.cjs +250 -0
- package/lib/adapters/http.js +251 -131
- package/lib/adapters/index.js +33 -0
- package/lib/adapters/xhr.js +79 -56
- package/lib/axios.js +33 -25
- package/lib/cancel/CancelToken.js +91 -88
- package/lib/cancel/CanceledError.js +5 -4
- package/lib/cancel/isCancel.js +2 -2
- package/lib/core/Axios.js +127 -100
- package/lib/core/AxiosError.js +10 -7
- package/lib/core/AxiosHeaders.js +274 -0
- package/lib/core/InterceptorManager.js +61 -53
- package/lib/core/buildFullPath.js +5 -4
- package/lib/core/dispatchRequest.js +21 -39
- package/lib/core/mergeConfig.js +8 -7
- package/lib/core/settle.js +6 -4
- package/lib/core/transformData.js +15 -10
- package/lib/defaults/index.js +46 -39
- package/lib/defaults/transitional.js +1 -1
- package/lib/env/classes/FormData.js +2 -2
- package/lib/env/data.js +1 -3
- package/lib/helpers/AxiosTransformStream.js +191 -0
- package/lib/helpers/AxiosURLSearchParams.js +23 -7
- package/lib/helpers/bind.js +2 -2
- package/lib/helpers/buildURL.js +16 -7
- 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 +36 -15
- package/lib/helpers/fromDataURI.js +15 -13
- package/lib/helpers/isAbsoluteURL.js +3 -2
- package/lib/helpers/isAxiosError.js +4 -3
- package/lib/helpers/isURLSameOrigin.js +55 -56
- 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 +68 -18
- package/lib/helpers/toURLEncodedForm.js +5 -5
- package/lib/helpers/validator.js +20 -15
- package/lib/platform/browser/classes/FormData.js +1 -1
- package/lib/platform/browser/classes/URLSearchParams.js +2 -3
- package/lib/platform/browser/index.js +38 -6
- package/lib/platform/index.js +2 -2
- package/lib/platform/node/classes/FormData.js +2 -2
- package/lib/platform/node/classes/URLSearchParams.js +2 -3
- package/lib/platform/node/index.js +5 -4
- package/lib/utils.js +293 -191
- package/package.json +55 -22
- package/rollup.config.js +36 -6
- package/lib/helpers/normalizeHeaderName.js +0 -12
package/lib/core/Axios.js
CHANGED
@@ -1,137 +1,164 @@
|
|
1
1
|
'use strict';
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
3
|
+
import utils from './../utils.js';
|
4
|
+
import buildURL from '../helpers/buildURL.js';
|
5
|
+
import InterceptorManager from './InterceptorManager.js';
|
6
|
+
import dispatchRequest from './dispatchRequest.js';
|
7
|
+
import mergeConfig from './mergeConfig.js';
|
8
|
+
import buildFullPath from './buildFullPath.js';
|
9
|
+
import validator from '../helpers/validator.js';
|
10
|
+
import AxiosHeaders from './AxiosHeaders.js';
|
11
|
+
|
12
|
+
const validators = validator.validators;
|
13
|
+
|
12
14
|
/**
|
13
15
|
* Create a new instance of Axios
|
14
16
|
*
|
15
17
|
* @param {Object} instanceConfig The default config for the instance
|
16
|
-
*/
|
17
|
-
function Axios(instanceConfig) {
|
18
|
-
this.defaults = instanceConfig;
|
19
|
-
this.interceptors = {
|
20
|
-
request: new InterceptorManager(),
|
21
|
-
response: new InterceptorManager()
|
22
|
-
};
|
23
|
-
}
|
24
|
-
|
25
|
-
/**
|
26
|
-
* Dispatch a request
|
27
18
|
*
|
28
|
-
* @
|
29
|
-
* @param {?Object} config
|
19
|
+
* @return {Axios} A new instance of Axios
|
30
20
|
*/
|
31
|
-
Axios
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
config = configOrUrl || {};
|
21
|
+
class Axios {
|
22
|
+
constructor(instanceConfig) {
|
23
|
+
this.defaults = instanceConfig;
|
24
|
+
this.interceptors = {
|
25
|
+
request: new InterceptorManager(),
|
26
|
+
response: new InterceptorManager()
|
27
|
+
};
|
39
28
|
}
|
40
29
|
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
30
|
+
/**
|
31
|
+
* Dispatch a request
|
32
|
+
*
|
33
|
+
* @param {String|Object} configOrUrl The config specific for this request (merged with this.defaults)
|
34
|
+
* @param {?Object} config
|
35
|
+
*
|
36
|
+
* @returns {Promise} The Promise to be fulfilled
|
37
|
+
*/
|
38
|
+
request(configOrUrl, config) {
|
39
|
+
/*eslint no-param-reassign:0*/
|
40
|
+
// Allow for axios('example/url'[, config]) a la fetch API
|
41
|
+
if (typeof configOrUrl === 'string') {
|
42
|
+
config = config || {};
|
43
|
+
config.url = configOrUrl;
|
44
|
+
} else {
|
45
|
+
config = configOrUrl || {};
|
46
|
+
}
|
51
47
|
|
52
|
-
|
48
|
+
config = mergeConfig(this.defaults, config);
|
53
49
|
|
54
|
-
|
55
|
-
validator.assertOptions(transitional, {
|
56
|
-
silentJSONParsing: validators.transitional(validators.boolean),
|
57
|
-
forcedJSONParsing: validators.transitional(validators.boolean),
|
58
|
-
clarifyTimeoutError: validators.transitional(validators.boolean)
|
59
|
-
}, false);
|
60
|
-
}
|
50
|
+
const transitional = config.transitional;
|
61
51
|
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
52
|
+
if (transitional !== undefined) {
|
53
|
+
validator.assertOptions(transitional, {
|
54
|
+
silentJSONParsing: validators.transitional(validators.boolean),
|
55
|
+
forcedJSONParsing: validators.transitional(validators.boolean),
|
56
|
+
clarifyTimeoutError: validators.transitional(validators.boolean)
|
57
|
+
}, false);
|
68
58
|
}
|
69
59
|
|
70
|
-
|
60
|
+
// Set config.method
|
61
|
+
config.method = (config.method || this.defaults.method || 'get').toLowerCase();
|
62
|
+
|
63
|
+
// Flatten headers
|
64
|
+
const defaultHeaders = config.headers && utils.merge(
|
65
|
+
config.headers.common,
|
66
|
+
config.headers[config.method]
|
67
|
+
);
|
68
|
+
|
69
|
+
defaultHeaders && utils.forEach(
|
70
|
+
['delete', 'get', 'head', 'post', 'put', 'patch', 'common'],
|
71
|
+
function cleanHeaderConfig(method) {
|
72
|
+
delete config.headers[method];
|
73
|
+
}
|
74
|
+
);
|
75
|
+
|
76
|
+
config.headers = new AxiosHeaders(config.headers, defaultHeaders);
|
77
|
+
|
78
|
+
// filter out skipped interceptors
|
79
|
+
const requestInterceptorChain = [];
|
80
|
+
let synchronousRequestInterceptors = true;
|
81
|
+
this.interceptors.request.forEach(function unshiftRequestInterceptors(interceptor) {
|
82
|
+
if (typeof interceptor.runWhen === 'function' && interceptor.runWhen(config) === false) {
|
83
|
+
return;
|
84
|
+
}
|
71
85
|
|
72
|
-
|
73
|
-
});
|
86
|
+
synchronousRequestInterceptors = synchronousRequestInterceptors && interceptor.synchronous;
|
74
87
|
|
75
|
-
|
76
|
-
|
77
|
-
responseInterceptorChain.push(interceptor.fulfilled, interceptor.rejected);
|
78
|
-
});
|
88
|
+
requestInterceptorChain.unshift(interceptor.fulfilled, interceptor.rejected);
|
89
|
+
});
|
79
90
|
|
80
|
-
|
91
|
+
const responseInterceptorChain = [];
|
92
|
+
this.interceptors.response.forEach(function pushResponseInterceptors(interceptor) {
|
93
|
+
responseInterceptorChain.push(interceptor.fulfilled, interceptor.rejected);
|
94
|
+
});
|
81
95
|
|
82
|
-
|
83
|
-
|
96
|
+
let promise;
|
97
|
+
let i = 0;
|
98
|
+
let len;
|
84
99
|
|
85
|
-
|
86
|
-
|
100
|
+
if (!synchronousRequestInterceptors) {
|
101
|
+
const chain = [dispatchRequest.bind(this), undefined];
|
102
|
+
chain.unshift.apply(chain, requestInterceptorChain);
|
103
|
+
chain.push.apply(chain, responseInterceptorChain);
|
104
|
+
len = chain.length;
|
87
105
|
|
88
|
-
|
89
|
-
|
90
|
-
|
106
|
+
promise = Promise.resolve(config);
|
107
|
+
|
108
|
+
while (i < len) {
|
109
|
+
promise = promise.then(chain[i++], chain[i++]);
|
110
|
+
}
|
111
|
+
|
112
|
+
return promise;
|
91
113
|
}
|
92
114
|
|
93
|
-
|
94
|
-
}
|
115
|
+
len = requestInterceptorChain.length;
|
95
116
|
|
117
|
+
let newConfig = config;
|
118
|
+
|
119
|
+
i = 0;
|
120
|
+
|
121
|
+
while (i < len) {
|
122
|
+
const onFulfilled = requestInterceptorChain[i++];
|
123
|
+
const onRejected = requestInterceptorChain[i++];
|
124
|
+
try {
|
125
|
+
newConfig = onFulfilled(newConfig);
|
126
|
+
} catch (error) {
|
127
|
+
onRejected.call(this, error);
|
128
|
+
break;
|
129
|
+
}
|
130
|
+
}
|
96
131
|
|
97
|
-
var newConfig = config;
|
98
|
-
while (requestInterceptorChain.length) {
|
99
|
-
var onFulfilled = requestInterceptorChain.shift();
|
100
|
-
var onRejected = requestInterceptorChain.shift();
|
101
132
|
try {
|
102
|
-
|
133
|
+
promise = dispatchRequest.call(this, newConfig);
|
103
134
|
} catch (error) {
|
104
|
-
|
105
|
-
break;
|
135
|
+
return Promise.reject(error);
|
106
136
|
}
|
107
|
-
}
|
108
137
|
|
109
|
-
|
110
|
-
|
111
|
-
} catch (error) {
|
112
|
-
return Promise.reject(error);
|
113
|
-
}
|
138
|
+
i = 0;
|
139
|
+
len = responseInterceptorChain.length;
|
114
140
|
|
115
|
-
|
116
|
-
|
117
|
-
|
141
|
+
while (i < len) {
|
142
|
+
promise = promise.then(responseInterceptorChain[i++], responseInterceptorChain[i++]);
|
143
|
+
}
|
118
144
|
|
119
|
-
|
120
|
-
}
|
145
|
+
return promise;
|
146
|
+
}
|
121
147
|
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
}
|
148
|
+
getUri(config) {
|
149
|
+
config = mergeConfig(this.defaults, config);
|
150
|
+
const fullPath = buildFullPath(config.baseURL, config.url);
|
151
|
+
return buildURL(fullPath, config.params, config.paramsSerializer);
|
152
|
+
}
|
153
|
+
}
|
127
154
|
|
128
155
|
// Provide aliases for supported request methods
|
129
156
|
utils.forEach(['delete', 'get', 'head', 'options'], function forEachMethodNoData(method) {
|
130
157
|
/*eslint func-names:0*/
|
131
158
|
Axios.prototype[method] = function(url, config) {
|
132
159
|
return this.request(mergeConfig(config || {}, {
|
133
|
-
method
|
134
|
-
url
|
160
|
+
method,
|
161
|
+
url,
|
135
162
|
data: (config || {}).data
|
136
163
|
}));
|
137
164
|
};
|
@@ -143,12 +170,12 @@ utils.forEach(['post', 'put', 'patch'], function forEachMethodWithData(method) {
|
|
143
170
|
function generateHTTPMethod(isForm) {
|
144
171
|
return function httpMethod(url, data, config) {
|
145
172
|
return this.request(mergeConfig(config || {}, {
|
146
|
-
method
|
173
|
+
method,
|
147
174
|
headers: isForm ? {
|
148
175
|
'Content-Type': 'multipart/form-data'
|
149
176
|
} : {},
|
150
|
-
url
|
151
|
-
data
|
177
|
+
url,
|
178
|
+
data
|
152
179
|
}));
|
153
180
|
};
|
154
181
|
}
|
@@ -158,4 +185,4 @@ utils.forEach(['post', 'put', 'patch'], function forEachMethodWithData(method) {
|
|
158
185
|
Axios.prototype[method + 'Form'] = generateHTTPMethod(true);
|
159
186
|
});
|
160
187
|
|
161
|
-
|
188
|
+
export default Axios;
|
package/lib/core/AxiosError.js
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
'use strict';
|
2
2
|
|
3
|
-
|
3
|
+
import utils from '../utils.js';
|
4
4
|
|
5
5
|
/**
|
6
6
|
* Create an Error with the specified message, config, error code, request and response.
|
@@ -10,6 +10,7 @@ var utils = require('../utils');
|
|
10
10
|
* @param {Object} [config] The config.
|
11
11
|
* @param {Object} [request] The request.
|
12
12
|
* @param {Object} [response] The response.
|
13
|
+
*
|
13
14
|
* @returns {Error} The created error.
|
14
15
|
*/
|
15
16
|
function AxiosError(message, code, config, request, response) {
|
@@ -51,8 +52,8 @@ utils.inherits(AxiosError, Error, {
|
|
51
52
|
}
|
52
53
|
});
|
53
54
|
|
54
|
-
|
55
|
-
|
55
|
+
const prototype = AxiosError.prototype;
|
56
|
+
const descriptors = {};
|
56
57
|
|
57
58
|
[
|
58
59
|
'ERR_BAD_OPTION_VALUE',
|
@@ -68,7 +69,7 @@ var descriptors = {};
|
|
68
69
|
'ERR_NOT_SUPPORT',
|
69
70
|
'ERR_INVALID_URL'
|
70
71
|
// eslint-disable-next-line func-names
|
71
|
-
].forEach(
|
72
|
+
].forEach(code => {
|
72
73
|
descriptors[code] = {value: code};
|
73
74
|
});
|
74
75
|
|
@@ -76,11 +77,13 @@ Object.defineProperties(AxiosError, descriptors);
|
|
76
77
|
Object.defineProperty(prototype, 'isAxiosError', {value: true});
|
77
78
|
|
78
79
|
// eslint-disable-next-line func-names
|
79
|
-
AxiosError.from =
|
80
|
-
|
80
|
+
AxiosError.from = (error, code, config, request, response, customProps) => {
|
81
|
+
const axiosError = Object.create(prototype);
|
81
82
|
|
82
83
|
utils.toFlatObject(error, axiosError, function filter(obj) {
|
83
84
|
return obj !== Error.prototype;
|
85
|
+
}, prop => {
|
86
|
+
return prop !== 'isAxiosError';
|
84
87
|
});
|
85
88
|
|
86
89
|
AxiosError.call(axiosError, error.message, code, config, request, response);
|
@@ -94,4 +97,4 @@ AxiosError.from = function(error, code, config, request, response, customProps)
|
|
94
97
|
return axiosError;
|
95
98
|
};
|
96
99
|
|
97
|
-
|
100
|
+
export default AxiosError;
|
@@ -0,0 +1,274 @@
|
|
1
|
+
'use strict';
|
2
|
+
|
3
|
+
import utils from '../utils.js';
|
4
|
+
import parseHeaders from '../helpers/parseHeaders.js';
|
5
|
+
|
6
|
+
const $internals = Symbol('internals');
|
7
|
+
const $defaults = Symbol('defaults');
|
8
|
+
|
9
|
+
function normalizeHeader(header) {
|
10
|
+
return header && String(header).trim().toLowerCase();
|
11
|
+
}
|
12
|
+
|
13
|
+
function normalizeValue(value) {
|
14
|
+
if (value === false || value == null) {
|
15
|
+
return value;
|
16
|
+
}
|
17
|
+
|
18
|
+
return String(value);
|
19
|
+
}
|
20
|
+
|
21
|
+
function parseTokens(str) {
|
22
|
+
const tokens = Object.create(null);
|
23
|
+
const tokensRE = /([^\s,;=]+)\s*(?:=\s*([^,;]+))?/g;
|
24
|
+
let match;
|
25
|
+
|
26
|
+
while ((match = tokensRE.exec(str))) {
|
27
|
+
tokens[match[1]] = match[2];
|
28
|
+
}
|
29
|
+
|
30
|
+
return tokens;
|
31
|
+
}
|
32
|
+
|
33
|
+
function matchHeaderValue(context, value, header, filter) {
|
34
|
+
if (utils.isFunction(filter)) {
|
35
|
+
return filter.call(this, value, header);
|
36
|
+
}
|
37
|
+
|
38
|
+
if (!utils.isString(value)) return;
|
39
|
+
|
40
|
+
if (utils.isString(filter)) {
|
41
|
+
return value.indexOf(filter) !== -1;
|
42
|
+
}
|
43
|
+
|
44
|
+
if (utils.isRegExp(filter)) {
|
45
|
+
return filter.test(value);
|
46
|
+
}
|
47
|
+
}
|
48
|
+
|
49
|
+
function formatHeader(header) {
|
50
|
+
return header.trim()
|
51
|
+
.toLowerCase().replace(/([a-z\d])(\w*)/g, (w, char, str) => {
|
52
|
+
return char.toUpperCase() + str;
|
53
|
+
});
|
54
|
+
}
|
55
|
+
|
56
|
+
function buildAccessors(obj, header) {
|
57
|
+
const accessorName = utils.toCamelCase(' ' + header);
|
58
|
+
|
59
|
+
['get', 'set', 'has'].forEach(methodName => {
|
60
|
+
Object.defineProperty(obj, methodName + accessorName, {
|
61
|
+
value: function(arg1, arg2, arg3) {
|
62
|
+
return this[methodName].call(this, header, arg1, arg2, arg3);
|
63
|
+
},
|
64
|
+
configurable: true
|
65
|
+
});
|
66
|
+
});
|
67
|
+
}
|
68
|
+
|
69
|
+
function findKey(obj, key) {
|
70
|
+
key = key.toLowerCase();
|
71
|
+
const keys = Object.keys(obj);
|
72
|
+
let i = keys.length;
|
73
|
+
let _key;
|
74
|
+
while (i-- > 0) {
|
75
|
+
_key = keys[i];
|
76
|
+
if (key === _key.toLowerCase()) {
|
77
|
+
return _key;
|
78
|
+
}
|
79
|
+
}
|
80
|
+
return null;
|
81
|
+
}
|
82
|
+
|
83
|
+
function AxiosHeaders(headers, defaults) {
|
84
|
+
headers && this.set(headers);
|
85
|
+
this[$defaults] = defaults || null;
|
86
|
+
}
|
87
|
+
|
88
|
+
Object.assign(AxiosHeaders.prototype, {
|
89
|
+
set: function(header, valueOrRewrite, rewrite) {
|
90
|
+
const self = this;
|
91
|
+
|
92
|
+
function setHeader(_value, _header, _rewrite) {
|
93
|
+
const lHeader = normalizeHeader(_header);
|
94
|
+
|
95
|
+
if (!lHeader) {
|
96
|
+
throw new Error('header name must be a non-empty string');
|
97
|
+
}
|
98
|
+
|
99
|
+
const key = findKey(self, lHeader);
|
100
|
+
|
101
|
+
if (key && _rewrite !== true && (self[key] === false || _rewrite === false)) {
|
102
|
+
return;
|
103
|
+
}
|
104
|
+
|
105
|
+
if (utils.isArray(_value)) {
|
106
|
+
_value = _value.map(normalizeValue);
|
107
|
+
} else {
|
108
|
+
_value = normalizeValue(_value);
|
109
|
+
}
|
110
|
+
|
111
|
+
self[key || _header] = _value;
|
112
|
+
}
|
113
|
+
|
114
|
+
if (utils.isPlainObject(header)) {
|
115
|
+
utils.forEach(header, (_value, _header) => {
|
116
|
+
setHeader(_value, _header, valueOrRewrite);
|
117
|
+
});
|
118
|
+
} else {
|
119
|
+
setHeader(valueOrRewrite, header, rewrite);
|
120
|
+
}
|
121
|
+
|
122
|
+
return this;
|
123
|
+
},
|
124
|
+
|
125
|
+
get: function(header, parser) {
|
126
|
+
header = normalizeHeader(header);
|
127
|
+
|
128
|
+
if (!header) return undefined;
|
129
|
+
|
130
|
+
const key = findKey(this, header);
|
131
|
+
|
132
|
+
if (key) {
|
133
|
+
const value = this[key];
|
134
|
+
|
135
|
+
if (!parser) {
|
136
|
+
return value;
|
137
|
+
}
|
138
|
+
|
139
|
+
if (parser === true) {
|
140
|
+
return parseTokens(value);
|
141
|
+
}
|
142
|
+
|
143
|
+
if (utils.isFunction(parser)) {
|
144
|
+
return parser.call(this, value, key);
|
145
|
+
}
|
146
|
+
|
147
|
+
if (utils.isRegExp(parser)) {
|
148
|
+
return parser.exec(value);
|
149
|
+
}
|
150
|
+
|
151
|
+
throw new TypeError('parser must be boolean|regexp|function');
|
152
|
+
}
|
153
|
+
},
|
154
|
+
|
155
|
+
has: function(header, matcher) {
|
156
|
+
header = normalizeHeader(header);
|
157
|
+
|
158
|
+
if (header) {
|
159
|
+
const key = findKey(this, header);
|
160
|
+
|
161
|
+
return !!(key && (!matcher || matchHeaderValue(this, this[key], key, matcher)));
|
162
|
+
}
|
163
|
+
|
164
|
+
return false;
|
165
|
+
},
|
166
|
+
|
167
|
+
delete: function(header, matcher) {
|
168
|
+
const self = this;
|
169
|
+
let deleted = false;
|
170
|
+
|
171
|
+
function deleteHeader(_header) {
|
172
|
+
_header = normalizeHeader(_header);
|
173
|
+
|
174
|
+
if (_header) {
|
175
|
+
const key = findKey(self, _header);
|
176
|
+
|
177
|
+
if (key && (!matcher || matchHeaderValue(self, self[key], key, matcher))) {
|
178
|
+
delete self[key];
|
179
|
+
|
180
|
+
deleted = true;
|
181
|
+
}
|
182
|
+
}
|
183
|
+
}
|
184
|
+
|
185
|
+
if (utils.isArray(header)) {
|
186
|
+
header.forEach(deleteHeader);
|
187
|
+
} else {
|
188
|
+
deleteHeader(header);
|
189
|
+
}
|
190
|
+
|
191
|
+
return deleted;
|
192
|
+
},
|
193
|
+
|
194
|
+
clear: function() {
|
195
|
+
return Object.keys(this).forEach(this.delete.bind(this));
|
196
|
+
},
|
197
|
+
|
198
|
+
normalize: function(format) {
|
199
|
+
const self = this;
|
200
|
+
const headers = {};
|
201
|
+
|
202
|
+
utils.forEach(this, (value, header) => {
|
203
|
+
const key = findKey(headers, header);
|
204
|
+
|
205
|
+
if (key) {
|
206
|
+
self[key] = normalizeValue(value);
|
207
|
+
delete self[header];
|
208
|
+
return;
|
209
|
+
}
|
210
|
+
|
211
|
+
const normalized = format ? formatHeader(header) : String(header).trim();
|
212
|
+
|
213
|
+
if (normalized !== header) {
|
214
|
+
delete self[header];
|
215
|
+
}
|
216
|
+
|
217
|
+
self[normalized] = normalizeValue(value);
|
218
|
+
|
219
|
+
headers[normalized] = true;
|
220
|
+
});
|
221
|
+
|
222
|
+
return this;
|
223
|
+
},
|
224
|
+
|
225
|
+
toJSON: function() {
|
226
|
+
const obj = Object.create(null);
|
227
|
+
|
228
|
+
utils.forEach(Object.assign({}, this[$defaults] || null, this),
|
229
|
+
(value, header) => {
|
230
|
+
if (value == null || value === false) return;
|
231
|
+
obj[header] = utils.isArray(value) ? value.join(', ') : value;
|
232
|
+
});
|
233
|
+
|
234
|
+
return obj;
|
235
|
+
}
|
236
|
+
});
|
237
|
+
|
238
|
+
Object.assign(AxiosHeaders, {
|
239
|
+
from: function(thing) {
|
240
|
+
if (utils.isString(thing)) {
|
241
|
+
return new this(parseHeaders(thing));
|
242
|
+
}
|
243
|
+
return thing instanceof this ? thing : new this(thing);
|
244
|
+
},
|
245
|
+
|
246
|
+
accessor: function(header) {
|
247
|
+
const internals = this[$internals] = (this[$internals] = {
|
248
|
+
accessors: {}
|
249
|
+
});
|
250
|
+
|
251
|
+
const accessors = internals.accessors;
|
252
|
+
const prototype = this.prototype;
|
253
|
+
|
254
|
+
function defineAccessor(_header) {
|
255
|
+
const lHeader = normalizeHeader(_header);
|
256
|
+
|
257
|
+
if (!accessors[lHeader]) {
|
258
|
+
buildAccessors(prototype, _header);
|
259
|
+
accessors[lHeader] = true;
|
260
|
+
}
|
261
|
+
}
|
262
|
+
|
263
|
+
utils.isArray(header) ? header.forEach(defineAccessor) : defineAccessor(header);
|
264
|
+
|
265
|
+
return this;
|
266
|
+
}
|
267
|
+
});
|
268
|
+
|
269
|
+
AxiosHeaders.accessor(['Content-Type', 'Content-Length', 'Accept', 'Accept-Encoding', 'User-Agent']);
|
270
|
+
|
271
|
+
utils.freezeMethods(AxiosHeaders.prototype);
|
272
|
+
utils.freezeMethods(AxiosHeaders);
|
273
|
+
|
274
|
+
export default AxiosHeaders;
|