Comparing version 1.1.3 to 1.2.1
47
index.js
@@ -1,3 +0,2 @@ | ||
/*global window*/ | ||
var extend = require("xtend") | ||
var window = require("global/window") | ||
var once = require("once") | ||
@@ -11,6 +10,5 @@ | ||
var _window = typeof window === "undefined" ? {} : window | ||
var XHR = _window.XMLHttpRequest || noop | ||
var XHR = window.XMLHttpRequest || noop | ||
var XDR = "withCredentials" in (new XHR()) ? | ||
_window.XMLHttpRequest : _window.XDomainRequest | ||
window.XMLHttpRequest : window.XDomainRequest | ||
@@ -28,3 +26,2 @@ module.exports = createXHR | ||
var xhr | ||
var uri = options.uri | ||
@@ -38,2 +35,14 @@ if (options.cors) { | ||
var uri = xhr.url = options.uri | ||
var method = xhr.method = options.method || "GET" | ||
var body = options.body || options.data | ||
var headers = xhr.headers = options.headers || {} | ||
var isJson = false | ||
if ("json" in options) { | ||
isJson = true | ||
headers["Content-Type"] = "application/json" | ||
body = JSON.stringify(options.json) | ||
} | ||
xhr.onreadystatechange = readystatechange | ||
@@ -46,13 +55,14 @@ xhr.onload = load | ||
} | ||
// hate IE | ||
xhr.ontimeout = noop | ||
xhr.open(options.method || "GET", uri) | ||
xhr.open(method, uri) | ||
xhr.timeout = "timeout" in options ? options.timeout : 5000 | ||
if (options.headers && xhr.setRequestHeader) { | ||
Object.keys(options.headers).forEach(function (key) { | ||
xhr.setRequestHeader(key, options.headers[key]) | ||
if ( xhr.setRequestHeader) { | ||
Object.keys(headers).forEach(function (key) { | ||
xhr.setRequestHeader(key, headers[key]) | ||
}) | ||
} | ||
xhr.send(options.body || options.data) | ||
xhr.send(body) | ||
@@ -62,3 +72,5 @@ return xhr | ||
function readystatechange() { | ||
xhr.readyState === 4 && load() | ||
if (xhr.readyState === 4) { | ||
load() | ||
} | ||
} | ||
@@ -69,3 +81,4 @@ | ||
var status = xhr.statusCode = xhr.status | ||
xhr.body = xhr.response || xhr.responseText || xhr.responseXML | ||
var body = xhr.body = xhr.response || | ||
xhr.responseText || xhr.responseXML | ||
@@ -80,3 +93,9 @@ if (status === 0 || (status >= 400 && status < 600)) { | ||
callback(error, xhr, xhr.body) | ||
if (isJson) { | ||
try { | ||
body = xhr.body = JSON.parse(body) | ||
} catch (e) {} | ||
} | ||
callback(error, xhr, body) | ||
} | ||
@@ -83,0 +102,0 @@ |
{ | ||
"name": "xhr", | ||
"version": "1.1.3", | ||
"version": "1.2.1", | ||
"description": "small xhr abstraction", | ||
@@ -5,0 +5,0 @@ "keywords": [], |
@@ -29,3 +29,4 @@ # xhr | ||
headers: Object?, | ||
body: String? | ||
body: String?, | ||
json: Object? | ||
} | ||
@@ -83,2 +84,10 @@ xhr := (XhrOptions, Callback<Response>) => Request | ||
### `options.json` | ||
A valid JSON serializable value to be send to the server. If this | ||
is set then we serialize the value and use that as the body. | ||
We also set the Content-Type to `"application/json"`. | ||
Additionally the response body is parsed as JSON | ||
## MIT Licenced | ||
@@ -85,0 +94,0 @@ |
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
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
7091
80
102