Skip to content

Commit

Permalink
Merge pull request #131 from ciprian-dragomir/feature/#129-use-standa…
Browse files Browse the repository at this point in the history
…rd-url-encoding

Change applyQuery to use standard encoding for query parameters.
  • Loading branch information
janhommes authored Mar 29, 2022
2 parents f79d59e + 757716d commit 1449db5
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 30 deletions.
30 changes: 11 additions & 19 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions src/ORequest.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { ORequest } from "./ORequest";

describe("applyQuery", () => {
it('does not use "application/x-www-form-urlencoded" encoding', () => {
expect(new ORequest(new URL("https://example.com"), {}).applyQuery({
$top: 4,
$filter: "foo eq bar or startsWith(foo, 'baz')",
}).url.href)
// Space is encoded as "%20" and not "+"
.toBe("https://example.com/?%24top=4&%24filter=foo%20eq%20bar%20or%20startsWith%28foo%2C%20%27baz%27%29");
});

it("considers existing query parameters, overwrites with entries from queries", () => {
expect(new ORequest(new URL("https://example.com?$top=5&$orderby=Some+Field&$search=some%20term"), {}).applyQuery({
$top: 4,
$filter: "foo eq bar or startsWith(foo, 'baz')",
}).url.href)
.toBe("https://example.com/?%24top=4&%24filter=foo%20eq%20bar%20or%20startsWith%28foo%2C%20%27baz%27%29&%24orderby=Some%20Field&%24search=some%20term");
});
});
23 changes: 14 additions & 9 deletions src/ORequest.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { OdataQuery } from "./OdataQuery";

const encodeURIComponentStrict =
(str: string) => encodeURIComponent(str)
.replace(/[!'()*]/g, (c) => `%${c.charCodeAt(0).toString(16).toUpperCase()}`);

export class ORequest {
public url: URL;

Expand All @@ -16,15 +20,16 @@ export class ORequest {
return fetch(req, this.config);
}

public applyQuery(query?: OdataQuery) {
for (const key in query) {
if (query.hasOwnProperty(key)) {
if (this.url.searchParams.get(key)) {
this.url.searchParams.set(key, query[key]);
} else {
this.url.searchParams.append(key, query[key]);
}
public applyQuery(query: OdataQuery = {}) {
this.url.searchParams.forEach((value, key) => {
if (!query.hasOwnProperty(key)) {
query[key] = value;
}
}
});

this.url.search = Object.entries(query)
.map(([key, value]) => `${encodeURIComponentStrict(key)}=${encodeURIComponentStrict(value)}`)
.join("&");
return this;
}
}
4 changes: 2 additions & 2 deletions src/o.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ describe("Instant request", () => {

// expect
expect(decodeURIComponent((data as Response).url)).toContain(
"People?$top=1&$filter=FirstName+eq+'john'"
"People?$top=1&$filter=FirstName eq 'john'"
);
});

Expand Down Expand Up @@ -247,7 +247,7 @@ describe("Request handling", () => {

// expect
expect(decodeURIComponent(req[0].url)).toContain(
"People?$top=1&$filter=FirstName+eq+'john'"
"People?$top=1&$filter=FirstName eq 'john'"
);
});

Expand Down

0 comments on commit 1449db5

Please sign in to comment.