Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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