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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# CHANGELOG

## v5.1.0

### Bug Fixes
- Fix incorrect encoding of multi-segment endpoint paths in `callCustomEndpoint` [#435](https://github.com/SalesforceCommerceCloud/commerce-sdk/pull/435)

## v5.0.0

### API Versions
Expand Down
109 changes: 109 additions & 0 deletions src/static/helpers/customApi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,4 +286,113 @@ describe("callCustomEndpoint", () => {
(runFetchPassedArgs[1].queryParameters as QueryParameters).siteId
).to.equal(copyOptions.parameters.siteId);
});

it("should support multi-segment paths even with special characters", async () => {
const { shortCode, organizationId } =
clientConfig.parameters as CommonParameters;
const { apiName } = options.customApiPathParameters;

const endpointPath = "multi/segment/path/Special,Summer%";
const expectedEndpointPath = "multi/segment/path/Special%2CSummer%25";

const nockBasePath = `https://${shortCode}.api.commercecloud.salesforce.com`;
const nockEndpointPath = `/custom/${apiName}/v2/organizations/${
organizationId as string
}/${expectedEndpointPath}`;
nock(nockBasePath).post(nockEndpointPath).query(true).reply(200);

const copyOptions = {
...options,
customApiPathParameters: {
...options.customApiPathParameters,
endpointPath,
},
};

await callCustomEndpoint({
options: copyOptions,
clientConfig,
rawResponse: true,
});

const runFetchPassedArgs = runFetchSpy.getCall(0).args;
expect(runFetchSpy.callCount).to.equal(1);

// Verify path parameters contain the unencoded segment values
const pathParams = runFetchPassedArgs[1]
.pathParameters as CustomApiParameters &
CommonParameters & {
endpointPathSegment0?: string;
endpointPathSegment1?: string;
endpointPathSegment2?: string;
endpointPathSegment3?: string;
};
expect(pathParams.endpointPathSegment0).to.equal("multi");
expect(pathParams.endpointPathSegment1).to.equal("segment");
expect(pathParams.endpointPathSegment2).to.equal("path");
expect(pathParams.endpointPathSegment3).to.equal("Special,Summer%");

// Verify the path template uses the segment placeholders
expect(runFetchPassedArgs[1].path).to.equal(
"/organizations/{organizationId}/{endpointPathSegment0}/{endpointPathSegment1}/{endpointPathSegment2}/{endpointPathSegment3}"
);
});

it("should normalize endpoint path with multiple slashes", async () => {
const { shortCode, organizationId } =
clientConfig.parameters as CommonParameters;
const { apiName } = options.customApiPathParameters;

const endpointPath = "multi/segment///path////Special,Summer%";
const expectedEndpointPath = "multi/segment/path/Special%2CSummer%25";

const nockBasePath = `https://${shortCode}.api.commercecloud.salesforce.com`;
const nockEndpointPath = `/custom/${apiName}/v2/organizations/${
organizationId as string
}/${expectedEndpointPath}`;
nock(nockBasePath).post(nockEndpointPath).query(true).reply(200);

const copyOptions = {
...options,
customApiPathParameters: {
apiName: options.customApiPathParameters.apiName,
apiVersion: options.customApiPathParameters.apiVersion,
// endpointPath is intentionally omitted so it falls back to clientConfig.parameters
},
};

await callCustomEndpoint({
options: copyOptions,
clientConfig: {
...clientConfig,
parameters: {
...clientConfig.parameters,
endpointPath,
},
},
rawResponse: true,
});

const runFetchPassedArgs = runFetchSpy.getCall(0).args;
expect(runFetchSpy.callCount).to.equal(1);

// Verify path parameters contain the normalized segment values (empty segments filtered out)
const pathParams = runFetchPassedArgs[1]
.pathParameters as CustomApiParameters &
CommonParameters & {
endpointPathSegment0?: string;
endpointPathSegment1?: string;
endpointPathSegment2?: string;
endpointPathSegment3?: string;
};
expect(pathParams.endpointPathSegment0).to.equal("multi");
expect(pathParams.endpointPathSegment1).to.equal("segment");
expect(pathParams.endpointPathSegment2).to.equal("path");
expect(pathParams.endpointPathSegment3).to.equal("Special,Summer%");

// Verify the path template uses the segment placeholders (no extra slashes)
expect(runFetchPassedArgs[1].path).to.equal(
"/organizations/{organizationId}/{endpointPathSegment0}/{endpointPathSegment1}/{endpointPathSegment2}/{endpointPathSegment3}"
);
});
});
37 changes: 34 additions & 3 deletions src/static/helpers/customApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const contentTypeHeaderExists = (
export type CustomApiParameters = {
organizationId?: string;
shortCode?: string;
endpointName?: string;
endpointPath?: string;
apiName?: string;
apiVersion?: string;
};
Expand Down Expand Up @@ -137,10 +137,41 @@ export const callCustomEndpoint = async (args: {
};
}

const currentEndpointPath = (options?.customApiPathParameters?.endpointPath ||
clientConfig.parameters?.endpointPath) as string;
let newEndpointPath = currentEndpointPath;
const endpointPathSegments = {} as Record<string, string>;

// Normalize and template the endpointPath so each segment is encoded as a path param.
// Example:
// currentEndpointPath: "action/categories/Special,Summer" ->
// endpointPathParams: { endpointPathSegment0: "action", endpointPathSegment1: "categories", endpointPathSegment2: "Special,Summer" }
// newEndpointPath: "{endpointPathSegment0}/{endpointPathSegment1}/{endpointPathSegment2}/"
// The TemplateURL will then encode the path parameters and construct the URL with the encoded path parameters
// The resulting endpointPath will be: "actions/categories/Special%2CSummer"
if (currentEndpointPath.includes("/")) {
// Normalize endpoint path by removing multiple consecutive slashes
const segments = currentEndpointPath
.split("/")
.filter((segment) => segment !== "");
newEndpointPath = "";
segments.forEach((segment: string, index: number) => {
const key = `endpointPathSegment${index}`;
endpointPathSegments[key] = segment;
newEndpointPath += `{${key}}/`;
});
// Remove the trailing slash added after the last segment
// as TemplateURL does not expect a trailing slash
newEndpointPath = newEndpointPath.slice(0, -1);
}

const sdkOptions = {
client: { clientConfig: clientConfigCopy },
path: "/organizations/{organizationId}/{endpointPath}",
pathParameters: pathParams as PathParameters,
path: `/organizations/{organizationId}/${newEndpointPath}`,
pathParameters: {
...pathParams,
...endpointPathSegments,
} as PathParameters,
queryParameters: optionsCopy.parameters,
headers: optionsCopy.headers,
rawResponse,
Expand Down