diff --git a/src/util/parameterize.ts b/src/util/parameterize.ts index f0c6cf1..2988e45 100644 --- a/src/util/parameterize.ts +++ b/src/util/parameterize.ts @@ -34,15 +34,9 @@ const parameterize = (obj: any, prefix?: string): string => { return str.join("&") } -// IE does not encode by default like other browsers const maybeEncode = (value: string): string => { - var isBrowser = - typeof window !== "undefined" && - typeof window.navigator.userAgent !== "undefined" - const isIE = isBrowser && window.navigator.userAgent.match(/(MSIE|Trident)/) - const isEncoded = typeof value === "string" && value.indexOf("%") !== -1 - const shouldEncode = isBrowser && isIE && !isEncoded - return shouldEncode ? encodeURIComponent(value) : value + var isEncoded = typeof value === "string" && decodeURI(value) !== value + return isEncoded ? value : encodeURIComponent(value) } export { parameterize as default } diff --git a/test/integration/finders.test.ts b/test/integration/finders.test.ts index 7ea5891..4c7eb7b 100644 --- a/test/integration/finders.test.ts +++ b/test/integration/finders.test.ts @@ -574,7 +574,7 @@ describe("Model finders", () => { describe("#includes", () => { beforeEach(() => { - fetchMock.get("http://example.com/api/v1/people?include=a.b,a.c.d", { + fetchMock.get("http://example.com/api/v1/people?include=a.b%2Ca.c.d", { data: [ { id: "2", diff --git a/test/integration/relations.test.ts b/test/integration/relations.test.ts index 1d5cace..bf3e272 100644 --- a/test/integration/relations.test.ts +++ b/test/integration/relations.test.ts @@ -59,7 +59,7 @@ describe("Relations", () => { describe("#find()", () => { beforeEach(() => { fetchMock.get( - "http://example.com/api/v1/authors/1?include=books,multi_words", + "http://example.com/api/v1/authors/1?include=books%2Cmulti_words", generateMockResponse("authors") ) }) @@ -103,7 +103,7 @@ describe("Relations", () => { describe("when keyCase is snake_case", () => { beforeEach(() => { fetchMock.get( - "http://example.com/api/v1/non_fiction_authors/1?include=books,multi_words", + "http://example.com/api/v1/non_fiction_authors/1?include=books%2Cmulti_words", generateMockResponse("non_fiction_authors") ) }) diff --git a/test/unit/scope.test.ts b/test/unit/scope.test.ts index c5dc5a8..f93b331 100644 --- a/test/unit/scope.test.ts +++ b/test/unit/scope.test.ts @@ -342,7 +342,17 @@ describe("Scope", () => { .stats({ total: "count" }) .includes({ a: ["b", { c: "d" }] }) expect(scope.toQueryParams()).to.eq( - "page[number]=2&page[size]=10&filter[foo_bar]=baz&sort=foo,-bar&fields[people]=name,age&stats[total]=count&include=a.b,a.c.d" + "page[number]=2&page[size]=10&filter[foo_bar]=baz&sort=foo,-bar&fields[people]=name,age&stats[total]=count&include=a.b%2Ca.c.d" + ) + }) + + it("correctly encodes special characters", () => { + scope = scope.where({ + octothorp: "one # two", + ampersand: "three & four" + }) + expect(scope.toQueryParams()).to.eq( + "filter[octothorp]=one%20%23%20two&filter[ampersand]=three%20%26%20four" ) }) @@ -366,7 +376,7 @@ describe("Scope", () => { }) it("casts arrays correctly", () => { - scope = scope.extraParams({ foo: "bar,baz" }) + scope = scope.extraParams({ foo: ["bar", "baz"] }) expect(scope.toQueryParams()).to.eq("foo=bar,baz") })