From ebba3a93f0ac87ac8d9fe0c0fcb5320eb405d351 Mon Sep 17 00:00:00 2001 From: Jmeas Date: Thu, 6 Apr 2017 20:44:37 -0700 Subject: [PATCH 1/2] Add support for options.qs --- README.md | 39 +++++++++++++++++++++++++++++++++++++++ index.js | 11 ++++++++++- 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index a2e7f85..b968aec 100644 --- a/README.md +++ b/README.md @@ -60,6 +60,7 @@ type XhrOptions = String | { withCredentials: Boolean?, responseType: String?, beforeSend: Function? + qs: Any? } xhr := (XhrOptions, Callback) => Request ``` @@ -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. @@ -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: @@ -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 diff --git a/index.js b/index.js index cfa102a..4d421f2 100644 --- a/index.js +++ b/index.js @@ -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) { @@ -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 || {} From 807745f5654bdf6d49ca60c6427f5424a4759d8e Mon Sep 17 00:00:00 2001 From: Jmeas Date: Fri, 7 Apr 2017 09:57:12 -0700 Subject: [PATCH 2/2] fixup! Add support for options.qs --- README.md | 16 ++++++++-------- index.js | 8 ++++---- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index b968aec..70e1d06 100644 --- a/README.md +++ b/README.md @@ -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). @@ -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: { diff --git a/index.js b/index.js index 4d421f2..bd5911d 100644 --- a/index.js +++ b/index.js @@ -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) { @@ -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