import { Accessor } from 'solid-js'; import { Fn, EventHookOn, MaybeAccessor } from '@solidjs-use/shared'; interface UseFetchReturn { /** * Indicates if the fetch request has finished */ isFinished: Accessor; /** * The statusCode of the HTTP fetch response */ statusCode: Accessor; /** * The raw response of the fetch response */ response: Accessor; /** * Any fetch errors that may have occurred */ error: Accessor; /** * The fetch response body on success, may either be JSON or text */ data: Accessor; /** * Indicates if the request is currently being fetched. */ isFetching: Accessor; /** * Indicates if the fetch request is able to be aborted */ canAbort: Accessor; /** * Indicates if the fetch request was aborted */ aborted: Accessor; /** * Abort the fetch request */ abort: Fn; /** * Manually call the fetch * (default not throwing error) */ execute: (throwOnFailed?: boolean) => Promise; /** * Fires after the fetch request has finished */ onFetchResponse: EventHookOn; /** * Fires after a fetch request error */ onFetchError: EventHookOn; /** * Fires after a fetch has completed */ onFetchFinally: EventHookOn; get: () => UseFetchReturn & PromiseLike>; post: (payload?: MaybeAccessor, type?: string) => UseFetchReturn & PromiseLike>; put: (payload?: MaybeAccessor, type?: string) => UseFetchReturn & PromiseLike>; delete: (payload?: MaybeAccessor, type?: string) => UseFetchReturn & PromiseLike>; patch: (payload?: MaybeAccessor, type?: string) => UseFetchReturn & PromiseLike>; head: (payload?: MaybeAccessor, type?: string) => UseFetchReturn & PromiseLike>; options: (payload?: MaybeAccessor, type?: string) => UseFetchReturn & PromiseLike>; json: () => UseFetchReturn & PromiseLike>; text: () => UseFetchReturn & PromiseLike>; blob: () => UseFetchReturn & PromiseLike>; arrayBuffer: () => UseFetchReturn & PromiseLike>; formData: () => UseFetchReturn & PromiseLike>; } type Combination = 'overwrite' | 'chain'; interface BeforeFetchContext { /** * The createMemo url of the current request */ url: string; /** * The request options of the current request */ options: RequestInit; /** * Cancels the current request */ cancel: Fn; } interface AfterFetchContext { response: Response; data: T | null; } interface OnFetchErrorContext { error: E; data: T | null; } interface UseFetchOptions { /** * Fetch function */ fetch?: typeof window.fetch; /** * Will automatically run fetch when `useFetch` is used * * @default true */ immediate?: boolean; /** * Will automatically refetch when: * - the URL is changed if the URL is a Accessor * - the payload is changed if the payload is a Accessor * * @default false */ refetch?: MaybeAccessor; /** * Initial data before the request finished * * @default null */ initialData?: any; /** * Timeout for abort request after number of millisecond * `0` means use browser default * * @default 0 */ timeout?: number; /** * Will run immediately before the fetch request is dispatched */ beforeFetch?: (ctx: BeforeFetchContext) => Promise | void> | Partial | void; /** * Will run immediately after the fetch request is returned. * Runs after any 2xx response */ afterFetch?: (ctx: AfterFetchContext) => Promise> | Partial; /** * Will run immediately after the fetch request is returned. * Runs after any 4xx and 5xx response */ onFetchError?: (ctx: { data: any; response: Response | null; error: any; }) => Promise> | Partial; } interface CreateFetchOptions { /** * The base URL that will be prefixed to all urls unless urls are absolute */ baseUrl?: MaybeAccessor; /** * Determine the inherit behavior for beforeFetch, afterFetch, onFetchError * @default 'chain' */ combination?: Combination; /** * Default Options for the useFetch function */ options?: UseFetchOptions; /** * Options for the fetch request */ fetchOptions?: RequestInit; } declare function createFetch(config?: CreateFetchOptions): typeof useFetch; /** * Reactive Fetch API. * * @see https://solidjs-use.github.io/solidjs-use/core/useFetch */ declare function useFetch(url: MaybeAccessor): UseFetchReturn & PromiseLike>; declare function useFetch(url: MaybeAccessor, useFetchOptions: UseFetchOptions): UseFetchReturn & PromiseLike>; declare function useFetch(url: MaybeAccessor, options: RequestInit, useFetchOptions?: UseFetchOptions): UseFetchReturn & PromiseLike>; export { AfterFetchContext, BeforeFetchContext, CreateFetchOptions, OnFetchErrorContext, UseFetchOptions, UseFetchReturn, createFetch, useFetch };