axios 0.16.0 → 0.17.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 {
@@ -44,21 +44,23 @@ export interface AxiosRequestConfig {
44
44
  cancelToken?: CancelToken;
45
45
  }
46
46
 
47
- export interface AxiosResponse {
48
- data: any;
47
+ export interface AxiosResponse<T = any> {
48
+ data: T;
49
49
  status: number;
50
50
  statusText: string;
51
51
  headers: any;
52
52
  config: AxiosRequestConfig;
53
+ request?: any;
53
54
  }
54
55
 
55
56
  export interface AxiosError extends Error {
56
57
  config: AxiosRequestConfig;
57
58
  code?: string;
59
+ request?: any;
58
60
  response?: AxiosResponse;
59
61
  }
60
62
 
61
- export interface AxiosPromise extends Promise<AxiosResponse> {
63
+ export interface AxiosPromise<T = any> extends Promise<AxiosResponse<T>> {
62
64
  }
63
65
 
64
66
  export interface CancelStatic {
@@ -90,7 +92,7 @@ export interface CancelTokenSource {
90
92
  }
91
93
 
92
94
  export interface AxiosInterceptorManager<V> {
93
- use(onFulfilled: (value: V) => V | Promise<V>, onRejected?: (error: any) => any): number;
95
+ use(onFulfilled?: (value: V) => V | Promise<V>, onRejected?: (error: any) => any): number;
94
96
  eject(id: number): void;
95
97
  }
96
98
 
@@ -100,13 +102,13 @@ export interface AxiosInstance {
100
102
  request: AxiosInterceptorManager<AxiosRequestConfig>;
101
103
  response: AxiosInterceptorManager<AxiosResponse>;
102
104
  };
103
- request(config: AxiosRequestConfig): AxiosPromise;
104
- get(url: string, config?: AxiosRequestConfig): AxiosPromise;
105
+ request<T = any>(config: AxiosRequestConfig): AxiosPromise<T>;
106
+ get<T = any>(url: string, config?: AxiosRequestConfig): AxiosPromise<T>;
105
107
  delete(url: string, config?: AxiosRequestConfig): AxiosPromise;
106
108
  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;
109
+ post<T = any>(url: string, data?: any, config?: AxiosRequestConfig): AxiosPromise<T>;
110
+ put<T = any>(url: string, data?: any, config?: AxiosRequestConfig): AxiosPromise<T>;
111
+ patch<T = any>(url: string, data?: any, config?: AxiosRequestConfig): AxiosPromise<T>;
110
112
  }
111
113
 
112
114
  export interface AxiosStatic extends AxiosInstance {
@@ -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,23 +19,24 @@ 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.isArrayBuffer(data)) {
31
+ if (Buffer.isBuffer(data)) {
32
+ // Nothing to do...
33
+ } else if (utils.isArrayBuffer(data)) {
34
34
  data = new Buffer(new Uint8Array(data));
35
35
  } else if (utils.isString(data)) {
36
36
  data = new Buffer(data, 'utf-8');
37
37
  } else {
38
38
  return reject(createError(
39
- 'Data after transformation must be a string, an ArrayBuffer, or a Stream',
39
+ 'Data after transformation must be a string, an ArrayBuffer, a Buffer, or a Stream',
40
40
  config
41
41
  ));
42
42
  }
@@ -82,7 +82,7 @@ module.exports = function httpAdapter(config) {
82
82
  };
83
83
 
84
84
  var proxy = config.proxy;
85
- if (!proxy) {
85
+ if (!proxy && proxy !== false) {
86
86
  var proxyEnv = protocol.slice(0, -1) + '_proxy';
87
87
  var proxyUrl = process.env[proxyEnv] || process.env[proxyEnv.toUpperCase()];
88
88
  if (proxyUrl) {
@@ -117,7 +117,9 @@ module.exports = function httpAdapter(config) {
117
117
  }
118
118
 
119
119
  var transport;
120
- if (config.maxRedirects === 0) {
120
+ if (config.transport) {
121
+ transport = config.transport;
122
+ } else if (config.maxRedirects === 0) {
121
123
  transport = isHttps ? https : http;
122
124
  } else {
123
125
  if (config.maxRedirects) {
@@ -128,7 +130,7 @@ module.exports = function httpAdapter(config) {
128
130
 
129
131
  // Create the request
130
132
  var req = transport.request(options, function handleResponse(res) {
131
- if (aborted) return;
133
+ if (req.aborted) return;
132
134
 
133
135
  // Response has been received so kill timer that handles request timeout
134
136
  clearTimeout(timer);
@@ -149,12 +151,15 @@ module.exports = function httpAdapter(config) {
149
151
  break;
150
152
  }
151
153
 
154
+ // return the last request in case of redirects
155
+ var lastRequest = res.req || req;
156
+
152
157
  var response = {
153
158
  status: res.statusCode,
154
159
  statusText: res.statusMessage,
155
160
  headers: res.headers,
156
161
  config: config,
157
- request: req
162
+ request: lastRequest
158
163
  };
159
164
 
160
165
  if (config.responseType === 'stream') {
@@ -167,13 +172,14 @@ module.exports = function httpAdapter(config) {
167
172
 
168
173
  // make sure the content length is not over the maxContentLength if specified
169
174
  if (config.maxContentLength > -1 && Buffer.concat(responseBuffer).length > config.maxContentLength) {
170
- reject(createError('maxContentLength size of ' + config.maxContentLength + ' exceeded', config));
175
+ reject(createError('maxContentLength size of ' + config.maxContentLength + ' exceeded',
176
+ config, null, lastRequest));
171
177
  }
172
178
  });
173
179
 
174
180
  stream.on('error', function handleStreamError(err) {
175
- if (aborted) return;
176
- reject(enhanceError(err, config));
181
+ if (req.aborted) return;
182
+ reject(enhanceError(err, config, null, lastRequest));
177
183
  });
178
184
 
179
185
  stream.on('end', function handleStreamEnd() {
@@ -190,29 +196,25 @@ module.exports = function httpAdapter(config) {
190
196
 
191
197
  // Handle errors
192
198
  req.on('error', function handleRequestError(err) {
193
- if (aborted) return;
194
- reject(enhanceError(err, config));
199
+ if (req.aborted) return;
200
+ reject(enhanceError(err, config, null, req));
195
201
  });
196
202
 
197
203
  // Handle request timeout
198
204
  if (config.timeout && !timer) {
199
205
  timer = setTimeout(function handleRequestTimeout() {
200
206
  req.abort();
201
- reject(createError('timeout of ' + config.timeout + 'ms exceeded', config, 'ECONNABORTED'));
202
- aborted = true;
207
+ reject(createError('timeout of ' + config.timeout + 'ms exceeded', config, 'ECONNABORTED', req));
203
208
  }, config.timeout);
204
209
  }
205
210
 
206
211
  if (config.cancelToken) {
207
212
  // Handle cancellation
208
213
  config.cancelToken.promise.then(function onCanceled(cancel) {
209
- if (aborted) {
210
- return;
211
- }
214
+ if (req.aborted) return;
212
215
 
213
216
  req.abort();
214
217
  reject(cancel);
215
- aborted = true;
216
218
  });
217
219
  }
218
220
 
@@ -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
@@ -35,11 +33,7 @@ Axios.prototype.request = function request(config) {
35
33
  }
36
34
 
37
35
  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
- }
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
@@ -32,6 +32,7 @@ var defaults = {
32
32
  normalizeHeaderName(headers, 'Content-Type');
33
33
  if (utils.isFormData(data) ||
34
34
  utils.isArrayBuffer(data) ||
35
+ utils.isBuffer(data) ||
35
36
  utils.isStream(data) ||
36
37
  utils.isFile(data) ||
37
38
  utils.isBlob(data)
@@ -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
 
@@ -206,7 +207,7 @@ function forEach(obj, fn) {
206
207
  }
207
208
 
208
209
  // Force an array if not already something iterable
209
- if (typeof obj !== 'object' && !isArray(obj)) {
210
+ if (typeof obj !== 'object') {
210
211
  /*eslint no-param-reassign:0*/
211
212
  obj = [obj];
212
213
  }
@@ -281,6 +282,7 @@ function extend(a, b, thisArg) {
281
282
  module.exports = {
282
283
  isArray: isArray,
283
284
  isArrayBuffer: isArrayBuffer,
285
+ isBuffer: isBuffer,
284
286
  isFormData: isFormData,
285
287
  isArrayBufferView: isArrayBufferView,
286
288
  isString: isString,
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "axios",
3
- "version": "0.16.0",
3
+ "version": "0.17.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",
@@ -72,6 +73,13 @@
72
73
  },
73
74
  "typings": "./index.d.ts",
74
75
  "dependencies": {
75
- "follow-redirects": "1.0.0"
76
- }
76
+ "follow-redirects": "^1.2.5",
77
+ "is-buffer": "^1.1.5"
78
+ },
79
+ "bundlesize": [
80
+ {
81
+ "path": "./dist/axios.min.js",
82
+ "threshold": "5kB"
83
+ }
84
+ ]
77
85
  }