-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
use-fetch.types.ts
107 lines (104 loc) · 2.95 KB
/
use-fetch.types.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import { RequestInstance, ExtractResponseType, ExtractErrorType, CacheValueType } from "@hyper-fetch/core";
import { UseRequestEventsActionsType, UseTrackedStateActions, UseTrackedStateType } from "helpers";
import { InvalidationKeyType } from "types";
import { isEqual } from "utils";
export type UseFetchOptionsType<T extends RequestInstance> = {
/**
* Refetch dependencies
*/
dependencies?: any[];
/**
* Disable fetching
*/
disabled?: boolean;
/**
* If `true` it will rerender only when values used by our component gets changed. Otherwise it will rerender on any change.
*/
dependencyTracking?: boolean;
/**
* If `true` it will refetch data in background no matter if we have it from cache.
*/
revalidateOnMount?: boolean;
/**
* If cache is empty we can use placeholder data.
*/
initialData?: CacheValueType<ExtractResponseType<T>, ExtractErrorType<T>>["data"] | null;
/**
* Enable/disable refresh data
*/
refresh?: boolean;
/**
* Refresh data interval time
*/
refreshTime?: number;
/**
* Enable/disable data refresh if our tab is not focused(used by user at given time).
*/
refreshBlurred?: boolean;
/**
* Enable/disable data refresh if user leaves current tab.
*/
refreshOnBlur?: boolean;
/**
* Enable/disable data refresh if user enters current tab.
*/
refreshOnFocus?: boolean;
/**
* Enable/disable data refresh if network is restored.
*/
refreshOnReconnect?: boolean;
/**
* Enable/disable debouncing for often changing keys or refreshing, to limit requests to server.
*/
bounce?: boolean;
/**
* Deep comparison function for hook to check for equality in incoming data, to limit rerenders.
*/
deepCompare?: boolean | typeof isEqual;
} & (
| {
/**
* Possibility to choose between debounce and throttle approaches
*/
bounceType?: "debounce";
/**
* How long it should bounce requests.
*/
bounceTime?: number;
}
| {
/**
* Possibility to choose between debounce and throttle approaches
*/
bounceType: "throttle";
/**
* How long it should bounce requests.
*/
bounceTime?: number;
/**
* ONLY in throttle mode - options for handling last bounce event
*/
bounceTimeout?: number;
}
);
export type UseFetchReturnType<T extends RequestInstance> = UseTrackedStateType<T> &
UseTrackedStateActions<T> &
UseRequestEventsActionsType<T> & {
/**
* Data related to current state of the bounce usage
*/
bounce: {
/**
* Active state of the bounce method
*/
active: boolean;
/**
* Method to stop the active bounce method execution
*/
reset: () => void;
};
/**
* Revalidate current request resource or pass custom key to trigger it by invalidationKey(Regex / cacheKey).
*/
revalidate: (invalidateKey?: InvalidationKeyType | InvalidationKeyType[]) => void;
};