axios 0.16.1 → 0.18.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.

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;
@@ -10,7 +10,6 @@ var httpsFollow = require('follow-redirects').https;
10
10
  var url = require('url');
11
11
  var zlib = require('zlib');
12
12
  var pkg = require('./../../package.json');
13
- var Buffer = require('buffer').Buffer;
14
13
  var createError = require('../core/createError');
15
14
  var enhanceError = require('../core/enhanceError');
16
15
 
@@ -20,17 +19,16 @@ module.exports = function httpAdapter(config) {
20
19
  var data = config.data;
21
20
  var headers = config.headers;
22
21
  var timer;
23
- var aborted = false;
24
22
 
25
23
  // Set User-Agent (required by some servers)
26
24
  // Only set header if it hasn't been set in config
27
- // See https://github.com/mzabriskie/axios/issues/69
25
+ // See https://github.com/axios/axios/issues/69
28
26
  if (!headers['User-Agent'] && !headers['user-agent']) {
29
27
  headers['User-Agent'] = 'axios/' + pkg.version;
30
28
  }
31
29
 
32
30
  if (data && !utils.isStream(data)) {
33
- if (utils.isBuffer(data)) {
31
+ if (Buffer.isBuffer(data)) {
34
32
  // Nothing to do...
35
33
  } else if (utils.isArrayBuffer(data)) {
36
34
  data = new Buffer(new Uint8Array(data));
@@ -74,8 +72,6 @@ module.exports = function httpAdapter(config) {
74
72
  var agent = isHttps ? config.httpsAgent : config.httpAgent;
75
73
 
76
74
  var options = {
77
- hostname: parsed.hostname,
78
- port: parsed.port,
79
75
  path: buildURL(parsed.path, config.params, config.paramsSerializer).replace(/^\?/, ''),
80
76
  method: config.method,
81
77
  headers: headers,
@@ -83,8 +79,15 @@ module.exports = function httpAdapter(config) {
83
79
  auth: auth
84
80
  };
85
81
 
82
+ if (config.socketPath) {
83
+ options.socketPath = config.socketPath;
84
+ } else {
85
+ options.hostname = parsed.hostname;
86
+ options.port = parsed.port;
87
+ }
88
+
86
89
  var proxy = config.proxy;
87
- if (!proxy) {
90
+ if (!proxy && proxy !== false) {
88
91
  var proxyEnv = protocol.slice(0, -1) + '_proxy';
89
92
  var proxyUrl = process.env[proxyEnv] || process.env[proxyEnv.toUpperCase()];
90
93
  if (proxyUrl) {
@@ -119,7 +122,9 @@ module.exports = function httpAdapter(config) {
119
122
  }
120
123
 
121
124
  var transport;
122
- if (config.maxRedirects === 0) {
125
+ if (config.transport) {
126
+ transport = config.transport;
127
+ } else if (config.maxRedirects === 0) {
123
128
  transport = isHttps ? https : http;
124
129
  } else {
125
130
  if (config.maxRedirects) {
@@ -128,9 +133,13 @@ module.exports = function httpAdapter(config) {
128
133
  transport = isHttps ? httpsFollow : httpFollow;
129
134
  }
130
135
 
136
+ if (config.maxContentLength && config.maxContentLength > -1) {
137
+ options.maxBodyLength = config.maxContentLength;
138
+ }
139
+
131
140
  // Create the request
132
141
  var req = transport.request(options, function handleResponse(res) {
133
- if (aborted) return;
142
+ if (req.aborted) return;
134
143
 
135
144
  // Response has been received so kill timer that handles request timeout
136
145
  clearTimeout(timer);
@@ -172,13 +181,14 @@ module.exports = function httpAdapter(config) {
172
181
 
173
182
  // make sure the content length is not over the maxContentLength if specified
174
183
  if (config.maxContentLength > -1 && Buffer.concat(responseBuffer).length > config.maxContentLength) {
175
- reject(createError('maxContentLength size of ' + config.maxContentLength + ' exceeded', config));
184
+ reject(createError('maxContentLength size of ' + config.maxContentLength + ' exceeded',
185
+ config, null, lastRequest));
176
186
  }
177
187
  });
178
188
 
179
189
  stream.on('error', function handleStreamError(err) {
180
- if (aborted) return;
181
- reject(enhanceError(err, config));
190
+ if (req.aborted) return;
191
+ reject(enhanceError(err, config, null, lastRequest));
182
192
  });
183
193
 
184
194
  stream.on('end', function handleStreamEnd() {
@@ -195,29 +205,25 @@ module.exports = function httpAdapter(config) {
195
205
 
196
206
  // Handle errors
197
207
  req.on('error', function handleRequestError(err) {
198
- if (aborted) return;
199
- reject(enhanceError(err, config));
208
+ if (req.aborted) return;
209
+ reject(enhanceError(err, config, null, req));
200
210
  });
201
211
 
202
212
  // Handle request timeout
203
213
  if (config.timeout && !timer) {
204
214
  timer = setTimeout(function handleRequestTimeout() {
205
215
  req.abort();
206
- reject(createError('timeout of ' + config.timeout + 'ms exceeded', config, 'ECONNABORTED'));
207
- aborted = true;
216
+ reject(createError('timeout of ' + config.timeout + 'ms exceeded', config, 'ECONNABORTED', req));
208
217
  }, config.timeout);
209
218
  }
210
219
 
211
220
  if (config.cancelToken) {
212
221
  // Handle cancellation
213
222
  config.cancelToken.promise.then(function onCanceled(cancel) {
214
- if (aborted) {
215
- return;
216
- }
223
+ if (req.aborted) return;
217
224
 
218
225
  req.abort();
219
226
  reject(cancel);
220
- aborted = true;
221
227
  });
222
228
  }
223
229
 
@@ -66,7 +66,7 @@ module.exports = function xhrAdapter(config) {
66
66
  var responseData = !config.responseType || config.responseType === 'text' ? request.responseText : request.response;
67
67
  var response = {
68
68
  data: responseData,
69
- // IE sends 1223 instead of 204 (https://github.com/mzabriskie/axios/issues/201)
69
+ // IE sends 1223 instead of 204 (https://github.com/axios/axios/issues/201)
70
70
  status: request.status === 1223 ? 204 : request.status,
71
71
  statusText: request.status === 1223 ? 'No Content' : request.statusText,
72
72
  headers: responseHeaders,
@@ -84,7 +84,7 @@ module.exports = function xhrAdapter(config) {
84
84
  request.onerror = function handleError() {
85
85
  // Real errors are hidden from us by the browser
86
86
  // onerror should only fire if it's a network error
87
- reject(createError('Network Error', config));
87
+ reject(createError('Network Error', config, null, request));
88
88
 
89
89
  // Clean up request
90
90
  request = null;
@@ -92,7 +92,8 @@ module.exports = function xhrAdapter(config) {
92
92
 
93
93
  // Handle timeout
94
94
  request.ontimeout = function handleTimeout() {
95
- reject(createError('timeout of ' + config.timeout + 'ms exceeded', config, 'ECONNABORTED'));
95
+ reject(createError('timeout of ' + config.timeout + 'ms exceeded', config, 'ECONNABORTED',
96
+ request));
96
97
 
97
98
  // Clean up request
98
99
  request = null;
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,12 +32,8 @@ Axios.prototype.request = function request(config) {
34
32
  }, arguments[1]);
35
33
  }
36
34
 
37
- config = utils.merge(defaults, this.defaults, { method: 'get' }, config);
38
-
39
- // Support baseURL config
40
- if (config.baseURL && !isAbsoluteURL(config.url)) {
41
- config.url = combineURLs(config.baseURL, config.url);
42
- }
35
+ config = utils.merge(defaults, {method: 'get'}, this.defaults, config);
36
+ config.method = config.method.toLowerCase();
43
37
 
44
38
  // Hook up interceptors middleware
45
39
  var chain = [dispatchRequest, undefined];
@@ -3,15 +3,16 @@
3
3
  var enhanceError = require('./enhanceError');
4
4
 
5
5
  /**
6
- * Create an Error with the specified message, config, error code, and response.
6
+ * Create an Error with the specified message, config, error code, request and response.
7
7
  *
8
8
  * @param {string} message The error message.
9
9
  * @param {Object} config The config.
10
10
  * @param {string} [code] The error code (for example, 'ECONNABORTED').
11
- @ @param {Object} [response] The response.
11
+ * @param {Object} [request] The request.
12
+ * @param {Object} [response] The response.
12
13
  * @returns {Error} The created error.
13
14
  */
14
- module.exports = function createError(message, config, code, response) {
15
+ module.exports = function createError(message, config, code, request, response) {
15
16
  var error = new Error(message);
16
- return enhanceError(error, config, code, response);
17
+ return enhanceError(error, config, code, request, response);
17
18
  };
@@ -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
 
@@ -6,14 +6,16 @@
6
6
  * @param {Error} error The error to update.
7
7
  * @param {Object} config The config.
8
8
  * @param {string} [code] The error code (for example, 'ECONNABORTED').
9
- @ @param {Object} [response] The response.
9
+ * @param {Object} [request] The request.
10
+ * @param {Object} [response] The response.
10
11
  * @returns {Error} The error.
11
12
  */
12
- module.exports = function enhanceError(error, config, code, response) {
13
+ module.exports = function enhanceError(error, config, code, request, response) {
13
14
  error.config = config;
14
15
  if (code) {
15
16
  error.code = code;
16
17
  }
18
+ error.request = request;
17
19
  error.response = response;
18
20
  return error;
19
21
  };
@@ -19,6 +19,7 @@ module.exports = function settle(resolve, reject, response) {
19
19
  'Request failed with status code ' + response.status,
20
20
  response.config,
21
21
  null,
22
+ response.request,
22
23
  response
23
24
  ));
24
25
  }
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
@@ -1,6 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  var bind = require('./helpers/bind');
4
+ var isBuffer = require('is-buffer');
4
5
 
5
6
  /*global toString:true*/
6
7
 
@@ -18,16 +19,6 @@ function isArray(val) {
18
19
  return toString.call(val) === '[object Array]';
19
20
  }
20
21
 
21
- /**
22
- * Determine if a value is a Node Buffer
23
- *
24
- * @param {Object} val The value to test
25
- * @returns {boolean} True if value is a Node Buffer, otherwise false
26
- */
27
- function isBuffer(val) {
28
- return ((typeof Buffer !== 'undefined') && (Buffer.isBuffer) && (Buffer.isBuffer(val)));
29
- }
30
-
31
22
  /**
32
23
  * Determine if a value is an ArrayBuffer
33
24
  *
@@ -216,7 +207,7 @@ function forEach(obj, fn) {
216
207
  }
217
208
 
218
209
  // Force an array if not already something iterable
219
- if (typeof obj !== 'object' && !isArray(obj)) {
210
+ if (typeof obj !== 'object') {
220
211
  /*eslint no-param-reassign:0*/
221
212
  obj = [obj];
222
213
  }
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "axios",
3
- "version": "0.16.1",
3
+ "version": "0.18.0",
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,6 +71,13 @@
72
71
  },
73
72
  "typings": "./index.d.ts",
74
73
  "dependencies": {
75
- "follow-redirects": "^1.2.3"
76
- }
74
+ "follow-redirects": "^1.3.0",
75
+ "is-buffer": "^1.1.5"
76
+ },
77
+ "bundlesize": [
78
+ {
79
+ "path": "./dist/axios.min.js",
80
+ "threshold": "5kB"
81
+ }
82
+ ]
77
83
  }