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) }]); });