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
This commit is contained in:
Ronald Langeveld 2024-06-19 10:27:07 +07:00 committed by GitHub
parent 5154e8d24f
commit 9e1a70eed7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 38 additions and 1 deletions

View File

@ -20,6 +20,7 @@ export interface Meta {
interface QueryOptions<ResponseData> { interface QueryOptions<ResponseData> {
dataType: string dataType: string
path: string path: string
headers?: Record<string, string>;
defaultSearchParams?: Record<string, string>; defaultSearchParams?: Record<string, string>;
permissions?: string[]; permissions?: string[];
returnData?: (originalData: unknown) => ResponseData; returnData?: (originalData: unknown) => ResponseData;
@ -38,7 +39,7 @@ export const createQuery = <ResponseData>(options: QueryOptions<ResponseData>) =
const result = useQuery<ResponseData>({ const result = useQuery<ResponseData>({
enabled: options.permissions ? usePermission(options.permissions) : true, enabled: options.permissions ? usePermission(options.permissions) : true,
queryKey: [options.dataType, url], queryKey: [options.dataType, url],
queryFn: () => fetchApi(url), queryFn: () => fetchApi(url, {...options}),
...query ...query
}); });
@ -145,6 +146,7 @@ export const createQueryWithId = <ResponseData>(options: Omit<QueryOptions<Respo
interface MutationOptions<ResponseData, Payload> extends Omit<QueryOptions<ResponseData>, 'dataType' | 'path'>, Omit<RequestOptions, 'body'> { interface MutationOptions<ResponseData, Payload> extends Omit<QueryOptions<ResponseData>, 'dataType' | 'path'>, Omit<RequestOptions, 'body'> {
path: (payload: Payload) => string; path: (payload: Payload) => string;
headers?: Record<string, string>;
body?: (payload: Payload) => FormData | object; body?: (payload: Payload) => FormData | object;
searchParams?: (payload: Payload) => { [key: string]: string; }; searchParams?: (payload: Payload) => { [key: string]: string; };
invalidateQueries?: { dataType: string; }; invalidateQueries?: { dataType: string; };

View File

@ -60,12 +60,47 @@ describe('API hooks', function () {
expect(mock.calls.length).toBe(1); expect(mock.calls.length).toBe(1);
expect(mock.calls[0]).toEqual(['http://localhost:3000/ghost/api/admin/test/', { expect(mock.calls[0]).toEqual(['http://localhost:3000/ghost/api/admin/test/', {
credentials: 'include', credentials: 'include',
dataType: 'test',
headers: { headers: {
'app-pragma': 'no-cache', 'app-pragma': 'no-cache',
'x-ghost-version': '5.x' 'x-ghost-version': '5.x'
}, },
method: 'GET', method: 'GET',
mode: 'cors', 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) signal: expect.any(AbortSignal)
}]); }]);
}); });