axios 0.14.0 → 0.15.3

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.

@@ -55,6 +55,8 @@ module.exports = function httpAdapter(config) {
55
55
 
56
56
  // Parse url
57
57
  var parsed = url.parse(config.url);
58
+ var protocol = parsed.protocol || 'http:';
59
+
58
60
  if (!auth && parsed.auth) {
59
61
  var urlAuth = parsed.auth.split(':');
60
62
  var urlUsername = urlAuth[0] || '';
@@ -66,7 +68,7 @@ module.exports = function httpAdapter(config) {
66
68
  delete headers.Authorization;
67
69
  }
68
70
 
69
- var isHttps = parsed.protocol === 'https:';
71
+ var isHttps = protocol === 'https:';
70
72
  var agent = isHttps ? config.httpsAgent : config.httpAgent;
71
73
 
72
74
  var options = {
@@ -81,7 +83,7 @@ module.exports = function httpAdapter(config) {
81
83
 
82
84
  var proxy = config.proxy;
83
85
  if (!proxy) {
84
- var proxyEnv = parsed.protocol.slice(0, -1) + '_proxy';
86
+ var proxyEnv = protocol.slice(0, -1) + '_proxy';
85
87
  var proxyUrl = process.env[proxyEnv] || process.env[proxyEnv.toUpperCase()];
86
88
  if (proxyUrl) {
87
89
  var parsedProxyUrl = url.parse(proxyUrl);
@@ -89,13 +91,29 @@ module.exports = function httpAdapter(config) {
89
91
  host: parsedProxyUrl.hostname,
90
92
  port: parsedProxyUrl.port
91
93
  };
94
+
95
+ if (parsedProxyUrl.auth) {
96
+ var proxyUrlAuth = parsedProxyUrl.auth.split(':');
97
+ proxy.auth = {
98
+ username: proxyUrlAuth[0],
99
+ password: proxyUrlAuth[1]
100
+ };
101
+ }
92
102
  }
93
103
  }
94
104
 
95
105
  if (proxy) {
106
+ options.hostname = proxy.host;
96
107
  options.host = proxy.host;
108
+ options.headers.host = parsed.hostname + (parsed.port ? ':' + parsed.port : '');
97
109
  options.port = proxy.port;
98
- options.path = parsed.protocol + '//' + parsed.hostname + (parsed.port ? ':' + parsed.port : '') + options.path;
110
+ options.path = protocol + '//' + parsed.hostname + (parsed.port ? ':' + parsed.port : '') + options.path;
111
+
112
+ // Basic proxy authorization
113
+ if (proxy.auth) {
114
+ var base64 = new Buffer(proxy.auth.username + ':' + proxy.auth.password, 'utf8').toString('base64');
115
+ options.headers['Proxy-Authorization'] = 'Basic ' + base64;
116
+ }
99
117
  }
100
118
 
101
119
  var transport;
@@ -185,6 +203,19 @@ module.exports = function httpAdapter(config) {
185
203
  }, config.timeout);
186
204
  }
187
205
 
206
+ if (config.cancelToken) {
207
+ // Handle cancellation
208
+ config.cancelToken.promise.then(function onCanceled(cancel) {
209
+ if (aborted) {
210
+ return;
211
+ }
212
+
213
+ req.abort();
214
+ reject(cancel);
215
+ aborted = true;
216
+ });
217
+ }
218
+
188
219
  // Send the request
189
220
  if (utils.isStream(data)) {
190
221
  data.pipe(req);
@@ -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) {
@@ -55,7 +55,9 @@ module.exports = function xhrAdapter(config) {
55
55
 
56
56
  // The request errored out and we didn't get a response, this will be
57
57
  // handled by onerror instead
58
- if (request.status === 0) {
58
+ // With one exception: request that using file: protocol, most browsers
59
+ // will return status as 0 even though it's a successful request
60
+ if (request.status === 0 && !(request.responseURL && request.responseURL.indexOf('file:') === 0)) {
59
61
  return;
60
62
  }
61
63
 
@@ -151,6 +153,19 @@ module.exports = function xhrAdapter(config) {
151
153
  request.upload.addEventListener('progress', config.onUploadProgress);
152
154
  }
153
155
 
156
+ if (config.cancelToken) {
157
+ // Handle cancellation
158
+ config.cancelToken.promise.then(function onCanceled(cancel) {
159
+ if (!request) {
160
+ return;
161
+ }
162
+
163
+ request.abort();
164
+ reject(cancel);
165
+ // Clean up request
166
+ request = null;
167
+ });
168
+ }
154
169
 
155
170
  if (requestData === undefined) {
156
171
  requestData = null;
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,16 +25,21 @@ 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
 
38
+ // Expose Cancel & CancelToken
39
+ axios.Cancel = require('./cancel/Cancel');
40
+ axios.CancelToken = require('./cancel/CancelToken');
41
+ axios.isCancel = require('./cancel/isCancel');
42
+
37
43
  // Expose all/spread
38
44
  axios.all = function all(promises) {
39
45
  return Promise.all(promises);
@@ -0,0 +1,19 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * A `Cancel` is an object that is thrown when an operation is canceled.
5
+ *
6
+ * @class
7
+ * @param {string=} message The message.
8
+ */
9
+ function Cancel(message) {
10
+ this.message = message;
11
+ }
12
+
13
+ Cancel.prototype.toString = function toString() {
14
+ return 'Cancel' + (this.message ? ': ' + this.message : '');
15
+ };
16
+
17
+ Cancel.prototype.__CANCEL__ = true;
18
+
19
+ module.exports = Cancel;
@@ -0,0 +1,57 @@
1
+ 'use strict';
2
+
3
+ var Cancel = require('./Cancel');
4
+
5
+ /**
6
+ * A `CancelToken` is an object that can be used to request cancellation of an operation.
7
+ *
8
+ * @class
9
+ * @param {Function} executor The executor function.
10
+ */
11
+ function CancelToken(executor) {
12
+ if (typeof executor !== 'function') {
13
+ throw new TypeError('executor must be a function.');
14
+ }
15
+
16
+ var resolvePromise;
17
+ this.promise = new Promise(function promiseExecutor(resolve) {
18
+ resolvePromise = resolve;
19
+ });
20
+
21
+ var token = this;
22
+ executor(function cancel(message) {
23
+ if (token.reason) {
24
+ // Cancellation has already been requested
25
+ return;
26
+ }
27
+
28
+ token.reason = new Cancel(message);
29
+ resolvePromise(token.reason);
30
+ });
31
+ }
32
+
33
+ /**
34
+ * Throws a `Cancel` if cancellation has been requested.
35
+ */
36
+ CancelToken.prototype.throwIfRequested = function throwIfRequested() {
37
+ if (this.reason) {
38
+ throw this.reason;
39
+ }
40
+ };
41
+
42
+ /**
43
+ * Returns an object that contains a new `CancelToken` and a function that, when called,
44
+ * cancels the `CancelToken`.
45
+ */
46
+ CancelToken.source = function source() {
47
+ var cancel;
48
+ var token = new CancelToken(function executor(c) {
49
+ cancel = c;
50
+ });
51
+ return {
52
+ token: token,
53
+ cancel: cancel
54
+ };
55
+ };
56
+
57
+ module.exports = CancelToken;
@@ -0,0 +1,5 @@
1
+ 'use strict';
2
+
3
+ module.exports = function isCancel(value) {
4
+ return !!(value && value.__CANCEL__);
5
+ };
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()
@@ -2,15 +2,27 @@
2
2
 
3
3
  var utils = require('./../utils');
4
4
  var transformData = require('./transformData');
5
+ var isCancel = require('../cancel/isCancel');
6
+ var defaults = require('../defaults');
5
7
 
6
8
  /**
7
- * Dispatch a request to the server using whichever adapter
8
- * is supported by the current environment.
9
+ * Throws a `Cancel` if cancellation has been requested.
10
+ */
11
+ function throwIfCancellationRequested(config) {
12
+ if (config.cancelToken) {
13
+ config.cancelToken.throwIfRequested();
14
+ }
15
+ }
16
+
17
+ /**
18
+ * Dispatch a request to the server using the configured adapter.
9
19
  *
10
20
  * @param {object} config The config that is to be used for the request
11
21
  * @returns {Promise} The Promise to be fulfilled
12
22
  */
13
23
  module.exports = function dispatchRequest(config) {
24
+ throwIfCancellationRequested(config);
25
+
14
26
  // Ensure headers exist
15
27
  config.headers = config.headers || {};
16
28
 
@@ -35,41 +47,33 @@ module.exports = function dispatchRequest(config) {
35
47
  }
36
48
  );
37
49
 
38
- var adapter;
50
+ var adapter = config.adapter || defaults.adapter;
39
51
 
40
- if (typeof config.adapter === 'function') {
41
- // For custom adapter support
42
- adapter = config.adapter;
43
- } else if (typeof XMLHttpRequest !== 'undefined') {
44
- // For browsers use XHR adapter
45
- adapter = require('../adapters/xhr');
46
- } else if (typeof process !== 'undefined') {
47
- // For node use HTTP adapter
48
- adapter = require('../adapters/http');
49
- }
52
+ return adapter(config).then(function onAdapterResolution(response) {
53
+ throwIfCancellationRequested(config);
50
54
 
51
- return Promise.resolve(config)
52
- // Wrap synchronous adapter errors and pass configuration
53
- .then(adapter)
54
- .then(function onFulfilled(response) {
55
- // Transform response data
56
- response.data = transformData(
57
- response.data,
58
- response.headers,
59
- config.transformResponse
60
- );
55
+ // Transform response data
56
+ response.data = transformData(
57
+ response.data,
58
+ response.headers,
59
+ config.transformResponse
60
+ );
61
+
62
+ return response;
63
+ }, function onAdapterRejection(reason) {
64
+ if (!isCancel(reason)) {
65
+ throwIfCancellationRequested(config);
61
66
 
62
- return response;
63
- }, function onRejected(error) {
64
67
  // Transform response data
65
- if (error && error.response) {
66
- error.response.data = transformData(
67
- error.response.data,
68
- error.response.headers,
68
+ if (reason && reason.response) {
69
+ reason.response.data = transformData(
70
+ reason.response.data,
71
+ reason.response.headers,
69
72
  config.transformResponse
70
73
  );
71
74
  }
75
+ }
72
76
 
73
- return Promise.reject(error);
74
- });
77
+ return Promise.reject(reason);
78
+ });
75
79
  };
package/lib/defaults.js CHANGED
@@ -14,7 +14,21 @@ function setContentTypeIfUnset(headers, value) {
14
14
  }
15
15
  }
16
16
 
17
- module.exports = {
17
+ function getDefaultAdapter() {
18
+ var adapter;
19
+ if (typeof XMLHttpRequest !== 'undefined') {
20
+ // For browsers use XHR adapter
21
+ adapter = require('./adapters/xhr');
22
+ } else if (typeof process !== 'undefined') {
23
+ // For node use HTTP adapter
24
+ adapter = require('./adapters/http');
25
+ }
26
+ return adapter;
27
+ }
28
+
29
+ var defaults = {
30
+ adapter: getDefaultAdapter(),
31
+
18
32
  transformRequest: [function transformRequest(data, headers) {
19
33
  normalizeHeaderName(headers, 'Content-Type');
20
34
  if (utils.isFormData(data) ||
@@ -50,15 +64,6 @@ module.exports = {
50
64
  return data;
51
65
  }],
52
66
 
53
- headers: {
54
- common: {
55
- 'Accept': 'application/json, text/plain, */*'
56
- },
57
- patch: utils.merge(DEFAULT_CONTENT_TYPE),
58
- post: utils.merge(DEFAULT_CONTENT_TYPE),
59
- put: utils.merge(DEFAULT_CONTENT_TYPE)
60
- },
61
-
62
67
  timeout: 0,
63
68
 
64
69
  xsrfCookieName: 'XSRF-TOKEN',
@@ -70,3 +75,19 @@ module.exports = {
70
75
  return status >= 200 && status < 300;
71
76
  }
72
77
  };
78
+
79
+ defaults.headers = {
80
+ common: {
81
+ 'Accept': 'application/json, text/plain, */*'
82
+ }
83
+ };
84
+
85
+ utils.forEach(['delete', 'get', 'head'], function forEachMehtodNoData(method) {
86
+ defaults.headers[method] = {};
87
+ });
88
+
89
+ utils.forEach(['post', 'put', 'patch'], function forEachMethodWithData(method) {
90
+ defaults.headers[method] = utils.merge(DEFAULT_CONTENT_TYPE);
91
+ });
92
+
93
+ module.exports = defaults;
package/lib/utils.js CHANGED
@@ -217,7 +217,7 @@ function forEach(obj, fn) {
217
217
  } else {
218
218
  // Iterate over object keys
219
219
  for (var key in obj) {
220
- if (obj.hasOwnProperty(key)) {
220
+ if (Object.prototype.hasOwnProperty.call(obj, key)) {
221
221
  fn.call(null, obj[key], key, obj);
222
222
  }
223
223
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "axios",
3
- "version": "0.14.0",
3
+ "version": "0.15.3",
4
4
  "description": "Promise based HTTP client for the browser and node.js",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -32,22 +32,22 @@
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-typings": "^0.1.5",
46
+ "grunt-webpack": "^1.0.18",
47
+ "istanbul-instrumenter-loader": "^1.0.0",
48
48
  "jasmine-core": "^2.4.1",
49
- "karma": "^0.13.22",
50
- "karma-chrome-launcher": "^1.0.1",
49
+ "karma": "^1.3.0",
50
+ "karma-chrome-launcher": "^2.0.0",
51
51
  "karma-coverage": "^1.0.0",
52
52
  "karma-firefox-launcher": "^1.0.0",
53
53
  "karma-jasmine": "^1.0.2",
@@ -55,23 +55,24 @@
55
55
  "karma-opera-launcher": "^1.0.0",
56
56
  "karma-phantomjs-launcher": "^1.0.0",
57
57
  "karma-safari-launcher": "^1.0.0",
58
- "karma-sauce-launcher": "^1.0.0",
58
+ "karma-sauce-launcher": "^1.1.0",
59
59
  "karma-sinon": "^1.0.5",
60
60
  "karma-sourcemap-loader": "^0.3.7",
61
61
  "karma-webpack": "^1.7.0",
62
- "load-grunt-tasks": "3.4.1",
62
+ "load-grunt-tasks": "^3.5.2",
63
63
  "minimist": "^1.2.0",
64
64
  "phantomjs-prebuilt": "^2.1.7",
65
65
  "sinon": "^1.17.4",
66
66
  "webpack": "^1.13.1",
67
67
  "webpack-dev-server": "^1.14.1",
68
- "url-search-params": "^0.5.0"
68
+ "url-search-params": "^0.6.1",
69
+ "typescript": "^2.0.3"
69
70
  },
70
71
  "browser": {
71
72
  "./lib/adapters/http.js": "./lib/adapters/xhr.js"
72
73
  },
73
- "typings": "./axios.d.ts",
74
+ "typings": "./index.d.ts",
74
75
  "dependencies": {
75
- "follow-redirects": "0.0.7"
76
+ "follow-redirects": "1.0.0"
76
77
  }
77
78
  }