UNPKG

1.24 kBTypeScriptView Raw
1/**
2 * @name useFetch
3 * @author Fajar Rizky Hidayat <fajarrizkyhidayat@gmail.com>
4 */
5
6interface FetchProps<T = any> {
7 service: (resp?: import(".").EventSend<T>) => any;
8 selector: (resp: any) => any;
9 defaultValue?: T;
10 disabledOnDidMount?: boolean;
11 debounceTime?: number;
12 cache?: {
13 key?: string;
14 /**
15 * @name ttl
16 * @description in seconds
17 * */
18 ttl?: number;
19 /**
20 * @name PeriodTime
21 * @description in ms
22 * */
23 periodTime?: number;
24 periodically?: boolean;
25 };
26 deps?: any[];
27 getData?: (value: any) => void;
28}
29
30type UseFetch<T = any> = {
31 isEmpty: boolean;
32 loading: boolean;
33 data: T;
34 query: object;
35 setQuery: (value: any | ((obj: any) => any)) => void;
36 clear: (fields?: {
37 except?: ReadonlyArray<keyof T>;
38 only?: ReadonlyArray<keyof T>;
39 }) => void;
40 refresh: () => void;
41 add: (newValue: T, position?: "start" | "end" | number) => void;
42 update: (condition: (data: T) => boolean, newValue: Partial<T>) => void;
43 destroy: (condition: (data: T) => boolean) => void;
44 getQuery: (key: string, defaultValue?: any) => any;
45 has: (key: string) => boolean;
46 cancel: () => void;
47};
48
49export default function useFetch<T>(props: FetchProps<T>): UseFetch<T>;