Skip to content

Commit

Permalink
Fix special characters encoding (#1)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
mdcarreira and Micael Carreira authored Jan 26, 2023
1 parent 6474ff6 commit 40e72d1
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 13 deletions.
10 changes: 2 additions & 8 deletions src/util/parameterize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
2 changes: 1 addition & 1 deletion test/integration/finders.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions test/integration/relations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
)
})
Expand Down Expand Up @@ -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")
)
})
Expand Down
14 changes: 12 additions & 2 deletions test/unit/scope.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
})

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

it("casts arrays correctly", () => {
scope = scope.extraParams({ foo: "bar,baz" })
scope = scope.extraParams({ foo: ["bar", "baz"] })
expect(<string>scope.toQueryParams()).to.eq("foo=bar,baz")
})

Expand Down

0 comments on commit 40e72d1

Please sign in to comment.