/** * react-fetch v1.49.0 * * Copyright (c) resourge. * * This source code is licensed under the MIT license found in the * LICENSE.md file in the root directory of this source tree. * * @license MIT */ import { type State, type UseFetchError } from '../services/NotificationService'; /** * Hook to fetch and set data. * It will do the loading, error, set data, manually abort request if component is unmounted, and/or triggering other useFetch/useFetchCallback's * * Note: When initialState is set, it will also trigger an useEffect, otherwise it's just a method * @param method - method to set fetch data * @param config {@link FetchConfig} - fetch config's. They override default HttpProvider config's. * @example * ```Typescript // Fetch with useState // const { // data, // error, // fetch, // isLoading, // setFetchState // } = useFetch( async (Http) => { return Http.get("url") }, { initialState: [] } ); // Fetch without useEffect // const { // error, // fetch, // isLoading // } = useFetch( async (Http) => { return Http.get("url") } ); ``` */ type NoUndefinedField = { [P in keyof A]-?: NoUndefinedField>; }; export type BaseFetch = { error: UseFetchError; /** * True for when is request something */ isLoading: boolean; }; export type FetchConfig = { /** * When false useEffect will not trigger fetch * * Note: It is not included in the deps. * @default true */ enable?: boolean; /** * Instead of triggering global LoadingService, load a specific LoadingService. * @default string */ loadingService?: string; /** * Doesn't trigger any Loading * @default false */ silent?: boolean; }; export type FetchEffect = BaseFetch & { /** * Fetch Method with loading */ fetch: (...args: Partial) => Promise; }; export type FetchEffectConfig = FetchConfig & { /** * useEffect dependencies. * Basically works on useEffect dependencies * @default [] */ deps?: readonly any[]; /** * Default data values. */ initialState?: never; /** * Function that executes only on useEffect finally */ onEffectEnd?: () => void; /** * Serves to restore scroll position */ scrollRestoration?: ((behavior?: ScrollBehavior) => void) | Array<(behavior?: ScrollBehavior) => void>; }; export type FetchMethod = BaseFetch & { /** * Fetch Method with loading */ fetch: (...args: A) => Promise; }; export type FetchState = FetchEffect & { data: Result; /** * To set fetch state manually */ setFetchState: (data: Result) => void; }; export type FetchStateConfig = Omit & { /** * Default data values. */ initialState: (() => A) | A; /** * On Data Change */ onDataChange?: (value: A) => void; /** * Fetch on window focus * @default true when initialState is defined. */ onWindowFocus?: boolean; }; export declare function useFetch(method: (this: NoUndefinedField>, ...args: Partial) => Promise, config: FetchStateConfig): FetchState; export declare function useFetch(method: (this: State, ...args: Partial) => Promise, config: FetchEffectConfig): FetchEffect; export declare function useFetch(method: (this: State, ...args: A) => Promise, config?: FetchConfig): FetchMethod; export {};