Skip to content

Commit

Permalink
🐛 Prevent appending an extra ?/& for empty query strings. #114
Browse files Browse the repository at this point in the history
  • Loading branch information
elbywan committed Aug 6, 2021
1 parent b65878f commit 15464a8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/wretcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ export class Wretcher {
// Internal helpers

const appendQueryParams = (url: string, qp: object | string, replace: boolean) => {
let queryString
let queryString : string

if (typeof qp === "string") {
queryString = qp
Expand All @@ -322,6 +322,10 @@ const appendQueryParams = (url: string, qp: object | string, replace: boolean) =
}

const split = url.split("?")

if (!queryString)
return replace ? split[0] : url

if (replace || split.length < 2)
return split[0] + "?" + queryString

Expand Down
10 changes: 10 additions & 0 deletions test/node/wretch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,16 @@ describe("Wretch", function () {
expect(await wretch(`${_URL}/json/null`).get().json(_ => true)).toEqual(true)
expect(await wretch(`${_URL}/json/null`).get().json(_ => false)).toEqual(false)
})

it("should not append an extra character (&/?) when trying to append or replace empty query params", function() {
const w = wretch(_URL)
expect(w.query("")._url).toBe(_URL)
expect(w.query("", true)._url).toBe(_URL)
expect(w.query("a=1").query("", true)._url).toBe(_URL)
expect(w.query({})._url).toBe(_URL)
expect(w.query({}, true)._url).toBe(_URL)
expect(w.query({a: 1}).query({}, true)._url).toBe(_URL)
})
})

describe("Mix", function () {
Expand Down

0 comments on commit 15464a8

Please sign in to comment.