@hyper-fetch/react 1.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (84) hide show
  1. package/.eslintrc.json +7 -0
  2. package/README.md +82 -0
  3. package/dist/config-provider/config-provider.d.ts +13 -0
  4. package/dist/config-provider/config-provider.types.d.ts +17 -0
  5. package/dist/config-provider/index.d.ts +2 -0
  6. package/dist/helpers/index.d.ts +2 -0
  7. package/dist/helpers/use-command-events/index.d.ts +2 -0
  8. package/dist/helpers/use-command-events/use-command-events.hooks.d.ts +9 -0
  9. package/dist/helpers/use-command-events/use-command-events.types.d.ts +80 -0
  10. package/dist/helpers/use-tracked-state/index.d.ts +4 -0
  11. package/dist/helpers/use-tracked-state/use-tracked-state.constants.d.ts +2 -0
  12. package/dist/helpers/use-tracked-state/use-tracked-state.hooks.d.ts +11 -0
  13. package/dist/helpers/use-tracked-state/use-tracked-state.types.d.ts +72 -0
  14. package/dist/helpers/use-tracked-state/use-tracked-state.utils.d.ts +7 -0
  15. package/dist/hooks/use-app-manager/index.d.ts +2 -0
  16. package/dist/hooks/use-app-manager/use-app-manager.hooks.d.ts +3 -0
  17. package/dist/hooks/use-app-manager/use-app-manager.types.d.ts +18 -0
  18. package/dist/hooks/use-cache/index.d.ts +3 -0
  19. package/dist/hooks/use-cache/use-cache.constants.d.ts +5 -0
  20. package/dist/hooks/use-cache/use-cache.hooks.d.ts +3 -0
  21. package/dist/hooks/use-cache/use-cache.types.d.ts +35 -0
  22. package/dist/hooks/use-fetch/index.d.ts +4 -0
  23. package/dist/hooks/use-fetch/use-fetch.constants.d.ts +7 -0
  24. package/dist/hooks/use-fetch/use-fetch.hooks.d.ts +9 -0
  25. package/dist/hooks/use-fetch/use-fetch.types.d.ts +85 -0
  26. package/dist/hooks/use-fetch/use-fetch.utils.d.ts +1 -0
  27. package/dist/hooks/use-queue/index.d.ts +3 -0
  28. package/dist/hooks/use-queue/use-queue.constants.d.ts +3 -0
  29. package/dist/hooks/use-queue/use-queue.hooks.d.ts +9 -0
  30. package/dist/hooks/use-queue/use-queue.types.d.ts +48 -0
  31. package/dist/hooks/use-submit/index.d.ts +3 -0
  32. package/dist/hooks/use-submit/use-submit.constants.d.ts +7 -0
  33. package/dist/hooks/use-submit/use-submit.hooks.d.ts +9 -0
  34. package/dist/hooks/use-submit/use-submit.types.d.ts +101 -0
  35. package/dist/index.cjs.js +2 -0
  36. package/dist/index.cjs.js.map +7 -0
  37. package/dist/index.d.ts +8 -0
  38. package/dist/index.esm.js +2 -0
  39. package/dist/index.esm.js.map +7 -0
  40. package/dist/types/index.d.ts +1 -0
  41. package/dist/types/invalidation.types.d.ts +2 -0
  42. package/dist/utils/bounce.utils.d.ts +7 -0
  43. package/dist/utils/deep-equal.utils.d.ts +13 -0
  44. package/dist/utils/index.d.ts +2 -0
  45. package/package.json +74 -0
  46. package/src/config-provider/config-provider.tsx +30 -0
  47. package/src/config-provider/config-provider.types.ts +21 -0
  48. package/src/config-provider/index.ts +2 -0
  49. package/src/helpers/index.ts +2 -0
  50. package/src/helpers/use-command-events/index.ts +2 -0
  51. package/src/helpers/use-command-events/use-command-events.hooks.ts +283 -0
  52. package/src/helpers/use-command-events/use-command-events.types.ts +108 -0
  53. package/src/helpers/use-tracked-state/index.ts +4 -0
  54. package/src/helpers/use-tracked-state/use-tracked-state.constants.ts +10 -0
  55. package/src/helpers/use-tracked-state/use-tracked-state.hooks.ts +227 -0
  56. package/src/helpers/use-tracked-state/use-tracked-state.types.ts +85 -0
  57. package/src/helpers/use-tracked-state/use-tracked-state.utils.ts +82 -0
  58. package/src/hooks/use-app-manager/index.ts +2 -0
  59. package/src/hooks/use-app-manager/use-app-manager.hooks.ts +36 -0
  60. package/src/hooks/use-app-manager/use-app-manager.types.ts +18 -0
  61. package/src/hooks/use-cache/index.ts +3 -0
  62. package/src/hooks/use-cache/use-cache.constants.ts +5 -0
  63. package/src/hooks/use-cache/use-cache.hooks.ts +90 -0
  64. package/src/hooks/use-cache/use-cache.types.ts +45 -0
  65. package/src/hooks/use-fetch/index.ts +4 -0
  66. package/src/hooks/use-fetch/use-fetch.constants.ts +25 -0
  67. package/src/hooks/use-fetch/use-fetch.hooks.ts +265 -0
  68. package/src/hooks/use-fetch/use-fetch.types.ts +90 -0
  69. package/src/hooks/use-fetch/use-fetch.utils.ts +7 -0
  70. package/src/hooks/use-queue/index.ts +3 -0
  71. package/src/hooks/use-queue/use-queue.constants.ts +7 -0
  72. package/src/hooks/use-queue/use-queue.hooks.ts +112 -0
  73. package/src/hooks/use-queue/use-queue.types.ts +51 -0
  74. package/src/hooks/use-submit/index.ts +3 -0
  75. package/src/hooks/use-submit/use-submit.constants.ts +17 -0
  76. package/src/hooks/use-submit/use-submit.hooks.ts +219 -0
  77. package/src/hooks/use-submit/use-submit.types.ts +119 -0
  78. package/src/index.ts +8 -0
  79. package/src/types/index.ts +1 -0
  80. package/src/types/invalidation.types.ts +3 -0
  81. package/src/utils/bounce.utils.ts +9 -0
  82. package/src/utils/deep-equal.utils.ts +64 -0
  83. package/src/utils/index.ts +2 -0
  84. package/tsconfig.json +9 -0
