diff --git a/src/wretcher.ts b/src/wretcher.ts index cd67c27..9e24760 100644 --- a/src/wretcher.ts +++ b/src/wretcher.ts @@ -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 @@ -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 diff --git a/test/node/wretch.spec.ts b/test/node/wretch.spec.ts index bba042b..873700e 100644 --- a/test/node/wretch.spec.ts +++ b/test/node/wretch.spec.ts @@ -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 () {