From bb4c2522a80e844fac7281d6914d854efdcef450 Mon Sep 17 00:00:00 2001 From: Romain Le Cellier Date: Mon, 20 Feb 2023 17:13:18 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8(frontend)=20add=20auth=20token=20into?= =?UTF-8?q?=20generated=20api?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Generated api client need's to handle joanie authentification --- .../js/api/joanie/__specs__/index.spec.ts | 34 +++++++++++++++++++ src/frontend/js/api/joanie/index.ts | 18 ++++++---- 2 files changed, 45 insertions(+), 7 deletions(-) create mode 100644 src/frontend/js/api/joanie/__specs__/index.spec.ts diff --git a/src/frontend/js/api/joanie/__specs__/index.spec.ts b/src/frontend/js/api/joanie/__specs__/index.spec.ts new file mode 100644 index 0000000000..b4fadc7ecc --- /dev/null +++ b/src/frontend/js/api/joanie/__specs__/index.spec.ts @@ -0,0 +1,34 @@ +import fetchMock from 'fetch-mock'; +import { RichieContextFactory as mockRichieContextFactory } from 'utils/test/factories/richie'; +import { RICHIE_USER_TOKEN } from 'settings'; + +import { joanieApi } from '..'; + +jest.mock('utils/context', () => ({ + __esModule: true, + default: mockRichieContextFactory({ + authentication: { backend: 'fonzie', endpoint: 'https://demo.endpoint' }, + joanie_backend: { endpoint: 'https://joanie.endpoint' }, + }).one(), +})); + +describe('joanieApi', () => { + it('test', async () => { + fetchMock.get('https://joanie.endpoint/api/v1.0/addresses/addressId/', []); + await joanieApi.api.apiV10AddressesRetrieve('addressId'); + + let lastCall = fetchMock.lastCall(); + const visitorHeader = lastCall && lastCall[1]?.headers; + // TS see visitorHeader has HeadersInit instead of Headers and + // didn't accept get() as a possible function. + // @ts-ignore + expect(visitorHeader?.get('Authorization')).toBeNull(); + + sessionStorage.setItem(RICHIE_USER_TOKEN, 'TEST_TOKEN'); + await joanieApi.api.apiV10AddressesRetrieve('addressId'); + lastCall = fetchMock.lastCall(); + const userHeader = lastCall && lastCall[1]?.headers; + // @ts-ignore + expect(userHeader?.get('Authorization')).toBe('Bearer TEST_TOKEN'); + }); +}); diff --git a/src/frontend/js/api/joanie/index.ts b/src/frontend/js/api/joanie/index.ts index 5e1a84898c..32fff2c654 100644 --- a/src/frontend/js/api/joanie/index.ts +++ b/src/frontend/js/api/joanie/index.ts @@ -1,6 +1,6 @@ import context from 'utils/context'; -import { JOANIE_API_VERSION } from 'settings'; -import { ApiClientJoanie, OpenAPIConfig } from './gen'; +import { JOANIE_API_VERSION, RICHIE_USER_TOKEN } from 'settings'; +import { ApiClientJoanie, ApiError, OpenAPIConfig } from './gen'; /** * Build Joanie API Routes interface. @@ -16,14 +16,18 @@ const getAPIEndpoint = () => { return `${endpoint}/api/${version}`; }; -// TODO add auth with jwt const config: OpenAPIConfig = { BASE: getAPIEndpoint(), VERSION: '1', - WITH_CREDENTIALS: true, - CREDENTIALS: 'include', - // TOKEN: + WITH_CREDENTIALS: false, + CREDENTIALS: 'omit', + TOKEN: async () => { + return sessionStorage.getItem(RICHIE_USER_TOKEN) || ''; + }, }; export const joanieApi = new ApiClientJoanie(config); -export * from './hooks'; + +export const isApiError = (error: unknown): error is ApiError => { + return (error as ApiError).name === 'ApiError'; +};