Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Add support for custom query parameters @W-13974911@ #139

Merged
merged 7 commits into from
Jan 16, 2024
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
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
linux-tests:
strategy:
matrix:
node: [10, 12, 14, 16]
node: [10, 12, 14, 16, 18, 20]
fail-fast: false
runs-on: ubuntu-latest
steps:
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ The Salesforce Commerce SDK (Isomorphic) allows easy interaction with the B2C Co

### Requirements

- Node `^12.x`, `^14.x`, or `^16.x`
- Node `^12.x`, `^14.x`, `^16.x`, `^18.x` or `^20.x`


### Installation

Expand Down
36 changes: 36 additions & 0 deletions src/test/parameters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,40 @@ describe('Parameters', () => {
new Error('Missing required path parameter: organizationId')
);
});

it('allow custom query params', async () => {
const customersClient = new ShopperCustomers({
parameters: {
// shortCode is a base URI parameter, not path/query, so it *must* be in the config
shortCode: SHORT_CODE,
},
});

const options = {
parameters: {
siteId: SITE_ID,
organizationId: ORGANIZATION_ID,
clientId: CLIENT_ID,
c_validCustomParam: 'custom_param',
invalidParam: 'invalid_param',
},
body: {type: 'guest'},
};

nock(`https://${SHORT_CODE}.api.commercecloud.salesforce.com`)
.post(
`/customer/shopper-customers/v1/organizations/${ORGANIZATION_ID}/customers/actions/login`
)
.query({
siteId: SITE_ID,
clientId: CLIENT_ID,
// expect `c_validCustomParam` but not `invalidParam`
c_validCustomParam: 'custom_param',
})
.reply(200, MOCK_RESPONSE);

const response = await customersClient.authorizeCustomer(options);

expect(response).toEqual(MOCK_RESPONSE);
Comment on lines +144 to +177
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice test case!

});
});
14 changes: 10 additions & 4 deletions templates/operations.ts.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
{{#each request.queryParameters}}
{{{name}}}{{#if (not (is required "true"))}}?{{/if}}: {{{ getTypeFromParameter this}}}
{{/each}}
}, ConfigParameters>,
} & { [key in `c_${string}`]: any }, ConfigParameters>,
headers?: { [key: string]: string },
{{#if (isRequestWithPayload request)}}
body: {{{getPayloadTypeFromRequest request}}}
Expand Down Expand Up @@ -72,7 +72,7 @@
{{#each request.queryParameters}}
{{{name}}}{{#if (not (is required "true"))}}?{{/if}}: {{{ getTypeFromParameter this}}}
{{/each}}
}, ConfigParameters>,
} & { [key in `c_${string}`]: any }, ConfigParameters>,
headers?: { [key: string]: string },
{{#if (isRequestWithPayload request)}}
body: {{{getPayloadTypeFromRequest request}}}
Expand Down Expand Up @@ -113,7 +113,7 @@
{{#each request.queryParameters}}
{{{name}}}{{#if (not (is required "true"))}}?{{/if}}: {{{ getTypeFromParameter this}}}
{{/each}}
}, ConfigParameters>,
} & { [key in `c_${string}`]: any }, ConfigParameters>,
headers?: { [key: string]: string },
{{#if (isRequestWithPayload request)}}
body: {{{getPayloadTypeFromRequest request}}}
Expand Down Expand Up @@ -142,7 +142,7 @@
{{/if}}
{{/each}}

const queryParams: {{{@root.name.upperCamelCase}}}QueryParameters = {};
const queryParams: {{{@root.name.upperCamelCase}}}QueryParameters & { [key in `c_${string}`]: any } = {};
{{#each request.queryParameters}}
if (optionParams["{{{name}}}"] !== undefined) {
queryParams["{{{name}}}"] = optionParams["{{{name}}}"];
Expand All @@ -156,6 +156,12 @@
{{/if}}
{{/each}}

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]
}
})

const url = new TemplateURL(
"{{{../path}}}",
this.clientConfig.baseUri,
Expand Down