axios 0.25.0 → 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
- var defaults = require('../defaults');
17
- var Cancel = require('../cancel/Cancel');
14
+ var transitionalDefaults = require('../defaults/transitional');
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(':');
@@ -138,6 +153,16 @@ module.exports = function httpAdapter(config) {
138
153
  var isHttpsRequest = isHttps.test(protocol);
139
154
  var agent = isHttpsRequest ? config.httpsAgent : config.httpAgent;
140
155
 
156
+ try {
157
+ buildURL(parsed.path, config.params, config.paramsSerializer).replace(/^\?/, '');
158
+ } catch (err) {
159
+ var customErr = new Error(err.message);
160
+ customErr.config = config;
161
+ customErr.url = config.url;
162
+ customErr.exists = true;
163
+ reject(customErr);
164
+ }
165
+
141
166
  var options = {
142
167
  path: buildURL(parsed.path, config.params, config.paramsSerializer).replace(/^\?/, ''),
143
168
  method: config.method.toUpperCase(),
@@ -217,6 +242,9 @@ module.exports = function httpAdapter(config) {
217
242
  if (config.maxRedirects) {
218
243
  options.maxRedirects = config.maxRedirects;
219
244
  }
245
+ if (config.beforeRedirect) {
246
+ options.beforeRedirect = config.beforeRedirect;
247
+ }
220
248
  transport = isHttpsProxy ? httpsFollow : httpFollow;
221
249
  }
222
250
 
@@ -278,8 +306,8 @@ module.exports = function httpAdapter(config) {
278
306
  // stream.destoy() emit aborted event before calling reject() on Node.js v16
279
307
  rejected = true;
280
308
  stream.destroy();
281
- reject(createError('maxContentLength size of ' + config.maxContentLength + ' exceeded',
282
- config, null, lastRequest));
309
+ reject(new AxiosError('maxContentLength size of ' + config.maxContentLength + ' exceeded',
310
+ AxiosError.ERR_BAD_RESPONSE, config, lastRequest));
283
311
  }
284
312
  });
285
313
 
@@ -288,12 +316,17 @@ module.exports = function httpAdapter(config) {
288
316
  return;
289
317
  }
290
318
  stream.destroy();
291
- 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
+ ));
292
325
  });
293
326
 
294
327
  stream.on('error', function handleStreamError(err) {
295
328
  if (req.aborted) return;
296
- reject(enhanceError(err, config, null, lastRequest));
329
+ reject(AxiosError.from(err, null, config, lastRequest));
297
330
  });
298
331
 
299
332
  stream.on('end', function handleStreamEnd() {
@@ -307,7 +340,7 @@ module.exports = function httpAdapter(config) {
307
340
  }
308
341
  response.data = responseData;
309
342
  } catch (err) {
310
- reject(enhanceError(err, config, err.code, response.request, response));
343
+ reject(AxiosError.from(err, null, config, response.request, response));
311
344
  }
312
345
  settle(resolve, reject, response);
313
346
  });
@@ -316,8 +349,9 @@ module.exports = function httpAdapter(config) {
316
349
 
317
350
  // Handle errors
318
351
  req.on('error', function handleRequestError(err) {
319
- if (req.aborted && err.code !== 'ERR_FR_TOO_MANY_REDIRECTS') return;
320
- 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));
321
355
  });
322
356
 
323
357
  // set tcp keep alive to prevent drop connection by peer
