Skip to content

Commit

Permalink
Fixes encoding of special characters.
Browse files Browse the repository at this point in the history
Solution was taken from existing PR in original spraypaint repository:
graphiti-api#104
  • Loading branch information
Micael Carreira committed Jan 26, 2023
1 parent 85c0c29 commit 8a502ed
Show file tree
Hide file tree
Showing 4 changed files with 7 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
4 changes: 2 additions & 2 deletions test/unit/scope.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ 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"
)
})

Expand Down Expand Up @@ -376,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 8a502ed

Please sign in to comment.