@resourge/react-fetch
Version:
[](LICENSE)
137 lines (136 loc) • 3.87 kB
TypeScript
/**
* react-fetch v1.41.3
*
* 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, T extends any[]> = BaseFetch & {
/**
* Fetch Method with loading
*/
fetch: (...args: T) => Promise<Result>;
};
export type FetchEffect<Result, T extends any[]> = BaseFetch & {
/**
* Fetch Method with loading
*/
fetch: (...args: Partial<T>) => Promise<Result>;
};
export type FetchState<Result, T extends any[]> = FetchEffect<Result, T> & {
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<T> = Omit<FetchEffectConfig, 'initialState'> & {
/**
* Default data values.
*/
initialState: T | (() => T);
/**
* On Data Change
*/
onDataChange?: (value: T) => 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<T> = {
[P in keyof T]-?: NoUndefinedField<NonNullable<T[P]>>;
};
export declare function useFetch<Result, T extends any[]>(method: (this: NoUndefinedField<State<Result>>, ...args: Partial<T>) => Promise<Result>, config: FetchStateConfig<Result>): FetchState<Result, T>;
export declare function useFetch<Result, T extends any[]>(method: (this: State<Result>, ...args: Partial<T>) => Promise<Result>, config: FetchEffectConfig): FetchEffect<Result, T>;
export declare function useFetch<Result, T extends any[]>(method: (this: State<Result>, ...args: T) => Promise<Result>, config?: FetchConfig): FetchMethod<Result, T>;
export {};