-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
use-submit.types.ts
116 lines (113 loc) · 3.99 KB
/
use-submit.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
108
109
110
111
112
113
114
115
116
import { CacheValueType, ExtractError, ExtractResponse, CommandInstance, FetchMethodType } from "@hyper-fetch/core";
import { isEqual } from "utils";
import {
OnErrorCallbackType,
OnFinishedCallbackType,
OnProgressCallbackType,
OnStartCallbackType,
OnSuccessCallbackType,
UseTrackedStateType,
UseTrackedStateActions,
} from "helpers";
import { InvalidationKeyType } from "types";
export type UseSubmitOptionsType<T extends CommandInstance> = {
/**
* Disable fetching
*/
disabled?: boolean;
/**
* If cache is empty we can use placeholder data.
*/
initialData?: CacheValueType<ExtractResponse<T>, ExtractError<T>>["data"] | null;
/**
* Enable/disable debouncing for often changing keys or refreshing, to limit requests to server.
*/
bounce?: boolean;
/**
* Possibility to choose between debounce and throttle approaches
*/
bounceType?: "debounce" | "throttle";
/**
* How long it should bounce requests.
*/
bounceTime?: number;
/**
* If `true` it will rerender only when values used by our component gets changed. Otherwise it will rerender on any change.
*/
dependencyTracking?: boolean;
/**
* Deep comparison function for hook to check for equality in incoming data, to limit rerenders.
*/
deepCompare?: boolean | typeof isEqual;
};
export type UseSubmitReturnType<CommandType extends CommandInstance> = Omit<
UseTrackedStateType<CommandType>,
"loading"
> &
UseTrackedStateActions<CommandType> & {
/**
* Callback which allows to cancel ongoing requests from given queueKey.
*/
abort: () => void;
/**
* Helper hook listening on success response.
*/
onSubmitSuccess: (callback: OnSuccessCallbackType<CommandType>) => void;
/**
* Helper hook listening on error response.
*/
onSubmitError: (callback: OnErrorCallbackType<CommandType>) => void;
/**
* Helper hook listening on any response.
*/
onSubmitFinished: (callback: OnFinishedCallbackType<CommandType>) => void;
/**
* Helper hook listening on request start.
*/
onSubmitRequestStart: (callback: OnStartCallbackType<CommandType>) => void;
/**
* Helper hook listening on response start(before we receive all data from server).
*/
onSubmitResponseStart: (callback: OnStartCallbackType<CommandType>) => void;
/**
* Helper hook listening on download progress ETA. We can later match given requests by their id's or command instance which holds all data which is being transferred.
*/
onSubmitDownloadProgress: (callback: OnProgressCallbackType) => void;
/**
* Helper hook listening on upload progress ETA. We can later match given requests by their id's or command instance which holds all data which is being transferred.
*/
onSubmitUploadProgress: (callback: OnProgressCallbackType) => void;
/**
* Helper hook listening on aborting of requests. Abort events are not triggering onError callbacks.
*/
onSubmitAbort: (callback: OnErrorCallbackType<CommandType>) => void;
/**
* Helper hook listening on request going into offline awaiting for network connection to be restored. It will not trigger onError when 'offline' mode is set on command.
*/
onSubmitOfflineError: (callback: OnErrorCallbackType<CommandType>) => void;
/**
* Method responsible for triggering requests. It return Promise which will be resolved with the request.
*/
submit: FetchMethodType<CommandType>;
/**
* Request loading state
*/
submitting: boolean;
/**
* 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 command resource or pass custom key to trigger it by invalidationKey(Regex / cacheKey).
*/
revalidate: (invalidateKey: InvalidationKeyType | InvalidationKeyType[]) => void;
};