axios 1.1.3 → 1.2.0-alpha.1

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/lib/core/Axios.js CHANGED
@@ -47,7 +47,7 @@ class Axios {
47
47
 
48
48
  config = mergeConfig(this.defaults, config);
49
49
 
50
- const {transitional, paramsSerializer} = config;
50
+ const {transitional, paramsSerializer, headers} = config;
51
51
 
52
52
  if (transitional !== undefined) {
53
53
  validator.assertOptions(transitional, {
@@ -67,20 +67,22 @@ class Axios {
67
67
  // Set config.method
68
68
  config.method = (config.method || this.defaults.method || 'get').toLowerCase();
69
69
 
70
+ let contextHeaders;
71
+
70
72
  // Flatten headers
71
- const defaultHeaders = config.headers && utils.merge(
72
- config.headers.common,
73
- config.headers[config.method]
73
+ contextHeaders = headers && utils.merge(
74
+ headers.common,
75
+ headers[config.method]
74
76
  );
75
77
 
76
- defaultHeaders && utils.forEach(
78
+ contextHeaders && utils.forEach(
77
79
  ['delete', 'get', 'head', 'post', 'put', 'patch', 'common'],
78
- function cleanHeaderConfig(method) {
79
- delete config.headers[method];
80
+ (method) => {
81
+ delete headers[method];
80
82
  }
81
83
  );
82
84
 
83
- config.headers = new AxiosHeaders(config.headers, defaultHeaders);
85
+ config.headers = AxiosHeaders.concat(contextHeaders, headers);
84
86
 
85
87
  // filter out skipped interceptors
86
88
  const requestInterceptorChain = [];
@@ -4,7 +4,6 @@ import utils from '../utils.js';
4
4
  import parseHeaders from '../helpers/parseHeaders.js';
5
5
 
6
6
  const $internals = Symbol('internals');
7
- const $defaults = Symbol('defaults');
8
7
 
9
8
  function normalizeHeader(header) {
10
9
  return header && String(header).trim().toLowerCase();
@@ -30,6 +29,10 @@ function parseTokens(str) {
30
29
  return tokens;
31
30
  }
32
31
 
32
+ function isValidHeaderName(str) {
33
+ return /^[-_a-zA-Z]+$/.test(str.trim());
34
+ }
35
+
33
36
  function matchHeaderValue(context, value, header, filter) {
34
37
  if (utils.isFunction(filter)) {
35
38
  return filter.call(this, value, header);
@@ -66,27 +69,12 @@ function buildAccessors(obj, header) {
66
69
  });
67
70
  }
68
71
 
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
- }
72
+ class AxiosHeaders {
73
+ constructor(headers) {
74
+ headers && this.set(headers);
79
75
  }
80
- return null;
81
- }
82
-
83
- function AxiosHeaders(headers, defaults) {
84
- headers && this.set(headers);
85
- this[$defaults] = defaults || null;
86
- }
87
76
 
88
- Object.assign(AxiosHeaders.prototype, {
89
- set: function(header, valueOrRewrite, rewrite) {
77
+ set(header, valueOrRewrite, rewrite) {
90
78
  const self = this;
91
79
 
92
80
  function setHeader(_value, _header, _rewrite) {
@@ -96,69 +84,70 @@ Object.assign(AxiosHeaders.prototype, {
96
84
  throw new Error('header name must be a non-empty string');
97
85
  }
98
86
 
99
- const key = findKey(self, lHeader);
87
+ const key = utils.findKey(self, lHeader);
100
88
 
101
- if (key && _rewrite !== true && (self[key] === false || _rewrite === false)) {
102
- return;
89
+ if(!key || self[key] === undefined || _rewrite === true || (_rewrite === undefined && self[key] !== false)) {
90
+ self[key || _header] = normalizeValue(_value);
103
91
  }
104
-
105
- self[key || _header] = normalizeValue(_value);
106
92
  }
107
93
 
108
- if (utils.isPlainObject(header)) {
109
- utils.forEach(header, (_value, _header) => {
110
- setHeader(_value, _header, valueOrRewrite);
111
- });
94
+ const setHeaders = (headers, _rewrite) =>
95
+ utils.forEach(headers, (_value, _header) => setHeader(_value, _header, _rewrite));
96
+
97
+ if (utils.isPlainObject(header) || header instanceof this.constructor) {
98
+ setHeaders(header, valueOrRewrite)
99
+ } else if(utils.isString(header) && (header = header.trim()) && !isValidHeaderName(header)) {
100
+ setHeaders(parseHeaders(header), valueOrRewrite);
112
101
  } else {
113
- setHeader(valueOrRewrite, header, rewrite);
102
+ header != null && setHeader(valueOrRewrite, header, rewrite);
114
103
  }
115
104
 
116
105
  return this;
117
- },
106
+ }
118
107
 
119
- get: function(header, parser) {
108
+ get(header, parser) {
120
109
  header = normalizeHeader(header);
121
110
 
122
- if (!header) return undefined;
111
+ if (header) {
112
+ const key = utils.findKey(this, header);
123
113
 
124
- const key = findKey(this, header);
114
+ if (key) {
115
+ const value = this[key];
125
116
 
126
- if (key) {
127
- const value = this[key];
117
+ if (!parser) {
118
+ return value;
119
+ }
128
120
 
129
- if (!parser) {
130
- return value;
131
- }
121
+ if (parser === true) {
122
+ return parseTokens(value);
123
+ }
132
124
 
133
- if (parser === true) {
134
- return parseTokens(value);
135
- }
125
+ if (utils.isFunction(parser)) {
126
+ return parser.call(this, value, key);
127
+ }
136
128
 
137
- if (utils.isFunction(parser)) {
138
- return parser.call(this, value, key);
139
- }
129
+ if (utils.isRegExp(parser)) {
130
+ return parser.exec(value);
131
+ }
140
132
 
141
- if (utils.isRegExp(parser)) {
142
- return parser.exec(value);
133
+ throw new TypeError('parser must be boolean|regexp|function');
143
134
  }
144
-
145
- throw new TypeError('parser must be boolean|regexp|function');
146
135
  }
147
- },
136
+ }
148
137
 
149
- has: function(header, matcher) {
138
+ has(header, matcher) {
150
139
  header = normalizeHeader(header);
151
140
 
152
141
  if (header) {
153
- const key = findKey(this, header);
142
+ const key = utils.findKey(this, header);
154
143
 
155
144
  return !!(key && (!matcher || matchHeaderValue(this, this[key], key, matcher)));
156
145
  }
157
146
 
158
147
  return false;
159
- },
148
+ }
160
149
 
161
- delete: function(header, matcher) {
150
+ delete(header, matcher) {
162
151
  const self = this;
163
152
  let deleted = false;
164
153
 
@@ -166,7 +155,7 @@ Object.assign(AxiosHeaders.prototype, {
166
155
  _header = normalizeHeader(_header);
167
156
 
168
157
  if (_header) {
169
- const key = findKey(self, _header);
158
+ const key = utils.findKey(self, _header);
170
159
 
171
160
  if (key && (!matcher || matchHeaderValue(self, self[key], key, matcher))) {
172
161
  delete self[key];
@@ -183,18 +172,18 @@ Object.assign(AxiosHeaders.prototype, {
183
172
  }
184
173
 
185
174
  return deleted;
186
- },
175
+ }
187
176
 
188
- clear: function() {
177
+ clear() {
189
178
  return Object.keys(this).forEach(this.delete.bind(this));
190
- },
179
+ }
191
180
 
192
- normalize: function(format) {
181
+ normalize(format) {
193
182
  const self = this;
194
183
  const headers = {};
195
184
 
196
185
  utils.forEach(this, (value, header) => {
197
- const key = findKey(headers, header);
186
+ const key = utils.findKey(headers, header);
198
187
 
199
188
  if (key) {
200
189
  self[key] = normalizeValue(value);
@@ -214,30 +203,47 @@ Object.assign(AxiosHeaders.prototype, {
214
203
  });
215
204
 
216
205
  return this;
217
- },
206
+ }
207
+
208
+ concat(...targets) {
209
+ return this.constructor.concat(this, ...targets);
210
+ }
218
211
 
219
- toJSON: function(asStrings) {
212
+ toJSON(asStrings) {
220
213
  const obj = Object.create(null);
221
214
 
222
- utils.forEach(Object.assign({}, this[$defaults] || null, this),
223
- (value, header) => {
224
- if (value == null || value === false) return;
225
- obj[header] = asStrings && utils.isArray(value) ? value.join(', ') : value;
226
- });
215
+ utils.forEach(this, (value, header) => {
216
+ value != null && value !== false && (obj[header] = asStrings && utils.isArray(value) ? value.join(', ') : value);
217
+ });
227
218
 
228
219
  return obj;
229
220
  }
230
- });
231
221
 
232
- Object.assign(AxiosHeaders, {
233
- from: function(thing) {
234
- if (utils.isString(thing)) {
235
- return new this(parseHeaders(thing));
236
- }
222
+ [Symbol.iterator]() {
223
+ return Object.entries(this.toJSON())[Symbol.iterator]();
224
+ }
225
+
226
+ toString() {
227
+ return Object.entries(this.toJSON()).map(([header, value]) => header + ': ' + value).join('\n');
228
+ }
229
+
230
+ get [Symbol.toStringTag]() {
231
+ return 'AxiosHeaders';
232
+ }
233
+
234
+ static from(thing) {
237
235
  return thing instanceof this ? thing : new this(thing);
238
- },
236
+ }
239
237
 
240
- accessor: function(header) {
238
+ static concat(first, ...targets) {
239
+ const computed = new this(first);
240
+
241
+ targets.forEach((target) => computed.set(target));
242
+
243
+ return computed;
244
+ }
245
+
246
+ static accessor(header) {
241
247
  const internals = this[$internals] = (this[$internals] = {
242
248
  accessors: {}
243
249
  });
@@ -258,7 +264,7 @@ Object.assign(AxiosHeaders, {
258
264
 
259
265
  return this;
260
266
  }
261
- });
267
+ }
262
268
 
263
269
  AxiosHeaders.accessor(['Content-Type', 'Content-Length', 'Accept', 'Accept-Encoding', 'User-Agent']);
264
270
 
@@ -41,6 +41,10 @@ export default function dispatchRequest(config) {
41
41
  config.transformRequest
42
42
  );
43
43
 
44
+ if (['post', 'put', 'patch'].indexOf(config.method) !== -1) {
45
+ config.headers.setContentType('application/x-www-form-urlencoded', false);
46
+ }
47
+
44
48
  const adapter = config.adapter || defaults.adapter;
45
49
 
46
50
  return adapter(config).then(function onAdapterResolution(response) {
@@ -1,6 +1,9 @@
1
1
  'use strict';
2
2
 
3
3
  import utils from '../utils.js';
4
+ import AxiosHeaders from "./AxiosHeaders.js";
5
+
6
+ const headersToObject = (thing) => thing instanceof AxiosHeaders ? thing.toJSON() : thing;
4
7
 
5
8
  /**
6
9
  * Config-specific merge-function which creates a new config-object
@@ -16,9 +19,9 @@ export default function mergeConfig(config1, config2) {
16
19
  config2 = config2 || {};
17
20
  const config = {};
18
21
 
19
- function getMergedValue(target, source) {
22
+ function getMergedValue(target, source, caseless) {
20
23
  if (utils.isPlainObject(target) && utils.isPlainObject(source)) {
21
- return utils.merge(target, source);
24
+ return utils.merge.call({caseless}, target, source);
22
25
  } else if (utils.isPlainObject(source)) {
23
26
  return utils.merge({}, source);
24
27
  } else if (utils.isArray(source)) {
@@ -28,72 +31,73 @@ export default function mergeConfig(config1, config2) {
28
31
  }
29
32
 
30
33
  // eslint-disable-next-line consistent-return
31
- function mergeDeepProperties(prop) {
32
- if (!utils.isUndefined(config2[prop])) {
33
- return getMergedValue(config1[prop], config2[prop]);
34
- } else if (!utils.isUndefined(config1[prop])) {
35
- return getMergedValue(undefined, config1[prop]);
34
+ function mergeDeepProperties(a, b, caseless) {
35
+ if (!utils.isUndefined(b)) {
36
+ return getMergedValue(a, b, caseless);
37
+ } else if (!utils.isUndefined(a)) {
38
+ return getMergedValue(undefined, a, caseless);
36
39
  }
37
40
  }
38
41
 
39
42
  // eslint-disable-next-line consistent-return
40
- function valueFromConfig2(prop) {
41
- if (!utils.isUndefined(config2[prop])) {
42
- return getMergedValue(undefined, config2[prop]);
43
+ function valueFromConfig2(a, b) {
44
+ if (!utils.isUndefined(b)) {
45
+ return getMergedValue(undefined, b);
43
46
  }
44
47
  }
45
48
 
46
49
  // eslint-disable-next-line consistent-return
47
- function defaultToConfig2(prop) {
48
- if (!utils.isUndefined(config2[prop])) {
49
- return getMergedValue(undefined, config2[prop]);
50
- } else if (!utils.isUndefined(config1[prop])) {
51
- return getMergedValue(undefined, config1[prop]);
50
+ function defaultToConfig2(a, b) {
51
+ if (!utils.isUndefined(b)) {
52
+ return getMergedValue(undefined, b);
53
+ } else if (!utils.isUndefined(a)) {
54
+ return getMergedValue(undefined, a);
52
55
  }
53
56
  }
54
57
 
55
58
  // eslint-disable-next-line consistent-return
56
- function mergeDirectKeys(prop) {
59
+ function mergeDirectKeys(a, b, prop) {
57
60
  if (prop in config2) {
58
- return getMergedValue(config1[prop], config2[prop]);
61
+ return getMergedValue(a, b);
59
62
  } else if (prop in config1) {
60
- return getMergedValue(undefined, config1[prop]);
63
+ return getMergedValue(undefined, a);
61
64
  }
62
65
  }
63
66
 
64
67
  const mergeMap = {
65
- 'url': valueFromConfig2,
66
- 'method': valueFromConfig2,
67
- 'data': valueFromConfig2,
68
- 'baseURL': defaultToConfig2,
69
- 'transformRequest': defaultToConfig2,
70
- 'transformResponse': defaultToConfig2,
71
- 'paramsSerializer': defaultToConfig2,
72
- 'timeout': defaultToConfig2,
73
- 'timeoutMessage': defaultToConfig2,
74
- 'withCredentials': defaultToConfig2,
75
- 'adapter': defaultToConfig2,
76
- 'responseType': defaultToConfig2,
77
- 'xsrfCookieName': defaultToConfig2,
78
- 'xsrfHeaderName': defaultToConfig2,
79
- 'onUploadProgress': defaultToConfig2,
80
- 'onDownloadProgress': defaultToConfig2,
81
- 'decompress': defaultToConfig2,
82
- 'maxContentLength': defaultToConfig2,
83
- 'maxBodyLength': defaultToConfig2,
84
- 'beforeRedirect': defaultToConfig2,
85
- 'transport': defaultToConfig2,
86
- 'httpAgent': defaultToConfig2,
87
- 'httpsAgent': defaultToConfig2,
88
- 'cancelToken': defaultToConfig2,
89
- 'socketPath': defaultToConfig2,
90
- 'responseEncoding': defaultToConfig2,
91
- 'validateStatus': mergeDirectKeys
68
+ url: valueFromConfig2,
69
+ method: valueFromConfig2,
70
+ data: valueFromConfig2,
71
+ baseURL: defaultToConfig2,
72
+ transformRequest: defaultToConfig2,
73
+ transformResponse: defaultToConfig2,
74
+ paramsSerializer: defaultToConfig2,
75
+ timeout: defaultToConfig2,
76
+ timeoutMessage: defaultToConfig2,
77
+ withCredentials: defaultToConfig2,
78
+ adapter: defaultToConfig2,
79
+ responseType: defaultToConfig2,
80
+ xsrfCookieName: defaultToConfig2,
81
+ xsrfHeaderName: defaultToConfig2,
82
+ onUploadProgress: defaultToConfig2,
83
+ onDownloadProgress: defaultToConfig2,
84
+ decompress: defaultToConfig2,
85
+ maxContentLength: defaultToConfig2,
86
+ maxBodyLength: defaultToConfig2,
87
+ beforeRedirect: defaultToConfig2,
88
+ transport: defaultToConfig2,
89
+ httpAgent: defaultToConfig2,
90
+ httpsAgent: defaultToConfig2,
91
+ cancelToken: defaultToConfig2,
92
+ socketPath: defaultToConfig2,
93
+ responseEncoding: defaultToConfig2,
94
+ validateStatus: mergeDirectKeys,
95
+ headers: (a, b) => mergeDeepProperties(headersToObject(a), headersToObject(b), true)
92
96
  };
93
97
 
94
98
  utils.forEach(Object.keys(config1).concat(Object.keys(config2)), function computeConfigValue(prop) {
95
99
  const merge = mergeMap[prop] || mergeDeepProperties;
96
- const configValue = merge(prop);
100
+ const configValue = merge(config1[prop], config2[prop], prop);
97
101
  (utils.isUndefined(configValue) && merge !== mergeDirectKeys) || (config[prop] = configValue);
98
102
  });
99
103
 
@@ -10,7 +10,7 @@ import formDataToJSON from '../helpers/formDataToJSON.js';
10
10
  import adapters from '../adapters/index.js';
11
11
 
12
12
  const DEFAULT_CONTENT_TYPE = {
13
- 'Content-Type': 'application/x-www-form-urlencoded'
13
+ 'Content-Type': undefined
14
14
  };
15
15
 
16
16
  /**
package/lib/env/data.js CHANGED
@@ -1 +1 @@
1
- export const VERSION = "1.1.3";
1
+ export const VERSION = "1.2.0-alpha.1";
package/lib/utils.js CHANGED
@@ -228,7 +228,7 @@ const trim = (str) => str.trim ?
228
228
  * @param {Function} fn The callback to invoke for each item
229
229
  *
230
230
  * @param {Boolean} [allOwnKeys = false]
231
- * @returns {void}
231
+ * @returns {any}
232
232
  */
233
233
  function forEach(obj, fn, {allOwnKeys = false} = {}) {
234
234
  // Don't bother if no value provided
@@ -263,6 +263,24 @@ function forEach(obj, fn, {allOwnKeys = false} = {}) {
263
263
  }
264
264
  }
265
265
 
266
+ function findKey(obj, key) {
267
+ key = key.toLowerCase();
268
+ const keys = Object.keys(obj);
269
+ let i = keys.length;
270
+ let _key;
271
+ while (i-- > 0) {
272
+ _key = keys[i];
273
+ if (key === _key.toLowerCase()) {
274
+ return _key;
275
+ }
276
+ }
277
+ return null;
278
+ }
279
+
280
+ const _global = typeof self === "undefined" ? typeof global === "undefined" ? this : global : self;
281
+
282
+ const isContextDefined = (context) => !isUndefined(context) && context !== _global;
283
+
266
284
  /**
267
285
  * Accepts varargs expecting each argument to be an object, then
268
286
  * immutably merges the properties of each object and returns result.
@@ -282,16 +300,18 @@ function forEach(obj, fn, {allOwnKeys = false} = {}) {
282
300
  * @returns {Object} Result of all merge properties
283
301
  */
284
302
  function merge(/* obj1, obj2, obj3, ... */) {
303
+ const {caseless} = isContextDefined(this) && this || {};
285
304
  const result = {};
286
305
  const assignValue = (val, key) => {
287
- if (isPlainObject(result[key]) && isPlainObject(val)) {
288
- result[key] = merge(result[key], val);
306
+ const targetKey = caseless && findKey(result, key) || key;
307
+ if (isPlainObject(result[targetKey]) && isPlainObject(val)) {
308
+ result[targetKey] = merge(result[targetKey], val);
289
309
  } else if (isPlainObject(val)) {
290
- result[key] = merge({}, val);
310
+ result[targetKey] = merge({}, val);
291
311
  } else if (isArray(val)) {
292
- result[key] = val.slice();
312
+ result[targetKey] = val.slice();
293
313
  } else {
294
- result[key] = val;
314
+ result[targetKey] = val;
295
315
  }
296
316
  }
297
317
 
@@ -527,6 +547,11 @@ const reduceDescriptors = (obj, reducer) => {
527
547
 
528
548
  const freezeMethods = (obj) => {
529
549
  reduceDescriptors(obj, (descriptor, name) => {
550
+ // skip restricted props in strict mode
551
+ if (isFunction(obj) && ['arguments', 'caller', 'callee'].indexOf(name) !== -1) {
552
+ return false;
553
+ }
554
+
530
555
  const value = obj[name];
531
556
 
532
557
  if (!isFunction(value)) return;
@@ -540,7 +565,7 @@ const freezeMethods = (obj) => {
540
565
 
541
566
  if (!descriptor.set) {
542
567
  descriptor.set = () => {
543
- throw Error('Can not read-only method \'' + name + '\'');
568
+ throw Error('Can not rewrite read-only method \'' + name + '\'');
544
569
  };
545
570
  }
546
571
  });
@@ -609,5 +634,8 @@ export default {
609
634
  toObjectSet,
610
635
  toCamelCase,
611
636
  noop,
612
- toFiniteNumber
637
+ toFiniteNumber,
638
+ findKey,
639
+ global: _global,
640
+ isContextDefined
613
641
  };
package/package.json CHANGED
@@ -1,27 +1,33 @@
1
1
  {
2
2
  "name": "axios",
3
- "version": "1.1.3",
3
+ "version": "1.2.0-alpha.1",
4
4
  "description": "Promise based HTTP client for the browser and node.js",
5
5
  "main": "index.js",
6
6
  "exports": {
7
7
  ".": {
8
+ "types": {
9
+ "require": "./index.d.cts",
10
+ "default": "./index.d.ts"
11
+ },
8
12
  "browser": {
9
- "require": "./dist/node/axios.cjs",
13
+ "require": "./dist/browser/axios.cjs",
10
14
  "default": "./index.js"
11
15
  },
12
16
  "default": {
13
17
  "require": "./dist/node/axios.cjs",
14
18
  "default": "./index.js"
15
19
  }
16
- }
20
+ },
21
+ "./package.json": "./package.json"
17
22
  },
18
23
  "type": "module",
19
24
  "types": "index.d.ts",
20
25
  "scripts": {
21
- "test": "npm run test:eslint && npm run test:mocha && npm run test:karma && npm run test:dtslint",
26
+ "test": "npm run test:eslint && npm run test:mocha && npm run test:karma && npm run test:exports && npm run test:dtslint",
22
27
  "test:eslint": "node bin/ssl_hotfix.js eslint lib/**/*.js",
23
28
  "test:dtslint": "node bin/ssl_hotfix.js dtslint",
24
29
  "test:mocha": "node bin/ssl_hotfix.js mocha test/unit/**/*.js --timeout 30000 --exit",
30
+ "test:exports": "node bin/ssl_hotfix.js mocha test/module/test.js --timeout 30000 --exit",
25
31
  "test:karma": "node bin/ssl_hotfix.js cross-env LISTEN_ADDR=:: karma start karma.conf.cjs --single-run",
26
32
  "test:karma:server": "node bin/ssl_hotfix.js cross-env karma start karma.conf.cjs",
27
33
  "start": "node ./sandbox/server.js",
@@ -94,7 +100,7 @@
94
100
  "sinon": "^4.5.0",
95
101
  "stream-throttle": "^0.1.3",
96
102
  "terser-webpack-plugin": "^4.2.3",
97
- "typescript": "^4.6.3",
103
+ "typescript": "^4.8.4",
98
104
  "url-search-params": "^0.10.0"
99
105
  },
100
106
  "browser": {
@@ -130,5 +136,6 @@
130
136
  "Yasu Flores (https://github.com/yasuf)",
131
137
  "Ben Carp (https://github.com/carpben)",
132
138
  "Daniel Lopretto (https://github.com/timemachine3030)"
133
- ]
139
+ ],
140
+ "sideEffects": false
134
141
  }