axios 0.15.2 → 0.16.2

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.

@@ -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
 
@@ -30,13 +29,15 @@ module.exports = function httpAdapter(config) {
30
29
  }
31
30
 
32
31
  if (data && !utils.isStream(data)) {
33
- if (utils.isArrayBuffer(data)) {
32
+ if (Buffer.isBuffer(data)) {
33
+ // Nothing to do...
34
+ } else if (utils.isArrayBuffer(data)) {
34
35
  data = new Buffer(new Uint8Array(data));
35
36
  } else if (utils.isString(data)) {
36
37
  data = new Buffer(data, 'utf-8');
37
38
  } else {
38
39
  return reject(createError(
39
- 'Data after transformation must be a string, an ArrayBuffer, or a Stream',
40
+ 'Data after transformation must be a string, an ArrayBuffer, a Buffer, or a Stream',
40
41
  config
41
42
  ));
42
43
  }
@@ -55,6 +56,8 @@ module.exports = function httpAdapter(config) {
55
56
 
56
57
  // Parse url
57
58
  var parsed = url.parse(config.url);
59
+ var protocol = parsed.protocol || 'http:';
60
+
58
61
  if (!auth && parsed.auth) {
59
62
  var urlAuth = parsed.auth.split(':');
60
63
  var urlUsername = urlAuth[0] || '';
@@ -66,7 +69,7 @@ module.exports = function httpAdapter(config) {
66
69
  delete headers.Authorization;
67
70
  }
68
71
 
69
- var isHttps = parsed.protocol === 'https:';
72
+ var isHttps = protocol === 'https:';
70
73
  var agent = isHttps ? config.httpsAgent : config.httpAgent;
71
74
 
72
75
  var options = {
@@ -81,7 +84,7 @@ module.exports = function httpAdapter(config) {
81
84
 
82
85
  var proxy = config.proxy;
83
86
  if (!proxy) {
84
- var proxyEnv = parsed.protocol.slice(0, -1) + '_proxy';
87
+ var proxyEnv = protocol.slice(0, -1) + '_proxy';
85
88
  var proxyUrl = process.env[proxyEnv] || process.env[proxyEnv.toUpperCase()];
86
89
  if (proxyUrl) {
87
90
  var parsedProxyUrl = url.parse(proxyUrl);
@@ -89,13 +92,29 @@ module.exports = function httpAdapter(config) {
89
92
  host: parsedProxyUrl.hostname,
90
93
  port: parsedProxyUrl.port
91
94
  };
95
+
96
+ if (parsedProxyUrl.auth) {
97
+ var proxyUrlAuth = parsedProxyUrl.auth.split(':');
98
+ proxy.auth = {
99
+ username: proxyUrlAuth[0],
100
+ password: proxyUrlAuth[1]
101
+ };
102
+ }
92
103
  }
93
104
  }
94
105
 
95
106
  if (proxy) {
107
+ options.hostname = proxy.host;
96
108
  options.host = proxy.host;
109
+ options.headers.host = parsed.hostname + (parsed.port ? ':' + parsed.port : '');
97
110
  options.port = proxy.port;
98
- options.path = parsed.protocol + '//' + parsed.hostname + (parsed.port ? ':' + parsed.port : '') + options.path;
111
+ options.path = protocol + '//' + parsed.hostname + (parsed.port ? ':' + parsed.port : '') + options.path;
112
+
113
+ // Basic proxy authorization
114
+ if (proxy.auth) {
115
+ var base64 = new Buffer(proxy.auth.username + ':' + proxy.auth.password, 'utf8').toString('base64');
116
+ options.headers['Proxy-Authorization'] = 'Basic ' + base64;
117
+ }
99
118
  }
100
119
 
101
120
  var transport;
@@ -131,12 +150,15 @@ module.exports = function httpAdapter(config) {
131
150
  break;
132
151
  }
133
152
 
153
+ // return the last request in case of redirects
154
+ var lastRequest = res.req || req;
155
+
134
156
  var response = {
135
157
  status: res.statusCode,
136
158
  statusText: res.statusMessage,
137
159
  headers: res.headers,
138
160
  config: config,
139
- request: req
161
+ request: lastRequest
140
162
  };
141
163
 
142
164
  if (config.responseType === 'stream') {
@@ -149,13 +171,14 @@ module.exports = function httpAdapter(config) {
149
171
 
150
172
  // make sure the content length is not over the maxContentLength if specified
151
173
  if (config.maxContentLength > -1 && Buffer.concat(responseBuffer).length > config.maxContentLength) {
152
- reject(createError('maxContentLength size of ' + config.maxContentLength + ' exceeded', config));
174
+ reject(createError('maxContentLength size of ' + config.maxContentLength + ' exceeded',
175
+ config, null, lastRequest));
153
176
  }
154
177
  });
155
178
 
156
179
  stream.on('error', function handleStreamError(err) {
157
180
  if (aborted) return;
158
- reject(enhanceError(err, config));
181
+ reject(enhanceError(err, config, null, lastRequest));
159
182
  });
160
183
 
161
184
  stream.on('end', function handleStreamEnd() {
@@ -173,14 +196,14 @@ module.exports = function httpAdapter(config) {
173
196
  // Handle errors
174
197
  req.on('error', function handleRequestError(err) {
175
198
  if (aborted) return;
176
- reject(enhanceError(err, config));
199
+ reject(enhanceError(err, config, null, req));
177
200
  });
178
201
 
179
202
  // Handle request timeout
180
203
  if (config.timeout && !timer) {
181
204
  timer = setTimeout(function handleRequestTimeout() {
182
205
  req.abort();
183
- reject(createError('timeout of ' + config.timeout + 'ms exceeded', config, 'ECONNABORTED'));
206
+ reject(createError('timeout of ' + config.timeout + 'ms exceeded', config, 'ECONNABORTED', req));
184
207
  aborted = true;
185
208
  }, config.timeout);
186
209
  }
@@ -6,7 +6,7 @@ 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) || require('./../helpers/btoa');
9
+ var btoa = (typeof window !== 'undefined' && window.btoa && window.btoa.bind(window)) || require('./../helpers/btoa');
10
10
 
11
11
  module.exports = function xhrAdapter(config) {
12
12
  return new Promise(function dispatchXhrRequest(resolve, reject) {
@@ -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;
@@ -137,7 +138,9 @@ module.exports = function xhrAdapter(config) {
137
138
  try {
138
139
  request.responseType = config.responseType;
139
140
  } catch (e) {
140
- if (request.responseType !== 'json') {
141
+ // Expected DOMException thrown by browsers not compatible XMLHttpRequest Level 2.
142
+ // But, this can be suppressed for 'json' type as it can be parsed by default 'transformResponse' function.
143
+ if (config.responseType !== 'json') {
141
144
  throw e;
142
145
  }
143
146
  }
package/lib/axios.js CHANGED
@@ -3,6 +3,7 @@
3
3
  var utils = require('./utils');
4
4
  var bind = require('./helpers/bind');
5
5
  var Axios = require('./core/Axios');
6
+ var defaults = require('./defaults');
6
7
 
7
8
  /**
8
9
  * Create an instance of Axios
@@ -24,14 +25,14 @@ function createInstance(defaultConfig) {
24
25
  }
25
26
 
26
27
  // Create the default instance to be exported
27
- var axios = createInstance();
28
+ var axios = createInstance(defaults);
28
29
 
29
30
  // Expose Axios class to allow class inheritance
30
31
  axios.Axios = Axios;
31
32
 
32
33
  // Factory for creating new instances
33
- axios.create = function create(defaultConfig) {
34
- return createInstance(defaultConfig);
34
+ axios.create = function create(instanceConfig) {
35
+ return createInstance(utils.merge(defaults, instanceConfig));
35
36
  };
36
37
 
37
38
  // Expose Cancel & CancelToken
package/lib/core/Axios.js CHANGED
@@ -10,10 +10,10 @@ var combineURLs = require('./../helpers/combineURLs');
10
10
  /**
11
11
  * Create a new instance of Axios
12
12
  *
13
- * @param {Object} defaultConfig The default config for the instance
13
+ * @param {Object} instanceConfig The default config for the instance
14
14
  */
15
- function Axios(defaultConfig) {
16
- this.defaults = utils.merge(defaults, defaultConfig);
15
+ function Axios(instanceConfig) {
16
+ this.defaults = instanceConfig;
17
17
  this.interceptors = {
18
18
  request: new InterceptorManager(),
19
19
  response: new InterceptorManager()
@@ -35,6 +35,7 @@ Axios.prototype.request = function request(config) {
35
35
  }
36
36
 
37
37
  config = utils.merge(defaults, this.defaults, { method: 'get' }, config);
38
+ config.method = config.method.toLowerCase();
38
39
 
39
40
  // Support baseURL config
40
41
  if (config.baseURL && !isAbsoluteURL(config.url)) {
@@ -61,7 +62,7 @@ Axios.prototype.request = function request(config) {
61
62
  };
62
63
 
63
64
  // Provide aliases for supported request methods
64
- utils.forEach(['delete', 'get', 'head'], function forEachMethodNoData(method) {
65
+ utils.forEach(['delete', 'get', 'head', 'options'], function forEachMethodNoData(method) {
65
66
  /*eslint func-names:0*/
66
67
  Axios.prototype[method] = function(url, config) {
67
68
  return this.request(utils.merge(config || {}, {
@@ -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
  };
@@ -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
@@ -3,7 +3,6 @@
3
3
  var utils = require('./utils');
4
4
  var normalizeHeaderName = require('./helpers/normalizeHeaderName');
5
5
 
6
- var PROTECTION_PREFIX = /^\)\]\}',?\n/;
7
6
  var DEFAULT_CONTENT_TYPE = {
8
7
  'Content-Type': 'application/x-www-form-urlencoded'
9
8
  };
@@ -26,13 +25,14 @@ function getDefaultAdapter() {
26
25
  return adapter;
27
26
  }
28
27
 
29
- module.exports = {
28
+ var defaults = {
30
29
  adapter: getDefaultAdapter(),
31
30
 
32
31
  transformRequest: [function transformRequest(data, headers) {
33
32
  normalizeHeaderName(headers, 'Content-Type');
34
33
  if (utils.isFormData(data) ||
35
34
  utils.isArrayBuffer(data) ||
35
+ utils.isBuffer(data) ||
36
36
  utils.isStream(data) ||
37
37
  utils.isFile(data) ||
38
38
  utils.isBlob(data)
@@ -56,7 +56,6 @@ module.exports = {
56
56
  transformResponse: [function transformResponse(data) {
57
57
  /*eslint no-param-reassign:0*/
58
58
  if (typeof data === 'string') {
59
- data = data.replace(PROTECTION_PREFIX, '');
60
59
  try {
61
60
  data = JSON.parse(data);
62
61
  } catch (e) { /* Ignore */ }
@@ -64,15 +63,6 @@ module.exports = {
64
63
  return data;
65
64
  }],
66
65
 
67
- headers: {
68
- common: {
69
- 'Accept': 'application/json, text/plain, */*'
70
- },
71
- patch: utils.merge(DEFAULT_CONTENT_TYPE),
72
- post: utils.merge(DEFAULT_CONTENT_TYPE),
73
- put: utils.merge(DEFAULT_CONTENT_TYPE)
74
- },
75
-
76
66
  timeout: 0,
77
67
 
78
68
  xsrfCookieName: 'XSRF-TOKEN',
@@ -84,3 +74,19 @@ module.exports = {
84
74
  return status >= 200 && status < 300;
85
75
  }
86
76
  };
77
+
78
+ defaults.headers = {
79
+ common: {
80
+ 'Accept': 'application/json, text/plain, */*'
81
+ }
82
+ };
83
+
84
+ utils.forEach(['delete', 'get', 'head'], function forEachMethodNoData(method) {
85
+ defaults.headers[method] = {};
86
+ });
87
+
88
+ utils.forEach(['post', 'put', 'patch'], function forEachMethodWithData(method) {
89
+ defaults.headers[method] = utils.merge(DEFAULT_CONTENT_TYPE);
90
+ });
91
+
92
+ module.exports = defaults;
@@ -8,5 +8,7 @@
8
8
  * @returns {string} The combined URL
9
9
  */
10
10
  module.exports = function combineURLs(baseURL, relativeURL) {
11
- return baseURL.replace(/\/+$/, '') + '/' + relativeURL.replace(/^\/+/, '');
11
+ return relativeURL
12
+ ? baseURL.replace(/\/+$/, '') + '/' + relativeURL.replace(/^\/+/, '')
13
+ : baseURL;
12
14
  };
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
 
@@ -175,13 +176,15 @@ function trim(str) {
175
176
  * typeof document -> undefined
176
177
  *
177
178
  * react-native:
178
- * typeof document.createElement -> undefined
179
+ * navigator.product -> 'ReactNative'
179
180
  */
180
181
  function isStandardBrowserEnv() {
182
+ if (typeof navigator !== 'undefined' && navigator.product === 'ReactNative') {
183
+ return false;
184
+ }
181
185
  return (
182
186
  typeof window !== 'undefined' &&
183
- typeof document !== 'undefined' &&
184
- typeof document.createElement === 'function'
187
+ typeof document !== 'undefined'
185
188
  );
186
189
  }
187
190
 
@@ -279,6 +282,7 @@ function extend(a, b, thisArg) {
279
282
  module.exports = {
280
283
  isArray: isArray,
281
284
  isArrayBuffer: isArrayBuffer,
285
+ isBuffer: isBuffer,
282
286
  isFormData: isFormData,
283
287
  isArrayBufferView: isArrayBufferView,
284
288
  isString: isString,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "axios",
3
- "version": "0.15.2",
3
+ "version": "0.16.2",
4
4
  "description": "Promise based HTTP client for the browser and node.js",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -32,22 +32,21 @@
32
32
  "homepage": "https://github.com/mzabriskie/axios",
33
33
  "devDependencies": {
34
34
  "coveralls": "^2.11.9",
35
- "es6-promise": "^3.2.1",
36
- "grunt": "0.4.5",
37
- "grunt-banner": "0.6.0",
38
- "grunt-cli": "0.1.13",
39
- "grunt-contrib-clean": "1.0.0",
40
- "grunt-contrib-nodeunit": "1.0.0",
41
- "grunt-contrib-watch": "0.6.1",
42
- "grunt-eslint": "18.0.0",
43
- "grunt-karma": "0.12.1",
44
- "grunt-ts": "5.3.2",
45
- "grunt-typings": "0.1.5",
46
- "grunt-webpack": "1.0.11",
47
- "istanbul-instrumenter-loader": "^0.2.0",
35
+ "es6-promise": "^4.0.5",
36
+ "grunt": "^1.0.1",
37
+ "grunt-banner": "^0.6.0",
38
+ "grunt-cli": "^1.2.0",
39
+ "grunt-contrib-clean": "^1.0.0",
40
+ "grunt-contrib-nodeunit": "^1.0.0",
41
+ "grunt-contrib-watch": "^1.0.0",
42
+ "grunt-eslint": "^19.0.0",
43
+ "grunt-karma": "^2.0.0",
44
+ "grunt-ts": "^6.0.0-beta.3",
45
+ "grunt-webpack": "^1.0.18",
46
+ "istanbul-instrumenter-loader": "^1.0.0",
48
47
  "jasmine-core": "^2.4.1",
49
- "karma": "^0.13.22",
50
- "karma-chrome-launcher": "^1.0.1",
48
+ "karma": "^1.3.0",
49
+ "karma-chrome-launcher": "^2.0.0",
51
50
  "karma-coverage": "^1.0.0",
52
51
  "karma-firefox-launcher": "^1.0.0",
53
52
  "karma-jasmine": "^1.0.2",
@@ -55,23 +54,25 @@
55
54
  "karma-opera-launcher": "^1.0.0",
56
55
  "karma-phantomjs-launcher": "^1.0.0",
57
56
  "karma-safari-launcher": "^1.0.0",
58
- "karma-sauce-launcher": "^1.0.0",
57
+ "karma-sauce-launcher": "^1.1.0",
59
58
  "karma-sinon": "^1.0.5",
60
59
  "karma-sourcemap-loader": "^0.3.7",
61
60
  "karma-webpack": "^1.7.0",
62
- "load-grunt-tasks": "3.4.1",
61
+ "load-grunt-tasks": "^3.5.2",
63
62
  "minimist": "^1.2.0",
64
63
  "phantomjs-prebuilt": "^2.1.7",
65
64
  "sinon": "^1.17.4",
66
65
  "webpack": "^1.13.1",
67
66
  "webpack-dev-server": "^1.14.1",
68
- "url-search-params": "^0.5.0"
67
+ "url-search-params": "^0.6.1",
68
+ "typescript": "^2.0.3"
69
69
  },
70
70
  "browser": {
71
71
  "./lib/adapters/http.js": "./lib/adapters/xhr.js"
72
72
  },
73
- "typings": "./axios.d.ts",
73
+ "typings": "./index.d.ts",
74
74
  "dependencies": {
75
- "follow-redirects": "0.0.7"
75
+ "follow-redirects": "^1.2.3",
76
+ "is-buffer": "^1.1.5"
76
77
  }
77
78
  }
package/typings.json DELETED
@@ -1,5 +0,0 @@
1
- {
2
- "dependencies": {
3
- "es6-promise": "registry:npm/es6-promise#3.0.0+20160723033700"
4
- }
5
- }