Gathering detailed insights and metrics for xhr
Gathering detailed insights and metrics for xhr
Gathering detailed insights and metrics for xhr
Gathering detailed insights and metrics for xhr
npm install xhr
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
802 Stars
297 Commits
96 Forks
23 Watching
23 Branches
42 Contributors
Updated on 21 Nov 2024
Minified
Minified + Gzipped
JavaScript (100%)
Cumulative downloads
Total Downloads
Last day
-2%
327,537
Compared to previous day
Last week
2.1%
1,830,666
Compared to previous week
Last month
10.7%
7,466,319
Compared to previous month
Last year
-6.4%
80,780,556
Compared to previous year
4
5
A small XMLHttpRequest wrapper. Designed for use with browserify, webpack etc.
API is a subset of request so you can write code that works in both node.js and the browser by using require('request')
in your code and telling your browser bundler to load xhr
instead of request
.
For browserify, add a browser field to your package.json
:
"browser": {
"request": "xhr"
}
For webpack, add a resolve.alias field to your configuration:
"resolve": {
"alias": {
"request$": "xhr"
}
}
Browser support: IE8+ and everything else.
npm install xhr
1var xhr = require("xhr") 2 3xhr({ 4 method: "post", 5 body: someJSONString, 6 uri: "/foo", 7 headers: { 8 "Content-Type": "application/json" 9 } 10}, function (err, resp, body) { 11 // check resp.statusCode 12})
var req = xhr(options, callback)
1type XhrOptions = String | { 2 useXDR: Boolean?, 3 sync: Boolean?, 4 uri: String, 5 url: String, 6 method: String?, 7 timeout: Number?, 8 headers: Object?, 9 body: String? | Object?, 10 json: Boolean? | Object?, 11 username: String?, 12 password: String?, 13 withCredentials: Boolean?, 14 responseType: String?, 15 beforeSend: Function? 16} 17xhr := (XhrOptions, Callback<Response>) => Request
the returned object is either an XMLHttpRequest
instance
or an XDomainRequest
instance (if on IE8/IE9 &&
options.useXDR
is set to true
)
Your callback will be called once with the arguments
( Error
, response
, body
) where the response is an object:
1{ 2 body: Object||String, 3 statusCode: Number, 4 method: String, 5 headers: {}, 6 url: String, 7 rawRequest: xhr 8}
body
: HTTP response body - XMLHttpRequest.response
, XMLHttpRequest.responseText
or
XMLHttpRequest.responseXML
depending on the request type.rawRequest
: Original XMLHttpRequest
instance
or XDomainRequest
instance (if on IE8/IE9 &&
options.useXDR
is set to true
)headers
: A collection of headers where keys are header names converted to lowercaseYour callback will be called with an Error
if there is an error in the browser that prevents sending the request.
A HTTP 500 response is not going to cause an error to be returned.
var req = xhr(url, callback)
-
a simple string instead of the options. In this case, a GET request will be made to that url.
var req = xhr(url, options, callback)
-
the above may also be called with the standard set of options.
var req = xhr.{post, put, patch, del, head, get}(url, callback)
var req = xhr.{post, put, patch, del, head, get}(options, callback)
var req = xhr.{post, put, patch, del, head, get}(url, options, callback)
The xhr
module has convience functions attached that will make requests with the given method.
Each function is named after its method, with the exception of DELETE
which is called xhr.del
for compatibility.
The method shorthands may be combined with the url-first form of xhr
for succinct and descriptive requests. For example,
1xhr.post('/post-to-me', function(err, resp) { 2 console.log(resp.body) 3})
or
1xhr.del('/delete-me', { headers: { my: 'auth' } }, function (err, resp) { 2 console.log(resp.statusCode); 3})
options.method
Specify the method the XMLHttpRequest
should be opened
with. Passed to XMLHttpRequest.open
. Defaults to "GET"
options.useXDR
Specify whether this is a cross origin (CORS) request for IE<10.
Switches IE to use XDomainRequest
instead of XMLHttpRequest
.
Ignored in other browsers.
Note that headers cannot be set on an XDomainRequest instance.
options.sync
Specify whether this is a synchrounous request. Note that when this is true the callback will be called synchronously. In most cases this option should not be used. Only use if you know what you are doing!
options.body
Pass in body to be send across the XMLHttpRequest
.
Generally should be a string. But anything that's valid as
a parameter to XMLHttpRequest.send
should work (Buffer for file, etc.).
If options.json
is true
, then this must be a JSON-serializable object. options.body
is passed to JSON.stringify
and sent.
options.uri
or options.url
The uri to send a request to. Passed to XMLHttpRequest.open
. options.url
and options.uri
are aliases for each other.
options.headers
An object of headers that should be set on the request. The
key, value pair is passed to XMLHttpRequest.setRequestHeader
options.timeout
Number of miliseconds to wait for response. Defaults to 0 (no timeout). Ignored when options.sync
is true.
options.json
Set to true
to send request as application/json
(see options.body
) and parse response from JSON.
For backwards compatibility options.json
can also be a valid JSON-serializable value to be sent to the server. Additionally the response body is still parsed as JSON
For sending booleans as JSON body see FAQ
options.withCredentials
Specify whether user credentials are to be included in a cross-origin
request. Sets XMLHttpRequest.withCredentials
. Defaults to false.
A wildcard *
cannot be used in the Access-Control-Allow-Origin
header when withCredentials
is true.
The header needs to specify your origin explicitly or browser will abort the request.
options.responseType
Determines the data type of the response
. Sets XMLHttpRequest.responseType
. For example, a responseType
of document
will return a parsed Document
object as the response.body
for an XML resource.
options.beforeSend
A function being called right before the send
method of the XMLHttpRequest
or XDomainRequest
instance is called. The XMLHttpRequest
or XDomainRequest
instance is passed as an argument.
options.xhr
Pass an XMLHttpRequest
object (or something that acts like one) to use instead of constructing a new one using the XMLHttpRequest
or XDomainRequest
constructors. Useful for testing.
options.json
- you can set it to true
on a GET request to tell xhr
to parse the response body.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)options.body
should be a string. You need to serialize your object before passing to xhr
for sending.options.json:true
with options.body
for convenience - then xhr
will do the serialization and set content-type accordingly..pipe()
etc.
"true"
as body by passing it as options.json
anymore?
true
as a value was a bug. Despite what JSON.stringify
does, the string "true"
is not valid JSON. If you're sending booleans as JSON, please consider wrapping them in an object or array to save yourself from more trouble in the future. To bring back the old behavior, hardcode options.json
to true
and set options.body
to your boolean value.onprogress
listener?
beforeSend
function for non-standard things that are browser specific. In this case:1xhr({ 2 ... 3 beforeSend: function(xhrObject){ 4 xhrObject.onprogress = function(){} 5 } 6})
You can override the constructor used to create new requests for testing. When you're making a new request:
1xhr({ xhr: new MockXMLHttpRequest() })
or you can override the constructors used to create requests at the module level:
1xhr.XMLHttpRequest = MockXMLHttpRequest 2xhr.XDomainRequest = MockXDomainRequest
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 9/19 approved changesets -- score normalized to 4
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
37 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More