axios 0.9.0 → 0.11.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.

@@ -5,7 +5,8 @@ var buildURL = require('./../helpers/buildURL');
5
5
  var parseHeaders = require('./../helpers/parseHeaders');
6
6
  var transformData = require('./../helpers/transformData');
7
7
  var isURLSameOrigin = require('./../helpers/isURLSameOrigin');
8
- var btoa = window.btoa || require('./../helpers/btoa');
8
+ var btoa = (typeof window !== 'undefined' && window.btoa) || require('./../helpers/btoa');
9
+ var settle = require('../helpers/settle');
9
10
 
10
11
  module.exports = function xhrAdapter(resolve, reject, config) {
11
12
  var requestData = config.data;
@@ -16,11 +17,18 @@ module.exports = function xhrAdapter(resolve, reject, config) {
16
17
  }
17
18
 
18
19
  var request = new XMLHttpRequest();
20
+ var loadEvent = 'onreadystatechange';
21
+ var xDomain = false;
19
22
 
20
23
  // For IE 8/9 CORS support
21
24
  // Only supports POST and GET calls and doesn't returns the response headers.
22
- if (window.XDomainRequest && !('withCredentials' in request) && !isURLSameOrigin(config.url)) {
25
+ // DON'T do this for testing b/c XMLHttpRequest is mocked, not XDomainRequest.
26
+ if (process.env.NODE_ENV !== 'test' && typeof window !== 'undefined' && window.XDomainRequest && !('withCredentials' in request) && !isURLSameOrigin(config.url)) {
23
27
  request = new window.XDomainRequest();
28
+ loadEvent = 'onload';
29
+ xDomain = true;
30
+ request.onprogress = function handleProgress() {};
31
+ request.ontimeout = function handleTimeout() {};
24
32
  }
25
33
 
26
34
  // HTTP basic authentication
@@ -36,28 +44,56 @@ module.exports = function xhrAdapter(resolve, reject, config) {
36
44
  request.timeout = config.timeout;
37
45
 
38
46
  // Listen for ready state
39
- request.onload = function handleLoad() {
40
- if (!request) {
47
+ request[loadEvent] = function handleLoad() {
48
+ if (!request || (request.readyState !== 4 && !xDomain)) {
41
49
  return;
42
50
  }
51
+
52
+ // The request errored out and we didn't get a response, this will be
53
+ // handled by onerror instead
54
+ if (request.status === 0) {
55
+ return;
56
+ }
57
+
43
58
  // Prepare the response
44
59
  var responseHeaders = 'getAllResponseHeaders' in request ? parseHeaders(request.getAllResponseHeaders()) : null;
45
- var responseData = ['text', ''].indexOf(config.responseType || '') !== -1 ? request.responseText : request.response;
60
+ var responseData = !config.responseType || config.responseType === 'text' ? request.responseText : request.response;
46
61
  var response = {
47
62
  data: transformData(
48
63
  responseData,
49
64
  responseHeaders,
50
65
  config.transformResponse
51
66
  ),
52
- status: request.status,
53
- statusText: request.statusText,
67
+ // IE sends 1223 instead of 204 (https://github.com/mzabriskie/axios/issues/201)
68
+ status: request.status === 1223 ? 204 : request.status,
69
+ statusText: request.status === 1223 ? 'No Content' : request.statusText,
54
70
  headers: responseHeaders,
55
- config: config
71
+ config: config,
72
+ request: request
56
73
  };
57
- // Resolve or reject the Promise based on the status
58
- ((request.status >= 200 && request.status < 300) || (!('status' in request) && request.responseText) ?
59
- resolve :
60
- reject)(response);
74
+
75
+ settle(resolve, reject, response);
76
+
77
+ // Clean up request
78
+ request = null;
79
+ };
80
+
81
+ // Handle low level network errors
82
+ request.onerror = function handleError() {
83
+ // Real errors are hidden from us by the browser
84
+ // onerror should only fire if it's a network error
85
+ reject(new Error('Network Error'));
86
+
87
+ // Clean up request
88
+ request = null;
89
+ };
90
+
91
+ // Handle timeout
92
+ request.ontimeout = function handleTimeout() {
93
+ var err = new Error('timeout of ' + config.timeout + 'ms exceeded');
94
+ err.timeout = config.timeout;
95
+ err.code = 'ECONNABORTED';
96
+ reject(err);
61
97
 
62
98
  // Clean up request
63
99
  request = null;
@@ -108,8 +144,17 @@ module.exports = function xhrAdapter(resolve, reject, config) {
108
144
  }
109
145
  }
110
146
 
111
- if (utils.isArrayBuffer(requestData)) {
112
- requestData = new DataView(requestData);
147
+ // Handle progress if needed
148
+ if (config.progress) {
149
+ if (config.method === 'post' || config.method === 'put') {
150
+ request.upload.addEventListener('progress', config.progress);
151
+ } else if (config.method === 'get') {
152
+ request.addEventListener('progress', config.progress);
153
+ }
154
+ }
155
+
156
+ if (requestData === undefined) {
157
+ requestData = null;
113
158
  }
114
159
 
115
160
  // Send the request
package/lib/axios.js CHANGED
@@ -78,23 +78,23 @@ Axios.prototype.request = function request(config) {
78
78
 
79
79
  var defaultInstance = new Axios(defaults);
80
80
  var axios = module.exports = bind(Axios.prototype.request, defaultInstance);
81
+ module.exports.Axios = Axios;
81
82
 
83
+ // Expose properties from defaultInstance
84
+ axios.defaults = defaultInstance.defaults;
85
+ axios.interceptors = defaultInstance.interceptors;
86
+
87
+ // Factory for creating new instances
82
88
  axios.create = function create(defaultConfig) {
83
89
  return new Axios(defaultConfig);
84
90
  };
85
91
 
86
- // Expose defaults
87
- axios.defaults = defaultInstance.defaults;
88
-
89
92
  // Expose all/spread
90
93
  axios.all = function all(promises) {
91
94
  return Promise.all(promises);
92
95
  };
93
96
  axios.spread = require('./helpers/spread');
94
97
 
95
- // Expose interceptors
96
- axios.interceptors = defaultInstance.interceptors;
97
-
98
98
  // Provide aliases for supported request methods
99
99
  utils.forEach(['delete', 'get', 'head'], function forEachMethodNoData(method) {
100
100
  /*eslint func-names:0*/
package/lib/defaults.js CHANGED
@@ -8,11 +8,8 @@ var DEFAULT_CONTENT_TYPE = {
8
8
  };
9
9
 
10
10
  module.exports = {
11
- transformRequest: [function transformResponseJSON(data, headers) {
12
- if (utils.isFormData(data)) {
13
- return data;
14
- }
15
- if (utils.isArrayBuffer(data)) {
11
+ transformRequest: [function transformRequest(data, headers) {
12
+ if (utils.isFormData(data) || utils.isArrayBuffer(data) || utils.isStream(data)) {
16
13
  return data;
17
14
  }
18
15
  if (utils.isArrayBufferView(data)) {
@@ -36,7 +33,7 @@ module.exports = {
36
33
  return data;
37
34
  }],
38
35
 
39
- transformResponse: [function transformResponseJSON(data) {
36
+ transformResponse: [function transformResponse(data) {
40
37
  /*eslint no-param-reassign:0*/
41
38
  if (typeof data === 'string') {
42
39
  data = data.replace(PROTECTION_PREFIX, '');
@@ -59,5 +56,11 @@ module.exports = {
59
56
  timeout: 0,
60
57
 
61
58
  xsrfCookieName: 'XSRF-TOKEN',
62
- xsrfHeaderName: 'X-XSRF-TOKEN'
59
+ xsrfHeaderName: 'X-XSRF-TOKEN',
60
+
61
+ maxContentLength: -1,
62
+
63
+ validateStatus: function validateStatus(status) {
64
+ return status >= 200 && status < 300;
65
+ }
63
66
  };
@@ -4,12 +4,12 @@
4
4
 
5
5
  var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
6
6
 
7
- function InvalidCharacterError(message) {
8
- this.message = message;
7
+ function E() {
8
+ this.message = 'String contains an invalid character';
9
9
  }
10
- InvalidCharacterError.prototype = new Error;
11
- InvalidCharacterError.prototype.code = 5;
12
- InvalidCharacterError.prototype.name = 'InvalidCharacterError';
10
+ E.prototype = new Error;
11
+ E.prototype.code = 5;
12
+ E.prototype.name = 'InvalidCharacterError';
13
13
 
14
14
  function btoa(input) {
15
15
  var str = String(input);
@@ -26,7 +26,7 @@ function btoa(input) {
26
26
  ) {
27
27
  charCode = str.charCodeAt(idx += 3 / 4);
28
28
  if (charCode > 0xFF) {
29
- throw new InvalidCharacterError('INVALID_CHARACTER_ERR: DOM Exception 5');
29
+ throw new E();
30
30
  }
31
31
  block = block << 8 | charCode;
32
32
  }
@@ -0,0 +1,18 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * Resolve or reject a Promise based on response status.
5
+ *
6
+ * @param {Function} resolve A function that resolves the promise.
7
+ * @param {Function} reject A function that rejects the promise.
8
+ * @param {object} response The response.
9
+ */
10
+ module.exports = function settle(resolve, reject, response) {
11
+ var validateStatus = response.config.validateStatus;
12
+ // Note: status is not exposed by XDomainRequest
13
+ if (!response.status || !validateStatus || validateStatus(response.status)) {
14
+ resolve(response);
15
+ } else {
16
+ reject(response);
17
+ }
18
+ };
package/lib/utils.js CHANGED
@@ -33,7 +33,7 @@ function isArrayBuffer(val) {
33
33
  * @returns {boolean} True if value is an FormData, otherwise false
34
34
  */
35
35
  function isFormData(val) {
36
- return toString.call(val) === '[object FormData]';
36
+ return (typeof FormData !== 'undefined') && (val instanceof FormData);
37
37
  }
38
38
 
39
39
  /**
@@ -122,6 +122,26 @@ function isBlob(val) {
122
122
  return toString.call(val) === '[object Blob]';
123
123
  }
124
124
 
125
+ /**
126
+ * Determine if a value is a Function
127
+ *
128
+ * @param {Object} val The value to test
129
+ * @returns {boolean} True if value is a Function, otherwise false
130
+ */
131
+ function isFunction(val) {
132
+ return toString.call(val) === '[object Function]';
133
+ }
134
+
135
+ /**
136
+ * Determine if a value is a Stream
137
+ *
138
+ * @param {Object} val The value to test
139
+ * @returns {boolean} True if value is a Stream, otherwise false
140
+ */
141
+ function isStream(val) {
142
+ return isObject(val) && isFunction(val.pipe);
143
+ }
144
+
125
145
  /**
126
146
  * Trim excess whitespace off the beginning and end of a string
127
147
  *
@@ -237,6 +257,8 @@ module.exports = {
237
257
  isDate: isDate,
238
258
  isFile: isFile,
239
259
  isBlob: isBlob,
260
+ isFunction: isFunction,
261
+ isStream: isStream,
240
262
  isStandardBrowserEnv: isStandardBrowserEnv,
241
263
  forEach: forEach,
242
264
  merge: merge,
package/package.json CHANGED
@@ -1,12 +1,15 @@
1
1
  {
2
2
  "name": "axios",
3
- "version": "0.9.0",
3
+ "version": "0.11.1",
4
4
  "description": "Promise based HTTP client for the browser and node.js",
5
5
  "main": "index.js",
6
6
  "scripts": {
7
- "build": "./node_modules/.bin/grunt build",
8
- "test": "./node_modules/.bin/grunt test",
7
+ "test": "grunt test",
9
8
  "start": "node ./sandbox/server.js",
9
+ "build": "NODE_ENV=production grunt build",
10
+ "preversion": "npm test",
11
+ "version": "npm run build && grunt version && git add -A dist && git add CHANGELOG.md bower.json package.json",
12
+ "postversion": "git push && git push --tags",
10
13
  "examples": "node ./examples/server.js",
11
14
  "coveralls": "cat coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js"
12
15
  },
@@ -28,33 +31,38 @@
28
31
  },
29
32
  "homepage": "https://github.com/mzabriskie/axios",
30
33
  "devDependencies": {
31
- "coveralls": "2.11.6",
32
- "es6-promise": "3.0.2",
34
+ "coveralls": "2.11.8",
35
+ "es6-promise": "3.1.2",
33
36
  "grunt": "0.4.5",
34
37
  "grunt-banner": "0.6.0",
35
38
  "grunt-cli": "0.1.13",
36
- "grunt-contrib-clean": "0.7.0",
37
- "grunt-contrib-nodeunit": "0.4.1",
39
+ "grunt-contrib-clean": "1.0.0",
40
+ "grunt-contrib-nodeunit": "1.0.0",
38
41
  "grunt-contrib-watch": "0.6.1",
39
- "grunt-eslint": "17.3.1",
42
+ "grunt-eslint": "18.0.0",
40
43
  "grunt-karma": "0.12.1",
41
44
  "grunt-ts": "5.3.2",
42
- "grunt-update-json": "0.2.2",
43
45
  "grunt-webpack": "1.0.11",
44
- "istanbul-instrumenter-loader": "^0.1.3",
46
+ "istanbul-instrumenter-loader": "^0.2.0",
45
47
  "jasmine-core": "2.4.1",
46
- "karma": "0.13.19",
47
- "karma-coverage": "0.5.3",
48
- "karma-jasmine": "0.3.6",
48
+ "karma": "0.13.21",
49
+ "karma-chrome-launcher": "^0.2.2",
50
+ "karma-coverage": "0.5.4",
51
+ "karma-firefox-launcher": "^0.1.7",
52
+ "karma-jasmine": "0.3.7",
49
53
  "karma-jasmine-ajax": "0.1.13",
50
- "karma-phantomjs-launcher": "0.2.3",
54
+ "karma-opera-launcher": "^0.3.0",
55
+ "karma-phantomjs-launcher": "1.0.0",
56
+ "karma-safari-launcher": "^0.1.1",
57
+ "karma-sauce-launcher": "^0.3.1",
51
58
  "karma-sinon": "1.0.4",
52
59
  "karma-sourcemap-loader": "0.3.7",
53
60
  "karma-webpack": "1.7.0",
54
- "load-grunt-tasks": "3.4.0",
61
+ "load-grunt-tasks": "3.4.1",
55
62
  "minimist": "1.2.0",
56
- "phantomjs": "1.9.19",
57
- "webpack": "1.12.11",
63
+ "phantomjs-prebuilt": "2.1.6",
64
+ "sinon": "1.17.3",
65
+ "webpack": "1.12.14",
58
66
  "webpack-dev-server": "1.14.1"
59
67
  },
60
68
  "browser": {