import { UndefinedInitialDataOptions, useQuery } from '@tanstack/react-query'; import { useMemo } from 'react'; import fetchJson, { ApiError, FetchJsonOptions, } from 'Utilities/Fetch/fetchJson'; import getQueryPath from 'Utilities/Fetch/getQueryPath'; import getQueryString, { QueryParams } from 'Utilities/Fetch/getQueryString'; export interface QueryOptions extends FetchJsonOptions { queryParams?: QueryParams; queryOptions?: | Omit, 'queryKey' | 'queryFn'> | undefined; } const useApiQuery = (options: QueryOptions) => { const { queryKey, requestOptions } = useMemo(() => { const { path: path, queryOptions, queryParams, ...otherOptions } = options; return { queryKey: [path, queryParams], requestOptions: { ...otherOptions, path: getQueryPath(path) + getQueryString(queryParams), headers: { ...options.headers, 'X-Api-Key': window.Sonarr.apiKey, 'X-Sonarr-Client': 'Sonarr', }, }, }; }, [options]); return { queryKey, ...useQuery({ ...options.queryOptions, queryKey, queryFn: async ({ signal }) => fetchJson({ ...requestOptions, signal }), }), }; }; export default useApiQuery;