@@ -332,10 +366,10 @@ module.exports = function httpAdapter(config) {
332
366
  var timeout = parseInt(config.timeout, 10);
333
367
 
334
368
  if (isNaN(timeout)) {
335
- reject(createError(
369
+ reject(new AxiosError(
336
370
  'error trying to parse `config.timeout` to int',
371
+ AxiosError.ERR_BAD_OPTION_VALUE,
337
372
  config,
338
- 'ERR_PARSE_TIMEOUT',
339
373
  req
340
374
  ));
341
375
 
@@ -349,11 +383,11 @@ module.exports = function httpAdapter(config) {
349
383
  // ClientRequest.setTimeout will be fired on the specify milliseconds, and can make sure that abort() will be fired after connect.
350
384
  req.setTimeout(timeout, function handleRequestTimeout() {
351
385
  req.abort();
352
- var transitional = config.transitional || defaults.transitional;
353
- reject(createError(
386
+ var transitional = config.transitional || transitionalDefaults;
387
+ reject(new AxiosError(
354
388
  'timeout of ' + timeout + 'ms exceeded',
389
+ transitional.clarifyTimeoutError ? AxiosError.ETIMEDOUT : AxiosError.ECONNABORTED,
355
390
  config,
356
- transitional.clarifyTimeoutError ? 'ETIMEDOUT' : 'ECONNABORTED',
357
391
  req
358
392
  ));
359
393
  });
@@ -366,7 +400,7 @@ module.exports = function httpAdapter(config) {
366
400
  if (req.aborted) return;
367
401
 
368
402
  req.abort();
369
- reject(!cancel || (cancel && cancel.type) ? new Cancel('canceled') : cancel);
403
+ reject(!cancel || (cancel && cancel.type) ? new CanceledError() : cancel);
370
404
  };
371
405
 
372
406
  config.cancelToken && config.cancelToken.subscribe(onCanceled);
@@ -379,7 +413,7 @@ module.exports = function httpAdapter(config) {
379
413
  // Send the request
380
414
  if (utils.isStream(data)) {
381
415
  data.on('error', function handleStreamError(err) {
382
- reject(enhanceError(err, config, null, req));
416
+ reject(AxiosError.from(err, config, null, req));
383
417
  }).pipe(req);
384
418
  } else {
385
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');
11
- var defaults = require('../defaults');
12
- var Cancel = require('../cancel/Cancel');
10
+ var url = require('url');
11
+ var transitionalDefaults = require('../defaults/transitional');
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;
@@ -123,14 +123,14 @@ module.exports = function xhrAdapter(config) {
123
123
  // Handle timeout
124
124
  request.ontimeout = function handleTimeout() {
125
125
  var timeoutErrorMessage = config.timeout ? 'timeout of ' + config.timeout + 'ms exceeded' : 'timeout exceeded';
126
- var transitional = config.transitional || defaults.transitional;
126
+ var transitional = config.transitional || transitionalDefaults;
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;
@@ -36,10 +37,6 @@ Axios.prototype.request = function request(configOrUrl, config) {
36
37
  config = configOrUrl || {};
37
38
  }
38
39
 
39
- if (!config.url) {
40
- throw new Error('Provided config url is not valid');
41
- }
42
-
43
40
  config = mergeConfig(this.defaults, config);
44
41
 
45
42
  // Set config.method
@@ -122,11 +119,9 @@ Axios.prototype.request = function request(configOrUrl, config) {
122
119
  };
123
120
 
124
121
  Axios.prototype.getUri = function getUri(config) {
125
- if (!config.url) {
126
- throw new Error('Provided config url is not valid');
127
- }
128
122
  config = mergeConfig(this.defaults, config);
129
- 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);
130
125
  };
131
126
 
132
127
  // Provide aliases for supported request methods
@@ -143,13 +138,23 @@ utils.forEach(['delete', 'get', 'head', 'options'], function forEachMethodNoData
143
138
 
144
139
  utils.forEach(['post', 'put', 'patch'], function forEachMethodWithData(method) {
145
140
  /*eslint func-names:0*/
146
- Axios.prototype[method] = function(url, data, config) {
147
- return this.request(mergeConfig(config || {}, {
148
- method: method,
149
- url: url,
150
- data: data
151
- }));
152
- };
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);
153
158
  });
154
159
 
155
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
  ));
@@ -1,7 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  var utils = require('./../utils');
4
- var defaults = require('./../defaults');
4
+ var defaults = require('../defaults');
5
5
 
6
6
  /**
7
7
  * Transform the data for a request or a response
@@ -0,0 +1,2 @@
1
+ // eslint-disable-next-line strict
2
+ module.exports = require('form-data');