Sexhr provides a simple API around XHR2 without limiting any of its power. The goal is to have a tiny promise-enabled library that makes working with XHR2 simpler.
Sexhr only has one exported function.
This function performs the request and returns a promise. If the browser making the request lacks promises, please include Bluebird or another library like it.
Sexhr()
takes a single objcet that describes the request. Its options are as
follows:
url
- (string) The URL we're requesting. If this is absent, an error is thrown.method
- (string) The HTTP method we're sending. Defaults to 'get'.data
- (any) The data we pass toXMLHttpRequest.send
. This can be a string, a blob, form data, etc.querydata
- (object) A set of key/value pairs to write to the querystring. If the given URL already has a querystring, then we append to it.emulate
- (boolean) If true andmethod
is not "get" or "post", then the value passed tomethod
will be put into a URL param in the request. For instance if{url: '/slappy', method: 'delete', emulate: true}
is given, then the final request would bePOST /slappy?_method=DELETE
.response_type
- (string) The type of response we want. See the MDN responseType docs for possible values. Sexhr resolves with the XMLHttpRequest.response object, soresponse_type
will affect what the value the returned promise gets.timeout
- (number) Milliseconds to wait before the request times out (is passed directly toXMLHttpRequest.timeout
).headers
- (object) This is a hash of headers to set into the XHR object.on*
- (function(*)) Allows setting callbacks directly onto the XHR object. For instance, you could passSexhr({url: '/file/123', onprogress: myprogressfn})
andmyprogressfn()
would be called whenever the XHR's progress event fired. Note thaton*
settings are disabled foronload
,onerror
, andonabort
because Sexhr uses these directly.upload.on*
- (function(*)) Allows setting upload callbacks directly onto theXMLHttpRequest.upload
object. Example:Sexhr({url: '/files', method: 'post', upload: {onprogress: myuploadprogress}})
override
- (function(XHR)) If passed, Sexhr will pass itsXMLHttpRequest
object to this function just before it sends it out, allowing you to make any needed overrides here. If you choose to overrideonload
,onerror
, oronabort
then you void Sexhr's nonexistent warranty.
Sexhr makes no assumptions about the data you pass or the data that's handed back, so it doesn't do anything like JSON encoding/decoding. That's all up to you.
The error handler (see examples) will be triggered if the HTTP status code is >= 400.
GET a JSON object:
Sexhr({url: '/data/averages.json'})
.spread(function(json, xhr) {
// "json" is returned as a string so we must parse it ourselves
myapp.averages = JSON.parse(json);
// we can look at the return headers as well
console.log('content type: ', xhr.getResponseHeader('Content-Type'));
})
.catch(function(err) {
// access the XHR object:
var type = err.xhr.getResponseHeader('Content-Type');
// get the error code. this will be -1 on XHR error, -2 if aborted, and
// if the HTTP status code of the XHR request was >= 400, it's the value
// in xhr.status (the HTTP error code)
var code = err.code;
// get the error message. this will be 'error' on XHR error, 'aborted'
// if aborted, and otherwise will be the HTTP response body for the
// request if the HTTP status code is >= 400
var errmsg = err.msg;
});
POST some data:
Sexhr({url: '/uploads', method: 'post', data: 'mai text file'})
.spread(function(res) {
console.log('done: ', res);
})
DELETE a resource:
// actual request will be "POST /posts/10?_method=DELETE"
Sexhr({url: '/posts/10', method: 'delete'})
.then(function() {
conosle.log('deleted!');
});
MIT.