axios 0.26.1 → 0.27.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.
@@ -1,55 +1,72 @@
1
1
  'use strict';
2
2
 
3
- function combinedKey(parentKey, elKey) {
4
- return parentKey + '.' + elKey;
5
- }
3
+ var utils = require('../utils');
4
+
5
+ /**
6
+ * Convert a data object to FormData
7
+ * @param {Object} obj
8
+ * @param {?Object} [formData]
9
+ * @returns {Object}
10
+ **/
11
+
12
+ function toFormData(obj, formData) {
13
+ // eslint-disable-next-line no-param-reassign
14
+ formData = formData || new FormData();
15
+
16
+ var stack = [];
6
17
 
7
- function buildFormData(formData, data, parentKey) {
8
- if (Array.isArray(data)) {
9
- data.forEach(function buildArray(el, i) {
10
- buildFormData(formData, el, combinedKey(parentKey, i));
11
- });
12
- } else if (
13
- typeof data === 'object' &&
14
- !(data instanceof File || data === null)
15
- ) {
16
- Object.keys(data).forEach(function buildObject(key) {
17
- buildFormData(
18
- formData,
19
- data[key],
20
- parentKey ? combinedKey(parentKey, key) : key
21
- );
22
- });
23
- } else {
24
- if (data === undefined) {
25
- return;
18
+ function convertValue(value) {
19
+ if (value === null) return '';
20
+
21
+ if (utils.isDate(value)) {
22
+ return value.toISOString();
23
+ }
24
+
25
+ if (utils.isArrayBuffer(value) || utils.isTypedArray(value)) {
26
+ return typeof Blob === 'function' ? new Blob([value]) : Buffer.from(value);
26
27
  }
27
28
 
28
- var value =
29
- typeof data === 'boolean' || typeof data === 'number'
30
- ? data.toString()
31
- : data;
32
- formData.append(parentKey, value);
29
+ return value;
33
30
  }
34
- }
35
31
 
36
- /**
37
- * convert a data object to FormData
38
- *
39
- * type FormDataPrimitive = string | Blob | number | boolean
40
- * interface FormDataNest {
41
- * [x: string]: FormVal
42
- * }
43
- *
44
- * type FormVal = FormDataNest | FormDataPrimitive
45
- *
46
- * @param {FormVal} data
47
- */
48
-
49
- module.exports = function getFormData(data) {
50
- var formData = new FormData();
51
-
52
- buildFormData(formData, data);
32
+ function build(data, parentKey) {
33
+ if (utils.isPlainObject(data) || utils.isArray(data)) {
34
+ if (stack.indexOf(data) !== -1) {
35
+ throw Error('Circular reference detected in ' + parentKey);
36
+ }
37
+
38
+ stack.push(data);
39
+
40
+ utils.forEach(data, function each(value, key) {
41
+ if (utils.isUndefined(value)) return;
42
+ var fullKey = parentKey ? parentKey + '.' + key : key;
43
+ var arr;
44
+
45
+ if (value && !parentKey && typeof value === 'object') {
46
+ if (utils.endsWith(key, '{}')) {
47
+ // eslint-disable-next-line no-param-reassign
48
+ value = JSON.stringify(value);
49
+ } else if (utils.endsWith(key, '[]') && (arr = utils.toArray(value))) {
50
+ // eslint-disable-next-line func-names
51
+ arr.forEach(function(el) {
52
+ !utils.isUndefined(el) && formData.append(fullKey, convertValue(el));
53
+ });
54
+ return;
55
+ }
56
+ }
57
+
58
+ build(value, fullKey);
59
+ });
60
+
61
+ stack.pop();
62
+ } else {
63
+ formData.append(parentKey, convertValue(data));
64
+ }
65
+ }
66
+
67
+ build(obj);
53
68
 
54
69
  return formData;
55
- };
70
+ }
71
+
72
+ module.exports = toFormData;
@@ -1,6 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  var VERSION = require('../env/data').version;
4
+ var AxiosError = require('../core/AxiosError');
4
5
 
5
6
  var validators = {};
6
7
 
@@ -28,7 +29,10 @@ validators.transitional = function transitional(validator, version, message) {
28
29
  // eslint-disable-next-line func-names
29
30
  return function(value, opt, opts) {
30
31
  if (validator === false) {
31
- throw new Error(formatMessage(opt, ' has been removed' + (version ? ' in ' + version : '')));
32
+ throw new AxiosError(
33
+ formatMessage(opt, ' has been removed' + (version ? ' in ' + version : '')),
34
+ AxiosError.ERR_DEPRECATED
35
+ );
32
36
  }
33
37
 
34
38
  if (version && !deprecatedWarnings[opt]) {
@@ -55,7 +59,7 @@ validators.transitional = function transitional(validator, version, message) {
55
59
 
56
60
  function assertOptions(options, schema, allowUnknown) {
57
61
  if (typeof options !== 'object') {
58
- throw new TypeError('options must be an object');
62
+ throw new AxiosError('options must be an object', AxiosError.ERR_BAD_OPTION_VALUE);
59
63
  }
60
64
  var keys = Object.keys(options);
61
65
  var i = keys.length;
@@ -66,12 +70,12 @@ function assertOptions(options, schema, allowUnknown) {
66
70
  var value = options[opt];
67
71
  var result = value === undefined || validator(value, opt, options);
68
72
  if (result !== true) {
69
- throw new TypeError('option ' + opt + ' must be ' + result);
73
+ throw new AxiosError('option ' + opt + ' must be ' + result, AxiosError.ERR_BAD_OPTION_VALUE);
70
74
  }
71
75
  continue;
72
76
  }
73
77
  if (allowUnknown !== true) {
74
- throw Error('Unknown option ' + opt);
78
+ throw new AxiosError('Unknown option ' + opt, AxiosError.ERR_BAD_OPTION);
75
79
  }
76
80
  }
77
81
  }
package/lib/utils.js CHANGED
@@ -6,6 +6,38 @@ var bind = require('./helpers/bind');
6
6
 
7
7
  var toString = Object.prototype.toString;
8
8
 
9
+ // eslint-disable-next-line func-names
10
+ var kindOf = (function(cache) {
11
+ // eslint-disable-next-line func-names
12
+ return function(thing) {
13
+ var str = toString.call(thing);
14
+ return cache[str] || (cache[str] = str.slice(8, -1).toLowerCase());
15
+ };
16
+ })(Object.create(null));
17
+
18
+ function kindOfTest(type) {
19
+ type = type.toLowerCase();
20
+ return function isKindOf(thing) {
21
+ return kindOf(thing) === type;
22
+ };
23
+ }
24
+
25
+ /**
26
+ * Array with axios supported protocols.
27
+ */
28
+ var supportedProtocols = [ 'http:', 'https:', 'file:' ];
29
+
30
+ /**
31
+ * Returns URL protocol passed as param if is not undefined or null,
32
+ * otherwise just returns 'http:'
33
+ *
34
+ * @param {String} protocol The String value of URL protocol
35
+ * @returns {String} Protocol if the value is not undefined or null
36
+ */
37
+ function getProtocol(protocol) {
38
+ return protocol || 'http:';
39
+ }
40
+
9
41
  /**
10
42
  * Determine if a value is an Array
11
43
  *
@@ -40,22 +72,12 @@ function isBuffer(val) {
40
72
  /**
41
73
  * Determine if a value is an ArrayBuffer
42
74
  *
75
+ * @function
43
76
  * @param {Object} val The value to test
44
77
  * @returns {boolean} True if value is an ArrayBuffer, otherwise false
45
78
  */
46
- function isArrayBuffer(val) {
47
- return toString.call(val) === '[object ArrayBuffer]';
48
- }
79
+ var isArrayBuffer = kindOfTest('ArrayBuffer');
49
80
 
50
- /**
51
- * Determine if a value is a FormData
52
- *
53
- * @param {Object} val The value to test
54
- * @returns {boolean} True if value is an FormData, otherwise false
55
- */
56
- function isFormData(val) {
57
- return toString.call(val) === '[object FormData]';
58
- }
59
81
 
60
82
  /**
61
83
  * Determine if a value is a view on an ArrayBuffer
@@ -110,7 +132,7 @@ function isObject(val) {
110
132
  * @return {boolean} True if value is a plain Object, otherwise false
111
133
  */
112
134
  function isPlainObject(val) {
113
- if (toString.call(val) !== '[object Object]') {
135
+ if (kindOf(val) !== 'object') {
114
136
  return false;
115
137
  }
116
138
 
@@ -121,32 +143,38 @@ function isPlainObject(val) {
121
143
  /**
122
144
  * Determine if a value is a Date
123
145
  *
146
+ * @function
124
147
  * @param {Object} val The value to test
125
148
  * @returns {boolean} True if value is a Date, otherwise false
126
149
  */
127
- function isDate(val) {
128
- return toString.call(val) === '[object Date]';
129
- }
150
+ var isDate = kindOfTest('Date');
130
151
 
131
152
  /**
132
153
  * Determine if a value is a File
133
154
  *
155
+ * @function
134
156
  * @param {Object} val The value to test
135
157
  * @returns {boolean} True if value is a File, otherwise false
136
158
  */
137
- function isFile(val) {
138
- return toString.call(val) === '[object File]';
139
- }
159
+ var isFile = kindOfTest('File');
140
160
 
141
161
  /**
142
162
  * Determine if a value is a Blob
143
163
  *
164
+ * @function
144
165
  * @param {Object} val The value to test
145
166
  * @returns {boolean} True if value is a Blob, otherwise false
146
167
  */
147
- function isBlob(val) {
148
- return toString.call(val) === '[object Blob]';
149
- }
168
+ var isBlob = kindOfTest('Blob');
169
+
170
+ /**
171
+ * Determine if a value is a FileList
172
+ *
173
+ * @function
174
+ * @param {Object} val The value to test
175
+ * @returns {boolean} True if value is a File, otherwise false
176
+ */
177
+ var isFileList = kindOfTest('FileList');
150
178
 
151
179
  /**
152
180
  * Determine if a value is a Function
@@ -169,14 +197,27 @@ function isStream(val) {
169
197
  }
170
198
 
171
199
  /**
172
- * Determine if a value is a URLSearchParams object
200
+ * Determine if a value is a FormData
173
201
  *
202
+ * @param {Object} thing The value to test
203
+ * @returns {boolean} True if value is an FormData, otherwise false
204
+ */
205
+ function isFormData(thing) {
206
+ var pattern = '[object FormData]';
207
+ return thing && (
208
+ (typeof FormData === 'function' && thing instanceof FormData) ||
209
+ toString.call(thing) === pattern ||
210
+ (isFunction(thing.toString) && thing.toString() === pattern)
211
+ );
212
+ }
213
+
214
+ /**
215
+ * Determine if a value is a URLSearchParams object
216
+ * @function
174
217
  * @param {Object} val The value to test
175
218
  * @returns {boolean} True if value is a URLSearchParams object, otherwise false
176
219
  */
177
- function isURLSearchParams(val) {
178
- return toString.call(val) === '[object URLSearchParams]';
179
- }
220
+ var isURLSearchParams = kindOfTest('URLSearchParams');
180
221
 
181
222
  /**
182
223
  * Trim excess whitespace off the beginning and end of a string
@@ -323,7 +364,97 @@ function stripBOM(content) {
323
364
  return content;
324
365
  }
325
366
 
367
+ /**
368
+ * Inherit the prototype methods from one constructor into another
369
+ * @param {function} constructor
370
+ * @param {function} superConstructor
371
+ * @param {object} [props]
372
+ * @param {object} [descriptors]
373
+ */
374
+
375
+ function inherits(constructor, superConstructor, props, descriptors) {
376
+ constructor.prototype = Object.create(superConstructor.prototype, descriptors);
377
+ constructor.prototype.constructor = constructor;
378
+ props && Object.assign(constructor.prototype, props);
379
+ }
380
+
381
+ /**
382
+ * Resolve object with deep prototype chain to a flat object
383
+ * @param {Object} sourceObj source object
384
+ * @param {Object} [destObj]
385
+ * @param {Function} [filter]
386
+ * @returns {Object}
387
+ */
388
+
389
+ function toFlatObject(sourceObj, destObj, filter) {
390
+ var props;
391
+ var i;
392
+ var prop;
393
+ var merged = {};
394
+
395
+ destObj = destObj || {};
396
+
397
+ do {
398
+ props = Object.getOwnPropertyNames(sourceObj);
399
+ i = props.length;
400
+ while (i-- > 0) {
401
+ prop = props[i];
402
+ if (!merged[prop]) {
403
+ destObj[prop] = sourceObj[prop];
404
+ merged[prop] = true;
405
+ }
406
+ }
407
+ sourceObj = Object.getPrototypeOf(sourceObj);
408
+ } while (sourceObj && (!filter || filter(sourceObj, destObj)) && sourceObj !== Object.prototype);
409
+
410
+ return destObj;
411
+ }
412
+
413
+ /*
414
+ * determines whether a string ends with the characters of a specified string
415
+ * @param {String} str
416
+ * @param {String} searchString
417
+ * @param {Number} [position= 0]
418
+ * @returns {boolean}
419
+ */
420
+ function endsWith(str, searchString, position) {
421
+ str = String(str);
422
+ if (position === undefined || position > str.length) {
423
+ position = str.length;
424
+ }
425
+ position -= searchString.length;
426
+ var lastIndex = str.indexOf(searchString, position);
427
+ return lastIndex !== -1 && lastIndex === position;
428
+ }
429
+
430
+
431
+ /**
432
+ * Returns new array from array like object
433
+ * @param {*} [thing]
434
+ * @returns {Array}
435
+ */
436
+ function toArray(thing) {
437
+ if (!thing) return null;
438
+ var i = thing.length;
439
+ if (isUndefined(i)) return null;
440
+ var arr = new Array(i);
441
+ while (i-- > 0) {
442
+ arr[i] = thing[i];
443
+ }
444
+ return arr;
445
+ }
446
+
447
+ // eslint-disable-next-line func-names
448
+ var isTypedArray = (function(TypedArray) {
449
+ // eslint-disable-next-line func-names
450
+ return function(thing) {
451
+ return TypedArray && thing instanceof TypedArray;
452
+ };
453
+ })(typeof Uint8Array !== 'undefined' && Object.getPrototypeOf(Uint8Array));
454
+
326
455
  module.exports = {
456
+ supportedProtocols: supportedProtocols,
457
+ getProtocol: getProtocol,
327
458
  isArray: isArray,
328
459
  isArrayBuffer: isArrayBuffer,
329
460
  isBuffer: isBuffer,
@@ -345,5 +476,13 @@ module.exports = {
345
476
  merge: merge,
346
477
  extend: extend,
347
478
  trim: trim,
348
- stripBOM: stripBOM
479
+ stripBOM: stripBOM,
480
+ inherits: inherits,
481
+ toFlatObject: toFlatObject,
482
+ kindOf: kindOf,
483
+ kindOfTest: kindOfTest,
484
+ endsWith: endsWith,
485
+ toArray: toArray,
486
+ isTypedArray: isTypedArray,
487
+ isFileList: isFileList
349
488
  };
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "axios",
3
- "version": "0.26.1",
3
+ "version": "0.27.0",
4
4
  "description": "Promise based HTTP client for the browser and node.js",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
7
7
  "scripts": {
8
8
  "test": "grunt test && dtslint",
9
9
  "start": "node ./sandbox/server.js",
10
- "build": "NODE_ENV=production grunt build",
10
+ "build": "cross-env NODE_ENV=production grunt build",
11
11
  "preversion": "grunt version && npm test",
12
12
  "version": "npm run build && git add -A dist && git add CHANGELOG.md bower.json package.json",
13
13
  "postversion": "git push && git push --tags",
@@ -33,24 +33,26 @@
33
33
  },
34
34
  "homepage": "https://axios-http.com",
35
35
  "devDependencies": {
36
- "abortcontroller-polyfill": "^1.5.0",
37
- "coveralls": "^3.0.0",
38
- "dtslint": "^4.1.6",
39
- "es6-promise": "^4.2.4",
40
- "grunt": "^1.3.0",
36
+ "abortcontroller-polyfill": "^1.7.3",
37
+ "coveralls": "^3.1.1",
38
+ "cross-env": "^7.0.3",
39
+ "dtslint": "^4.2.1",
40
+ "es6-promise": "^4.2.8",
41
+ "formidable": "^2.0.1",
42
+ "grunt": "^1.4.1",
41
43
  "grunt-banner": "^0.6.0",
42
- "grunt-cli": "^1.2.0",
43
- "grunt-contrib-clean": "^1.1.0",
44
- "grunt-contrib-watch": "^1.0.0",
45
- "grunt-eslint": "^23.0.0",
46
- "grunt-karma": "^4.0.0",
44
+ "grunt-cli": "^1.4.3",
45
+ "grunt-contrib-clean": "^2.0.0",
46
+ "grunt-contrib-watch": "^1.1.0",
47
+ "grunt-eslint": "^24.0.0",
48
+ "grunt-karma": "^4.0.2",
47
49
  "grunt-mocha-test": "^0.13.3",
48
- "grunt-webpack": "^4.0.2",
49
- "istanbul-instrumenter-loader": "^1.0.0",
50
+ "grunt-webpack": "^5.0.0",
51
+ "istanbul-instrumenter-loader": "^3.0.1",
50
52
  "jasmine-core": "^2.4.1",
51
- "karma": "^6.3.2",
52
- "karma-chrome-launcher": "^3.1.0",
53
- "karma-firefox-launcher": "^2.1.0",
53
+ "karma": "^6.3.17",
54
+ "karma-chrome-launcher": "^3.1.1",
55
+ "karma-firefox-launcher": "^2.1.2",
54
56
  "karma-jasmine": "^1.1.1",
55
57
  "karma-jasmine-ajax": "^0.1.13",
56
58
  "karma-safari-launcher": "^1.0.0",
@@ -58,24 +60,26 @@
58
60
  "karma-sinon": "^1.0.5",
59
61
  "karma-sourcemap-loader": "^0.3.8",
60
62
  "karma-webpack": "^4.0.2",
61
- "load-grunt-tasks": "^3.5.2",
62
- "minimist": "^1.2.0",
63
+ "load-grunt-tasks": "^5.1.0",
64
+ "minimist": "^1.2.6",
63
65
  "mocha": "^8.2.1",
64
66
  "sinon": "^4.5.0",
65
67
  "terser-webpack-plugin": "^4.2.3",
66
- "typescript": "^4.0.5",
68
+ "typescript": "^4.6.3",
67
69
  "url-search-params": "^0.10.0",
68
70
  "webpack": "^4.44.2",
69
71
  "webpack-dev-server": "^3.11.0"
70
72
  },
71
73
  "browser": {
72
- "./lib/adapters/http.js": "./lib/adapters/xhr.js"
74
+ "./lib/adapters/http.js": "./lib/adapters/xhr.js",
75
+ "./lib/defaults/env/FormData.js": "./lib/helpers/null.js"
73
76
  },
74
77
  "jsdelivr": "dist/axios.min.js",
75
78
  "unpkg": "dist/axios.min.js",
76
79
  "typings": "./index.d.ts",
77
80
  "dependencies": {
78
- "follow-redirects": "^1.14.8"
81
+ "follow-redirects": "^1.14.9",
82
+ "form-data": "^4.0.0"
79
83
  },
80
84
  "bundlesize": [
81
85
  {
@@ -1,19 +0,0 @@
1
- 'use strict';
2
-
3
- /**
4
- * A `Cancel` is an object that is thrown when an operation is canceled.
5
- *
6
- * @class
7
- * @param {string=} message The message.
8
- */
9
- function Cancel(message) {
10
- this.message = message;
11
- }
12
-
13
- Cancel.prototype.toString = function toString() {
14
- return 'Cancel' + (this.message ? ': ' + this.message : '');
15
- };
16
-
17
- Cancel.prototype.__CANCEL__ = true;
18
-
19
- module.exports = Cancel;
@@ -1,18 +0,0 @@
1
- 'use strict';
2
-
3
- var enhanceError = require('./enhanceError');
4
-
5
- /**
6
- * Create an Error with the specified message, config, error code, request and response.
7
- *
8
- * @param {string} message The error message.
9
- * @param {Object} config The config.
10
- * @param {string} [code] The error code (for example, 'ECONNABORTED').
11
- * @param {Object} [request] The request.
12
- * @param {Object} [response] The response.
13
- * @returns {Error} The created error.
14
- */
15
- module.exports = function createError(message, config, code, request, response) {
16
- var error = new Error(message);
17
- return enhanceError(error, config, code, request, response);
18
- };
@@ -1,43 +0,0 @@
1
- 'use strict';
2
-
3
- /**
4
- * Update an Error with the specified config, error code, and response.
5
- *
6
- * @param {Error} error The error to update.
7
- * @param {Object} config The config.
8
- * @param {string} [code] The error code (for example, 'ECONNABORTED').
9
- * @param {Object} [request] The request.
10
- * @param {Object} [response] The response.
11
- * @returns {Error} The error.
12
- */
13
- module.exports = function enhanceError(error, config, code, request, response) {
14
- error.config = config;
15
- if (code) {
16
- error.code = code;
17
- }
18
-
19
- error.request = request;
20
- error.response = response;
21
- error.isAxiosError = true;
22
-
23
- error.toJSON = function toJSON() {
24
- return {
25
- // Standard
26
- message: this.message,
27
- name: this.name,
28
- // Microsoft
29
- description: this.description,
30
- number: this.number,
31
- // Mozilla
32
- fileName: this.fileName,
33
- lineNumber: this.lineNumber,
34
- columnNumber: this.columnNumber,
35
- stack: this.stack,
36
- // Axios
37
- config: this.config,
38
- code: this.code,
39
- status: this.response && this.response.status ? this.response.status : null
40
- };
41
- };
42
- return error;
43
- };