axios 0.19.2 → 0.21.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/index.d.ts CHANGED
@@ -29,15 +29,16 @@ export type Method =
29
29
  | 'post' | 'POST'
30
30
  | 'put' | 'PUT'
31
31
  | 'patch' | 'PATCH'
32
+ | 'purge' | 'PURGE'
32
33
  | 'link' | 'LINK'
33
34
  | 'unlink' | 'UNLINK'
34
35
 
35
- export type ResponseType =
36
- | 'arraybuffer'
37
- | 'blob'
38
- | 'document'
39
- | 'json'
40
- | 'text'
36
+ export type ResponseType =
37
+ | 'arraybuffer'
38
+ | 'blob'
39
+ | 'document'
40
+ | 'json'
41
+ | 'text'
41
42
  | 'stream'
42
43
 
43
44
  export interface AxiosRequestConfig {
@@ -61,13 +62,15 @@ export interface AxiosRequestConfig {
61
62
  onUploadProgress?: (progressEvent: any) => void;
62
63
  onDownloadProgress?: (progressEvent: any) => void;
63
64
  maxContentLength?: number;
64
- validateStatus?: (status: number) => boolean;
65
+ validateStatus?: ((status: number) => boolean) | null;
66
+ maxBodyLength?: number;
65
67
  maxRedirects?: number;
66
68
  socketPath?: string | null;
67
69
  httpAgent?: any;
68
70
  httpsAgent?: any;
69
71
  proxy?: AxiosProxyConfig | false;
70
72
  cancelToken?: CancelToken;
73
+ decompress?: boolean;
71
74
  }
72
75
 
73
76
  export interface AxiosResponse<T = any> {
@@ -150,8 +153,9 @@ export interface AxiosStatic extends AxiosInstance {
150
153
  isCancel(value: any): boolean;
151
154
  all<T>(values: (T | Promise<T>)[]): Promise<T[]>;
152
155
  spread<T, R>(callback: (...args: T[]) => R): (array: T[]) => R;
156
+ isAxiosError(payload: any): payload is AxiosError;
153
157
  }
154
158
 
155
- declare const Axios: AxiosStatic;
159
+ declare const axios: AxiosStatic;
156
160
 
157
- export default Axios;
161
+ export default axios;
@@ -16,6 +16,31 @@ var enhanceError = require('../core/enhanceError');
16
16
 
17
17
  var isHttps = /https:?/;
18
18
 
19
+ /**
20
+ *
21
+ * @param {http.ClientRequestArgs} options
22
+ * @param {AxiosProxyConfig} proxy
23
+ * @param {string} location
24
+ */
25
+ function setProxy(options, proxy, location) {
26
+ options.hostname = proxy.host;
27
+ options.host = proxy.host;
28
+ options.port = proxy.port;
29
+ options.path = location;
30
+
31
+ // Basic proxy authorization
32
+ if (proxy.auth) {
33
+ var base64 = Buffer.from(proxy.auth.username + ':' + proxy.auth.password, 'utf8').toString('base64');
34
+ options.headers['Proxy-Authorization'] = 'Basic ' + base64;
35
+ }
36
+
37
+ // If a proxy is used, any redirects must also pass through the proxy
38
+ options.beforeRedirect = function beforeRedirect(redirection) {
39
+ redirection.headers.host = redirection.host;
40
+ setProxy(redirection, proxy, redirection.href);
41
+ };
42
+ }
43
+
19
44
  /*eslint consistent-return:0*/
20
45
  module.exports = function httpAdapter(config) {
21
46
  return new Promise(function dispatchHttpRequest(resolvePromise, rejectPromise) {
@@ -126,11 +151,11 @@ module.exports = function httpAdapter(config) {
126
151
  });
127
152
  }
128
153
 
129
-
130
154
  if (shouldProxy) {
131
155
  proxy = {
132
156
  host: parsedProxyUrl.hostname,
133
- port: parsedProxyUrl.port
157
+ port: parsedProxyUrl.port,
158
+ protocol: parsedProxyUrl.protocol
134
159
  };
135
160
 
136
161
  if (parsedProxyUrl.auth) {
@@ -145,17 +170,8 @@ module.exports = function httpAdapter(config) {
145
170
  }
146
171
 
147
172
  if (proxy) {
148
- options.hostname = proxy.host;
149
- options.host = proxy.host;
150
173
  options.headers.host = parsed.hostname + (parsed.port ? ':' + parsed.port : '');
151
- options.port = proxy.port;
152
- options.path = protocol + '//' + parsed.hostname + (parsed.port ? ':' + parsed.port : '') + options.path;
153
-
154
- // Basic proxy authorization
155
- if (proxy.auth) {
156
- var base64 = Buffer.from(proxy.auth.username + ':' + proxy.auth.password, 'utf8').toString('base64');
157
- options.headers['Proxy-Authorization'] = 'Basic ' + base64;
158
- }
174
+ setProxy(options, proxy, protocol + '//' + parsed.hostname + (parsed.port ? ':' + parsed.port : '') + options.path);
159
175
  }
160
176
 
161
177
  var transport;
@@ -171,8 +187,8 @@ module.exports = function httpAdapter(config) {
171
187
  transport = isHttpsProxy ? httpsFollow : httpFollow;
172
188
  }
173
189
 
174
- if (config.maxContentLength && config.maxContentLength > -1) {
175
- options.maxBodyLength = config.maxContentLength;
190
+ if (config.maxBodyLength > -1) {
191
+ options.maxBodyLength = config.maxBodyLength;
176
192
  }
177
193
 
178
194
  // Create the request
@@ -181,22 +197,27 @@ module.exports = function httpAdapter(config) {
181
197
 
182
198
  // uncompress the response body transparently if required
183
199
  var stream = res;
184
- switch (res.headers['content-encoding']) {
185
- /*eslint default-case:0*/
186
- case 'gzip':
187
- case 'compress':
188
- case 'deflate':
189
- // add the unzipper to the body stream processing pipeline
190
- stream = (res.statusCode === 204) ? stream : stream.pipe(zlib.createUnzip());
191
-
192
- // remove the content-encoding in order to not confuse downstream operations
193
- delete res.headers['content-encoding'];
194
- break;
195
- }
196
200
 
197
201
  // return the last request in case of redirects
198
202
  var lastRequest = res.req || req;
199
203
 
204
+
205
+ // if no content, is HEAD request or decompress disabled we should not decompress
206
+ if (res.statusCode !== 204 && lastRequest.method !== 'HEAD' && config.decompress !== false) {
207
+ switch (res.headers['content-encoding']) {
208
+ /*eslint default-case:0*/
209
+ case 'gzip':
210
+ case 'compress':
211
+ case 'deflate':
212
+ // add the unzipper to the body stream processing pipeline
213
+ stream = stream.pipe(zlib.createUnzip());
214
+
215
+ // remove the content-encoding in order to not confuse downstream operations
216
+ delete res.headers['content-encoding'];
217
+ break;
218
+ }
219
+ }
220
+
200
221
  var response = {
201
222
  status: res.statusCode,
202
223
  statusText: res.statusMessage,
@@ -230,6 +251,9 @@ module.exports = function httpAdapter(config) {
230
251
  var responseData = Buffer.concat(responseBuffer);
231
252
  if (config.responseType !== 'arraybuffer') {
232
253
  responseData = responseData.toString(config.responseEncoding);
254
+ if (!config.responseEncoding || config.responseEncoding === 'utf8') {
255
+ responseData = utils.stripBOM(responseData);
256
+ }
233
257
  }
234
258
 
235
259
  response.data = responseData;
@@ -240,7 +264,7 @@ module.exports = function httpAdapter(config) {
240
264
 
241
265
  // Handle errors
242
266
  req.on('error', function handleRequestError(err) {
243
- if (req.aborted) return;
267
+ if (req.aborted && err.code !== 'ERR_FR_TOO_MANY_REDIRECTS') return;
244
268
  reject(enhanceError(err, config, null, req));
245
269
  });
246
270
 
@@ -2,6 +2,7 @@
2
2
 
3
3
  var utils = require('./../utils');
4
4
  var settle = require('./../core/settle');
5
+ var cookies = require('./../helpers/cookies');
5
6
  var buildURL = require('./../helpers/buildURL');
6
7
  var buildFullPath = require('../core/buildFullPath');
7
8
  var parseHeaders = require('./../helpers/parseHeaders');
@@ -22,7 +23,7 @@ module.exports = function xhrAdapter(config) {
22
23
  // HTTP basic authentication
23
24
  if (config.auth) {
24
25
  var username = config.auth.username || '';
25
- var password = config.auth.password || '';
26
+ var password = config.auth.password ? unescape(encodeURIComponent(config.auth.password)) : '';
26
27
  requestHeaders.Authorization = 'Basic ' + btoa(username + ':' + password);
27
28
  }
28
29
 
@@ -103,8 +104,6 @@ module.exports = function xhrAdapter(config) {
103
104
  // This is only done if running in a standard browser environment.
104
105
  // Specifically not if we're in a web worker, or react-native.
105
106
  if (utils.isStandardBrowserEnv()) {
106
- var cookies = require('./../helpers/cookies');
107
-
108
107
  // Add xsrf header
109
108
  var xsrfValue = (config.withCredentials || isURLSameOrigin(fullPath)) && config.xsrfCookieName ?
110
109
  cookies.read(config.xsrfCookieName) :
@@ -170,7 +169,7 @@ module.exports = function xhrAdapter(config) {
170
169
  });
171
170
  }
172
171
 
173
- if (requestData === undefined) {
172
+ if (!requestData) {
174
173
  requestData = null;
175
174
  }
176
175
 
package/lib/axios.js CHANGED
@@ -47,6 +47,9 @@ axios.all = function all(promises) {
47
47
  };
48
48
  axios.spread = require('./helpers/spread');
49
49
 
50
+ // Expose isAxiosError
51
+ axios.isAxiosError = require('./helpers/isAxiosError');
52
+
50
53
  module.exports = axios;
51
54
 
52
55
  // Allow use of default import syntax in TypeScript
package/lib/core/Axios.js CHANGED
@@ -73,9 +73,10 @@ Axios.prototype.getUri = function getUri(config) {
73
73
  utils.forEach(['delete', 'get', 'head', 'options'], function forEachMethodNoData(method) {
74
74
  /*eslint func-names:0*/
75
75
  Axios.prototype[method] = function(url, config) {
76
- return this.request(utils.merge(config || {}, {
76
+ return this.request(mergeConfig(config || {}, {
77
77
  method: method,
78
- url: url
78
+ url: url,
79
+ data: (config || {}).data
79
80
  }));
80
81
  };
81
82
  });
@@ -83,7 +84,7 @@ utils.forEach(['delete', 'get', 'head', 'options'], function forEachMethodNoData
83
84
  utils.forEach(['post', 'put', 'patch'], function forEachMethodWithData(method) {
84
85
  /*eslint func-names:0*/
85
86
  Axios.prototype[method] = function(url, data, config) {
86
- return this.request(utils.merge(config || {}, {
87
+ return this.request(mergeConfig(config || {}, {
87
88
  method: method,
88
89
  url: url,
89
90
  data: data
@@ -20,7 +20,7 @@ module.exports = function enhanceError(error, config, code, request, response) {
20
20
  error.response = response;
21
21
  error.isAxiosError = true;
22
22
 
23
- error.toJSON = function() {
23
+ error.toJSON = function toJSON() {
24
24
  return {
25
25
  // Standard
26
26
  message: this.message,
@@ -15,59 +15,73 @@ module.exports = function mergeConfig(config1, config2) {
15
15
  config2 = config2 || {};
16
16
  var config = {};
17
17
 
18
- var valueFromConfig2Keys = ['url', 'method', 'params', 'data'];
19
- var mergeDeepPropertiesKeys = ['headers', 'auth', 'proxy'];
18
+ var valueFromConfig2Keys = ['url', 'method', 'data'];
19
+ var mergeDeepPropertiesKeys = ['headers', 'auth', 'proxy', 'params'];
20
20
  var defaultToConfig2Keys = [
21
- 'baseURL', 'url', 'transformRequest', 'transformResponse', 'paramsSerializer',
22
- 'timeout', 'withCredentials', 'adapter', 'responseType', 'xsrfCookieName',
23
- 'xsrfHeaderName', 'onUploadProgress', 'onDownloadProgress',
24
- 'maxContentLength', 'validateStatus', 'maxRedirects', 'httpAgent',
25
- 'httpsAgent', 'cancelToken', 'socketPath'
21
+ 'baseURL', 'transformRequest', 'transformResponse', 'paramsSerializer',
22
+ 'timeout', 'timeoutMessage', 'withCredentials', 'adapter', 'responseType', 'xsrfCookieName',
23
+ 'xsrfHeaderName', 'onUploadProgress', 'onDownloadProgress', 'decompress',
24
+ 'maxContentLength', 'maxBodyLength', 'maxRedirects', 'transport', 'httpAgent',
25
+ 'httpsAgent', 'cancelToken', 'socketPath', 'responseEncoding'
26
26
  ];
27
+ var directMergeKeys = ['validateStatus'];
28
+
29
+ function getMergedValue(target, source) {
30
+ if (utils.isPlainObject(target) && utils.isPlainObject(source)) {
31
+ return utils.merge(target, source);
32
+ } else if (utils.isPlainObject(source)) {
33
+ return utils.merge({}, source);
34
+ } else if (utils.isArray(source)) {
35
+ return source.slice();
36
+ }
37
+ return source;
38
+ }
39
+
40
+ function mergeDeepProperties(prop) {
41
+ if (!utils.isUndefined(config2[prop])) {
42
+ config[prop] = getMergedValue(config1[prop], config2[prop]);
43
+ } else if (!utils.isUndefined(config1[prop])) {
44
+ config[prop] = getMergedValue(undefined, config1[prop]);
45
+ }
46
+ }
27
47
 
28
48
  utils.forEach(valueFromConfig2Keys, function valueFromConfig2(prop) {
29
- if (typeof config2[prop] !== 'undefined') {
30
- config[prop] = config2[prop];
49
+ if (!utils.isUndefined(config2[prop])) {
50
+ config[prop] = getMergedValue(undefined, config2[prop]);
31
51
  }
32
52
  });
33
53
 
34
- utils.forEach(mergeDeepPropertiesKeys, function mergeDeepProperties(prop) {
35
- if (utils.isObject(config2[prop])) {
36
- config[prop] = utils.deepMerge(config1[prop], config2[prop]);
37
- } else if (typeof config2[prop] !== 'undefined') {
38
- config[prop] = config2[prop];
39
- } else if (utils.isObject(config1[prop])) {
40
- config[prop] = utils.deepMerge(config1[prop]);
41
- } else if (typeof config1[prop] !== 'undefined') {
42
- config[prop] = config1[prop];
54
+ utils.forEach(mergeDeepPropertiesKeys, mergeDeepProperties);
55
+
56
+ utils.forEach(defaultToConfig2Keys, function defaultToConfig2(prop) {
57
+ if (!utils.isUndefined(config2[prop])) {
58
+ config[prop] = getMergedValue(undefined, config2[prop]);
59
+ } else if (!utils.isUndefined(config1[prop])) {
60
+ config[prop] = getMergedValue(undefined, config1[prop]);
43
61
  }
44
62
  });
45
63
 
46
- utils.forEach(defaultToConfig2Keys, function defaultToConfig2(prop) {
47
- if (typeof config2[prop] !== 'undefined') {
48
- config[prop] = config2[prop];
49
- } else if (typeof config1[prop] !== 'undefined') {
50
- config[prop] = config1[prop];
64
+ utils.forEach(directMergeKeys, function merge(prop) {
65
+ if (prop in config2) {
66
+ config[prop] = getMergedValue(config1[prop], config2[prop]);
67
+ } else if (prop in config1) {
68
+ config[prop] = getMergedValue(undefined, config1[prop]);
51
69
  }
52
70
  });
53
71
 
54
72
  var axiosKeys = valueFromConfig2Keys
55
73
  .concat(mergeDeepPropertiesKeys)
56
- .concat(defaultToConfig2Keys);
74
+ .concat(defaultToConfig2Keys)
75
+ .concat(directMergeKeys);
57
76
 
58
77
  var otherKeys = Object
59
- .keys(config2)
78
+ .keys(config1)
79
+ .concat(Object.keys(config2))
60
80
  .filter(function filterAxiosKeys(key) {
61
81
  return axiosKeys.indexOf(key) === -1;
62
82
  });
63
83
 
64
- utils.forEach(otherKeys, function otherKeysDefaultToConfig2(prop) {
65
- if (typeof config2[prop] !== 'undefined') {
66
- config[prop] = config2[prop];
67
- } else if (typeof config1[prop] !== 'undefined') {
68
- config[prop] = config1[prop];
69
- }
70
- });
84
+ utils.forEach(otherKeys, mergeDeepProperties);
71
85
 
72
86
  return config;
73
87
  };
@@ -11,7 +11,7 @@ var createError = require('./createError');
11
11
  */
12
12
  module.exports = function settle(resolve, reject, response) {
13
13
  var validateStatus = response.config.validateStatus;
14
- if (!validateStatus || validateStatus(response.status)) {
14
+ if (!response.status || !validateStatus || validateStatus(response.status)) {
15
15
  resolve(response);
16
16
  } else {
17
17
  reject(createError(
package/lib/defaults.js CHANGED
@@ -74,6 +74,7 @@ var defaults = {
74
74
  xsrfHeaderName: 'X-XSRF-TOKEN',
75
75
 
76
76
  maxContentLength: -1,
77
+ maxBodyLength: -1,
77
78
 
78
79
  validateStatus: function validateStatus(status) {
79
80
  return status >= 200 && status < 300;
@@ -4,7 +4,6 @@ var utils = require('./../utils');
4
4
 
5
5
  function encode(val) {
6
6
  return encodeURIComponent(val).
7
- replace(/%40/gi, '@').
8
7
  replace(/%3A/gi, ':').
9
8
  replace(/%24/g, '$').
10
9
  replace(/%2C/gi, ',').
@@ -0,0 +1,11 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * Determines whether the payload is an error thrown by Axios
5
+ *
6
+ * @param {*} payload The value to test
7
+ * @returns {boolean} True if the payload is an error thrown by Axios, otherwise false
8
+ */
9
+ module.exports = function isAxiosError(payload) {
10
+ return (typeof payload === 'object') && (payload.isAxiosError === true);
11
+ };
package/lib/utils.js CHANGED
@@ -105,6 +105,21 @@ function isObject(val) {
105
105
  return val !== null && typeof val === 'object';
106
106
  }
107
107
 
108
+ /**
109
+ * Determine if a value is a plain Object
110
+ *
111
+ * @param {Object} val The value to test
112
+ * @return {boolean} True if value is a plain Object, otherwise false
113
+ */
114
+ function isPlainObject(val) {
115
+ if (toString.call(val) !== '[object Object]') {
116
+ return false;
117
+ }
118
+
119
+ var prototype = Object.getPrototypeOf(val);
120
+ return prototype === null || prototype === Object.prototype;
121
+ }
122
+
108
123
  /**
109
124
  * Determine if a value is a Date
110
125
  *
@@ -261,34 +276,12 @@ function forEach(obj, fn) {
261
276
  function merge(/* obj1, obj2, obj3, ... */) {
262
277
  var result = {};
263
278
  function assignValue(val, key) {
264
- if (typeof result[key] === 'object' && typeof val === 'object') {
279
+ if (isPlainObject(result[key]) && isPlainObject(val)) {
265
280
  result[key] = merge(result[key], val);
266
- } else {
267
- result[key] = val;
268
- }
269
- }
270
-
271
- for (var i = 0, l = arguments.length; i < l; i++) {
272
- forEach(arguments[i], assignValue);
273
- }
274
- return result;
275
- }
276
-
277
- /**
278
- * Function equal to merge with the difference being that no reference
279
- * to original objects is kept.
280
- *
281
- * @see merge
282
- * @param {Object} obj1 Object to merge
283
- * @returns {Object} Result of all merge properties
284
- */
285
- function deepMerge(/* obj1, obj2, obj3, ... */) {
286
- var result = {};
287
- function assignValue(val, key) {
288
- if (typeof result[key] === 'object' && typeof val === 'object') {
289
- result[key] = deepMerge(result[key], val);
290
- } else if (typeof val === 'object') {
291
- result[key] = deepMerge({}, val);
281
+ } else if (isPlainObject(val)) {
282
+ result[key] = merge({}, val);
283
+ } else if (isArray(val)) {
284
+ result[key] = val.slice();
292
285
  } else {
293
286
  result[key] = val;
294
287
  }
@@ -319,6 +312,19 @@ function extend(a, b, thisArg) {
319
312
  return a;
320
313
  }
321
314
 
315
+ /**
316
+ * Remove byte order marker. This catches EF BB BF (the UTF-8 BOM)
317
+ *
318
+ * @param {string} content with BOM
319
+ * @return {string} content value without BOM
320
+ */
321
+ function stripBOM(content) {
322
+ if (content.charCodeAt(0) === 0xFEFF) {
323
+ content = content.slice(1);
324
+ }
325
+ return content;
326
+ }
327
+
322
328
  module.exports = {
323
329
  isArray: isArray,
324
330
  isArrayBuffer: isArrayBuffer,
@@ -328,6 +334,7 @@ module.exports = {
328
334
  isString: isString,
329
335
  isNumber: isNumber,
330
336
  isObject: isObject,
337
+ isPlainObject: isPlainObject,
331
338
  isUndefined: isUndefined,
332
339
  isDate: isDate,
333
340
  isFile: isFile,
@@ -338,7 +345,7 @@ module.exports = {
338
345
  isStandardBrowserEnv: isStandardBrowserEnv,
339
346
  forEach: forEach,
340
347
  merge: merge,
341
- deepMerge: deepMerge,
342
348
  extend: extend,
343
- trim: trim
349
+ trim: trim,
350
+ stripBOM: stripBOM
344
351
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "axios",
3
- "version": "0.19.2",
3
+ "version": "0.21.1",
4
4
  "description": "Promise based HTTP client for the browser and node.js",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -71,9 +71,11 @@
71
71
  "browser": {
72
72
  "./lib/adapters/http.js": "./lib/adapters/xhr.js"
73
73
  },
74
+ "jsdelivr": "dist/axios.min.js",
75
+ "unpkg": "dist/axios.min.js",
74
76
  "typings": "./index.d.ts",
75
77
  "dependencies": {
76
- "follow-redirects": "1.5.10"
78
+ "follow-redirects": "^1.10.0"
77
79
  },
78
80
  "bundlesize": [
79
81
  {