import { KeyedMutator, SWRHook, SWRConfiguration } from 'swr'; import { FetcherResult, FetcherOptions, AuthTokenHeader } from './fetcher.cjs'; import './MatsuriError.cjs'; type SWRKey = Parameters[0]; interface UseFetchState> extends FetcherResult { refetch: KeyedMutator; isLoading: boolean; isValidating: boolean; } interface UseFetchStateForSuspense> extends UseFetchState { data: T; } interface UseFetchKey { url: string; token: string | undefined; body: RequestInit["body"] | undefined; dependencies: SWRKey | undefined; } interface UseFetchOptions extends Omit { /** * keyの型を自動で反映させたければジェネリクスなどを利用してoptions.keyの型を持ってくる必要があるが、 * fetchを上書きしたいケースがそこまであるとは思えないため、fetchの引数の型を緩く指定している。 */ fetch?: (args: UseFetchKey) => Promise; swrConfig?: Partial & { fallbackData?: never; suspense?: false; }; authTokenHeader?: AuthTokenHeader; /** * 依存関係の変更を検知して再フェッチを行いたい場合に指定する。 */ dependencies?: SWRKey; } interface UseFetchOptionsWithSuspense extends Omit, "swrConfig"> { swrConfig: Partial & ({ fallbackData: unknown; } | { suspense: true; fallbackData?: unknown; }); } interface UseFetch { >(url: string | null, options?: UseFetchOptions): UseFetchState; >(url: string | null, options?: UseFetchOptionsWithSuspense): UseFetchStateForSuspense; } declare const useFetch: UseFetch; interface UseAuthFetch { >(token: string, url: string | null, options?: UseFetchOptions): UseFetchState; >(token: string, url: string | null, options?: UseFetchOptionsWithSuspense): UseFetchStateForSuspense; } declare const useAuthFetch: UseAuthFetch; declare const useAuthBearerFetch: UseAuthFetch; export { type UseFetchState, type UseFetchStateForSuspense, useAuthBearerFetch, useAuthFetch, useFetch };