package/package.json ADDED
@@ -0,0 +1,74 @@
1
+ {
2
+ "name": "@hyper-fetch/react",
3
+ "version": "1.6.0",
4
+ "private": false,
5
+ "description": "React hooks and utils for the hyper-fetch",
6
+ "author": "Maciej Pyrc <m.pyrc@interia.pl>",
7
+ "homepage": "https://hyperfetch.bettertyped.com/",
8
+ "license": "Apache-2.0",
9
+ "main": "dist/index.cjs.js",
10
+ "module": "dist/index.esm.js",
11
+ "types": "dist/index.d.ts",
12
+ "source": "src/index.ts",
13
+ "scripts": {
14
+ "prepublishOnly": "yarn build",
15
+ "clean": "npx rimraf dist",
16
+ "test": "jest --watchAll --maxWorkers=3",
17
+ "test-pipeline": "jest --watchAll=false --maxWorkers=1 --no-cache",
18
+ "start": "yarn build && npx run-when-changed --watch 'src/**/*.ts' --exec 'yarn build'",
19
+ "build": "yarn clean && node ../../scripts/build.js",
20
+ "postbuild": "ttsc --project tsconfig.json --outDir dist",
21
+ "lint": "eslint . --ext .js,.jsx,.tsx,.ts --fix",
22
+ "format": "prettier --write .",
23
+ "precommit": "yarn lint-staged",
24
+ "release": "yarn semantic-release --extends ../../.releaserc.json -t 'react-hyper-fetch-v${version}'"
25
+ },
26
+ "peerDependencies": {
27
+ "@hyper-fetch/core": "*",
28
+ "react": ">= 16.8.0"
29
+ },
30
+ "dependencies": {
31
+ "@better-typed/react-lifecycle-hooks": "^1.0.5",
32
+ "@better-typed/react-performance-hooks": "^0.8.0"
33
+ },
34
+ "lint-staged": {
35
+ "*.{js,jsx,ts,tsx}": [
36
+ "yarn jest --bail --findRelatedTests --passWithNoTests"
37
+ ]
38
+ },
39
+ "eslintConfig": {
40
+ "extends": [
41
+ "react-app",
42
+ "react-app/jest"
43
+ ]
44
+ },
45
+ "publishConfig": {
46
+ "access": "public",
47
+ "registry": "https://registry.npmjs.com/"
48
+ },
49
+ "repository": {
50
+ "type": "git",
51
+ "url": "git+https://github.com/BetterTyped/hyper-fetch.git",
52
+ "directory": "packages/core"
53
+ },
54
+ "bugs": {
55
+ "url": "https://github.com/BetterTyped/hyper-fetch/issues"
56
+ },
57
+ "keywords": [
58
+ "react",
59
+ "hyper-fetch",
60
+ "fetch",
61
+ "xhr",
62
+ "queue",
63
+ "cache",
64
+ "persist",
65
+ "hooks",
66
+ "useFetch",
67
+ "useSubmit",
68
+ "useCache",
69
+ "useQueue",
70
+ "graphql",
71
+ "swr",
72
+ "dispatcher"
73
+ ]
74
+ }
@@ -0,0 +1,30 @@
1
+ import React, { useContext, useMemo, useState } from "react";
2
+
3
+ import { ConfigProviderProps, ConfigProviderValueType } from "./config-provider.types";
4
+
5
+ const ConfigContext = React.createContext<ConfigProviderValueType>([{}, () => null]);
6
+
7
+ /**
8
+ * Context provider with configuration for hooks
9
+ * @param options
10
+ * @returns
11
+ */
12
+ export const ConfigProvider = ({ children, config }: ConfigProviderProps) => {
13
+ const [currentConfig, setConfig] = useState(config || {});
14
+
15
+ const value = useMemo(() => {
16
+ const contextValue: ConfigProviderValueType = [currentConfig, setConfig];
17
+ return contextValue;
18
+ }, [currentConfig]);
19
+
20
+ return <ConfigContext.Provider value={value}>{children}</ConfigContext.Provider>;
21
+ };
22
+
23
+ /**
24
+ * Hook to allow reading current context config
25
+ * @returns
26
+ */
27
+ export const useConfigProvider = (): ConfigProviderValueType => {
28
+ const config = useContext(ConfigContext);
29
+ return config;
30
+ };
@@ -0,0 +1,21 @@
1
+ import React from "react";
2
+ import { CommandInstance } from "@hyper-fetch/core";
3
+
4
+ import { UseFetchOptionsType } from "hooks/use-fetch";
5
+ import { UseSubmitOptionsType } from "hooks/use-submit";
6
+ import { UseCacheOptionsType } from "hooks/use-cache";
7
+ import { UseQueueOptionsType } from "hooks/use-queue";
8
+
9
+ export type ConfigProviderOptionsType = {
10
+ useFetchConfig?: Partial<UseFetchOptionsType<CommandInstance>>;
11
+ useSubmitConfig?: Partial<UseSubmitOptionsType<CommandInstance>>;
12
+ useCacheConfig?: Partial<UseCacheOptionsType<CommandInstance>>;
13
+ useQueueConfig?: Partial<UseQueueOptionsType>;
14
+ };
15
+
16
+ export type ConfigProviderProps = {
17
+ children: React.ReactNode;
18
+ config?: ConfigProviderOptionsType;
19
+ };
20
+
21
+ export type ConfigProviderValueType = [ConfigProviderOptionsType, (newConfig: ConfigProviderOptionsType) => void];
@@ -0,0 +1,2 @@
1
+ export * from "./config-provider";
2
+ export * from "./config-provider.types";
@@ -0,0 +1,2 @@
1
+ export * from "./use-command-events";
2
+ export * from "./use-tracked-state";
@@ -0,0 +1,2 @@
1
+ export * from "./use-command-events.hooks";
2
+ export * from "./use-command-events.types";
@@ -0,0 +1,283 @@
1
+ import { useRef } from "react";
2
+ import {
3
+ ExtractError,
4
+ ExtractResponse,
5
+ CommandInstance,
6
+ FetchProgressType,
7
+ ClientResponseType,
8
+ CommandEventDetails,
9
+ CommandResponseDetails,
10
+ CommandLoadingEventType,
11
+ } from "@hyper-fetch/core";
12
+ import { useWillUnmount } from "@better-typed/react-lifecycle-hooks";
13
+
14
+ import {
15
+ OnErrorCallbackType,
16
+ OnStartCallbackType,
17
+ OnSuccessCallbackType,
18
+ OnProgressCallbackType,
19
+ OnFinishedCallbackType,
20
+ UseCommandEventsDataMap,
21
+ UseCommandEventsReturnType,
22
+ UseCommandEventsPropsType,
23
+ UseCommandEventsLifecycleMap,
24
+ } from "helpers";
25
+
26
+ /**
27
+ * This is helper hook that handles main Hyper-Fetch event/data flow
28
+ * @internal
29
+ * @param options
30
+ * @returns
31
+ */
32
+ export const useCommandEvents = <T extends CommandInstance>({
33
+ command,
34
+ dispatcher,
35
+ logger,
36
+ actions,
37
+ setCacheData,
38
+ }: UseCommandEventsPropsType<T>): UseCommandEventsReturnType<T> => {
39
+ const { cache, commandManager } = command.builder;
40
+
41
+ // ******************
42
+ // Callbacks
43
+ // ******************
44
+
45
+ const onSuccessCallback = useRef<null | OnSuccessCallbackType<T>>(null);
46
+ const onErrorCallback = useRef<null | OnErrorCallbackType<T>>(null);
47
+ const onAbortCallback = useRef<null | OnErrorCallbackType<T>>(null);
48
+ const onOfflineErrorCallback = useRef<null | OnErrorCallbackType<T>>(null);
49
+ const onFinishedCallback = useRef<null | OnFinishedCallbackType<T>>(null);
50
+ const onRequestStartCallback = useRef<null | OnStartCallbackType<T>>(null);
51
+ const onResponseStartCallback = useRef<null | OnStartCallbackType<T>>(null);
52
+ const onDownloadProgressCallback = useRef<null | OnProgressCallbackType>(null);
53
+ const onUploadProgressCallback = useRef<null | OnProgressCallbackType>(null);
54
+
55
+ // ******************
56
+ // Listeners unmounting
57
+ // ******************
58
+
59
+ const lifecycleEvents = useRef<UseCommandEventsLifecycleMap>(new Map());
60
+ const dataEvents = useRef<UseCommandEventsDataMap | null>(null);
61
+
62
+ const removeLifecycleListener = (requestId: string) => {
63
+ const event = lifecycleEvents.current.get(requestId);
64
+ event?.unmount();
65
+ lifecycleEvents.current.delete(requestId);
66
+ };
67
+
68
+ const clearLifecycleListeners = () => {
69
+ const events = lifecycleEvents.current;
70
+ const listeners = Array.from(events.values());
71
+ listeners.forEach((value) => {
72
+ value.unmount();
73
+ });
74
+ events.clear();
75
+ };
76
+
77
+ // ******************
78
+ // Response handlers
79
+ // ******************
80
+
81
+ const handleResponseCallbacks = (
82
+ cmd: T,
83
+ data: ClientResponseType<ExtractResponse<T>, ExtractError<T>>,
84
+ details: CommandResponseDetails,
85
+ ) => {
86
+ const { isOffline, isFailed, isCanceled } = details;
87
+
88
+ if (command.offline && isOffline && isFailed) {
89
+ logger.debug("Performing offline error callback", { data, details });
90
+ onOfflineErrorCallback.current?.({ response: data[1] as ExtractError<T>, command: cmd, details });
91
+ } else if (isCanceled) {
92
+ logger.debug("Performing abort callback", { data, details });
93
+ onAbortCallback.current?.({ response: data[1] as ExtractError<T>, command: cmd, details });
94
+ } else if (!isFailed) {
95
+ logger.debug("Performing success callback", { data, details });
96
+ onSuccessCallback.current?.({ response: data[0] as ExtractResponse<T>, command: cmd, details });
97
+ } else {
98
+ logger.debug("Performing error callback", { data, details });
99
+ onErrorCallback.current?.({ response: data[1] as ExtractError<T>, command: cmd, details });
100
+ }
101
+ onFinishedCallback.current?.({ response: data, command: cmd, details });
102
+ };
103
+
104
+ // ******************
105
+ // Lifecycle
106
+ // ******************
107
+
108
+ const handleGetLoadingEvent = (queueKey: string) => {
109
+ return ({ loading }: CommandLoadingEventType) => {
110
+ const canDisableLoading = !loading && !dispatcher.hasRunningRequests(queueKey);
111
+ if (loading || canDisableLoading) {
112
+ actions.setLoading(loading, false);
113
+ }
114
+ };
115
+ };
116
+
117
+ const handleDownloadProgress = (progress: FetchProgressType, details: CommandEventDetails<T>) => {
118
+ onDownloadProgressCallback.current?.(progress, details);
119
+ };
120
+
121
+ const handleUploadProgress = (progress: FetchProgressType, details: CommandEventDetails<T>) => {
122
+ onUploadProgressCallback.current?.(progress, details);
123
+ };
124
+
125
+ const handleRequestStart = (cmd: T) => {
126
+ return (details: CommandEventDetails<T>) => {
127
+ onRequestStartCallback.current?.({ command: cmd, details });
128
+ };
129
+ };
130
+ const handleResponseStart = (cmd: T) => {
131
+ return (details: CommandEventDetails<T>) => {
132
+ onResponseStartCallback.current?.({ command: cmd, details });
133
+ };
134
+ };
135
+
136
+ const handleResponse = (cmd: T) => {
137
+ return (data: ClientResponseType<ExtractResponse<T>, ExtractError<T>>, details: CommandResponseDetails) => {
138
+ handleResponseCallbacks(cmd, data, details);
139
+ };
140
+ };
141
+
142
+ const handleRemove = ({ requestId }: CommandEventDetails<T>) => {
143
+ removeLifecycleListener(requestId);
144
+ };
145
+
146
+ // ******************
147
+ // Data Listeners
148
+ // ******************
149
+
150
+ const clearDataListener = () => {
151
+ dataEvents.current?.unmount();
152
+ dataEvents.current = null;
153
+ };
154
+
155
+ const addDataListener = (cmd: T) => {
156
+ // Data handlers
157
+ const loadingUnmount = commandManager.events.onLoading(cmd.queueKey, handleGetLoadingEvent(cmd.queueKey));
158
+ const getResponseUnmount = cache.events.onData<ExtractResponse<T>, ExtractError<T>>(cmd.cacheKey, setCacheData);
159
+
160
+ const unmount = () => {
161
+ loadingUnmount();
162
+ getResponseUnmount();
163
+ };
164
+
165
+ clearDataListener();
166
+ dataEvents.current = { unmount };
167
+
168
+ return unmount;
169
+ };
170
+
171
+ // ******************
172
+ // Lifecycle Listeners
173
+ // ******************
174
+
175
+ const addLifecycleListeners = (cmd: T, requestId?: string) => {
176
+ if (!requestId) {
177
+ const { queueKey, cacheKey } = cmd;
178
+ const requestStartUnmount = commandManager.events.onRequestStart(queueKey, handleRequestStart(cmd));
179
+ const responseStartUnmount = commandManager.events.onResponseStart(queueKey, handleResponseStart(cmd));
180
+ const uploadUnmount = commandManager.events.onUploadProgress(queueKey, handleUploadProgress);
181
+ const downloadUnmount = commandManager.events.onDownloadProgress(queueKey, handleDownloadProgress);
182
+ const responseUnmount = commandManager.events.onResponse(cacheKey, handleResponse(cmd));
183
+
184
+ const unmount = () => {
185
+ downloadUnmount();
186
+ uploadUnmount();
187
+ requestStartUnmount();
188
+ responseStartUnmount();
189
+ responseUnmount();
190
+ };
191
+
192
+ lifecycleEvents.current.set(queueKey, { unmount });
193
+
194
+ return unmount;
195
+ }
196
+ const requestRemove = commandManager.events.onRemoveById(requestId, handleRemove);
197
+ const requestStartUnmount = commandManager.events.onRequestStartById(requestId, handleRequestStart(cmd));
198
+ const responseStartUnmount = commandManager.events.onResponseStartById(requestId, handleResponseStart(cmd));
199
+ const responseUnmount = commandManager.events.onResponseById(requestId, handleResponse(cmd));
200
+ const uploadUnmount = commandManager.events.onUploadProgressById(requestId, handleUploadProgress);
201
+ const downloadUnmount = commandManager.events.onDownloadProgressById(requestId, handleDownloadProgress);
202
+
203
+ const unmount = () => {
204
+ requestRemove();
205
+ downloadUnmount();
206
+ uploadUnmount();
207
+ requestStartUnmount();
208
+ responseStartUnmount();
209
+ responseUnmount();
210
+ };
211
+
212
+ lifecycleEvents.current.set(requestId, { unmount });
213
+
214
+ return unmount;
215
+ };
216
+
217
+ // ******************
218
+ // Abort
219
+ // ******************
220
+
221
+ const abort = () => {
222
+ const { abortKey } = command;
223
+ const requests = dispatcher.getAllRunningRequest();
224
+ requests.forEach((request) => {
225
+ if (request.command.abortKey === abortKey) {
226
+ dispatcher.delete(request.command.queueKey, request.requestId, abortKey);
227
+ }
228
+ });
229
+ };
230
+
231
+ // ******************
232
+ // Lifecycle
233
+ // ******************
234
+
235
+ /**
236
+ * On unmount we want to clear all the listeners to prevent memory leaks
237
+ */
238
+ useWillUnmount(() => {
239
+ // Unmount listeners
240
+ clearLifecycleListeners();
241
+ clearDataListener();
242
+ });
243
+
244
+ return [
245
+ {
246
+ abort,
247
+ onSuccess: (callback: OnSuccessCallbackType<T>) => {
248
+ onSuccessCallback.current = callback;
249
+ },
250
+ onError: (callback: OnErrorCallbackType<T>) => {
251
+ onErrorCallback.current = callback;
252
+ },
253
+ onAbort: (callback: OnErrorCallbackType<T>) => {
254
+ onAbortCallback.current = callback;
255
+ },
256
+ onOfflineError: (callback: OnErrorCallbackType<T>) => {
257
+ onOfflineErrorCallback.current = callback;
258
+ },
259
+ onFinished: (callback: OnFinishedCallbackType<T>) => {
260
+ onFinishedCallback.current = callback;
261
+ },
262
+ onRequestStart: (callback: OnStartCallbackType<T>) => {
263
+ onRequestStartCallback.current = callback;
264
+ },
265
+ onResponseStart: (callback: OnStartCallbackType<T>) => {
266
+ onResponseStartCallback.current = callback;
267
+ },
268
+ onDownloadProgress: (callback: OnProgressCallbackType) => {
269
+ onDownloadProgressCallback.current = callback;
270
+ },
271
+ onUploadProgress: (callback: OnProgressCallbackType) => {
272
+ onUploadProgressCallback.current = callback;
273
+ },
274
+ },
275
+ {
276
+ addDataListener,
277
+ clearDataListener,
278
+ addLifecycleListeners,
279
+ removeLifecycleListener,
280
+ clearLifecycleListeners,
281
+ },
282
+ ];
283
+ };
@@ -0,0 +1,108 @@
1
+ import {
2
+ Dispatcher,
3
+ ExtractError,
4
+ CacheValueType,
5
+ ExtractResponse,
6
+ LoggerType,
7
+ FetchProgressType,
8
+ ExtractClientReturnType,
9
+ CommandEventDetails,
10
+ CommandInstance,
11
+ CommandResponseDetails,
12
+ } from "@hyper-fetch/core";
13
+
14
+ import { UseTrackedStateActions } from "helpers";
15
+
16
+ // Misc
17
+ export type UseCommandEventsDataMap = { unmount: VoidFunction };
18
+ export type UseCommandEventsLifecycleMap = Map<string, { unmount: VoidFunction }>;
19
+
20
+ // Props
21
+ export type UseCommandEventsPropsType<T extends CommandInstance> = {
22
+ command: T;
23
+ dispatcher: Dispatcher;
24
+ logger: LoggerType;
25
+ actions: UseTrackedStateActions<T>;
26
+ setCacheData: (cacheData: CacheValueType<ExtractResponse<T>, ExtractError<T>>) => void;
27
+ };
28
+
29
+ export type UseCommandEventsActionsType<T extends CommandInstance> = {
30
+ /**
31
+ * Callback which allows to cancel ongoing requests from given queueKey.
32
+ */
33
+ abort: () => void;
34
+ /**
35
+ * Helper hook listening on success response.
36
+ */
37
+ onSuccess: (callback: OnSuccessCallbackType<T>) => void;
38
+ /**
39
+ * Helper hook listening on error response.
40
+ */
41
+ onError: (callback: OnErrorCallbackType<T>) => void;
42
+ /**
43
+ * Helper hook listening on aborting of requests. Abort events are not triggering onError callbacks.
44
+ */
45
+ onAbort: (callback: OnErrorCallbackType<T>) => void;
46
+ /**
47
+ * 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.
48
+ */
49
+ onOfflineError: (callback: OnErrorCallbackType<T>) => void;
50
+ /**
51
+ * Helper hook listening on any response.
52
+ */
53
+ onFinished: (callback: OnFinishedCallbackType<T>) => void;
54
+ /**
55
+ * Helper hook listening on request start.
56
+ */
57
+ onRequestStart: (callback: OnStartCallbackType<T>) => void;
58
+ /**
59
+ * Helper hook listening on response start(before we receive all data from server).
60
+ */
61
+ onResponseStart: (callback: OnStartCallbackType<T>) => void;
62
+ /**
63
+ * 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.
64
+ */
65
+ onDownloadProgress: (callback: OnProgressCallbackType) => void;
66
+ /**
67
+ * 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.
68
+ */
69
+ onUploadProgress: (callback: OnProgressCallbackType) => void;
70
+ };
71
+
72
+ // Return
73
+ export type UseCommandEventsReturnType<T extends CommandInstance> = [
74
+ UseCommandEventsActionsType<T>,
75
+ {
76
+ addDataListener: (command: CommandInstance) => VoidFunction;
77
+ clearDataListener: VoidFunction;
78
+ addLifecycleListeners: (command: CommandInstance, requestId?: string) => VoidFunction;
79
+ removeLifecycleListener: (requestId: string) => void;
80
+ clearLifecycleListeners: () => void;
81
+ },
82
+ ];
83
+
84
+ // Lifecycle
85
+
86
+ export type CallbackParameters<Command, ResponseType> = {
87
+ response: ResponseType;
88
+ details: CommandResponseDetails;
89
+ command: Command;
90
+ };
91
+
92
+ export type OnSuccessCallbackType<Command extends CommandInstance> = (
93
+ params: CallbackParameters<Command, ExtractResponse<Command>>,
94
+ ) => void | Promise<void>;
95
+ export type OnErrorCallbackType<Command extends CommandInstance> = (
96
+ params: CallbackParameters<Command, ExtractError<Command>>,
97
+ ) => void | Promise<void>;
98
+ export type OnFinishedCallbackType<Command extends CommandInstance> = (
99
+ params: CallbackParameters<Command, ExtractClientReturnType<Command>>,
100
+ ) => void | Promise<void>;
101
+ export type OnStartCallbackType<Command extends CommandInstance> = (params: {
102
+ details: CommandEventDetails<Command>;
103
+ command: Command;
104
+ }) => void | Promise<void>;
105
+ export type OnProgressCallbackType = <Command extends CommandInstance>(
106
+ progress: FetchProgressType,
107
+ details: CommandEventDetails<Command>,
108
+ ) => void | Promise<void>;
@@ -0,0 +1,4 @@
1
+ export * from "./use-tracked-state.constants";
2
+ export * from "./use-tracked-state.hooks";
3
+ export * from "./use-tracked-state.types";
4
+ export * from "./use-tracked-state.utils";
@@ -0,0 +1,10 @@
1
+ import { UseTrackedStateType } from "./use-tracked-state.types";
2
+
3
+ export const initialState: UseTrackedStateType = {
4
+ data: null,
5
+ error: null,
6
+ loading: false,
7
+ status: null,
8
+ retries: 0,
9
+ timestamp: null,
10
+ };