Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 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: 3 additions & 2 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,10 @@
}
},
{
"files": ["./src/static/helpers/slasHelper.ts"],
"files": ["./src/static/helpers/slasHelper.ts", "./src/static/helpers/types.ts"],
"rules": {
"max-lines": "off"
"max-lines": "off",
"camelcase": "off"
}
}
],
Expand Down
3 changes: 1 addition & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ jobs:
linux-tests:
strategy:
matrix:
# TODO: remove older versions of node
node: [12, 14, 16, 18, 20, 22]
node: [20, 22]
fail-fast: false
runs-on: ubuntu-latest
steps:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ servers:
variables:
shortCode:
default: shortCode
x-sdk-classname: ShopperContexts
paths:
/organizations/{organizationId}/shopper-context/{usid}:
get:
Expand Down
7 changes: 6 additions & 1 deletion scripts/generate-oas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ const VERSION_TEMPLATE_LOCATION = path.join(
);

function kebabToCamelCase(str: string): string {
return str.replace(/-([a-z])/g, (match, letter: string) => letter.toUpperCase());
return str.replace(/-([a-z])/g, (match, letter: string) =>
letter.toUpperCase()
);
}

export function resolveApiName(name: string): string {
Expand All @@ -43,6 +45,9 @@ export function resolveApiName(name: string): string {
if (name === 'Shopper Seo OAS') {
return 'ShopperSEO';
}
if (name === 'Shopper Context OAS') {
return 'ShopperContexts';
}
return name.replace(/\s+/g, '').replace('OAS', '');
}

Expand Down
5 changes: 4 additions & 1 deletion src/static/helpers/slasHelper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,10 +233,13 @@

// There should be no code_challenge for private client
const expectedReqOptions = {
accessToken: "access_token",

Check warning on line 236 in src/static/helpers/slasHelper.test.ts

View workflow job for this annotation

GitHub Actions / linux-tests (20)

Replace `"access_token"` with `'access_token'`

Check warning on line 236 in src/static/helpers/slasHelper.test.ts

View workflow job for this annotation

GitHub Actions / linux-tests (22)

Replace `"access_token"` with `'access_token'`
Copy link
Contributor

Choose a reason for hiding this comment

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

heads up that I applied a fix to this test also as part of #198 .

In there, I replaced expectedReqOptions with a check that ensures code_challenge wasn't included.

client_id: 'client_id',
channel_id: 'site_id',
dnt: "false",

Check warning on line 239 in src/static/helpers/slasHelper.test.ts

View workflow job for this annotation

GitHub Actions / linux-tests (20)

Replace `"false"` with `'false'`

Check warning on line 239 in src/static/helpers/slasHelper.test.ts

View workflow job for this annotation

GitHub Actions / linux-tests (22)

Replace `"false"` with `'false'`
hint: 'hint',
redirect_uri: 'redirect_uri',
refreshToken: 'refresh_token',
response_type: 'code',
usid: 'usid',
};
Expand Down Expand Up @@ -415,7 +418,7 @@

// Assert the warning was logged
expect(consoleWarnSpy).toHaveBeenCalledWith(
expect.stringContaining('Invalid Parameter for authorizeCustomer: hello')
expect.stringContaining('Found unknown parameter for authorizeCustomer: hello, adding as query parameter anyway')

Check warning on line 421 in src/static/helpers/slasHelper.test.ts

View workflow job for this annotation

GitHub Actions / linux-tests (20)

Replace `'Found·unknown·parameter·for·authorizeCustomer:·hello,·adding·as·query·parameter·anyway'` with `⏎········'Found·unknown·parameter·for·authorizeCustomer:·hello,·adding·as·query·parameter·anyway'⏎······`

Check warning on line 421 in src/static/helpers/slasHelper.test.ts

View workflow job for this annotation

GitHub Actions / linux-tests (22)

Replace `'Found·unknown·parameter·for·authorizeCustomer:·hello,·adding·as·query·parameter·anyway'` with `⏎········'Found·unknown·parameter·for·authorizeCustomer:·hello,·adding·as·query·parameter·anyway'⏎······`
);

expect(getAccessTokenMock).toBeCalledWith(expectedTokenBody);
Expand Down
116 changes: 36 additions & 80 deletions src/static/helpers/slasHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,23 @@

import {customRandom, urlAlphabet} from 'nanoid';
import seedrandom, {PRNG} from 'seedrandom';
import {FetchOptions} from 'lib/clientConfig';
import {isBrowser} from './environment';

// TODO: replace Response with TokenResponse
import {
ShopperLogin,
ShopperLoginPathParameters,
ShopperLoginQueryParameters,
TokenResponse,
} from '../../lib/shopperLogin';
import ResponseError from '../responseError';
import TemplateURL from '../templateUrl';
import {
BaseUriParameters,
CustomQueryParameters,
CustomRequestBody,
ISlasClient,
TokenResponse,
} from './types';

export const stringToBase64 = isBrowser
Expand Down Expand Up @@ -111,12 +114,7 @@ export const generateCodeChallenge = async (
* @returns login url, user id and authorization code if available
*/
export async function authorize(
slasClient: ShopperLogin<{
shortCode: string;
organizationId: string;
clientId: string;
siteId: string;
}>,
slasClient: ISlasClient,
codeVerifier: string,
parameters: {
redirectURI: string;
Expand All @@ -143,7 +141,7 @@ export async function authorize(
slasClientCopy.clientConfig.fetchOptions = {
...slasClient.clientConfig.fetchOptions,
redirect: isBrowser ? 'follow' : 'manual',
};
} as FetchOptions;

const {hint, redirectURI, usid, ...restOfParams} = parameters;
const opts = {
Expand Down Expand Up @@ -194,13 +192,7 @@ export async function authorize(
* @returns authorization url and code verifier
*/
export async function authorizeIDP(
slasClient: ShopperLogin<{
shortCode: string;
organizationId: string;
clientId: string;
siteId: string;
version?: string;
}>,
slasClient: ISlasClient,
parameters: {
redirectURI: string;
hint: string;
Expand Down Expand Up @@ -239,11 +231,15 @@ export async function authorizeIDP(
...(usid && {usid}),
};

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

return {url: url.toString(), codeVerifier};
}
Expand All @@ -262,12 +258,7 @@ export async function authorizeIDP(
* @returns TokenResponse
*/
export async function loginIDPUser(
slasClient: ShopperLogin<{
shortCode: string;
organizationId: string;
clientId: string;
siteId: string;
}>,
slasClient: ISlasClient,
credentials: {
clientSecret?: string;
codeVerifier?: string;
Expand Down Expand Up @@ -307,10 +298,10 @@ export async function loginIDPUser(
},
body: tokenBody,
};
return slasClient.getAccessToken(optionsToken);
return slasClient.getAccessToken(optionsToken) as Promise<TokenResponse>;
}
// default is to use slas public client
return slasClient.getAccessToken({body: tokenBody});
return slasClient.getAccessToken({body: tokenBody}) as Promise<TokenResponse>;
}

/**
Expand All @@ -325,12 +316,7 @@ export async function loginIDPUser(
* @returns TokenResponse
*/
export async function loginGuestUserPrivate(
slasClient: ShopperLogin<{
shortCode: string;
organizationId: string;
clientId: string;
siteId: string;
}>,
slasClient: ISlasClient,
parameters: {
usid?: string;
dnt?: boolean;
Expand Down Expand Up @@ -361,7 +347,7 @@ export async function loginGuestUserPrivate(
},
};

return slasClient.getAccessToken(options);
return slasClient.getAccessToken(options) as Promise<TokenResponse>;
}

/**
Expand All @@ -374,12 +360,7 @@ export async function loginGuestUserPrivate(
* @returns TokenResponse
*/
export async function loginGuestUser(
slasClient: ShopperLogin<{
shortCode: string;
organizationId: string;
clientId: string;
siteId: string;
}>,
slasClient: ISlasClient,
parameters: {
redirectURI: string;
usid?: string;
Expand Down Expand Up @@ -413,7 +394,7 @@ export async function loginGuestUser(
...(dnt !== undefined && {dnt: dnt.toString()}),
};

return slasClient.getAccessToken({body: tokenBody});
return slasClient.getAccessToken({body: tokenBody}) as Promise<TokenResponse>;
}

/**
Expand All @@ -433,12 +414,7 @@ export async function loginGuestUser(
* @returns TokenResponse
*/
export async function loginRegisteredUserB2C(
slasClient: ShopperLogin<{
shortCode: string;
organizationId: string;
clientId: string;
siteId: string;
}>,
slasClient: ISlasClient,
credentials: {
username: string;
password: string;
Expand Down Expand Up @@ -466,7 +442,7 @@ export async function loginRegisteredUserB2C(
slasClientCopy.clientConfig.fetchOptions = {
...slasClient.clientConfig.fetchOptions,
redirect: isBrowser ? 'follow' : 'manual',
};
} as FetchOptions;

const authorization = `Basic ${stringToBase64(
`${credentials.username}:${credentials.password}`
Expand Down Expand Up @@ -522,10 +498,10 @@ export async function loginRegisteredUserB2C(
},
body: tokenBody,
};
return slasClient.getAccessToken(optionsToken);
return slasClient.getAccessToken(optionsToken) as Promise<TokenResponse>;
}
// default is to use slas public client
return slasClient.getAccessToken({body: tokenBody});
return slasClient.getAccessToken({body: tokenBody}) as Promise<TokenResponse>;
}

/* Function to send passwordless login token
Expand All @@ -542,12 +518,7 @@ export async function loginRegisteredUserB2C(
* @returns Promise of Response
*/
export async function authorizePasswordless(
slasClient: ShopperLogin<{
shortCode: string;
organizationId: string;
clientId: string;
siteId: string;
}>,
slasClient: ISlasClient,
credentials: {
clientSecret: string;
},
Expand Down Expand Up @@ -600,7 +571,7 @@ export async function authorizePasswordless(
body: tokenBody,
},
true
);
) as Promise<Response>;
}

/**
Expand All @@ -616,12 +587,7 @@ export async function authorizePasswordless(
* @returns Promise of Response or Object
*/
export async function getPasswordLessAccessToken(
slasClient: ShopperLogin<{
shortCode: string;
organizationId: string;
clientId: string;
siteId: string;
}>,
slasClient: ISlasClient,
credentials: {
clientSecret: string;
},
Expand Down Expand Up @@ -663,7 +629,7 @@ export async function getPasswordLessAccessToken(
organizationId: slasClient.clientConfig.parameters.organizationId,
},
body: tokenBody,
});
}) as Promise<TokenResponse>;
}

/**
Expand All @@ -677,12 +643,7 @@ export async function getPasswordLessAccessToken(
* @returns TokenResponse
*/
export function refreshAccessToken(
slasClient: ShopperLogin<{
shortCode: string;
organizationId: string;
clientId: string;
siteId: string;
}>,
slasClient: ISlasClient,
parameters: {
refreshToken: string;
dnt?: boolean;
Expand All @@ -707,10 +668,10 @@ export function refreshAccessToken(
},
body,
};
return slasClient.getAccessToken(options);
return slasClient.getAccessToken(options) as Promise<TokenResponse>;
}

return slasClient.getAccessToken({body});
return slasClient.getAccessToken({body}) as Promise<TokenResponse>;
}

/**
Expand All @@ -722,12 +683,7 @@ export function refreshAccessToken(
* @returns TokenResponse
*/
export function logout(
slasClient: ShopperLogin<{
shortCode: string;
organizationId: string;
clientId: string;
siteId: string;
}>,
slasClient: ISlasClient,
parameters: {
accessToken: string;
refreshToken: string;
Expand All @@ -742,5 +698,5 @@ export function logout(
client_id: slasClient.clientConfig.parameters.clientId,
channel_id: slasClient.clientConfig.parameters.siteId,
},
});
}) as Promise<TokenResponse>;
}
Loading
Loading