axios 0.22.0 → 0.26.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,9 +1,21 @@
1
- export interface AxiosTransformer {
2
- (data: any, headers?: Record<string, string>): any;
1
+ // TypeScript Version: 3.0
2
+
3
+ export type AxiosRequestHeaders = Record<string, string | number | boolean>;
4
+
5
+ export type AxiosResponseHeaders = Record<string, string> & {
6
+ "set-cookie"?: string[]
7
+ };
8
+
9
+ export interface AxiosRequestTransformer {
10
+ (data: any, headers?: AxiosRequestHeaders): any;
11
+ }
12
+
13
+ export interface AxiosResponseTransformer {
14
+ (data: any, headers?: AxiosResponseHeaders): any;
3
15
  }
4
16
 
5
17
  export interface AxiosAdapter {
6
- (config: AxiosRequestConfig): AxiosPromise<any>;
18
+ (config: AxiosRequestConfig): AxiosPromise;
7
19
  }
8
20
 
9
21
  export interface AxiosBasicCredentials {
@@ -16,7 +28,7 @@ export interface AxiosProxyConfig {
16
28
  port: number;
17
29
  auth?: {
18
30
  username: string;
19
- password:string;
31
+ password: string;
20
32
  };
21
33
  protocol?: string;
22
34
  }
@@ -31,7 +43,7 @@ export type Method =
31
43
  | 'patch' | 'PATCH'
32
44
  | 'purge' | 'PURGE'
33
45
  | 'link' | 'LINK'
34
- | 'unlink' | 'UNLINK'
46
+ | 'unlink' | 'UNLINK';
35
47
 
36
48
  export type ResponseType =
37
49
  | 'arraybuffer'
@@ -39,30 +51,45 @@ export type ResponseType =
39
51
  | 'document'
40
52
  | 'json'
41
53
  | 'text'
42
- | 'stream'
43
-
44
- export interface TransitionalOptions{
45
- silentJSONParsing: boolean;
46
- forcedJSONParsing: boolean;
47
- clarifyTimeoutError: boolean;
48
- }
49
-
50
- export interface AxiosRequestConfig<T = any> {
54
+ | 'stream';
55
+
56
+ export type responseEncoding =
57
+ | 'ascii' | 'ASCII'
58
+ | 'ansi' | 'ANSI'
59
+ | 'binary' | 'BINARY'
60
+ | 'base64' | 'BASE64'
61
+ | 'base64url' | 'BASE64URL'
62
+ | 'hex' | 'HEX'
63
+ | 'latin1' | 'LATIN1'
64
+ | 'ucs-2' | 'UCS-2'
65
+ | 'ucs2' | 'UCS2'
66
+ | 'utf-8' | 'UTF-8'
67
+ | 'utf8' | 'UTF8'
68
+ | 'utf16le' | 'UTF16LE';
69
+
70
+ export interface TransitionalOptions {
71
+ silentJSONParsing?: boolean;
72
+ forcedJSONParsing?: boolean;
73
+ clarifyTimeoutError?: boolean;
74
+ }
75
+
76
+ export interface AxiosRequestConfig<D = any> {
51
77
  url?: string;
52
78
  method?: Method;
53
79
  baseURL?: string;
54
- transformRequest?: AxiosTransformer | AxiosTransformer[];
55
- transformResponse?: AxiosTransformer | AxiosTransformer[];
56
- headers?: Record<string, string>;
80
+ transformRequest?: AxiosRequestTransformer | AxiosRequestTransformer[];
81
+ transformResponse?: AxiosResponseTransformer | AxiosResponseTransformer[];
82
+ headers?: AxiosRequestHeaders;
57
83
  params?: any;
58
84
  paramsSerializer?: (params: any) => string;
59
- data?: T;
85
+ data?: D;
60
86
  timeout?: number;
61
87
  timeoutErrorMessage?: string;
62
88
  withCredentials?: boolean;
63
89
  adapter?: AxiosAdapter;
64
90
  auth?: AxiosBasicCredentials;
65
91
  responseType?: ResponseType;
92
+ responseEncoding?: responseEncoding | string;
66
93
  xsrfCookieName?: string;
67
94
  xsrfHeaderName?: string;
68
95
  onUploadProgress?: (progressEvent: any) => void;
@@ -77,29 +104,48 @@ export interface AxiosRequestConfig<T = any> {
77
104
  proxy?: AxiosProxyConfig | false;
78
105
  cancelToken?: CancelToken;
79
106
  decompress?: boolean;
80
- transitional?: TransitionalOptions
107
+ transitional?: TransitionalOptions;
81
108
  signal?: AbortSignal;
109
+ insecureHTTPParser?: boolean;
110
+ }
111
+
112
+ export interface HeadersDefaults {
113
+ common: AxiosRequestHeaders;
114
+ delete: AxiosRequestHeaders;
115
+ get: AxiosRequestHeaders;
116
+ head: AxiosRequestHeaders;
117
+ post: AxiosRequestHeaders;
118
+ put: AxiosRequestHeaders;
119
+ patch: AxiosRequestHeaders;
120
+ options?: AxiosRequestHeaders;
121
+ purge?: AxiosRequestHeaders;
122
+ link?: AxiosRequestHeaders;
123
+ unlink?: AxiosRequestHeaders;
124
+ }
125
+
126
+ export interface AxiosDefaults<D = any> extends Omit<AxiosRequestConfig<D>, 'headers'> {
127
+ headers: HeadersDefaults;
82
128
  }
83
129
 
84
- export interface AxiosResponse<T = never> {
130
+ export interface AxiosResponse<T = any, D = any> {
85
131
  data: T;
86
132
  status: number;
87
133
  statusText: string;
88
- headers: Record<string, string>;
89
- config: AxiosRequestConfig<T>;
134
+ headers: AxiosResponseHeaders;
135
+ config: AxiosRequestConfig<D>;
90
136
  request?: any;
91
137
  }
92
138
 
93
- export interface AxiosError<T = never> extends Error {
94
- config: AxiosRequestConfig;
139
+ export interface AxiosError<T = any, D = any> extends Error {
140
+ config: AxiosRequestConfig<D>;
95
141
  code?: string;
96
142
  request?: any;
97
- response?: AxiosResponse<T>;
143
+ response?: AxiosResponse<T, D>;
98
144
  isAxiosError: boolean;
99
145
  toJSON: () => object;
100
146
  }
101
147
 
102
- export interface AxiosPromise<T = never> extends Promise<AxiosResponse<T>> {
148
+ export interface AxiosPromise<T = any> extends Promise<AxiosResponse<T>> {
103
149
  }
104
150
 
105
151
  export interface CancelStatic {
@@ -107,7 +153,7 @@ export interface CancelStatic {
107
153
  }
108
154
 
109
155
  export interface Cancel {
110
- message: string;
156
+ message: string | undefined;
111
157
  }
112
158
 
113
159
  export interface Canceler {
@@ -137,20 +183,20 @@ export interface AxiosInterceptorManager<V> {
137
183
 
138
184
  export class Axios {
139
185
  constructor(config?: AxiosRequestConfig);
140
- defaults: AxiosRequestConfig;
186
+ defaults: AxiosDefaults;
141
187
  interceptors: {
142
188
  request: AxiosInterceptorManager<AxiosRequestConfig>;
143
189
  response: AxiosInterceptorManager<AxiosResponse>;
144
190
  };
145
191
  getUri(config?: AxiosRequestConfig): string;
146
- request<T = never, R = AxiosResponse<T>> (config: AxiosRequestConfig<T>): Promise<R>;
147
- get<T = never, R = AxiosResponse<T>>(url: string, config?: AxiosRequestConfig<T>): Promise<R>;
148
- delete<T = never, R = AxiosResponse<T>>(url: string, config?: AxiosRequestConfig<T>): Promise<R>;
149
- head<T = never, R = AxiosResponse<T>>(url: string, config?: AxiosRequestConfig<T>): Promise<R>;
150
- options<T = never, R = AxiosResponse<T>>(url: string, config?: AxiosRequestConfig<T>): Promise<R>;
151
- post<T = never, R = AxiosResponse<T>>(url: string, data?: T, config?: AxiosRequestConfig<T>): Promise<R>;
152
- put<T = never, R = AxiosResponse<T>>(url: string, data?: T, config?: AxiosRequestConfig<T>): Promise<R>;
153
- patch<T = never, R = AxiosResponse<T>>(url: string, data?: T, config?: AxiosRequestConfig<T>): Promise<R>;
192
+ request<T = any, R = AxiosResponse<T>, D = any>(config: AxiosRequestConfig<D>): Promise<R>;
193
+ get<T = any, R = AxiosResponse<T>, D = any>(url: string, config?: AxiosRequestConfig<D>): Promise<R>;
194
+ delete<T = any, R = AxiosResponse<T>, D = any>(url: string, config?: AxiosRequestConfig<D>): Promise<R>;
195
+ head<T = any, R = AxiosResponse<T>, D = any>(url: string, config?: AxiosRequestConfig<D>): Promise<R>;
196
+ options<T = any, R = AxiosResponse<T>, D = any>(url: string, config?: AxiosRequestConfig<D>): Promise<R>;
197
+ post<T = any, R = AxiosResponse<T>, D = any>(url: string, data?: D, config?: AxiosRequestConfig<D>): Promise<R>;
198
+ put<T = any, R = AxiosResponse<T>, D = any>(url: string, data?: D, config?: AxiosRequestConfig<D>): Promise<R>;
199
+ patch<T = any, R = AxiosResponse<T>, D = any>(url: string, data?: D, config?: AxiosRequestConfig<D>): Promise<R>;
154
200
  }
155
201
 
156
202
  export interface AxiosInstance extends Axios {
@@ -165,7 +211,7 @@ export interface AxiosStatic extends AxiosInstance {
165
211
  Axios: typeof Axios;
166
212
  readonly VERSION: string;
167
213
  isCancel(value: any): boolean;
168
- all<T>(values: (T | Promise<T>)[]): Promise<T[]>;
214
+ all<T>(values: Array<T | Promise<T>>): Promise<T[]>;
169
215
  spread<T, R>(callback: (...args: T[]) => R): (array: T[]) => R;
170
216
  isAxiosError(payload: any): payload is AxiosError;
171
217
  }
@@ -60,8 +60,10 @@ module.exports = function httpAdapter(config) {
60
60
  done();
61
61
  resolvePromise(value);
62
62
  };
63
+ var rejected = false;
63
64
  var reject = function reject(value) {
64
65
  done();
66
+ rejected = true;
65
67
  rejectPromise(value);
66
68
  };
67
69
  var data = config.data;
@@ -99,6 +101,10 @@ module.exports = function httpAdapter(config) {
99
101
  ));
100
102
  }
101
103
 
104
+ if (config.maxBodyLength > -1 && data.length > config.maxBodyLength) {
105
+ return reject(createError('Request body larger than maxBodyLength limit', config));
106
+ }
107
+
102
108
  // Add Content-Length header if data exists
103
109
  if (!headerNames['content-length']) {
104
110
  headers['Content-Length'] = data.length;
@@ -132,6 +138,16 @@ module.exports = function httpAdapter(config) {
132
138
  var isHttpsRequest = isHttps.test(protocol);
133
139
  var agent = isHttpsRequest ? config.httpsAgent : config.httpAgent;
134
140
 
141
+ try {
142
+ buildURL(parsed.path, config.params, config.paramsSerializer).replace(/^\?/, '');
143
+ } catch (err) {
144
+ var customErr = new Error(err.message);
145
+ customErr.config = config;
146
+ customErr.url = config.url;
147
+ customErr.exists = true;
148
+ reject(customErr);
149
+ }
150
+
135
151
  var options = {
136
152
  path: buildURL(parsed.path, config.params, config.paramsSerializer).replace(/^\?/, ''),
137
153
  method: config.method.toUpperCase(),
@@ -269,27 +285,40 @@ module.exports = function httpAdapter(config) {
269
285
 
270
286
  // make sure the content length is not over the maxContentLength if specified
271
287
  if (config.maxContentLength > -1 && totalResponseBytes > config.maxContentLength) {
288
+ // stream.destoy() emit aborted event before calling reject() on Node.js v16
289
+ rejected = true;
272
290
  stream.destroy();
273
291
  reject(createError('maxContentLength size of ' + config.maxContentLength + ' exceeded',
274
292
  config, null, lastRequest));
275
293
  }
276
294
  });
277
295
 
296
+ stream.on('aborted', function handlerStreamAborted() {
297
+ if (rejected) {
298
+ return;
299
+ }
300
+ stream.destroy();
301
+ reject(createError('error request aborted', config, 'ERR_REQUEST_ABORTED', lastRequest));
302
+ });
303
+
278
304
  stream.on('error', function handleStreamError(err) {
279
305
  if (req.aborted) return;
280
306
  reject(enhanceError(err, config, null, lastRequest));
281
307
  });
282
308
 
283
309
  stream.on('end', function handleStreamEnd() {
284
- var responseData = Buffer.concat(responseBuffer);
285
- if (config.responseType !== 'arraybuffer') {
286
- responseData = responseData.toString(config.responseEncoding);
287
- if (!config.responseEncoding || config.responseEncoding === 'utf8') {
288
- responseData = utils.stripBOM(responseData);
310
+ try {
311
+ var responseData = responseBuffer.length === 1 ? responseBuffer[0] : Buffer.concat(responseBuffer);
312
+ if (config.responseType !== 'arraybuffer') {
313
+ responseData = responseData.toString(config.responseEncoding);
314
+ if (!config.responseEncoding || config.responseEncoding === 'utf8') {
315
+ responseData = utils.stripBOM(responseData);
316
+ }
289
317
  }
318
+ response.data = responseData;
319
+ } catch (err) {
320
+ reject(enhanceError(err, config, err.code, response.request, response));
290
321
  }
291
-
292
- response.data = responseData;
293
322
  settle(resolve, reject, response);
294
323
  });
295
324
  }
@@ -301,6 +330,12 @@ module.exports = function httpAdapter(config) {
301
330
  reject(enhanceError(err, config, null, req));
302
331
  });
303
332
 
333
+ // set tcp keep alive to prevent drop connection by peer
334
+ req.on('socket', function handleRequestSocket(socket) {
335
+ // default interval of sending ack packet is 1 minute
336
+ socket.setKeepAlive(true, 1000 * 60);
337
+ });
338
+
304
339
  // Handle request timeout
305
340
  if (config.timeout) {
306
341
  // This is forcing a int timeout to avoid problems if the `req` interface doesn't handle other types.
@@ -324,9 +359,15 @@ module.exports = function httpAdapter(config) {
324
359
  // ClientRequest.setTimeout will be fired on the specify milliseconds, and can make sure that abort() will be fired after connect.
325
360
  req.setTimeout(timeout, function handleRequestTimeout() {
326
361
  req.abort();
362
+ var timeoutErrorMessage = '';
363
+ if (config.timeoutErrorMessage) {
364
+ timeoutErrorMessage = config.timeoutErrorMessage;
365
+ } else {
366
+ timeoutErrorMessage = 'timeout of ' + config.timeout + 'ms exceeded';
367
+ }
327
368
  var transitional = config.transitional || defaults.transitional;
328
369
  reject(createError(
329
- 'timeout of ' + timeout + 'ms exceeded',
370
+ timeoutErrorMessage,
330
371
  config,
331
372
  transitional.clarifyTimeoutError ? 'ETIMEDOUT' : 'ECONNABORTED',
332
373
  req
@@ -122,7 +122,7 @@ module.exports = function xhrAdapter(config) {
122
122
 
123
123
  // Handle timeout
124
124
  request.ontimeout = function handleTimeout() {
125
- var timeoutErrorMessage = 'timeout of ' + config.timeout + 'ms exceeded';
125
+ var timeoutErrorMessage = config.timeout ? 'timeout of ' + config.timeout + 'ms exceeded' : 'timeout exceeded';
126
126
  var transitional = config.transitional || defaults.transitional;
127
127
  if (config.timeoutErrorMessage) {
128
128
  timeoutErrorMessage = config.timeoutErrorMessage;
package/lib/core/Axios.js CHANGED
@@ -26,14 +26,14 @@ function Axios(instanceConfig) {
26
26
  *
27
27
  * @param {Object} config The config specific for this request (merged with this.defaults)
28
28
  */
29
- Axios.prototype.request = function request(config) {
29
+ Axios.prototype.request = function request(configOrUrl, config) {
30
30
  /*eslint no-param-reassign:0*/
31
31
  // Allow for axios('example/url'[, config]) a la fetch API
32
- if (typeof config === 'string') {
33
- config = arguments[1] || {};
34
- config.url = arguments[0];
35
- } else {
32
+ if (typeof configOrUrl === 'string') {
36
33
  config = config || {};
34
+ config.url = configOrUrl;
35
+ } else {
36
+ config = configOrUrl || {};
37
37
  }
38
38
 
39
39
  config = mergeConfig(this.defaults, config);
package/lib/env/data.js CHANGED
@@ -1,3 +1,3 @@
1
1
  module.exports = {
2
- "version": "0.22.0"
2
+ "version": "0.26.0"
3
3
  };
@@ -10,5 +10,5 @@ module.exports = function isAbsoluteURL(url) {
10
10
  // A URL is considered absolute if it begins with "<scheme>://" or "//" (protocol-relative URL).
11
11
  // RFC 3986 defines scheme name as a sequence of characters beginning with a letter and followed
12
12
  // by any combination of letters, digits, plus, period, or hyphen.
13
- return /^([a-z][a-z\d\+\-\.]*:)?\/\//i.test(url);
13
+ return /^([a-z][a-z\d+\-.]*:)?\/\//i.test(url);
14
14
  };
@@ -1,5 +1,7 @@
1
1
  'use strict';
2
2
 
3
+ var utils = require('./../utils');
4
+
3
5
  /**
4
6
  * Determines whether the payload is an error thrown by Axios
5
7
  *
@@ -7,5 +9,5 @@
7
9
  * @returns {boolean} True if the payload is an error thrown by Axios, otherwise false
8
10
  */
9
11
  module.exports = function isAxiosError(payload) {
10
- return (typeof payload === 'object') && (payload.isAxiosError === true);
12
+ return utils.isObject(payload) && (payload.isAxiosError === true);
11
13
  };
@@ -0,0 +1,55 @@
1
+ 'use strict';
2
+
3
+ function combinedKey(parentKey, elKey) {
4
+ return parentKey + '.' + elKey;
5
+ }
6
+
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;
26
+ }
27
+
28
+ var value =
29
+ typeof data === 'boolean' || typeof data === 'number'
30
+ ? data.toString()
31
+ : data;
32
+ formData.append(parentKey, value);
33
+ }
34
+ }
35
+
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);
53
+
54
+ return formData;
55
+ };
package/lib/utils.js CHANGED
@@ -13,7 +13,7 @@ var toString = Object.prototype.toString;
13
13
  * @returns {boolean} True if value is an Array, otherwise false
14
14
  */
15
15
  function isArray(val) {
16
- return toString.call(val) === '[object Array]';
16
+ return Array.isArray(val);
17
17
  }
18
18
 
19
19
  /**
@@ -54,7 +54,7 @@ function isArrayBuffer(val) {
54
54
  * @returns {boolean} True if value is an FormData, otherwise false
55
55
  */
56
56
  function isFormData(val) {
57
- return (typeof FormData !== 'undefined') && (val instanceof FormData);
57
+ return toString.call(val) === '[object FormData]';
58
58
  }
59
59
 
60
60
  /**
@@ -68,7 +68,7 @@ function isArrayBufferView(val) {
68
68
  if ((typeof ArrayBuffer !== 'undefined') && (ArrayBuffer.isView)) {
69
69
  result = ArrayBuffer.isView(val);
70
70
  } else {
71
- result = (val) && (val.buffer) && (val.buffer instanceof ArrayBuffer);
71
+ result = (val) && (val.buffer) && (isArrayBuffer(val.buffer));
72
72
  }
73
73
  return result;
74
74
  }
@@ -175,7 +175,7 @@ function isStream(val) {
175
175
  * @returns {boolean} True if value is a URLSearchParams object, otherwise false
176
176
  */
177
177
  function isURLSearchParams(val) {
178
- return typeof URLSearchParams !== 'undefined' && val instanceof URLSearchParams;
178
+ return toString.call(val) === '[object URLSearchParams]';
179
179
  }
180
180
 
181
181
  /**
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "axios",
3
- "version": "0.22.0",
3
+ "version": "0.26.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
- "test": "grunt test",
8
+ "test": "grunt test && dtslint",
9
9
  "start": "node ./sandbox/server.js",
10
10
  "build": "NODE_ENV=production grunt build",
11
11
  "preversion": "grunt version && npm test",
@@ -35,6 +35,7 @@
35
35
  "devDependencies": {
36
36
  "abortcontroller-polyfill": "^1.5.0",
37
37
  "coveralls": "^3.0.0",
38
+ "dtslint": "^4.1.6",
38
39
  "es6-promise": "^4.2.4",
39
40
  "grunt": "^1.3.0",
40
41
  "grunt-banner": "^0.6.0",
@@ -44,7 +45,6 @@
44
45
  "grunt-eslint": "^23.0.0",
45
46
  "grunt-karma": "^4.0.0",
46
47
  "grunt-mocha-test": "^0.13.3",
47
- "grunt-ts": "^6.0.0-beta.19",
48
48
  "grunt-webpack": "^4.0.2",
49
49
  "istanbul-instrumenter-loader": "^1.0.0",
50
50
  "jasmine-core": "^2.4.1",
@@ -75,7 +75,7 @@
75
75
  "unpkg": "dist/axios.min.js",
76
76
  "typings": "./index.d.ts",
77
77
  "dependencies": {
78
- "follow-redirects": "^1.14.4"
78
+ "follow-redirects": "^1.14.8"
79
79
  },
80
80
  "bundlesize": [
81
81
  {
package/tsconfig.json ADDED
@@ -0,0 +1,14 @@
1
+ {
2
+ "compilerOptions": {
3
+ "module": "es2015",
4
+ "lib": ["dom", "es2015"],
5
+ "types": [],
6
+ "moduleResolution": "node",
7
+ "strict": true,
8
+ "noEmit": true,
9
+ "baseUrl": ".",
10
+ "paths": {
11
+ "axios": ["."]
12
+ }
13
+ }
14
+ }
package/tslint.json ADDED
@@ -0,0 +1,6 @@
1
+ {
2
+ "extends": "dtslint/dtslint.json",
3
+ "rules": {
4
+ "no-unnecessary-generics": false
5
+ }
6
+ }