Skip to content

Commit

Permalink
add apipaths map to clients
Browse files Browse the repository at this point in the history
  • Loading branch information
yunakim714 committed Oct 1, 2024
1 parent 36fa4b1 commit 0bd1ac3
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 24 deletions.
40 changes: 24 additions & 16 deletions src/static/helpers/slasHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@ import {isBrowser} from './environment';

import {
ShopperLogin,
ShopperLoginPathParameters,
ShopperLoginQueryParameters,
TokenRequest,
TokenResponse,
} from '../../lib/shopperLogin';
import ResponseError from '../responseError';
import TemplateURL from '../templateUrl';

export const stringToBase64 = isBrowser
? btoa
Expand Down Expand Up @@ -203,25 +206,30 @@ export async function authorizeIDP(
clientOptions.codeChallenge = await generateCodeChallenge(codeVerifier);
}

// Create a copy to override specific fetchOptions
const slasClientCopy = new ShopperLogin(slasClient.clientConfig);
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-assignment
const apiPath = ShopperLogin.apiPaths.authorizeCustomer;

const options = {
parameters: {
client_id: slasClient.clientConfig.parameters.clientId,
channel_id: slasClient.clientConfig.parameters.siteId,
...(clientOptions.codeChallenge && {
code_challenge: clientOptions.codeChallenge,
}),
hint: parameters.hint,
organizationId: slasClient.clientConfig.parameters.organizationId,
redirect_uri: parameters.redirectURI,
response_type: 'code',
...(parameters.usid && {usid: parameters.usid}),
},
const pathParams: ShopperLoginPathParameters = {
organizationId: slasClient.clientConfig.parameters.organizationId,
};

const url = await slasClientCopy.authorizeCustomer(options, true, true);
const queryParams: ShopperLoginQueryParameters = {
client_id: slasClient.clientConfig.parameters.clientId,
channel_id: slasClient.clientConfig.parameters.siteId,
...(clientOptions.codeChallenge && {
code_challenge: clientOptions.codeChallenge,
}),
hint: parameters.hint,
redirect_uri: parameters.redirectURI,
response_type: 'code',
...(parameters.usid && {usid: parameters.usid}),
};

const url = new TemplateURL(apiPath, slasClient.clientConfig.baseUri, {
pathParams,
queryParams,
origin: slasClient.clientConfig.proxy,
});

return {url: url.toString(), codeVerifier};
}
Expand Down
8 changes: 8 additions & 0 deletions templates/client.ts.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,14 @@ export class {{name.upperCamelCase}}<ConfigParameters extends {{{name.upperCamel

static readonly defaultBaseUri = "{{getBaseUriFromDocument model}}";

static readonly apiPaths = {
{{#each model.encodes.endPoints}}
{{#each operations}}
{{{name}}}: "{{{../path}}}",
{{/each}}
{{/each}}
};

constructor(config: ClientConfigInit<ConfigParameters>) {
const cfg = {...config}
if (!cfg.baseUri) cfg.baseUri = new.target.defaultBaseUri;
Expand Down
8 changes: 0 additions & 8 deletions templates/operations.ts.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@
* @param body - The data to send as the request body.
{{/if}}
* @param rawResponse - Set to true to return entire Response object instead of DTO.
* @param rawURL - Set to true to return the Request URL
* @returns A promise of type Response if rawResponse is true, a promise of type {{getReturnTypeFromOperation this}} otherwise.
* {{#if (eq (lowercase @root.metadata.categories.[CC Version Status].[0]) "beta")}}
* @beta
Expand All @@ -81,7 +80,6 @@
{{/if}}
}>,
rawResponse?: T,
rawURL?: T
): Promise<T extends true ? Response : {{getReturnTypeFromOperation this}}>;

/**
Expand All @@ -101,7 +99,6 @@
* @param body - The data to send as the request body.
{{/if}}
* @param rawResponse - Set to true to return entire Response object instead of DTO.
* @param rawURL - Set to true to return the Request URL
*
* @returns A promise of type Response if rawResponse is true, a promise of type {{getReturnTypeFromOperation this}} otherwise.
* {{#if (eq (lowercase @root.metadata.categories.[CC Version Status].[0]) "beta")}}
Expand All @@ -124,7 +121,6 @@
{{/if}}
}>,
rawResponse?: boolean,
rawURL?: boolean
): Promise<Response | {{getReturnTypeFromOperation this}} | TemplateURL> {
const optionParams = options?.parameters || ({} as Partial<NonNullable<NonNullable<typeof options>["parameters"]>>);
const configParams = this.clientConfig.parameters;
Expand Down Expand Up @@ -179,10 +175,6 @@
}
);

if (rawURL) {
return url
}

const headers: Record<string, string> = {
{{#if (isRequestWithPayload request)}}
"Content-Type": "{{{getMediaTypeFromRequest request}}}",
Expand Down

0 comments on commit 0bd1ac3

Please sign in to comment.