From 9e1a70eed7d5ea5e9acc72eb32d8abcdec76f36d Mon Sep 17 00:00:00 2001 From: Ronald Langeveld Date: Wed, 19 Jun 2024 10:27:07 +0700 Subject: [PATCH] Added custom headers to be passed to createQuery hook (#20402) ref MOM-239 - for ActivityPub + Egg - allow custom headers to be passed to createQuery hook in Admin X. - this includes the ability to use it inside `createQueryWithId` hook. - Added testing --- apps/admin-x-framework/src/utils/api/hooks.ts | 4 ++- .../test/unit/utils/api/hooks.test.tsx | 35 +++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/apps/admin-x-framework/src/utils/api/hooks.ts b/apps/admin-x-framework/src/utils/api/hooks.ts index 23bb0a78a8..8d26701507 100644 --- a/apps/admin-x-framework/src/utils/api/hooks.ts +++ b/apps/admin-x-framework/src/utils/api/hooks.ts @@ -20,6 +20,7 @@ export interface Meta { interface QueryOptions { dataType: string path: string + headers?: Record; defaultSearchParams?: Record; permissions?: string[]; returnData?: (originalData: unknown) => ResponseData; @@ -38,7 +39,7 @@ export const createQuery = (options: QueryOptions) = const result = useQuery({ enabled: options.permissions ? usePermission(options.permissions) : true, queryKey: [options.dataType, url], - queryFn: () => fetchApi(url), + queryFn: () => fetchApi(url, {...options}), ...query }); @@ -145,6 +146,7 @@ export const createQueryWithId = (options: Omit extends Omit, 'dataType' | 'path'>, Omit { path: (payload: Payload) => string; + headers?: Record; body?: (payload: Payload) => FormData | object; searchParams?: (payload: Payload) => { [key: string]: string; }; invalidateQueries?: { dataType: string; }; diff --git a/apps/admin-x-framework/test/unit/utils/api/hooks.test.tsx b/apps/admin-x-framework/test/unit/utils/api/hooks.test.tsx index 1245c3c63f..2099441d96 100644 --- a/apps/admin-x-framework/test/unit/utils/api/hooks.test.tsx +++ b/apps/admin-x-framework/test/unit/utils/api/hooks.test.tsx @@ -60,12 +60,47 @@ describe('API hooks', function () { expect(mock.calls.length).toBe(1); expect(mock.calls[0]).toEqual(['http://localhost:3000/ghost/api/admin/test/', { credentials: 'include', + dataType: 'test', headers: { 'app-pragma': 'no-cache', 'x-ghost-version': '5.x' }, method: 'GET', mode: 'cors', + path: '/test/', + signal: expect.any(AbortSignal) + }]); + }); + }); + + it('can add custom headers', async function () { + await withMockFetch({ + json: {test: 1} + }, async (mock) => { + const useTestQuery = createQuery({ + dataType: 'test', + path: '/test/', + headers: {'Content-Type': 'ALOHA'} + }); + + const {result} = renderHook(() => useTestQuery(), {wrapper}); + + await waitFor(() => expect(result.current.isLoading).toBe(false)); + + expect(result.current.data).toEqual({test: 1}); + + expect(mock.calls.length).toBe(1); + expect(mock.calls[0]).toEqual(['http://localhost:3000/ghost/api/admin/test/', { + credentials: 'include', + dataType: 'test', + headers: { + 'Content-Type': 'ALOHA', + 'app-pragma': 'no-cache', + 'x-ghost-version': '5.x' + }, + method: 'GET', + mode: 'cors', + path: '/test/', signal: expect.any(AbortSignal) }]); });