Skip to content

Commit

Permalink
fixup! 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 ebba3a9 commit 807745f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,10 @@ Pass an `XMLHttpRequest` object (or something that acts like one) to use instead
### `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`.
a serializer for `options.qs` out of the box: you must define it yourself as
`xhr.qsSerialize`.
If `options.qs` is defined, and `xhr.queryStringStringify` is not, then an
If `options.qs` is defined, and `xhr.qsSerialize` is not, then an
Error will be thrown.
For more, see [Query string support](#query-string-support).
Expand Down Expand Up @@ -250,18 +250,18 @@ 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.
To support the `qs` option, define an `xhr.qsSerialize` 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`.
`xhr.qsSerialize`.
```js
var xhr = require('xhr')
var qs = require('qs')

xhr.queryStringStringify = qs.stringify
xhr.qsSerialize = qs.stringify

xhr.get('/foo', {
qs: {
Expand Down
8 changes: 4 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +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
createXHR.qsSerialize = 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 @@ -140,13 +140,13 @@ function _createXHR(options) {
}
}

var qsStringifyDefined = isFunction(createXHR.queryStringStringify);
var qsStringifyDefined = isFunction(createXHR.qsSerialize);

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'.")
throw new Error("To use the 'qs' option, first define an 'xhr.qsSerialize' function.")
}

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

var key
var aborted
Expand Down

0 comments on commit 807745f

Please sign in to comment.