Skip to content

Commit

Permalink
Add support for options.qs
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesplease committed Apr 7, 2017
1 parent 14ef09a commit ebba3a9
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ type XhrOptions = String | {
withCredentials: Boolean?,
responseType: String?,
beforeSend: Function?
qs: Any?
}
xhr := (XhrOptions, Callback<Response>) => Request
```
Expand Down Expand Up @@ -193,6 +194,17 @@ A function being called right before the `send` method of the `XMLHttpRequest` o
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.qs`
A value to be transformed into a query string. This library does not provide
a parser for `options.qs` out of the box: you must define it yourself as
`xhr.queryStringStringify`.
If `options.qs` is defined, and `xhr.queryStringStringify` is not, then an
Error will be thrown.
For more, see [Query string support](#query-string-support).
## FAQ
- Why is my server's JSON response not parsed? I returned the right content-type.
Expand All @@ -216,6 +228,8 @@ xhr({
}
})
```
- How can I support query strings?
- See [Query string support](#query-string-support)
## Mocking Requests
You can override the constructor used to create new requests for testing. When you're making a new request:
Expand All @@ -231,6 +245,31 @@ xhr.XMLHttpRequest = MockXMLHttpRequest
xhr.XDomainRequest = MockXDomainRequest
```
## Query string support
There are many ways to stringify query parameters; consequently, `xhr` makes no
assumptions about how to handle them, and does not support the `qs` option out
of the box.
To support the `qs` option, you must define an `xhr.queryStringStringify`
function. This function accepts the value of `options.qs` as its first argument,
and returns a string that is appended to the URL.
You do not need to include a leading "?" in the value that you return from
`xhr.queryStringStringify`.
```js
var xhr = require('xhr')
var qs = require('qs')

xhr.queryStringStringify = qs.stringify

xhr.get('/foo', {
qs: {
bar: true
}
})
```
## MIT Licenced
[1]: http://xhr.spec.whatwg.org/#the-send()-method
Expand Down
11 changes: 10 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ var xtend = require("xtend")
module.exports = createXHR
createXHR.XMLHttpRequest = window.XMLHttpRequest || noop
createXHR.XDomainRequest = "withCredentials" in (new createXHR.XMLHttpRequest()) ? createXHR.XMLHttpRequest : window.XDomainRequest
createXHR.queryStringStringify = null // Define this as a function to support the `qs` option

forEachArray(["get", "put", "post", "patch", "head", "delete"], function(method) {
createXHR[method === "delete" ? "del" : method] = function(uri, options, callback) {
Expand Down Expand Up @@ -139,9 +140,17 @@ function _createXHR(options) {
}
}

var qsStringifyDefined = isFunction(createXHR.queryStringStringify);

if (options.qs && !qsStringifyDefined) {
throw new Error("You passed a 'qs' option, but did not define an 'xhr.queryStringStringify' function.\nYou must either omit the 'qs' option, or define 'xhr.queryStringStringify'.")
}

var qs = options.qs && qsStringifyDefined ? '?' + createXHR.queryStringStringify(options.qs) : '';

var key
var aborted
var uri = xhr.url = options.uri || options.url
var uri = xhr.url = (options.uri || options.url) + qs
var method = xhr.method = options.method || "GET"
var body = options.body || options.data
var headers = xhr.headers = options.headers || {}
Expand Down

0 comments on commit ebba3a9

Please sign in to comment.