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.
package/index.d.ts CHANGED
@@ -1,5 +1,4 @@
1
1
  // TypeScript Version: 3.0
2
-
3
2
  export type AxiosRequestHeaders = Record<string, string | number | boolean>;
4
3
 
5
4
  export type AxiosResponseHeaders = Record<string, string> & {
@@ -75,7 +74,7 @@ export interface TransitionalOptions {
75
74
 
76
75
  export interface AxiosRequestConfig<D = any> {
77
76
  url?: string;
78
- method?: Method;
77
+ method?: Method | string;
79
78
  baseURL?: string;
80
79
  transformRequest?: AxiosRequestTransformer | AxiosRequestTransformer[];
81
80
  transformResponse?: AxiosResponseTransformer | AxiosResponseTransformer[];
@@ -98,6 +97,7 @@ export interface AxiosRequestConfig<D = any> {
98
97
  validateStatus?: ((status: number) => boolean) | null;
99
98
  maxBodyLength?: number;
100
99
  maxRedirects?: number;
100
+ beforeRedirect?: (options: Record<string, any>, responseDetails: {headers: Record<string, string>}) => void;
101
101
  socketPath?: string | null;
102
102
  httpAgent?: any;
103
103
  httpsAgent?: any;
@@ -107,6 +107,9 @@ export interface AxiosRequestConfig<D = any> {
107
107
  transitional?: TransitionalOptions;
108
108
  signal?: AbortSignal;
109
109
  insecureHTTPParser?: boolean;
110
+ env?: {
111
+ FormData?: new (...args: any[]) => object;
112
+ };
110
113
  }
111
114
 
112
115
  export interface HeadersDefaults {
@@ -136,13 +139,35 @@ export interface AxiosResponse<T = any, D = any> {
136
139
  request?: any;
137
140
  }
138
141
 
139
- export interface AxiosError<T = any, D = any> extends Error {
142
+ export class AxiosError<T = unknown, D = any> extends Error {
143
+ constructor(
144
+ message?: string,
145
+ code?: string,
146
+ config?: AxiosRequestConfig<D>,
147
+ request?: any,
148
+ response?: AxiosResponse<T, D>
149
+ );
150
+
140
151
  config: AxiosRequestConfig<D>;
141
152
  code?: string;
142
153
  request?: any;
143
154
  response?: AxiosResponse<T, D>;
144
155
  isAxiosError: boolean;
156
+ status?: string;
145
157
  toJSON: () => object;
158
+ static readonly ERR_FR_TOO_MANY_REDIRECTS = "ERR_FR_TOO_MANY_REDIRECTS";
159
+ static readonly ERR_BAD_OPTION_VALUE = "ERR_BAD_OPTION_VALUE";
160
+ static readonly ERR_BAD_OPTION = "ERR_BAD_OPTION";
161
+ static readonly ERR_NETWORK = "ERR_NETWORK";
162
+ static readonly ERR_DEPRECATED = "ERR_DEPRECATED";
163
+ static readonly ERR_BAD_RESPONSE = "ERR_BAD_RESPONSE";
164
+ static readonly ERR_BAD_REQUEST = "ERR_BAD_REQUEST";
165
+ static readonly ERR_CANCELED = "ERR_CANCELED";
166
+ static readonly ECONNABORTED = "ECONNABORTED";
167
+ static readonly ETIMEDOUT = "ETIMEDOUT";
168
+ }
169
+
170
+ export class CanceledError<T> extends AxiosError<T> {
146
171
  }
147
172
 
148
173
  export interface AxiosPromise<T = any> extends Promise<AxiosResponse<T>> {
@@ -176,8 +201,13 @@ export interface CancelTokenSource {
176
201
  cancel: Canceler;
177
202
  }
178
203
 
204
+ export interface AxiosInterceptorOptions {
205
+ synchronous?: boolean;
206
+ runWhen?: (config: AxiosRequestConfig) => boolean;
207
+ }
208
+
179
209
  export interface AxiosInterceptorManager<V> {
180
- use<T = V>(onFulfilled?: (value: V) => T | Promise<T>, onRejected?: (error: any) => any): number;
210
+ use<T = V>(onFulfilled?: (value: V) => T | Promise<T>, onRejected?: (error: any) => any, options?: AxiosInterceptorOptions): number;
181
211
  eject(id: number): void;
182
212
  }
183
213
 
@@ -197,6 +227,9 @@ export class Axios {
197
227
  post<T = any, R = AxiosResponse<T>, D = any>(url: string, data?: D, config?: AxiosRequestConfig<D>): Promise<R>;
198
228
  put<T = any, R = AxiosResponse<T>, D = any>(url: string, data?: D, config?: AxiosRequestConfig<D>): Promise<R>;
199
229
  patch<T = any, R = AxiosResponse<T>, D = any>(url: string, data?: D, config?: AxiosRequestConfig<D>): Promise<R>;
230
+ postForm<T = any, R = AxiosResponse<T>, D = any>(url: string, data?: D, config?: AxiosRequestConfig<D>): Promise<R>;
231
+ putForm<T = any, R = AxiosResponse<T>, D = any>(url: string, data?: D, config?: AxiosRequestConfig<D>): Promise<R>;
232
+ patchForm<T = any, R = AxiosResponse<T>, D = any>(url: string, data?: D, config?: AxiosRequestConfig<D>): Promise<R>;
200
233
  }
201
234
 
202
235
  export interface AxiosInstance extends Axios {
@@ -11,10 +11,9 @@ var httpsFollow = require('follow-redirects').https;
11
11
  var url = require('url');
12
12
  var zlib = require('zlib');
13
13
  var VERSION = require('./../env/data').version;
14
- var createError = require('../core/createError');
15
- var enhanceError = require('../core/enhanceError');
16
14
  var transitionalDefaults = require('../defaults/transitional');
17
- var Cancel = require('../cancel/Cancel');
15
+ var AxiosError = require('../core/AxiosError');
16
+ var CanceledError = require('../cancel/CanceledError');
18
17
 
19
18
  var isHttps = /https:?/;
20
19
 
@@ -87,7 +86,10 @@ module.exports = function httpAdapter(config) {
87
86
  headers['User-Agent'] = 'axios/' + VERSION;
88
87
  }
89
88
 
90
- if (data && !utils.isStream(data)) {
89
+ // support for https://www.npmjs.com/package/form-data api
90
+ if (utils.isFormData(data) && utils.isFunction(data.getHeaders)) {
91
+ Object.assign(headers, data.getHeaders());
92
+ } else if (data && !utils.isStream(data)) {
91
93
  if (Buffer.isBuffer(data)) {
92
94
  // Nothing to do...
93
95
  } else if (utils.isArrayBuffer(data)) {
@@ -95,14 +97,19 @@ module.exports = function httpAdapter(config) {
95
97
  } else if (utils.isString(data)) {
96
98
  data = Buffer.from(data, 'utf-8');
97
99
  } else {
98
- return reject(createError(
100
+ return reject(new AxiosError(
99
101
  'Data after transformation must be a string, an ArrayBuffer, a Buffer, or a Stream',
102
+ AxiosError.ERR_BAD_REQUEST,
100
103
  config
101
104
  ));
102
105
  }
103
106
 
104
107
  if (config.maxBodyLength > -1 && data.length > config.maxBodyLength) {
105
- return reject(createError('Request body larger than maxBodyLength limit', config));
108
+ return reject(new AxiosError(
109
+ 'Request body larger than maxBodyLength limit',
110
+ AxiosError.ERR_BAD_REQUEST,
111
+ config
112
+ ));
106
113
  }
107
114
 
108
115
  // Add Content-Length header if data exists
@@ -122,7 +129,15 @@ module.exports = function httpAdapter(config) {
122
129
  // Parse url
123
130
  var fullPath = buildFullPath(config.baseURL, config.url);
124
131
  var parsed = url.parse(fullPath);
125
- var protocol = parsed.protocol || 'http:';
132
+ var protocol = utils.getProtocol(parsed.protocol);
133
+
134
+ if (!utils.supportedProtocols.includes(protocol)) {
135
+ return reject(new AxiosError(
136
+ 'Unsupported protocol ' + protocol,
137
+ AxiosError.ERR_BAD_REQUEST,
138
+ config
139
+ ));
140
+ }
126
141
 
127
142
  if (!auth && parsed.auth) {
128
143
  var urlAuth = parsed.auth.split(':');
@@ -227,6 +242,9 @@ module.exports = function httpAdapter(config) {
227
242
  if (config.maxRedirects) {
228
243
  options.maxRedirects = config.maxRedirects;
229
244
  }
245
+ if (config.beforeRedirect) {
246
+ options.beforeRedirect = config.beforeRedirect;
247
+ }
230
248
  transport = isHttpsProxy ? httpsFollow : httpFollow;
231
249
  }
232
250
 
@@ -288,8 +306,8 @@ module.exports = function httpAdapter(config) {
288
306
  // stream.destoy() emit aborted event before calling reject() on Node.js v16
289
307
  rejected = true;
290
308
  stream.destroy();
291
- reject(createError('maxContentLength size of ' + config.maxContentLength + ' exceeded',
292
- config, null, lastRequest));
309
+ reject(new AxiosError('maxContentLength size of ' + config.maxContentLength + ' exceeded',
310
+ AxiosError.ERR_BAD_RESPONSE, config, lastRequest));
293
311
  }
294
312
  });
295
313
 
@@ -298,12 +316,17 @@ module.exports = function httpAdapter(config) {
298
316
  return;
299
317
  }
300
318
  stream.destroy();
301
- reject(createError('error request aborted', config, 'ERR_REQUEST_ABORTED', lastRequest));
319
+ reject(new AxiosError(
320
+ 'maxContentLength size of ' + config.maxContentLength + ' exceeded',
321
+ AxiosError.ERR_BAD_RESPONSE,
322
+ config,
323
+ lastRequest
324
+ ));
302
325
  });
303
326
 
304
327
  stream.on('error', function handleStreamError(err) {
305
328
  if (req.aborted) return;
306
- reject(enhanceError(err, config, null, lastRequest));
329
+ reject(AxiosError.from(err, null, config, lastRequest));
307
330
  });
308
331
 
309
332
  stream.on('end', function handleStreamEnd() {
@@ -317,7 +340,7 @@ module.exports = function httpAdapter(config) {
317
340
  }
318
341
  response.data = responseData;
319
342
  } catch (err) {
320
- reject(enhanceError(err, config, err.code, response.request, response));
343
+ reject(AxiosError.from(err, null, config, response.request, response));
321
344
  }
322
345
  settle(resolve, reject, response);
323
346
  });
@@ -326,8 +349,9 @@ module.exports = function httpAdapter(config) {
326
349
 
327
350
  // Handle errors
328
351
  req.on('error', function handleRequestError(err) {
329
- if (req.aborted && err.code !== 'ERR_FR_TOO_MANY_REDIRECTS') return;
330
- reject(enhanceError(err, config, null, req));
352
+ // @todo remove
353
+ // if (req.aborted && err.code !== AxiosError.ERR_FR_TOO_MANY_REDIRECTS) return;
354
+ reject(AxiosError.from(err, null, config, req));
331
355
  });
332
356
 
333
357
  // set tcp keep alive to prevent drop connection by peer
@@ -342,10 +366,10 @@ module.exports = function httpAdapter(config) {
342
366
  var timeout = parseInt(config.timeout, 10);
343
367
 
344
368
  if (isNaN(timeout)) {
345
- reject(createError(
369
+ reject(new AxiosError(
346
370
  'error trying to parse `config.timeout` to int',
371
+ AxiosError.ERR_BAD_OPTION_VALUE,
347
372
  config,
348
- 'ERR_PARSE_TIMEOUT',
349
373
  req
350
374
  ));
351
375
 
@@ -359,17 +383,11 @@ module.exports = function httpAdapter(config) {
359
383
  // ClientRequest.setTimeout will be fired on the specify milliseconds, and can make sure that abort() will be fired after connect.
360
384
  req.setTimeout(timeout, function handleRequestTimeout() {
361
385
  req.abort();
362
- var timeoutErrorMessage = '';
363
- if (config.timeoutErrorMessage) {
364
- timeoutErrorMessage = config.timeoutErrorMessage;
365
- } else {
366
- timeoutErrorMessage = 'timeout of ' + config.timeout + 'ms exceeded';
367
- }
368
386
  var transitional = config.transitional || transitionalDefaults;
369
- reject(createError(
370
- timeoutErrorMessage,
387
+ reject(new AxiosError(
388
+ 'timeout of ' + timeout + 'ms exceeded',
389
+ transitional.clarifyTimeoutError ? AxiosError.ETIMEDOUT : AxiosError.ECONNABORTED,
371
390
  config,
372
- transitional.clarifyTimeoutError ? 'ETIMEDOUT' : 'ECONNABORTED',
373
391
  req
374
392
  ));
375
393
  });
@@ -382,7 +400,7 @@ module.exports = function httpAdapter(config) {
382
400
  if (req.aborted) return;
383
401
 
384
402
  req.abort();
385
- reject(!cancel || (cancel && cancel.type) ? new Cancel('canceled') : cancel);
403
+ reject(!cancel || (cancel && cancel.type) ? new CanceledError() : cancel);
386
404
  };
387
405
 
388
406
  config.cancelToken && config.cancelToken.subscribe(onCanceled);
@@ -395,7 +413,7 @@ module.exports = function httpAdapter(config) {
395
413
  // Send the request
396
414
  if (utils.isStream(data)) {
397
415
  data.on('error', function handleStreamError(err) {
398
- reject(enhanceError(err, config, null, req));
416
+ reject(AxiosError.from(err, config, null, req));
399
417
  }).pipe(req);
400
418
  } else {
401
419
  req.end(data);
@@ -7,9 +7,10 @@ var buildURL = require('./../helpers/buildURL');
7
7
  var buildFullPath = require('../core/buildFullPath');
8
8
  var parseHeaders = require('./../helpers/parseHeaders');
9
9
  var isURLSameOrigin = require('./../helpers/isURLSameOrigin');
10
- var createError = require('../core/createError');
10
+ var url = require('url');
11
11
  var transitionalDefaults = require('../defaults/transitional');
12
- var Cancel = require('../cancel/Cancel');
12
+ var AxiosError = require('../core/AxiosError');
13
+ var CanceledError = require('../cancel/CanceledError');
13
14
 
14
15
  module.exports = function xhrAdapter(config) {
15
16
  return new Promise(function dispatchXhrRequest(resolve, reject) {
@@ -27,10 +28,6 @@ module.exports = function xhrAdapter(config) {
27
28
  }
28
29
  }
29
30
 
30
- if (utils.isFormData(requestData)) {
31
- delete requestHeaders['Content-Type']; // Let the browser set it
32
- }
33
-
34
31
  var request = new XMLHttpRequest();
35
32
 
36
33
  // HTTP basic authentication
@@ -41,6 +38,9 @@ module.exports = function xhrAdapter(config) {
41
38
  }
42
39
 
43
40
  var fullPath = buildFullPath(config.baseURL, config.url);
41
+ var parsed = url.parse(fullPath);
42
+ var protocol = utils.getProtocol(parsed.protocol);
43
+
44
44
  request.open(config.method.toUpperCase(), buildURL(fullPath, config.params, config.paramsSerializer), true);
45
45
 
46
46
  // Set the request timeout in MS
@@ -104,7 +104,7 @@ module.exports = function xhrAdapter(config) {
104
104
  return;
105
105
  }
106
106
 
107
- reject(createError('Request aborted', config, 'ECONNABORTED', request));
107
+ reject(new AxiosError('Request aborted', AxiosError.ECONNABORTED, config, request));
108
108
 
109
109
  // Clean up request
110
110
  request = null;
@@ -114,7 +114,7 @@ module.exports = function xhrAdapter(config) {
114
114
  request.onerror = function handleError() {
115
115
  // Real errors are hidden from us by the browser
116
116
  // onerror should only fire if it's a network error
117
- reject(createError('Network Error', config, null, request));
117
+ reject(new AxiosError('Network Error', AxiosError.ERR_NETWORK, config, request, request));
118
118
 
119
119
  // Clean up request
120
120
  request = null;
@@ -127,10 +127,10 @@ module.exports = function xhrAdapter(config) {
127
127
  if (config.timeoutErrorMessage) {
128
128
  timeoutErrorMessage = config.timeoutErrorMessage;
129
129
  }
130
- reject(createError(
130
+ reject(new AxiosError(
131
131
  timeoutErrorMessage,
132
+ transitional.clarifyTimeoutError ? AxiosError.ETIMEDOUT : AxiosError.ECONNABORTED,
132
133
  config,
133
- transitional.clarifyTimeoutError ? 'ETIMEDOUT' : 'ECONNABORTED',
134
134
  request));
135
135
 
136
136
  // Clean up request
@@ -191,7 +191,7 @@ module.exports = function xhrAdapter(config) {
191
191
  if (!request) {
192
192
  return;
193
193
  }
194
- reject(!cancel || (cancel && cancel.type) ? new Cancel('canceled') : cancel);
194
+ reject(!cancel || (cancel && cancel.type) ? new CanceledError() : cancel);
195
195
  request.abort();
196
196
  request = null;
197
197
  };
@@ -206,6 +206,16 @@ module.exports = function xhrAdapter(config) {
206
206
  requestData = null;
207
207
  }
208
208
 
209
+ if (parsed.path === null) {
210
+ reject(new AxiosError('Malformed URL ' + fullPath, AxiosError.ERR_BAD_REQUEST, config));
211
+ return;
212
+ }
213
+
214
+ if (!utils.supportedProtocols.includes(protocol)) {
215
+ reject(new AxiosError('Unsupported protocol ' + protocol, AxiosError.ERR_BAD_REQUEST, config));
216
+ return;
217
+ }
218
+
209
219
  // Send the request
210
220
  request.send(requestData);
211
221
  });
package/lib/axios.js CHANGED
@@ -37,10 +37,17 @@ var axios = createInstance(defaults);
37
37
  axios.Axios = Axios;
38
38
 
39
39
  // Expose Cancel & CancelToken
40
- axios.Cancel = require('./cancel/Cancel');
40
+ axios.CanceledError = require('./cancel/CanceledError');
41
41
  axios.CancelToken = require('./cancel/CancelToken');
42
42
  axios.isCancel = require('./cancel/isCancel');
43
43
  axios.VERSION = require('./env/data').version;
44
+ axios.toFormData = require('./helpers/toFormData');
45
+
46
+ // Expose AxiosError class
47
+ axios.AxiosError = require('../lib/core/AxiosError');
48
+
49
+ // alias for CanceledError for backward compatibility
50
+ axios.Cancel = axios.CanceledError;
44
51
 
45
52
  // Expose all/spread
46
53
  axios.all = function all(promises) {
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- var Cancel = require('./Cancel');
3
+ var CanceledError = require('./CanceledError');
4
4
 
5
5
  /**
6
6
  * A `CancelToken` is an object that can be used to request cancellation of an operation.
@@ -56,13 +56,13 @@ function CancelToken(executor) {
56
56
  return;
57
57
  }
58
58
 
59
- token.reason = new Cancel(message);
59
+ token.reason = new CanceledError(message);
60
60
  resolvePromise(token.reason);
61
61
  });
62
62
  }
63
63
 
64
64
  /**
65
- * Throws a `Cancel` if cancellation has been requested.
65
+ * Throws a `CanceledError` if cancellation has been requested.
66
66
  */
67
67
  CancelToken.prototype.throwIfRequested = function throwIfRequested() {
68
68
  if (this.reason) {
@@ -0,0 +1,22 @@
1
+ 'use strict';
2
+
3
+ var AxiosError = require('../core/AxiosError');
4
+ var utils = require('../utils');
5
+
6
+ /**
7
+ * A `CanceledError` is an object that is thrown when an operation is canceled.
8
+ *
9
+ * @class
10
+ * @param {string=} message The message.
11
+ */
12
+ function CanceledError(message) {
13
+ // eslint-disable-next-line no-eq-null,eqeqeq
14
+ AxiosError.call(this, message == null ? 'canceled' : message, AxiosError.ERR_CANCELED);
15
+ this.name = 'CanceledError';
16
+ }
17
+
18
+ utils.inherits(CanceledError, AxiosError, {
19
+ __CANCEL__: true
20
+ });
21
+
22
+ module.exports = CanceledError;
package/lib/core/Axios.js CHANGED
@@ -5,6 +5,7 @@ var buildURL = require('../helpers/buildURL');
5
5
  var InterceptorManager = require('./InterceptorManager');
6
6
  var dispatchRequest = require('./dispatchRequest');
7
7
  var mergeConfig = require('./mergeConfig');
8
+ var buildFullPath = require('./buildFullPath');
8
9
  var validator = require('../helpers/validator');
9
10
 
10
11
  var validators = validator.validators;
@@ -119,7 +120,8 @@ Axios.prototype.request = function request(configOrUrl, config) {
119
120
 
120
121
  Axios.prototype.getUri = function getUri(config) {
121
122
  config = mergeConfig(this.defaults, config);
122
- return buildURL(config.url, config.params, config.paramsSerializer).replace(/^\?/, '');
123
+ var fullPath = buildFullPath(config.baseURL, config.url);
124
+ return buildURL(fullPath, config.params, config.paramsSerializer);
123
125
  };
124
126
 
125
127
  // Provide aliases for supported request methods
@@ -136,13 +138,23 @@ utils.forEach(['delete', 'get', 'head', 'options'], function forEachMethodNoData
136
138
 
137
139
  utils.forEach(['post', 'put', 'patch'], function forEachMethodWithData(method) {
138
140
  /*eslint func-names:0*/
139
- Axios.prototype[method] = function(url, data, config) {
140
- return this.request(mergeConfig(config || {}, {
141
- method: method,
142
- url: url,
143
- data: data
144
- }));
145
- };
141
+
142
+ function generateHTTPMethod(isForm) {
143
+ return function httpMethod(url, data, config) {
144
+ return this.request(mergeConfig(config || {}, {
145
+ method: method,
146
+ headers: isForm ? {
147
+ 'Content-Type': 'multipart/form-data'
148
+ } : {},
149
+ url: url,
150
+ data: data
151
+ }));
152
+ };
153
+ }
154
+
155
+ Axios.prototype[method] = generateHTTPMethod();
156
+
157
+ Axios.prototype[method + 'Form'] = generateHTTPMethod(true);
146
158
  });
147
159
 
148
160
  module.exports = Axios;
@@ -0,0 +1,86 @@
1
+ 'use strict';
2
+
3
+ var utils = require('../utils');
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 {string} [code] The error code (for example, 'ECONNABORTED').
10
+ * @param {Object} [config] The config.
11
+ * @param {Object} [request] The request.
12
+ * @param {Object} [response] The response.
13
+ * @returns {Error} The created error.
14
+ */
15
+ function AxiosError(message, code, config, request, response) {
16
+ Error.call(this);
17
+ this.message = message;
18
+ this.name = 'AxiosError';
19
+ code && (this.code = code);
20
+ config && (this.config = config);
21
+ request && (this.request = request);
22
+ response && (this.response = response);
23
+ }
24
+
25
+ utils.inherits(AxiosError, Error, {
26
+ toJSON: function toJSON() {
27
+ return {
28
+ // Standard
29
+ message: this.message,
30
+ name: this.name,
31
+ // Microsoft
32
+ description: this.description,
33
+ number: this.number,
34
+ // Mozilla
35
+ fileName: this.fileName,
36
+ lineNumber: this.lineNumber,
37
+ columnNumber: this.columnNumber,
38
+ stack: this.stack,
39
+ // Axios
40
+ config: this.config,
41
+ code: this.code,
42
+ status: this.response && this.response.status ? this.response.status : null
43
+ };
44
+ }
45
+ });
46
+
47
+ var prototype = AxiosError.prototype;
48
+ var descriptors = {};
49
+
50
+ [
51
+ 'ERR_BAD_OPTION_VALUE',
52
+ 'ERR_BAD_OPTION',
53
+ 'ECONNABORTED',
54
+ 'ETIMEDOUT',
55
+ 'ERR_NETWORK',
56
+ 'ERR_FR_TOO_MANY_REDIRECTS',
57
+ 'ERR_DEPRECATED',
58
+ 'ERR_BAD_RESPONSE',
59
+ 'ERR_BAD_REQUEST',
60
+ 'ERR_CANCELED'
61
+ // eslint-disable-next-line func-names
62
+ ].forEach(function(code) {
63
+ descriptors[code] = {value: code};
64
+ });
65
+
66
+ Object.defineProperties(AxiosError, descriptors);
67
+ Object.defineProperty(prototype, 'isAxiosError', {value: true});
68
+
69
+ // eslint-disable-next-line func-names
70
+ AxiosError.from = function(error, code, config, request, response, customProps) {
71
+ var axiosError = Object.create(prototype);
72
+
73
+ utils.toFlatObject(error, axiosError, function filter(obj) {
74
+ return obj !== Error.prototype;
75
+ });
76
+
77
+ AxiosError.call(axiosError, error.message, code, config, request, response);
78
+
79
+ axiosError.name = error.name;
80
+
81
+ customProps && Object.assign(axiosError, customProps);
82
+
83
+ return axiosError;
84
+ };
85
+
86
+ module.exports = AxiosError;
@@ -4,10 +4,10 @@ var utils = require('./../utils');
4
4
  var transformData = require('./transformData');
5
5
  var isCancel = require('../cancel/isCancel');
6
6
  var defaults = require('../defaults');
7
- var Cancel = require('../cancel/Cancel');
7
+ var CanceledError = require('../cancel/CanceledError');
8
8
 
9
9
  /**
10
- * Throws a `Cancel` if cancellation has been requested.
10
+ * Throws a `CanceledError` if cancellation has been requested.
11
11
  */
12
12
  function throwIfCancellationRequested(config) {
13
13
  if (config.cancelToken) {
@@ -15,7 +15,7 @@ function throwIfCancellationRequested(config) {
15
15
  }
16
16
 
17
17
  if (config.signal && config.signal.aborted) {
18
- throw new Cancel('canceled');
18
+ throw new CanceledError();
19
19
  }
20
20
  }
21
21
 
@@ -80,6 +80,7 @@ module.exports = function mergeConfig(config1, config2) {
80
80
  'decompress': defaultToConfig2,
81
81
  'maxContentLength': defaultToConfig2,
82
82
  'maxBodyLength': defaultToConfig2,
83
+ 'beforeRedirect': defaultToConfig2,
83
84
  'transport': defaultToConfig2,
84
85
  'httpAgent': defaultToConfig2,
85
86
  'httpsAgent': defaultToConfig2,
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- var createError = require('./createError');
3
+ var AxiosError = require('./AxiosError');
4
4
 
5
5
  /**
6
6
  * Resolve or reject a Promise based on response status.
@@ -14,10 +14,10 @@ module.exports = function settle(resolve, reject, response) {
14
14
  if (!response.status || !validateStatus || validateStatus(response.status)) {
15
15
  resolve(response);
16
16
  } else {
17
- reject(createError(
17
+ reject(new AxiosError(
18
18
  'Request failed with status code ' + response.status,
19
+ [AxiosError.ERR_BAD_REQUEST, AxiosError.ERR_BAD_RESPONSE][Math.floor(response.status / 100) - 4],
19
20
  response.config,
20
- null,
21
21
  response.request,
22
22
  response
23
23
  ));
@@ -0,0 +1,2 @@
1
+ // eslint-disable-next-line strict
2
+ module.exports = require('form-data');
@@ -2,8 +2,9 @@
2
2
 
3
3
  var utils = require('../utils');
4
4
  var normalizeHeaderName = require('../helpers/normalizeHeaderName');
5
- var enhanceError = require('../core/enhanceError');
5
+ var AxiosError = require('../core/AxiosError');
6
6
  var transitionalDefaults = require('./transitional');
7
+ var toFormData = require('../helpers/toFormData');
7
8
 
8
9
  var DEFAULT_CONTENT_TYPE = {
9
10
  'Content-Type': 'application/x-www-form-urlencoded'
@@ -68,10 +69,20 @@ var defaults = {
68
69
  setContentTypeIfUnset(headers, 'application/x-www-form-urlencoded;charset=utf-8');
69
70
  return data.toString();
70
71
  }
71
- if (utils.isObject(data) || (headers && headers['Content-Type'] === 'application/json')) {
72
+
73
+ var isObjectPayload = utils.isObject(data);
74
+ var contentType = headers && headers['Content-Type'];
75
+
76
+ var isFileList;
77
+
78
+ if ((isFileList = utils.isFileList(data)) || (isObjectPayload && contentType === 'multipart/form-data')) {
79
+ var _FormData = this.env && this.env.FormData;
80
+ return toFormData(isFileList ? {'files[]': data} : data, _FormData && new _FormData());
81
+ } else if (isObjectPayload || contentType === 'application/json') {
72
82
  setContentTypeIfUnset(headers, 'application/json');
73
83
  return stringifySafely(data);
74
84
  }
85
+
75
86
  return data;
76
87
  }],
77
88
 
@@ -87,7 +98,7 @@ var defaults = {
87
98
  } catch (e) {
88
99
  if (strictJSONParsing) {
89
100
  if (e.name === 'SyntaxError') {
90
- throw enhanceError(e, this, 'E_JSON_PARSE');
101
+ throw AxiosError.from(e, AxiosError.ERR_BAD_RESPONSE, this, null, this.response);
91
102
  }
92
103
  throw e;
93
104
  }
@@ -109,6 +120,10 @@ var defaults = {
109
120
  maxContentLength: -1,
110
121
  maxBodyLength: -1,
111
122
 
123
+ env: {
124
+ FormData: require('./env/FormData')
125
+ },
126
+
112
127
  validateStatus: function validateStatus(status) {
113
128
  return status >= 200 && status < 300;
114
129
  },
package/lib/env/data.js CHANGED
@@ -1,3 +1,3 @@
1
1
  module.exports = {
2
- "version": "0.26.1"
2
+ "version": "0.27.0"
3
3
  };
@@ -0,0 +1,2 @@
1
+ // eslint-disable-next-line strict
2
+ module.exports = null;