Skip to content

Commit

Permalink
Export parameter keys (@W-15676813@) (SalesforceCommerceCloud#158)
Browse files Browse the repository at this point in the history
* Export parameter keys (and the required ones too)

* Bump up maxSize limits

* Add whitespace

* Reorganize paramKeys

* Update bundle size limit

* Simplify `as const`

* Add a test
  • Loading branch information
vmarta authored Jun 6, 2024
1 parent 0e89af8 commit 361aed9
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 9 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -177,11 +177,11 @@
"bundlesize": [
{
"path": "lib/**/*.js",
"maxSize": "46 kB"
"maxSize": "48 kB"
},
{
"path": "commerce-sdk-isomorphic-with-deps.tgz",
"maxSize": "400 kB"
"maxSize": "410 kB"
}
],
"proxy": "https://SHORTCODE.api.commercecloud.salesforce.com"
Expand Down
4 changes: 4 additions & 0 deletions scripts/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ export function registerPartials(): void {
'operationsPartial',
path.join(TEMPLATE_DIRECTORY, 'operations.ts.hbs')
);
registerPartial(
'paramKeysPartial',
path.join(TEMPLATE_DIRECTORY, 'paramKeys.ts.hbs')
);
}

function addTemplates(apis: ApiMetadata, outputBasePath: string): ApiMetadata {
Expand Down
7 changes: 7 additions & 0 deletions src/test/parameters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ describe('Parameters', () => {
.reply(200, MOCK_RESPONSE)
);

it('has a list of parameter keys (and the required ones)', () => {
// eslint-disable-next-line
expect(ShopperSearch.paramKeys?.productSearch).toBeDefined();
// eslint-disable-next-line
expect(ShopperSearch.paramKeys?.productSearchRequired).toBeDefined();
});

it('can all be specified in config (no method parameters object)', async () => {
const searchClient = new ShopperSearch({
parameters: {
Expand Down
11 changes: 5 additions & 6 deletions templates/client.ts.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,12 @@ export type {{name.upperCamelCase}}Parameters = {{name.upperCamelCase}}PathParam
*
* <span style="font-size:.7em; display:block; text-align: right">
* API Version: {{metadata.version}}<br />
* Last Updated: {{metadata.updatedDate}}<br />
* Last Updated: {{metadata.updatedDate}}<br />
* </span>
{{#if (eq (lowercase metadata.categories.[CC Version Status].[0]) "beta")}}
* @beta
* {{#if (eq (lowercase metadata.categories.[CC Version Status].[0]) "beta")}}
* @beta
* {{/if}}
*

*
*/
export class {{name.upperCamelCase}}<ConfigParameters extends {{{name.upperCamelCase}}}Parameters & Record<string, unknown>> {
// baseUri is not required on ClientConfig, but we know that we provide one in the class constructor
Expand All @@ -101,6 +100,6 @@ export class {{name.upperCamelCase}}<ConfigParameters extends {{{name.upperCamel
this.clientConfig = new ClientConfig(cfg) as ClientConfig<ConfigParameters> & { baseUri: string };
}

{{> paramKeysPartial model}}
{{> operationsPartial model}}

}
3 changes: 2 additions & 1 deletion templates/operations.ts.hbs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{{#each encodes.endPoints}}
{{#each operations}}

/**
* {{{formatForTsDoc description}}}
*
Expand Down Expand Up @@ -193,7 +194,7 @@
method: "{{loud method}}",
headers,
{{#if (isRequestWithPayload request)}}body: this.clientConfig.transformRequest(options.body, headers){{/if}}
},
},
this.clientConfig,
rawResponse
)
Expand Down
24 changes: 24 additions & 0 deletions templates/paramKeys.ts.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
static readonly paramKeys = {
{{#each encodes.endPoints}}
{{#each operations}}
{{{name}}}: [
{{#each ../parameters}}
'{{{name}}}',
{{/each}}
{{#each request.queryParameters}}
'{{{name}}}',
{{/each}}
],
{{{name}}}Required: [
{{#each ../parameters}}
'{{{name}}}',
{{/each}}
{{#each request.queryParameters}}
{{#if (is required "true")}}
'{{{name}}}',
{{/if}}
{{/each}}
],
{{/each}}
{{/each}}
} as const;

0 comments on commit 361aed9

Please sign in to comment.