Skip to content
Merged
4 changes: 2 additions & 2 deletions src/static/version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
* SPDX-License-Identifier: BSD-3-Clause
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
export const USER_AGENT_HEADER = "user-agent";
export const USER_AGENT_VALUE = "commerce-sdk-isomorphic@3.3.0";
export const USER_AGENT_HEADER = 'user-agent';
export const USER_AGENT_VALUE = 'commerce-sdk-isomorphic@3.3.0';
22 changes: 19 additions & 3 deletions src/test/parameters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ describe('Parameters', () => {
expect(response).toEqual(MOCK_RESPONSE);
});

it('warns user when an invalid parameter is passed', async () => {
it('warns user when an unknown parameter is passed', async () => {
const searchClient = new ShopperSearch({
parameters: {
// shortCode is a base URI parameter, not path/query, so it *must* be in the config
Expand All @@ -191,17 +191,33 @@ describe('Parameters', () => {

const options = {
parameters: {
invalidParameter: 'this should prompt a warning',
unknownParam1: 'param1',
unknownParam2: 'param2',
},
body: {type: 'guest'},
};

nock.cleanAll();
nock(`https://${SHORT_CODE}.api.commercecloud.salesforce.com`)
.get(
`/search/shopper-search/v1/organizations/${ORGANIZATION_ID}/product-search`
)
.query({
siteId: SITE_ID,
unknownParam1: 'param1',
unknownParam2: 'param2',
})
.reply(200, MOCK_RESPONSE);

const warnSpy = jest.spyOn(console, 'warn');
const response = await searchClient.productSearch(options);

expect(response).toEqual(MOCK_RESPONSE);
expect(warnSpy).toHaveBeenCalledWith(
'Invalid Parameter for productSearch: invalidParameter'
'Found unknown parameter for productSearch: unknownParam1, adding as query parameter anyway'
);
expect(warnSpy).toHaveBeenCalledWith(
'Found unknown parameter for productSearch: unknownParam2, adding as query parameter anyway'
);
});
});
23 changes: 15 additions & 8 deletions templatesOas/apis.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ export class {{#lambda.titlecase}}{{#lambda.camelcase}}{{appName}}{{/lambda.came
{{/isHeaderParam}}
{{/isBodyParam}}
{{/allParams}}
} & { [key in `c_${string}`]: any }, ConfigParameters>
} & { [key: string]: any }, ConfigParameters>
headers?: { [key: string]: string },
{{#hasBodyParam}}
{{#bodyParam}}
Expand Down Expand Up @@ -282,7 +282,7 @@ export class {{#lambda.titlecase}}{{#lambda.camelcase}}{{appName}}{{/lambda.came
{{/isHeaderParam}}
{{/isBodyParam}}
{{/allParams}}
} & { [key in `c_${string}`]: any }, ConfigParameters>
} & { [key: string]: any }, ConfigParameters>
headers?: { [key: string]: string },
{{#hasBodyParam}}
{{#bodyParam}}
Expand Down Expand Up @@ -348,7 +348,7 @@ export class {{#lambda.titlecase}}{{#lambda.camelcase}}{{appName}}{{/lambda.came
{{/isHeaderParam}}
{{/isBodyParam}}
{{/allParams}}
} & { [key in `c_${string}`]: any }, ConfigParameters>,
} & { [key: string]: any }, ConfigParameters>,
headers?: { [key: string]: string },
{{#hasBodyParam}}
{{#bodyParam}}
Expand Down Expand Up @@ -390,7 +390,7 @@ export class {{#lambda.titlecase}}{{#lambda.camelcase}}{{appName}}{{/lambda.came
{{/required}}
{{/pathParams}}

const queryParams: {{#lambda.titlecase}}{{#lambda.camelcase}}{{appName}}{{/lambda.camelcase}}{{/lambda.titlecase}}QueryParameters & { [key in `c_${string}`]: any } = {};
const queryParams: {{#lambda.titlecase}}{{#lambda.camelcase}}{{appName}}{{/lambda.camelcase}}{{/lambda.titlecase}}QueryParameters & { [key: string]: any } = {};

{{#queryParams}}
if (optionParams["{{paramName}}"] !== undefined) {
Expand All @@ -406,11 +406,18 @@ export class {{#lambda.titlecase}}{{#lambda.camelcase}}{{appName}}{{/lambda.came
{{/queryParams}}

Object.keys(optionParams).forEach((key) => {
if(key.startsWith('c_') && optionParams[key as keyof typeof optionParams] !== undefined) {
queryParams[key as keyof typeof queryParams] = optionParams[key as keyof typeof optionParams]
} else if(!queryParams.hasOwnProperty(key) && !pathParams.hasOwnProperty(key)) {
console.warn(`Invalid Parameter for {{{nickname}}}: ${key}`)
const paramValue = optionParams[key as keyof typeof optionParams];
if(paramValue === undefined) {
return;
}
const isCustomParam = key.startsWith('c_')
if(!isCustomParam && ((key in queryParams) || (key in pathParams))) {
return;
}
if (!isCustomParam) {
console.warn(`Found unknown parameter for {{{nickname}}}: ${key}, adding as query parameter anyway`);
}
queryParams[key as keyof typeof queryParams] = paramValue;
})

const url = new TemplateURL(
Expand Down
Loading