Comparing version 0.1.0 to 0.2.0
102
lib/axios.js
var Promise = require('es6-promise').Promise; | ||
var buildUrl = require('./buildUrl'); | ||
var cookies = require('./cookies'); | ||
var defaults = require('./defaults'); | ||
var parseHeaders = require('./parseHeaders'); | ||
var transformData = require('./transformData'); | ||
var urlIsSameOrigin = require('./urlIsSameOrigin'); | ||
var utils = require('./utils'); | ||
var spread = require('./spread'); | ||
@@ -21,88 +17,14 @@ var axios = module.exports = function axios(config) { | ||
var promise = new Promise(function (resolve, reject) { | ||
// Create the request | ||
var request = new(XMLHttpRequest || ActiveXObject)('Microsoft.XMLHTTP'); | ||
var data = transformData( | ||
config.data, | ||
config.headers, | ||
config.transformRequest | ||
); | ||
// Open the request | ||
request.open(config.method, buildUrl(config.url, config.params), true); | ||
// Listen for ready state | ||
request.onreadystatechange = function () { | ||
if (request && request.readyState === 4) { | ||
// Prepare the response | ||
var headers = parseHeaders(request.getAllResponseHeaders()); | ||
var response = { | ||
data: transformData( | ||
request.responseText, | ||
headers, | ||
config.transformResponse | ||
), | ||
status: request.status, | ||
headers: headers, | ||
config: config | ||
}; | ||
// Resolve or reject the Promise based on the status | ||
(request.status >= 200 && request.status < 300 | ||
? resolve | ||
: reject)( | ||
response.data, | ||
response.status, | ||
response.headers, | ||
response.config | ||
); | ||
// Clean up request | ||
request = null; | ||
try { | ||
// For browsers use XHR adapter | ||
if (typeof window !== 'undefined') { | ||
require('./adapters/xhr')(resolve, reject, config); | ||
} | ||
}; | ||
// Merge headers and add to request | ||
var headers = utils.merge( | ||
defaults.headers.common, | ||
defaults.headers[config.method] || {}, | ||
config.headers || {} | ||
); | ||
// Add xsrf header | ||
var xsrfValue = urlIsSameOrigin(config.url) | ||
? cookies.read(config.xsrfCookieName || defaults.xsrfCookieName) | ||
: undefined; | ||
if (xsrfValue) { | ||
headers[config.xsrfHeaderName || defaults.xsrfHeaderName] = xsrfValue; | ||
} | ||
utils.forEach(headers, function (val, key) { | ||
// Remove Content-Type if data is undefined | ||
if (!data && key.toLowerCase() === 'content-type') { | ||
delete headers[key]; | ||
// For node use HTTP adapter | ||
else if (typeof process !== 'undefined') { | ||
require('./adapters/http')(resolve, reject, config); | ||
} | ||
// Otherwise add header to the request | ||
else { | ||
request.setRequestHeader(key, val); | ||
} | ||
}); | ||
// Add withCredentials to request if needed | ||
if (config.withCredentials) { | ||
request.withCredentials = true; | ||
} catch (e) { | ||
reject(e); | ||
} | ||
// Add responseType to request if needed | ||
if (config.responseType) { | ||
try { | ||
request.responseType = config.responseType; | ||
} catch (e) { | ||
if (request.responseType !== 'json') { | ||
throw e; | ||
} | ||
} | ||
} | ||
// Send the request | ||
request.send(data); | ||
}); | ||
@@ -132,2 +54,6 @@ | ||
// Expose all/spread | ||
axios.all = Promise.all; | ||
axios.spread = spread; | ||
// Provide aliases for supported request methods | ||
@@ -134,0 +60,0 @@ createShortMethods('delete', 'get', 'head'); |
{ | ||
"name": "axios", | ||
"version": "0.1.0", | ||
"description": "Promise based XHR library", | ||
"version": "0.2.0", | ||
"description": "Promise based HTTP client for the browser and node.js", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "grunt test", | ||
"start": "node ./sandbox/index.js" | ||
"start": "node ./sandbox/server.js" | ||
}, | ||
@@ -18,3 +18,4 @@ "repository": { | ||
"ajax", | ||
"promise" | ||
"promise", | ||
"node" | ||
], | ||
@@ -21,0 +22,0 @@ "author": "Matt Zabriskie", |
# axios [![Build Status](https://travis-ci.org/mzabriskie/axios.svg?branch=master)](https://travis-ci.org/mzabriskie/axios) | ||
Promise based XHR library | ||
Promise based HTTP client for the browser and node.js | ||
## Features | ||
- Making [XMLHttpRequests](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest) supporting the [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) API | ||
- Transforming request and response data | ||
- Make [XMLHttpRequests](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest) from the browser | ||
- Make [http](http://nodejs.org/api/http.html) requests from node.js | ||
- Supports the [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) API | ||
- Transform request and response data | ||
- Automatic transforms for JSON data | ||
- Client side support for protecting against [XSRF](http://en.wikipedia.org/wiki/Cross-site_request_forgery) | ||
- Specifying HTTP request headers | ||
- Automatic transforms for JSON data | ||
- Specify HTTP request headers | ||
@@ -41,2 +43,5 @@ ## Installing | ||
}) | ||
.catch(function (response) { | ||
console.log(response); | ||
}); | ||
@@ -52,2 +57,5 @@ // Optionally the request above could also be done as | ||
}) | ||
.catch(function (response) { | ||
console.log(response); | ||
}); | ||
``` | ||
@@ -65,2 +73,5 @@ | ||
}) | ||
.catch(function (response) { | ||
console.log(response); | ||
}); | ||
``` | ||
@@ -80,2 +91,19 @@ | ||
Performing multiple concurrent requests | ||
```js | ||
function getUserAccount() { | ||
return axios.get('/user/12345'); | ||
} | ||
function getUserPermissions() { | ||
return axios.get('/user/permissions/12345'); | ||
} | ||
axios.all([getUserAccount(), getUserPermissions()]) | ||
.then(axios.spread(function (acct, perms) { | ||
// Both requests are now complete | ||
})); | ||
``` | ||
## Request API | ||
@@ -108,2 +136,9 @@ | ||
### Concurrency | ||
Helper functions for dealing with concurrent requests. | ||
##### axios.all(iterable) | ||
##### axios.spread(callback) | ||
### Config | ||
@@ -191,3 +226,3 @@ | ||
## Thanks | ||
## Credits | ||
@@ -194,0 +229,0 @@ axios is heavily inspired by the [$http service](https://docs.angularjs.org/api/ng/service/$http) provided in [Angular](https://angularjs.org/). Ultimately axios is an effort to provide a standalone `$http`-like service for use outside of Angular. |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
2376442
22
14819
229
2