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

Feature/update get token for custom api urls #9

Merged
merged 2 commits into from
Aug 18, 2023
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
9 changes: 8 additions & 1 deletion __tests__/getToken.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,15 @@ describe('getToken', () => {
clientSecret
);

it('returns a string', async () => {
it('returns a string using the default api route', async () => {
let resultToken: string = await getToken(accessTokenSigned);
expect(typeof resultToken).toBe('string');
});
it('returns a string using a default api route', async () => {
let resultToken: string = await getToken(
accessTokenSigned,
'https://feature.promo.api.tincre.dev'
);
expect(typeof resultToken).toBe('string');
});
});
12 changes: 9 additions & 3 deletions src/getToken.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,17 @@ import fetch from 'cross-fetch';
* @param accessToken string JWT-encoded access token per
* https://tincre.dev/docs/guides/how-to-auth.
*
* @param promoApiUrl string | undefined base url for the Promo API, e.g. https://promo.api.tincre.dev.
*
* @returns a usable refresh token you can store on the client.
*/
export async function getToken(accessToken: string): Promise<string> {
const url = 'https://promo.api.tincre.dev/token';
export async function getToken(
accessToken: string,
promoApiUrl?: string
): Promise<string> {
const url = promoApiUrl
? promoApiUrl + '/token'
: 'https://promo.api.tincre.dev/token';
let refreshToken: string;
// Example POST method implementation:
// Default options are marked with *
Expand All @@ -37,7 +44,6 @@ export async function getToken(accessToken: string): Promise<string> {
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${accessToken}`,
// 'Content-Type': 'application/x-www-form-urlencoded',
},
});
console.debug(`getToken: awaiting response JSON`);
Expand Down
Loading