axios 0.16.2 → 0.18.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
@@ -1,9 +1,9 @@
1
1
  export interface AxiosTransformer {
2
- (data: any): any;
2
+ (data: any, headers?: any): any;
3
3
  }
4
4
 
5
5
  export interface AxiosAdapter {
6
- (config: AxiosRequestConfig): AxiosPromise;
6
+ (config: AxiosRequestConfig): AxiosPromise<any>;
7
7
  }
8
8
 
9
9
  export interface AxiosBasicCredentials {
@@ -14,6 +14,10 @@ export interface AxiosBasicCredentials {
14
14
  export interface AxiosProxyConfig {
15
15
  host: string;
16
16
  port: number;
17
+ auth?: {
18
+ username: string;
19
+ password:string;
20
+ }
17
21
  }
18
22
 
19
23
  export interface AxiosRequestConfig {
@@ -40,25 +44,27 @@ export interface AxiosRequestConfig {
40
44
  maxRedirects?: number;
41
45
  httpAgent?: any;
42
46
  httpsAgent?: any;
43
- proxy?: AxiosProxyConfig;
47
+ proxy?: AxiosProxyConfig | false;
44
48
  cancelToken?: CancelToken;
45
49
  }
46
50
 
47
- export interface AxiosResponse {
48
- data: any;
51
+ export interface AxiosResponse<T = any> {
52
+ data: T;
49
53
  status: number;
50
54
  statusText: string;
51
55
  headers: any;
52
56
  config: AxiosRequestConfig;
57
+ request?: any;
53
58
  }
54
59
 
55
60
  export interface AxiosError extends Error {
56
61
  config: AxiosRequestConfig;
57
62
  code?: string;
63
+ request?: any;
58
64
  response?: AxiosResponse;
59
65
  }
60
66
 
61
- export interface AxiosPromise extends Promise<AxiosResponse> {
67
+ export interface AxiosPromise<T = any> extends Promise<AxiosResponse<T>> {
62
68
  }
63
69
 
64
70
  export interface CancelStatic {
@@ -90,28 +96,28 @@ export interface CancelTokenSource {
90
96
  }
91
97
 
92
98
  export interface AxiosInterceptorManager<V> {
93
- use(onFulfilled: (value: V) => V | Promise<V>, onRejected?: (error: any) => any): number;
99
+ use(onFulfilled?: (value: V) => V | Promise<V>, onRejected?: (error: any) => any): number;
94
100
  eject(id: number): void;
95
101
  }
96
102
 
97
103
  export interface AxiosInstance {
104
+ (config: AxiosRequestConfig): AxiosPromise;
105
+ (url: string, config?: AxiosRequestConfig): AxiosPromise;
98
106
  defaults: AxiosRequestConfig;
99
107
  interceptors: {
100
108
  request: AxiosInterceptorManager<AxiosRequestConfig>;
101
109
  response: AxiosInterceptorManager<AxiosResponse>;
102
110
  };
103
- request(config: AxiosRequestConfig): AxiosPromise;
104
- get(url: string, config?: AxiosRequestConfig): AxiosPromise;
111
+ request<T = any>(config: AxiosRequestConfig): AxiosPromise<T>;
112
+ get<T = any>(url: string, config?: AxiosRequestConfig): AxiosPromise<T>;
105
113
  delete(url: string, config?: AxiosRequestConfig): AxiosPromise;
106
114
  head(url: string, config?: AxiosRequestConfig): AxiosPromise;
107
- post(url: string, data?: any, config?: AxiosRequestConfig): AxiosPromise;
108
- put(url: string, data?: any, config?: AxiosRequestConfig): AxiosPromise;
109
- patch(url: string, data?: any, config?: AxiosRequestConfig): AxiosPromise;
115
+ post<T = any>(url: string, data?: any, config?: AxiosRequestConfig): AxiosPromise<T>;
116
+ put<T = any>(url: string, data?: any, config?: AxiosRequestConfig): AxiosPromise<T>;
117
+ patch<T = any>(url: string, data?: any, config?: AxiosRequestConfig): AxiosPromise<T>;
110
118
  }
111
119
 
112
120
  export interface AxiosStatic extends AxiosInstance {
113
- (config: AxiosRequestConfig): AxiosPromise;
114
- (url: string, config?: AxiosRequestConfig): AxiosPromise;
115
121
  create(config?: AxiosRequestConfig): AxiosInstance;
116
122
  Cancel: CancelStatic;
117
123
  CancelToken: CancelTokenStatic;
@@ -19,11 +19,10 @@ module.exports = function httpAdapter(config) {
19
19
  var data = config.data;
20
20
  var headers = config.headers;
21
21
  var timer;
22
- var aborted = false;
23
22
 
24
23
  // Set User-Agent (required by some servers)
25
24
  // Only set header if it hasn't been set in config
26
- // See https://github.com/mzabriskie/axios/issues/69
25
+ // See https://github.com/axios/axios/issues/69
27
26
  if (!headers['User-Agent'] && !headers['user-agent']) {
28
27
  headers['User-Agent'] = 'axios/' + pkg.version;
29
28
  }
@@ -73,8 +72,6 @@ module.exports = function httpAdapter(config) {
73
72
  var agent = isHttps ? config.httpsAgent : config.httpAgent;
74
73
 
75
74
  var options = {
76
- hostname: parsed.hostname,
77
- port: parsed.port,
78
75
  path: buildURL(parsed.path, config.params, config.paramsSerializer).replace(/^\?/, ''),
79
76
  method: config.method,
80
77
  headers: headers,
@@ -82,8 +79,15 @@ module.exports = function httpAdapter(config) {
82
79
  auth: auth
83
80
  };
84
81
 
82
+ if (config.socketPath) {
83
+ options.socketPath = config.socketPath;
84
+ } else {
85
+ options.hostname = parsed.hostname;
86
+ options.port = parsed.port;
87
+ }
88
+
85
89
  var proxy = config.proxy;
86
- if (!proxy) {
90
+ if (!proxy && proxy !== false) {
87
91
  var proxyEnv = protocol.slice(0, -1) + '_proxy';
88
92
  var proxyUrl = process.env[proxyEnv] || process.env[proxyEnv.toUpperCase()];
89
93
  if (proxyUrl) {
@@ -118,7 +122,9 @@ module.exports = function httpAdapter(config) {
118
122
  }
119
123
 
120
124
  var transport;
121
- if (config.maxRedirects === 0) {
125
+ if (config.transport) {
126
+ transport = config.transport;
127
+ } else if (config.maxRedirects === 0) {
122
128
  transport = isHttps ? https : http;
123
129
  } else {
124
130
  if (config.maxRedirects) {
@@ -127,9 +133,13 @@ module.exports = function httpAdapter(config) {
127
133
  transport = isHttps ? httpsFollow : httpFollow;
128
134
  }
129
135
 
136
+ if (config.maxContentLength && config.maxContentLength > -1) {
137
+ options.maxBodyLength = config.maxContentLength;
138
+ }
139
+
130
140
  // Create the request
131
141
  var req = transport.request(options, function handleResponse(res) {
132
- if (aborted) return;
142
+ if (req.aborted) return;
133
143
 
134
144
  // Response has been received so kill timer that handles request timeout
135
145
  clearTimeout(timer);
@@ -171,13 +181,14 @@ module.exports = function httpAdapter(config) {
171
181
 
172
182
  // make sure the content length is not over the maxContentLength if specified
173
183
  if (config.maxContentLength > -1 && Buffer.concat(responseBuffer).length > config.maxContentLength) {
184
+ stream.destroy();
174
185
  reject(createError('maxContentLength size of ' + config.maxContentLength + ' exceeded',
175
186
  config, null, lastRequest));
176
187
  }
177
188
  });
178
189
 
179
190
  stream.on('error', function handleStreamError(err) {
180
- if (aborted) return;
191
+ if (req.aborted) return;
181
192
  reject(enhanceError(err, config, null, lastRequest));
182
193
  });
183
194
 
@@ -195,7 +206,7 @@ module.exports = function httpAdapter(config) {
195
206
 
196
207
  // Handle errors
197
208
  req.on('error', function handleRequestError(err) {
198
- if (aborted) return;
209
+ if (req.aborted) return;
199
210
  reject(enhanceError(err, config, null, req));
200
211
  });
201
212
 
@@ -204,20 +215,16 @@ module.exports = function httpAdapter(config) {
204
215
  timer = setTimeout(function handleRequestTimeout() {
205
216
  req.abort();
206
217
  reject(createError('timeout of ' + config.timeout + 'ms exceeded', config, 'ECONNABORTED', req));
207
- aborted = true;
208
218
  }, config.timeout);
209
219
  }
210
220
 
211
221
  if (config.cancelToken) {
212
222
  // Handle cancellation
213
223
  config.cancelToken.promise.then(function onCanceled(cancel) {
214
- if (aborted) {
215
- return;
216
- }
224
+ if (req.aborted) return;
217
225
 
218
226
  req.abort();
219
227
  reject(cancel);
220
- aborted = true;
221
228
  });
222
229
  }
223
230
 
@@ -6,7 +6,6 @@ var buildURL = require('./../helpers/buildURL');
6
6
  var parseHeaders = require('./../helpers/parseHeaders');
7
7
  var isURLSameOrigin = require('./../helpers/isURLSameOrigin');
8
8
  var createError = require('../core/createError');
9
- var btoa = (typeof window !== 'undefined' && window.btoa && window.btoa.bind(window)) || require('./../helpers/btoa');
10
9
 
11
10
  module.exports = function xhrAdapter(config) {
12
11
  return new Promise(function dispatchXhrRequest(resolve, reject) {
@@ -18,22 +17,6 @@ module.exports = function xhrAdapter(config) {
18
17
  }
19
18
 
20
19
  var request = new XMLHttpRequest();
21
- var loadEvent = 'onreadystatechange';
22
- var xDomain = false;
23
-
24
- // For IE 8/9 CORS support
25
- // Only supports POST and GET calls and doesn't returns the response headers.
26
- // DON'T do this for testing b/c XMLHttpRequest is mocked, not XDomainRequest.
27
- if (process.env.NODE_ENV !== 'test' &&
28
- typeof window !== 'undefined' &&
29
- window.XDomainRequest && !('withCredentials' in request) &&
30
- !isURLSameOrigin(config.url)) {
31
- request = new window.XDomainRequest();
32
- loadEvent = 'onload';
33
- xDomain = true;
34
- request.onprogress = function handleProgress() {};
35
- request.ontimeout = function handleTimeout() {};
36
- }
37
20
 
38
21
  // HTTP basic authentication
39
22
  if (config.auth) {
@@ -48,8 +31,8 @@ module.exports = function xhrAdapter(config) {
48
31
  request.timeout = config.timeout;
49
32
 
50
33
  // Listen for ready state
51
- request[loadEvent] = function handleLoad() {
52
- if (!request || (request.readyState !== 4 && !xDomain)) {
34
+ request.onreadystatechange = function handleLoad() {
35
+ if (!request || request.readyState !== 4) {
53
36
  return;
54
37
  }
55
38
 
@@ -66,9 +49,8 @@ module.exports = function xhrAdapter(config) {
66
49
  var responseData = !config.responseType || config.responseType === 'text' ? request.responseText : request.response;
67
50
  var response = {
68
51
  data: responseData,
69
- // IE sends 1223 instead of 204 (https://github.com/mzabriskie/axios/issues/201)
70
- status: request.status === 1223 ? 204 : request.status,
71
- statusText: request.status === 1223 ? 'No Content' : request.statusText,
52
+ status: request.status,
53
+ statusText: request.statusText,
72
54
  headers: responseHeaders,
73
55
  config: config,
74
56
  request: request
package/lib/core/Axios.js CHANGED
@@ -4,8 +4,6 @@ var defaults = require('./../defaults');
4
4
  var utils = require('./../utils');
5
5
  var InterceptorManager = require('./InterceptorManager');
6
6
  var dispatchRequest = require('./dispatchRequest');
7
- var isAbsoluteURL = require('./../helpers/isAbsoluteURL');
8
- var combineURLs = require('./../helpers/combineURLs');
9
7
 
10
8
  /**
11
9
  * Create a new instance of Axios
@@ -34,14 +32,9 @@ Axios.prototype.request = function request(config) {
34
32
  }, arguments[1]);
35
33
  }
36
34
 
37
- config = utils.merge(defaults, this.defaults, { method: 'get' }, config);
35
+ config = utils.merge(defaults, {method: 'get'}, this.defaults, config);
38
36
  config.method = config.method.toLowerCase();
39
37
 
40
- // Support baseURL config
41
- if (config.baseURL && !isAbsoluteURL(config.url)) {
42
- config.url = combineURLs(config.baseURL, config.url);
43
- }
44
-
45
38
  // Hook up interceptors middleware
46
39
  var chain = [dispatchRequest, undefined];
47
40
  var promise = Promise.resolve(config);
@@ -4,6 +4,8 @@ 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 isAbsoluteURL = require('./../helpers/isAbsoluteURL');
8
+ var combineURLs = require('./../helpers/combineURLs');
7
9
 
8
10
  /**
9
11
  * Throws a `Cancel` if cancellation has been requested.
@@ -23,6 +25,11 @@ function throwIfCancellationRequested(config) {
23
25
  module.exports = function dispatchRequest(config) {
24
26
  throwIfCancellationRequested(config);
25
27
 
28
+ // Support baseURL config
29
+ if (config.baseURL && !isAbsoluteURL(config.url)) {
30
+ config.url = combineURLs(config.baseURL, config.url);
31
+ }
32
+
26
33
  // Ensure headers exist
27
34
  config.headers = config.headers || {};
28
35
 
package/lib/defaults.js CHANGED
@@ -63,6 +63,10 @@ var defaults = {
63
63
  return data;
64
64
  }],
65
65
 
66
+ /**
67
+ * A timeout in milliseconds to abort a request. If set to 0 (default) a
68
+ * timeout is not created.
69
+ */
66
70
  timeout: 0,
67
71
 
68
72
  xsrfCookieName: 'XSRF-TOKEN',
@@ -41,9 +41,7 @@ module.exports = function buildURL(url, params, paramsSerializer) {
41
41
 
42
42
  if (utils.isArray(val)) {
43
43
  key = key + '[]';
44
- }
45
-
46
- if (!utils.isArray(val)) {
44
+ } else {
47
45
  val = [val];
48
46
  }
49
47
 
@@ -2,6 +2,15 @@
2
2
 
3
3
  var utils = require('./../utils');
4
4
 
5
+ // Headers whose duplicates are ignored by node
6
+ // c.f. https://nodejs.org/api/http.html#http_message_headers
7
+ var ignoreDuplicateOf = [
8
+ 'age', 'authorization', 'content-length', 'content-type', 'etag',
9
+ 'expires', 'from', 'host', 'if-modified-since', 'if-unmodified-since',
10
+ 'last-modified', 'location', 'max-forwards', 'proxy-authorization',
11
+ 'referer', 'retry-after', 'user-agent'
12
+ ];
13
+
5
14
  /**
6
15
  * Parse headers into an object
7
16
  *
@@ -29,7 +38,14 @@ module.exports = function parseHeaders(headers) {
29
38
  val = utils.trim(line.substr(i + 1));
30
39
 
31
40
  if (key) {
32
- parsed[key] = parsed[key] ? parsed[key] + ', ' + val : val;
41
+ if (parsed[key] && ignoreDuplicateOf.indexOf(key) >= 0) {
42
+ return;
43
+ }
44
+ if (key === 'set-cookie') {
45
+ parsed[key] = (parsed[key] ? parsed[key] : []).concat([val]);
46
+ } else {
47
+ parsed[key] = parsed[key] ? parsed[key] + ', ' + val : val;
48
+ }
33
49
  }
34
50
  });
35
51
 
package/lib/utils.js CHANGED
@@ -207,7 +207,7 @@ function forEach(obj, fn) {
207
207
  }
208
208
 
209
209
  // Force an array if not already something iterable
210
- if (typeof obj !== 'object' && !isArray(obj)) {
210
+ if (typeof obj !== 'object') {
211
211
  /*eslint no-param-reassign:0*/
212
212
  obj = [obj];
213
213
  }
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "axios",
3
- "version": "0.16.2",
3
+ "version": "0.18.1",
4
4
  "description": "Promise based HTTP client for the browser and node.js",
5
5
  "main": "index.js",
6
6
  "scripts": {
7
- "test": "grunt test",
7
+ "test": "grunt test && bundlesize",
8
8
  "start": "node ./sandbox/server.js",
9
9
  "build": "NODE_ENV=production grunt build",
10
10
  "preversion": "npm test",
@@ -15,7 +15,7 @@
15
15
  },
16
16
  "repository": {
17
17
  "type": "git",
18
- "url": "https://github.com/mzabriskie/axios.git"
18
+ "url": "https://github.com/axios/axios.git"
19
19
  },
20
20
  "keywords": [
21
21
  "xhr",
@@ -27,10 +27,11 @@
27
27
  "author": "Matt Zabriskie",
28
28
  "license": "MIT",
29
29
  "bugs": {
30
- "url": "https://github.com/mzabriskie/axios/issues"
30
+ "url": "https://github.com/axios/axios/issues"
31
31
  },
32
- "homepage": "https://github.com/mzabriskie/axios",
32
+ "homepage": "https://github.com/axios/axios",
33
33
  "devDependencies": {
34
+ "bundlesize": "^0.5.7",
34
35
  "coveralls": "^2.11.9",
35
36
  "es6-promise": "^4.0.5",
36
37
  "grunt": "^1.0.1",
@@ -52,7 +53,6 @@
52
53
  "karma-jasmine": "^1.0.2",
53
54
  "karma-jasmine-ajax": "^0.1.13",
54
55
  "karma-opera-launcher": "^1.0.0",
55
- "karma-phantomjs-launcher": "^1.0.0",
56
56
  "karma-safari-launcher": "^1.0.0",
57
57
  "karma-sauce-launcher": "^1.1.0",
58
58
  "karma-sinon": "^1.0.5",
@@ -60,7 +60,6 @@
60
60
  "karma-webpack": "^1.7.0",
61
61
  "load-grunt-tasks": "^3.5.2",
62
62
  "minimist": "^1.2.0",
63
- "phantomjs-prebuilt": "^2.1.7",
64
63
  "sinon": "^1.17.4",
65
64
  "webpack": "^1.13.1",
66
65
  "webpack-dev-server": "^1.14.1",
@@ -72,7 +71,13 @@
72
71
  },
73
72
  "typings": "./index.d.ts",
74
73
  "dependencies": {
75
- "follow-redirects": "^1.2.3",
76
- "is-buffer": "^1.1.5"
77
- }
74
+ "follow-redirects": "1.5.10",
75
+ "is-buffer": "^2.0.2"
76
+ },
77
+ "bundlesize": [
78
+ {
79
+ "path": "./dist/axios.min.js",
80
+ "threshold": "5kB"
81
+ }
82
+ ]
78
83
  }
@@ -1,36 +0,0 @@
1
- 'use strict';
2
-
3
- // btoa polyfill for IE<10 courtesy https://github.com/davidchambers/Base64.js
4
-
5
- var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
6
-
7
- function E() {
8
- this.message = 'String contains an invalid character';
9
- }
10
- E.prototype = new Error;
11
- E.prototype.code = 5;
12
- E.prototype.name = 'InvalidCharacterError';
13
-
14
- function btoa(input) {
15
- var str = String(input);
16
- var output = '';
17
- for (
18
- // initialize result and counter
19
- var block, charCode, idx = 0, map = chars;
20
- // if the next str index does not exist:
21
- // change the mapping table to "="
22
- // check if d has no fractional digits
23
- str.charAt(idx | 0) || (map = '=', idx % 1);
24
- // "8 - idx % 1 * 8" generates the sequence 2, 4, 6, 8
25
- output += map.charAt(63 & block >> 8 - idx % 1 * 8)
26
- ) {
27
- charCode = str.charCodeAt(idx += 3 / 4);
28
- if (charCode > 0xFF) {
29
- throw new E();
30
- }
31
- block = block << 8 | charCode;
32
- }
33
- return output;
34
- }
35
-
36
- module.exports = btoa;