UNPKG

@resourge/react-fetch

Version:

[![License](https://img.shields.io/badge/License-MIT-blue.svg)](LICENSE)

137 lines (136 loc) 3.87 kB
/** * react-fetch v1.43.1 * * 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'; export type BaseFetch = { error: UseFetchError; /** * True for when is request something */ isLoading: boolean; }; export type FetchMethod<Result, A extends any[]> = BaseFetch & { /** * Fetch Method with loading */ fetch: (...args: A) => Promise<Result>; }; export type FetchEffect<Result, A extends any[]> = BaseFetch & { /** * Fetch Method with loading */ fetch: (...args: Partial<A>) => Promise<Result>; }; export type FetchState<Result, A extends any[]> = FetchEffect<Result, A> & { data: Result; /** * To set fetch state manually */ setFetchState: (data: Result) => void; }; 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 FetchEffectConfig = FetchConfig & { /** * useEffect dependencies. * Basically works on useEffect dependencies * @default [] */ deps?: readonly any[]; /** * Fetch id */ id?: string; /** * 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 FetchStateConfig<A> = Omit<FetchEffectConfig, 'initialState'> & { /** * 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; }; /** * 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<A> = { [P in keyof A]-?: NoUndefinedField<NonNullable<A[P]>>; }; export declare function useFetch<Result, A extends any[]>(method: (this: NoUndefinedField<State<Result>>, ...args: Partial<A>) => Promise<Result>, config: FetchStateConfig<Result>): FetchState<Result, A>; export declare function useFetch<Result, A extends any[]>(method: (this: State<Result>, ...args: Partial<A>) => Promise<Result>, config: FetchEffectConfig): FetchEffect<Result, A>; export declare function useFetch<Result, A extends any[]>(method: (this: State<Result>, ...args: A) => Promise<Result>, config?: FetchConfig): FetchMethod<Result, A>; export {};