diff --git a/README.md b/README.md index d993346..f554755 100644 --- a/README.md +++ b/README.md @@ -285,9 +285,9 @@ await blogs.url(`/${id}`).delete().res() const noMoreBlogs = blogs.url("http://mywebsite.org/", true) ``` -#### query(qp: Object) +#### query(qp: object | string) -Converts a javascript object to query parameters, then appends this query string to the current url. +Converts a javascript object to query parameters, then appends this query string to the current url. String values are used as the query string verbatim. ```js let w = wretch("http://example.com") @@ -296,6 +296,8 @@ w = w.query({ a: 1, b: 2 }) // url is now http://example.com?a=1&b=2 w = w.query({ c: 3, d: [4, 5] }) // url is now http://example.com?c=3&d=4&d=5 +w = w.query("five&six&seven=eight") +// url is now http://example.com?five&six&seven=eight ``` #### options(options: Object, mixin: boolean = true) diff --git a/src/wretcher.ts b/src/wretcher.ts index 2bc18e6..6a51658 100644 --- a/src/wretcher.ts +++ b/src/wretcher.ts @@ -83,14 +83,17 @@ export class Wretcher { * Converts a javascript object to query parameters, * then appends this query string to the current url. * + * If given a string, use the string as the query verbatim. + * * ``` * let w = wretch("http://example.com") // url is http://example.com * w = w.query({ a: 1, b : 2 }) // url is now http://example.com?a=1&b=2 + * w = w.query("foo-bar-baz-woz") // url is now http://example.com?foo-bar-baz-woz * ``` * - * @param qp An object which will be converted. + * @param qp An object which will be converted, or a string which will be used verbatim. */ - query(qp: object) { + query(qp: object | string) { return this.selfFactory({ url: appendQueryParams(this._url, qp) }) } @@ -245,20 +248,24 @@ export class Wretcher { // Internal helpers -const appendQueryParams = (url: string, qp: object) => { - const usp = conf.polyfill("URLSearchParams", { instance: true }) - const index = url.indexOf("?") - for(const key in qp) { - if(qp[key] instanceof Array) { - for(const val of qp[key]) - usp.append(key, val) - } else { - usp.append(key, qp[key]) +const appendQueryParams = (url: string, qp: object | string) => { + let queryString + + if(typeof qp === "string") { + queryString = qp + } else { + const usp = conf.polyfill("URLSearchParams", { instance: true }) + for(const key in qp) { + if(qp[key] instanceof Array) { + for(const val of qp[key]) + usp.append(key, val) + } else { + usp.append(key, qp[key]) + } } + queryString = usp.toString() } - return ~index ? - `${url.substring(0, index)}?${usp.toString()}` : - `${url}?${usp.toString()}` + return `${url.split("?")[0]}?${queryString}` } const convertFormData = (formObject: object) => { diff --git a/test/wretch.spec.ts b/test/wretch.spec.ts index b9cf733..347dc39 100644 --- a/test/wretch.spec.ts +++ b/test/wretch.spec.ts @@ -303,6 +303,9 @@ describe("Wretch", function() { const obj5 = obj4.query({c: 6, d: [7, 8]}) expect(obj4["_url"]).toBe(`${_URL}?a=1%21&b=2`) expect(obj5["_url"]).toBe(`${_URL}?c=6&d=7&d=8`) + const obj6 = obj5.query('Literal[]=Query&String') + expect(obj5["_url"]).toBe(`${_URL}?c=6&d=7&d=8`) + expect(obj6["_url"]).toBe(`${_URL}?Literal[]=Query&String`) }) it("should set the Accept header", async function() {