Skip to content

Commit

Permalink
🏭 Accept string argument in .query() #20
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben Doherty authored and elbywan committed May 24, 2018
1 parent 57c6c6f commit b53cca3
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 16 deletions.
6 changes: 4 additions & 2 deletions README.md
Expand Up @@ -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")
Expand All @@ -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)
Expand Down
35 changes: 21 additions & 14 deletions src/wretcher.ts
Expand Up @@ -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) })
}

Expand Down Expand Up @@ -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) => {
Expand Down
3 changes: 3 additions & 0 deletions test/wretch.spec.ts
Expand Up @@ -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() {
Expand Down

0 comments on commit b53cca3

Please sign in to comment.