Skip to content

Commit 6aabb1c

Browse files
committed
encode path parameters by default
1 parent 841a931 commit 6aabb1c

File tree

5 files changed

+28
-7
lines changed

5 files changed

+28
-7
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ jobs:
99
linux-tests:
1010
strategy:
1111
matrix:
12-
# TODO: remove older node versions
13-
node: [10, 12, 14, 16, 18, 20, 22]
12+
node: [20, 22]
1413
fail-fast: false
1514
runs-on: ubuntu-latest
1615
steps:

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# CHANGELOG
22

3+
## 2.0.0
4+
* Path parameter special characters are encoded by default
5+
36
## 1.8.0
47
* Make request `body` for HTTP endpoint calls optional [#117](https://github.com/SalesforceCommerceCloud/commerce-sdk-core/pull/117/files)
58

src/base/resource.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,21 @@ export class Resource {
4141
* @returns Path with actual parameters
4242
*/
4343
substitutePathParameters(path = "", parameters: PathParameters = {}): string {
44+
const encodedPathParams: PathParameters = {};
45+
46+
Object.keys(parameters || {}).forEach((key) => {
47+
const value = parameters?.[key];
48+
if (value) {
49+
encodedPathParams[key] = encodeURIComponent(value);
50+
}
51+
});
52+
4453
return path.replace(/\{([^}]+)\}/g, (_entireMatch, param) => {
45-
if (parameters.hasOwnProperty(param) && parameters[param] !== undefined) {
46-
return parameters[param];
54+
if (
55+
encodedPathParams.hasOwnProperty(param) &&
56+
encodedPathParams[param] !== undefined
57+
) {
58+
return encodedPathParams[param];
4759
}
4860
throw new Error(
4961
`Failed to find a value for required path parameter '${param}'`

src/base/staticClient.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -263,9 +263,7 @@ export async function _delete(options: SdkFetchOptions): Promise<object> {
263263
* @returns Either the Response object or the DTO inside it wrapped in a promise,
264264
* depending upon options.rawResponse
265265
*/
266-
export async function _patch(
267-
options: SdkFetchOptions
268-
): Promise<object> {
266+
export async function _patch(options: SdkFetchOptions): Promise<object> {
269267
return runFetch("patch", options);
270268
}
271269

test/resource.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,4 +200,13 @@ describe("Resource class tests", () => {
200200
// URI decoded: baseUri/path?expand=availability,images&refine=price=(0..150)&refine=c_refinementColor=Red
201201
);
202202
});
203+
204+
it("returns correct url with path param with special characters", () => {
205+
assert.strictEqual(
206+
new Resource("https://example.com", {}, "/path/with/{special}/chars", {
207+
special: "test!@#$%",
208+
}).toString(),
209+
"https://example.com/path/with/test!%40%23%24%25/chars"
210+
);
211+
});
203212
});

0 commit comments

Comments
 (0)