Skip to content

Commit 40e72d1

Browse files
Fix special characters encoding (#1)
* Creates test for filtering with spectial characters * Fixes encoding of special characters. Solution was taken from existing PR in original spraypaint repository: graphiti-api#104 Co-authored-by: Micael Carreira <[email protected]>
1 parent 6474ff6 commit 40e72d1

File tree

4 files changed

+17
-13
lines changed

4 files changed

+17
-13
lines changed

src/util/parameterize.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,9 @@ const parameterize = (obj: any, prefix?: string): string => {
3434
return str.join("&")
3535
}
3636

37-
// IE does not encode by default like other browsers
3837
const maybeEncode = (value: string): string => {
39-
var isBrowser =
40-
typeof window !== "undefined" &&
41-
typeof window.navigator.userAgent !== "undefined"
42-
const isIE = isBrowser && window.navigator.userAgent.match(/(MSIE|Trident)/)
43-
const isEncoded = typeof value === "string" && value.indexOf("%") !== -1
44-
const shouldEncode = isBrowser && isIE && !isEncoded
45-
return shouldEncode ? encodeURIComponent(value) : value
38+
var isEncoded = typeof value === "string" && decodeURI(value) !== value
39+
return isEncoded ? value : encodeURIComponent(value)
4640
}
4741

4842
export { parameterize as default }

test/integration/finders.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,7 @@ describe("Model finders", () => {
574574

575575
describe("#includes", () => {
576576
beforeEach(() => {
577-
fetchMock.get("http://example.com/api/v1/people?include=a.b,a.c.d", {
577+
fetchMock.get("http://example.com/api/v1/people?include=a.b%2Ca.c.d", {
578578
data: [
579579
{
580580
id: "2",

test/integration/relations.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ describe("Relations", () => {
5959
describe("#find()", () => {
6060
beforeEach(() => {
6161
fetchMock.get(
62-
"http://example.com/api/v1/authors/1?include=books,multi_words",
62+
"http://example.com/api/v1/authors/1?include=books%2Cmulti_words",
6363
generateMockResponse("authors")
6464
)
6565
})
@@ -103,7 +103,7 @@ describe("Relations", () => {
103103
describe("when keyCase is snake_case", () => {
104104
beforeEach(() => {
105105
fetchMock.get(
106-
"http://example.com/api/v1/non_fiction_authors/1?include=books,multi_words",
106+
"http://example.com/api/v1/non_fiction_authors/1?include=books%2Cmulti_words",
107107
generateMockResponse("non_fiction_authors")
108108
)
109109
})

test/unit/scope.test.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,17 @@ describe("Scope", () => {
342342
.stats({ total: "count" })
343343
.includes({ a: ["b", { c: "d" }] })
344344
expect(scope.toQueryParams()).to.eq(
345-
"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"
345+
"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"
346+
)
347+
})
348+
349+
it("correctly encodes special characters", () => {
350+
scope = scope.where({
351+
octothorp: "one # two",
352+
ampersand: "three & four"
353+
})
354+
expect(scope.toQueryParams()).to.eq(
355+
"filter[octothorp]=one%20%23%20two&filter[ampersand]=three%20%26%20four"
346356
)
347357
})
348358

@@ -366,7 +376,7 @@ describe("Scope", () => {
366376
})
367377

368378
it("casts arrays correctly", () => {
369-
scope = scope.extraParams({ foo: "bar,baz" })
379+
scope = scope.extraParams({ foo: ["bar", "baz"] })
370380
expect(<string>scope.toQueryParams()).to.eq("foo=bar,baz")
371381
})
372382

0 commit comments

Comments
 (0)