JSPM

  • Created
  • Published
  • Downloads 1939930
  • Score
    100M100P100Q224285F

small xhr abstraction

Package Exports

  • xhr

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (xhr) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

xhr

A small xhr wrapper

Example

var xhr = require("xhr")

xhr({
    body: someJSONString,
    uri: "/foo",
    headers: {
        "Content-Type": "application/json"
    }
}, function (err, resp, body) {
    // resp === xhr
    // check resp.body or resp.statusCode
})

var req = xhr(options, callback)

type XhrOptions = String | {
    cors: Boolean?,
    uri: String,
    method: String?,
    timeout: Number?,
    headers: Object?,
    body: String?,
    json: Object?
}
xhr := (XhrOptions, Callback<Response>) => Request

the returned object is either an XMLHttpRequest instance or an XDomainRequest instance (if on IE8/IE9 && options.cors is set to true)

Your callback will be called once with the arguments ( Error, response , body ) where response is a response object containing { statusCode, body } and other properties of the XHR request and body will be either xhr.response, xhr.responseText or xhr.responseXML depending on the request type.

Your callback will be called with an Error if the resulting status of the request is either 0, 4xx or 5xx

If options is a string then it's a short hand for { method: "GET", uri: string }

options.method

Specify the method the XMLHttpRequest should be opened with. Passed to xhr.open. Defaults to "GET"

options.cors

Specify whether this is a cross domain request. Used in IE<10 to use XDomainRequest instead of XMLHttpRequest.

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 xhr.send should work

options.uri

The uri to send a request too. Passed to xhr.open

options.headers

An object of headers that should be set on the request. The key, value pair is passed to xhr.setRequestHeader

options.timeout

A numeric timeout to use for this xhr request. Defaults to 5 seconds.

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