import type { RequestErrorBody } from '@logto/schemas'; import { HTTPError } from 'ky'; import { useCallback } from 'react'; import { useTranslation } from 'react-i18next'; import type { BareFetcher } from 'swr'; import useApi, { RequestError } from './use-api'; type withTotalNumber = Array | number>; type useSwrFetcherHook = { (): BareFetcher; (): BareFetcher>; }; const useSwrFetcher: useSwrFetcherHook = () => { const api = useApi({ hideErrorToast: true }); const { t } = useTranslation(undefined, { keyPrefix: 'admin_console' }); const fetcher = useCallback>>( async (resource, init) => { try { const response = await api.get(resource, init); const data = await response.json(); if (typeof resource === 'string' && resource.includes('?')) { const parameters = new URLSearchParams(resource.split('?')[1]); if (parameters.get('page') && parameters.get('page_size')) { const number = response.headers.get('Total-Number'); if (!number) { throw new Error(t('errors.missing_total_number')); } return [data, Number(number)]; } } return data; } catch (error: unknown) { if (error instanceof HTTPError) { const { response } = error; const metadata = await response.json(); throw new RequestError(response.status, metadata); } throw error; } }, [api, t] ); return fetcher; }; export default useSwrFetcher;