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: delete token from local storage then delete utils functions #1038

Merged
merged 3 commits into from
Oct 17, 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
4 changes: 2 additions & 2 deletions src/packages/sdk/build-api.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ const postCommentFn = (username: string, info: unknown) => [
];

describe('compute api call description', () => {
it('should fetch an url with some options and chain the given then handler', () => {
it('should fetch an url with some options and chain the given then handler', async () => {
global.fetch = vi.fn();
const [url, options, thenHandler] = computeDscr(postCommentFn, [
const [url, options, thenHandler] = await computeDscr(postCommentFn, [
'john',
'some raw text',
]);
Expand Down
16 changes: 7 additions & 9 deletions src/packages/sdk/build-api.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import {
getToken,
isTokenValid,
} from '../auth/open-id-connect-auth/token-utils';
import { getOidc } from '../auth/create-oidc';
import { getEnvVar } from '../utils/env';

export const generateGenericApiEndpoints = (
Expand Down Expand Up @@ -77,7 +74,7 @@ export const defaultOptions = {

export const defaultThenHandler = (res: Response) => res.json();

export const computeDscr = (fn: any, [...args]) => {
export const computeDscr = async (fn: any, [...args]) => {
const dscr = fn(...args);
if (!Array.isArray(dscr)) {
throw new Error(
Expand All @@ -92,11 +89,12 @@ export const computeDscr = (fn: any, [...args]) => {
//headers.Accept) are lost. Hence, if a prop option is overriden (ie.
//headers), all relevant options should be present.
options = { ...defaultOptions, ...options };
const token = getToken();
if (token && isTokenValid(token)) {
const oidc = await getOidc();
if (oidc && oidc.isUserLoggedIn) {
const { accessToken } = oidc.getTokens();
options = {
...options,
headers: { ...options.headers, Authorization: `Bearer ${token}` },
headers: { ...options.headers, Authorization: `Bearer ${accessToken}` },
};
}
thenHandler = thenHandler || defaultThenHandler;
Expand All @@ -121,7 +119,7 @@ export const getBaseURI = () => {

export const buildCall = (context: string, resource: string, fn: any) => {
return async (...args: any[]) => {
const [path, options, thenHandler] = computeDscr(fn, args);
const [path, options, thenHandler] = await computeDscr(fn, args);
if (!options.method) {
options.method = guessMethod(resource);
}
Expand Down