Comparing version 2.2.0 to 2.2.1
20
index.js
"use strict"; | ||
var window = require("global/window") | ||
var once = require("once") | ||
var isFunction = require("is-function") | ||
@@ -59,3 +58,2 @@ var parseHeaders = require("parse-headers") | ||
} | ||
callback = once(callback) | ||
@@ -74,4 +72,4 @@ function readystatechange() { | ||
body = xhr.response | ||
} else if (xhr.responseType === "text" || !xhr.responseType) { | ||
body = xhr.responseText || xhr.responseXML | ||
} else { | ||
body = xhr.responseText || getXml(xhr) | ||
} | ||
@@ -104,2 +102,3 @@ | ||
callback(evt, failureResponse) | ||
callback = noop | ||
} | ||
@@ -137,2 +136,3 @@ | ||
callback(err, response, response.body) | ||
callback = noop | ||
@@ -223,2 +223,14 @@ } | ||
function getXml(xhr) { | ||
if (xhr.responseType === "document") { | ||
return xhr.responseXML | ||
} | ||
var firefoxBugTakenEffect = xhr.status === 204 && xhr.responseXML && xhr.responseXML.documentElement.nodeName === "parsererror" | ||
if (xhr.responseType === "" && !firefoxBugTakenEffect) { | ||
return xhr.responseXML | ||
} | ||
return null | ||
} | ||
function noop() {} |
{ | ||
"name": "xhr", | ||
"version": "2.2.0", | ||
"version": "2.2.1", | ||
"description": "small xhr abstraction", | ||
@@ -28,3 +28,2 @@ "keywords": [ | ||
"is-function": "^1.0.1", | ||
"once": "~1.1.1", | ||
"parse-headers": "^2.0.0", | ||
@@ -36,16 +35,11 @@ "xtend": "^4.0.0" | ||
"pre-commit": "1.0.10", | ||
"run-browser": "^2.0.2", | ||
"run-browser": "naugtur/run-browser", | ||
"tap-spec": "^4.0.2", | ||
"tape": "^4.0.0" | ||
}, | ||
"licenses": [ | ||
{ | ||
"type": "MIT", | ||
"url": "http://github.com/Raynos/xhr/raw/master/LICENSE" | ||
} | ||
], | ||
"license": "MIT", | ||
"scripts": { | ||
"test": "run-browser test/index.js -b | tap-spec", | ||
"browser": "run-browser test/index.js" | ||
"test": "run-browser test/index.js -b -m test/mock-server.js | tap-spec", | ||
"browser": "run-browser -m test/mock-server.js test/index.js" | ||
} | ||
} |
# xhr | ||
A small xhr wrapper. Designed for use with [browserify](http://browserify.org/). | ||
A small XMLHttpRequest wrapper. Designed for use with [browserify](http://browserify.org/), [webpack](https://webpack.github.io/) etc. | ||
API is a subset of [request](https://github.com/request/request) so that the same code can be used in the browser and Node.js. | ||
Browser support: IE8+ and everything else. | ||
@@ -60,4 +62,4 @@ | ||
``` | ||
- `body`: HTTP response body - [`xhr.response`][6], [`xhr.responseText`][7] or | ||
[`xhr.responseXML`][8] depending on the request type. | ||
- `body`: HTTP response body - [`XMLHttpRequest.response`][6], [`XMLHttpRequest.responseText`][7] or | ||
[`XMLHttpRequest.responseXML`][8] depending on the request type. | ||
- `rawRequest`: Original [`XMLHttpRequest`][3] instance | ||
@@ -109,3 +111,3 @@ or [`XDomainRequest`][4] instance (if on IE8/IE9 && | ||
Specify the method the [`XMLHttpRequest`][3] should be opened | ||
with. Passed to [`xhr.open`][2]. Defaults to "GET" | ||
with. Passed to [`XMLHttpRequest.open`][2]. Defaults to "GET" | ||
@@ -131,7 +133,7 @@ ### `options.useXDR` | ||
Generally should be a string. But anything that's valid as | ||
a parameter to [`xhr.send`][1] should work | ||
a parameter to [`XMLHttpRequest.send`][1] should work (Buffer for file, etc.). | ||
### `options.uri` or `options.url` | ||
The uri to send a request to. Passed to [`xhr.open`][2]. `options.url` and `options.uri` are aliases for each other. | ||
The uri to send a request to. Passed to [`XMLHttpRequest.open`][2]. `options.url` and `options.uri` are aliases for each other. | ||
@@ -141,3 +143,3 @@ ### `options.headers` | ||
An object of headers that should be set on the request. The | ||
key, value pair is passed to [`xhr.setRequestHeader`][9] | ||
key, value pair is passed to [`XMLHttpRequest.setRequestHeader`][9] | ||
@@ -159,3 +161,3 @@ ### `options.timeout` | ||
Specify whether user credentials are to be included in a cross-origin | ||
request. Sets [`xhr.withCredentials`][10]. Defaults to false. | ||
request. Sets [`XMLHttpRequest.withCredentials`][10]. Defaults to false. | ||
@@ -167,3 +169,3 @@ A wildcard `*` cannot be used in the `Access-Control-Allow-Origin` header when `withCredentials` is true. | ||
Determines the data type of the `response`. Sets [`xhr.responseType`][11]. For example, a `responseType` of `document` will return a parsed `Document` object as the `response.body` for an XML resource. | ||
Determines the data type of the `response`. Sets [`XMLHttpRequest.responseType`][11]. For example, a `responseType` of `document` will return a parsed `Document` object as the `response.body` for an XML resource. | ||
@@ -178,2 +180,15 @@ ### `options.beforeSend` | ||
## FAQ | ||
- Why is my server's JSON response not parsed? I returned the right content-type. | ||
- See `options.json` - you can set it to `true` on a GET request to tell `xhr` to parse the response body. | ||
- Without `options.json` body is returned as-is (a string or when `responseType` is set and the browser supports it - a result of parsing JSON or XML) | ||
- How do I send an object or array as POST body? | ||
- `options.body` should be a string. You need to serialize your object before passing to `xhr` for sending. | ||
- To serialize to JSON you can use | ||
`options.json` instead of `options.body` for convenience - then `xhr` will do the serialization and set content-type accordingly. | ||
- Where's stream API? `.pipe()` etc. | ||
- Not implemented. You can't reasonably have that in the browser. | ||
## Mocking Requests | ||
@@ -180,0 +195,0 @@ You can override the constructor used to create new requests for testing. When you're making a new request: |
@@ -45,6 +45,16 @@ var window = require("global/window") | ||
test("[func] Returns a falsy body for 204 responses", function(assert) { | ||
xhr({ | ||
uri: "/mock/no-content" | ||
}, function(err, resp, body) { | ||
assert.notOk(body, "body should be falsey") | ||
assert.equal(resp.statusCode, 204) | ||
assert.end() | ||
}) | ||
}) | ||
test("[func] Times out to an error ", function(assert) { | ||
xhr({ | ||
timeout: 1, | ||
uri: "/tests-bundle.js?should-take-a-bit-to-parse=1&" + (new Array(300)).join("cachebreaker=" + Math.random().toFixed(5) + "&") | ||
uri: "/mock/timeout" | ||
}, function(err, resp, body) { | ||
@@ -144,4 +154,3 @@ assert.ok(err instanceof Error, "should return error") | ||
xhr[method]({ | ||
uri: "http://httpbin.org/" + method, | ||
useXDR: true | ||
uri: "/mock/200ok" | ||
}, function(err, resp, body) { | ||
@@ -152,4 +161,2 @@ i++ | ||
assert.equal(resp.method, method.toUpperCase()) | ||
assert.notEqual(resp.body.length, 0) | ||
if (i === methods.length) assert.end() | ||
@@ -191,3 +198,3 @@ }) | ||
xhr.head({ | ||
uri: "https://httpbin.org/get", | ||
uri: "/mock/200ok", | ||
}, function(err, resp, body) { | ||
@@ -203,3 +210,3 @@ assert.ifError(err, "no err") | ||
test("xhr.head url shorthand", function(assert) { | ||
xhr.head("https://httpbin.org/get", function(err, resp, body) { | ||
xhr.head("/mock/200ok", function(err, resp, body) { | ||
assert.equal(resp.method, "HEAD") | ||
@@ -212,3 +219,3 @@ assert.end() | ||
xhr.del({ | ||
uri: "https://httpbin.org/delete" | ||
uri: "/mock/200ok" | ||
}, function(err, resp, body) { | ||
@@ -218,3 +225,2 @@ assert.ifError(err, "no err") | ||
assert.equal(resp.method, "DELETE") | ||
assert.notEqual(resp.body.length, 0) | ||
assert.end() | ||
@@ -225,3 +231,3 @@ }) | ||
test("xhr.del url shorthand", function(assert) { | ||
xhr.del("https://httpbin.org/delete", function(err, resp, body) { | ||
xhr.del("/mock/200ok", function(err, resp, body) { | ||
assert.equal(resp.method, "DELETE") | ||
@@ -228,0 +234,0 @@ assert.end() |
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
25531
4
8
454
214
- Removedonce@~1.1.1
- Removedonce@1.1.1(transitive)