@hyper-fetch/react 2.1.0 → 3.0.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 (56) hide show
  1. package/README.md +1 -1
  2. package/dist/config-provider/config-provider.types.d.ts +11 -5
  3. package/dist/helpers/index.d.ts +1 -1
  4. package/dist/helpers/use-request-events/index.d.ts +2 -0
  5. package/dist/helpers/use-request-events/use-request-events.hooks.d.ts +8 -0
  6. package/dist/helpers/{use-command-events/use-command-events.types.d.ts → use-request-events/use-request-events.types.d.ts} +24 -24
  7. package/dist/helpers/use-tracked-state/use-tracked-state.hooks.d.ts +2 -2
  8. package/dist/helpers/use-tracked-state/use-tracked-state.types.d.ts +12 -12
  9. package/dist/helpers/use-tracked-state/use-tracked-state.utils.d.ts +4 -4
  10. package/dist/hooks/use-app-manager/use-app-manager.hooks.d.ts +1 -1
  11. package/dist/hooks/use-cache/use-cache.hooks.d.ts +1 -1
  12. package/dist/hooks/use-cache/use-cache.types.d.ts +6 -6
  13. package/dist/hooks/use-emitter/use-emitter.hooks.d.ts +1 -1
  14. package/dist/hooks/use-event-messages/use-event-messages.hooks.d.ts +1 -1
  15. package/dist/hooks/use-fetch/use-fetch.constants.d.ts +2 -2
  16. package/dist/hooks/use-fetch/use-fetch.hooks.d.ts +2 -2
  17. package/dist/hooks/use-fetch/use-fetch.types.d.ts +10 -6
  18. package/dist/hooks/use-listener/use-listener.hooks.d.ts +3 -3
  19. package/dist/hooks/use-queue/use-queue.hooks.d.ts +2 -2
  20. package/dist/hooks/use-queue/use-queue.types.d.ts +7 -7
  21. package/dist/hooks/use-submit/use-submit.constants.d.ts +2 -2
  22. package/dist/hooks/use-submit/use-submit.hooks.d.ts +2 -2
  23. package/dist/hooks/use-submit/use-submit.types.d.ts +20 -16
  24. package/dist/index.cjs.js +1 -1
  25. package/dist/index.cjs.js.map +4 -4
  26. package/dist/index.esm.js +1 -1
  27. package/dist/index.esm.js.map +4 -4
  28. package/dist/types/invalidation.types.d.ts +2 -2
  29. package/package.json +3 -3
  30. package/src/config-provider/config-provider.types.ts +11 -5
  31. package/src/helpers/index.ts +1 -1
  32. package/src/helpers/use-request-events/index.ts +2 -0
  33. package/src/helpers/{use-command-events/use-command-events.hooks.ts → use-request-events/use-request-events.hooks.ts} +56 -53
  34. package/src/helpers/{use-command-events/use-command-events.types.ts → use-request-events/use-request-events.types.ts} +35 -35
  35. package/src/helpers/use-socket-state/use-socket-state.hooks.ts +1 -1
  36. package/src/helpers/use-tracked-state/use-tracked-state.hooks.ts +16 -16
  37. package/src/helpers/use-tracked-state/use-tracked-state.types.ts +15 -15
  38. package/src/helpers/use-tracked-state/use-tracked-state.utils.ts +22 -22
  39. package/src/hooks/use-app-manager/use-app-manager.hooks.ts +11 -11
  40. package/src/hooks/use-cache/use-cache.hooks.ts +13 -13
  41. package/src/hooks/use-cache/use-cache.types.ts +6 -6
  42. package/src/hooks/use-emitter/use-emitter.hooks.ts +5 -1
  43. package/src/hooks/use-event-messages/use-event-messages.hooks.ts +6 -2
  44. package/src/hooks/use-fetch/use-fetch.constants.ts +3 -2
  45. package/src/hooks/use-fetch/use-fetch.hooks.ts +26 -25
  46. package/src/hooks/use-fetch/use-fetch.types.ts +11 -7
  47. package/src/hooks/use-listener/use-listener.hooks.ts +9 -5
  48. package/src/hooks/use-queue/use-queue.hooks.ts +23 -23
  49. package/src/hooks/use-queue/use-queue.types.ts +7 -7
  50. package/src/hooks/use-submit/use-submit.constants.ts +3 -2
  51. package/src/hooks/use-submit/use-submit.hooks.ts +42 -41
  52. package/src/hooks/use-submit/use-submit.types.ts +28 -18
  53. package/src/types/invalidation.types.ts +2 -2
  54. package/dist/helpers/use-command-events/index.d.ts +0 -2
  55. package/dist/helpers/use-command-events/use-command-events.hooks.d.ts +0 -8
  56. package/src/helpers/use-command-events/index.ts +0 -2
@@ -1,15 +1,15 @@
1
1
  import { useRef } from "react";
2
2
  import {
3
- ExtractError,
4
- ExtractResponse,
5
- CommandInstance,
6
- FetchProgressType,
7
- ClientResponseType,
8
- CommandEventDetails,
9
- CommandResponseDetails,
10
- CommandLoadingEventType,
3
+ ExtractErrorType,
4
+ ExtractResponseType,
5
+ RequestInstance,
6
+ ProgressType,
7
+ ResponseType,
8
+ RequestEventType,
9
+ ResponseDetailsType,
10
+ RequestLoadingEventType,
11
11
  } from "@hyper-fetch/core";
12
- import { useWillUnmount } from "@better-typed/react-lifecycle-hooks";
12
+ import { useWillUnmount } from "@better-hooks/lifecycle";
13
13
 
14
14
  import {
15
15
  OnErrorCallbackType,
@@ -17,10 +17,10 @@ import {
17
17
  OnSuccessCallbackType,
18
18
  OnProgressCallbackType,
19
19
  OnFinishedCallbackType,
20
- UseCommandEventsDataMap,
21
- UseCommandEventsReturnType,
22
- UseCommandEventsPropsType,
23
- UseCommandEventsLifecycleMap,
20
+ UseRequestEventsDataMap,
21
+ UseRequestEventsReturnType,
22
+ UseRequestEventsPropsType,
23
+ UseRequestEventsLifecycleMap,
24
24
  } from "helpers";
25
25
 
26
26
  /**
@@ -29,14 +29,14 @@ import {
29
29
  * @param options
30
30
  * @returns
31
31
  */
32
- export const useCommandEvents = <T extends CommandInstance>({
33
- command,
32
+ export const useRequestEvents = <T extends RequestInstance>({
33
+ request,
34
34
  dispatcher,
35
35
  logger,
36
36
  actions,
37
37
  setCacheData,
38
- }: UseCommandEventsPropsType<T>): UseCommandEventsReturnType<T> => {
39
- const { cache, commandManager } = command.builder;
38
+ }: UseRequestEventsPropsType<T>): UseRequestEventsReturnType<T> => {
39
+ const { cache, requestManager } = request.client;
40
40
 
41
41
  // ******************
42
42
  // Callbacks
@@ -56,8 +56,8 @@ export const useCommandEvents = <T extends CommandInstance>({
56
56
  // Listeners unmounting
57
57
  // ******************
58
58
 
59
- const lifecycleEvents = useRef<UseCommandEventsLifecycleMap>(new Map());
60
- const dataEvents = useRef<UseCommandEventsDataMap | null>(null);
59
+ const lifecycleEvents = useRef<UseRequestEventsLifecycleMap>(new Map());
60
+ const dataEvents = useRef<UseRequestEventsDataMap | null>(null);
61
61
 
62
62
  const removeLifecycleListener = (requestId: string) => {
63
63
  const event = lifecycleEvents.current.get(requestId);
@@ -80,25 +80,25 @@ export const useCommandEvents = <T extends CommandInstance>({
80
80
 
81
81
  const handleResponseCallbacks = (
82
82
  cmd: T,
83
- data: ClientResponseType<ExtractResponse<T>, ExtractError<T>>,
84
- details: CommandResponseDetails,
83
+ data: ResponseType<ExtractResponseType<T>, ExtractErrorType<T>>,
84
+ details: ResponseDetailsType,
85
85
  ) => {
86
86
  const { isOffline, isFailed, isCanceled } = details;
87
87
 
88
- if (command.offline && isOffline && isFailed) {
88
+ if (request.offline && isOffline && isFailed) {
89
89
  logger.debug("Performing offline error callback", { data, details });
90
- onOfflineErrorCallback.current?.({ response: data[1] as ExtractError<T>, command: cmd, details });
90
+ onOfflineErrorCallback.current?.({ response: data[1] as ExtractErrorType<T>, request: cmd, details });
91
91
  } else if (isCanceled) {
92
92
  logger.debug("Performing abort callback", { data, details });
93
- onAbortCallback.current?.({ response: data[1] as ExtractError<T>, command: cmd, details });
93
+ onAbortCallback.current?.({ response: data[1] as ExtractErrorType<T>, request: cmd, details });
94
94
  } else if (!isFailed) {
95
95
  logger.debug("Performing success callback", { data, details });
96
- onSuccessCallback.current?.({ response: data[0] as ExtractResponse<T>, command: cmd, details });
96
+ onSuccessCallback.current?.({ response: data[0] as ExtractResponseType<T>, request: cmd, details });
97
97
  } else {
98
98
  logger.debug("Performing error callback", { data, details });
99
- onErrorCallback.current?.({ response: data[1] as ExtractError<T>, command: cmd, details });
99
+ onErrorCallback.current?.({ response: data[1] as ExtractErrorType<T>, request: cmd, details });
100
100
  }
101
- onFinishedCallback.current?.({ response: data, command: cmd, details });
101
+ onFinishedCallback.current?.({ response: data, request: cmd, details });
102
102
  };
103
103
 
104
104
  // ******************
@@ -106,7 +106,7 @@ export const useCommandEvents = <T extends CommandInstance>({
106
106
  // ******************
107
107
 
108
108
  const handleGetLoadingEvent = (queueKey: string) => {
109
- return ({ loading }: CommandLoadingEventType) => {
109
+ return ({ loading }: RequestLoadingEventType) => {
110
110
  const canDisableLoading = !loading && !dispatcher.hasRunningRequests(queueKey);
111
111
  if (loading || canDisableLoading) {
112
112
  actions.setLoading(loading, false);
@@ -114,32 +114,32 @@ export const useCommandEvents = <T extends CommandInstance>({
114
114
  };
115
115
  };
116
116
 
117
- const handleDownloadProgress = (progress: FetchProgressType, details: CommandEventDetails<T>) => {
117
+ const handleDownloadProgress = (progress: ProgressType, details: RequestEventType<T>) => {
118
118
  onDownloadProgressCallback.current?.(progress, details);
119
119
  };
120
120
 
121
- const handleUploadProgress = (progress: FetchProgressType, details: CommandEventDetails<T>) => {
121
+ const handleUploadProgress = (progress: ProgressType, details: RequestEventType<T>) => {
122
122
  onUploadProgressCallback.current?.(progress, details);
123
123
  };
124
124
 
125
125
  const handleRequestStart = (cmd: T) => {
126
- return (details: CommandEventDetails<T>) => {
127
- onRequestStartCallback.current?.({ command: cmd, details });
126
+ return (details: RequestEventType<T>) => {
127
+ onRequestStartCallback.current?.({ request: cmd, details });
128
128
  };
129
129
  };
130
130
  const handleResponseStart = (cmd: T) => {
131
- return (details: CommandEventDetails<T>) => {
132
- onResponseStartCallback.current?.({ command: cmd, details });
131
+ return (details: RequestEventType<T>) => {
132
+ onResponseStartCallback.current?.({ request: cmd, details });
133
133
  };
134
134
  };
135
135
 
136
136
  const handleResponse = (cmd: T) => {
137
- return (data: ClientResponseType<ExtractResponse<T>, ExtractError<T>>, details: CommandResponseDetails) => {
137
+ return (data: ResponseType<ExtractResponseType<T>, ExtractErrorType<T>>, details: ResponseDetailsType) => {
138
138
  handleResponseCallbacks(cmd, data, details);
139
139
  };
140
140
  };
141
141
 
142
- const handleRemove = ({ requestId }: CommandEventDetails<T>) => {
142
+ const handleRemove = ({ requestId }: RequestEventType<T>) => {
143
143
  removeLifecycleListener(requestId);
144
144
  };
145
145
 
@@ -154,8 +154,11 @@ export const useCommandEvents = <T extends CommandInstance>({
154
154
 
155
155
  const addDataListener = (cmd: T) => {
156
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);
157
+ const loadingUnmount = requestManager.events.onLoading(cmd.queueKey, handleGetLoadingEvent(cmd.queueKey));
158
+ const getResponseUnmount = cache.events.onData<ExtractResponseType<T>, ExtractErrorType<T>>(
159
+ cmd.cacheKey,
160
+ setCacheData,
161
+ );
159
162
 
160
163
  const unmount = () => {
161
164
  loadingUnmount();
@@ -175,11 +178,11 @@ export const useCommandEvents = <T extends CommandInstance>({
175
178
  const addLifecycleListeners = (cmd: T, requestId?: string) => {
176
179
  if (!requestId) {
177
180
  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));
181
+ const requestStartUnmount = requestManager.events.onRequestStart(queueKey, handleRequestStart(cmd));
182
+ const responseStartUnmount = requestManager.events.onResponseStart(queueKey, handleResponseStart(cmd));
183
+ const uploadUnmount = requestManager.events.onUploadProgress(queueKey, handleUploadProgress);
184
+ const downloadUnmount = requestManager.events.onDownloadProgress(queueKey, handleDownloadProgress);
185
+ const responseUnmount = requestManager.events.onResponse(cacheKey, handleResponse(cmd));
183
186
 
184
187
  const unmount = () => {
185
188
  downloadUnmount();
@@ -193,12 +196,12 @@ export const useCommandEvents = <T extends CommandInstance>({
193
196
 
194
197
  return unmount;
195
198
  }
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);
199
+ const requestRemove = requestManager.events.onRemoveById(requestId, handleRemove);
200
+ const requestStartUnmount = requestManager.events.onRequestStartById(requestId, handleRequestStart(cmd));
201
+ const responseStartUnmount = requestManager.events.onResponseStartById(requestId, handleResponseStart(cmd));
202
+ const responseUnmount = requestManager.events.onResponseById(requestId, handleResponse(cmd));
203
+ const uploadUnmount = requestManager.events.onUploadProgressById(requestId, handleUploadProgress);
204
+ const downloadUnmount = requestManager.events.onDownloadProgressById(requestId, handleDownloadProgress);
202
205
 
203
206
  const unmount = () => {
204
207
  requestRemove();
@@ -219,11 +222,11 @@ export const useCommandEvents = <T extends CommandInstance>({
219
222
  // ******************
220
223
 
221
224
  const abort = () => {
222
- const { abortKey } = command;
225
+ const { abortKey } = request;
223
226
  const requests = dispatcher.getAllRunningRequest();
224
- requests.forEach((request) => {
225
- if (request.command.abortKey === abortKey) {
226
- dispatcher.delete(request.command.queueKey, request.requestId, abortKey);
227
+ requests.forEach((requestData) => {
228
+ if (requestData.request.abortKey === abortKey) {
229
+ dispatcher.delete(requestData.request.queueKey, requestData.requestId, abortKey);
227
230
  }
228
231
  });
229
232
  };
@@ -1,32 +1,32 @@
1
1
  import {
2
2
  Dispatcher,
3
- ExtractError,
3
+ ExtractErrorType,
4
4
  CacheValueType,
5
- ExtractResponse,
5
+ ExtractResponseType,
6
6
  LoggerType,
7
- FetchProgressType,
8
- ExtractClientReturnType,
9
- CommandEventDetails,
10
- CommandInstance,
11
- CommandResponseDetails,
7
+ ProgressType,
8
+ ExtractAdapterReturnType,
9
+ RequestEventType,
10
+ RequestInstance,
11
+ ResponseDetailsType,
12
12
  } from "@hyper-fetch/core";
13
13
 
14
14
  import { UseTrackedStateActions } from "helpers";
15
15
 
16
16
  // Misc
17
- export type UseCommandEventsDataMap = { unmount: VoidFunction };
18
- export type UseCommandEventsLifecycleMap = Map<string, { unmount: VoidFunction }>;
17
+ export type UseRequestEventsDataMap = { unmount: VoidFunction };
18
+ export type UseRequestEventsLifecycleMap = Map<string, { unmount: VoidFunction }>;
19
19
 
20
20
  // Props
21
- export type UseCommandEventsPropsType<T extends CommandInstance> = {
22
- command: T;
21
+ export type UseRequestEventsPropsType<T extends RequestInstance> = {
22
+ request: T;
23
23
  dispatcher: Dispatcher;
24
24
  logger: LoggerType;
25
25
  actions: UseTrackedStateActions<T>;
26
- setCacheData: (cacheData: CacheValueType<ExtractResponse<T>, ExtractError<T>>) => void;
26
+ setCacheData: (cacheData: CacheValueType<ExtractResponseType<T>, ExtractErrorType<T>>) => void;
27
27
  };
28
28
 
29
- export type UseCommandEventsActionsType<T extends CommandInstance> = {
29
+ export type UseRequestEventsActionsType<T extends RequestInstance> = {
30
30
  /**
31
31
  * Callback that allows canceling ongoing requests from the given queueKey.
32
32
  */
@@ -44,7 +44,7 @@ export type UseCommandEventsActionsType<T extends CommandInstance> = {
44
44
  */
45
45
  onAbort: (callback: OnErrorCallbackType<T>) => void;
46
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.
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 request.
48
48
  */
49
49
  onOfflineError: (callback: OnErrorCallbackType<T>) => void;
50
50
  /**
@@ -60,22 +60,22 @@ export type UseCommandEventsActionsType<T extends CommandInstance> = {
60
60
  */
61
61
  onResponseStart: (callback: OnStartCallbackType<T>) => void;
62
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.
63
+ * Helper hook listening on download progress ETA. We can later match given requests by their id's or request instance which holds all data which is being transferred.
64
64
  */
65
65
  onDownloadProgress: (callback: OnProgressCallbackType) => void;
66
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.
67
+ * Helper hook listening on upload progress ETA. We can later match given requests by their id's or request instance which holds all data which is being transferred.
68
68
  */
69
69
  onUploadProgress: (callback: OnProgressCallbackType) => void;
70
70
  };
71
71
 
72
72
  // Return
73
- export type UseCommandEventsReturnType<T extends CommandInstance> = [
74
- UseCommandEventsActionsType<T>,
73
+ export type UseRequestEventsReturnType<T extends RequestInstance> = [
74
+ UseRequestEventsActionsType<T>,
75
75
  {
76
- addDataListener: (command: CommandInstance) => VoidFunction;
76
+ addDataListener: (request: RequestInstance) => VoidFunction;
77
77
  clearDataListener: VoidFunction;
78
- addLifecycleListeners: (command: CommandInstance, requestId?: string) => VoidFunction;
78
+ addLifecycleListeners: (request: RequestInstance, requestId?: string) => VoidFunction;
79
79
  removeLifecycleListener: (requestId: string) => void;
80
80
  clearLifecycleListeners: () => void;
81
81
  },
@@ -83,26 +83,26 @@ export type UseCommandEventsReturnType<T extends CommandInstance> = [
83
83
 
84
84
  // Lifecycle
85
85
 
86
- export type CallbackParameters<Command, ResponseType> = {
86
+ export type CallbackParameters<Request, ResponseType> = {
87
87
  response: ResponseType;
88
- details: CommandResponseDetails;
89
- command: Command;
88
+ details: ResponseDetailsType;
89
+ request: Request;
90
90
  };
91
91
 
92
- export type OnSuccessCallbackType<Command extends CommandInstance> = (
93
- params: CallbackParameters<Command, ExtractResponse<Command>>,
92
+ export type OnSuccessCallbackType<Request extends RequestInstance> = (
93
+ params: CallbackParameters<Request, ExtractResponseType<Request>>,
94
94
  ) => void | Promise<void>;
95
- export type OnErrorCallbackType<Command extends CommandInstance> = (
96
- params: CallbackParameters<Command, ExtractError<Command>>,
95
+ export type OnErrorCallbackType<Request extends RequestInstance> = (
96
+ params: CallbackParameters<Request, ExtractErrorType<Request>>,
97
97
  ) => void | Promise<void>;
98
- export type OnFinishedCallbackType<Command extends CommandInstance> = (
99
- params: CallbackParameters<Command, ExtractClientReturnType<Command>>,
98
+ export type OnFinishedCallbackType<Request extends RequestInstance> = (
99
+ params: CallbackParameters<Request, ExtractAdapterReturnType<Request>>,
100
100
  ) => void | Promise<void>;
101
- export type OnStartCallbackType<Command extends CommandInstance> = (params: {
102
- details: CommandEventDetails<Command>;
103
- command: Command;
101
+ export type OnStartCallbackType<Request extends RequestInstance> = (params: {
102
+ details: RequestEventType<Request>;
103
+ request: Request;
104
104
  }) => void | Promise<void>;
105
- export type OnProgressCallbackType = <Command extends CommandInstance>(
106
- progress: FetchProgressType,
107
- details: CommandEventDetails<Command>,
105
+ export type OnProgressCallbackType = <Request extends RequestInstance>(
106
+ progress: ProgressType,
107
+ details: RequestEventType<Request>,
108
108
  ) => void | Promise<void>;
@@ -1,6 +1,6 @@
1
1
  import { useRef } from "react";
2
2
  import { SocketInstance } from "@hyper-fetch/sockets";
3
- import { useDidMount, useDidUpdate, useForceUpdate } from "@better-typed/react-lifecycle-hooks";
3
+ import { useDidMount, useDidUpdate, useForceUpdate } from "@better-hooks/lifecycle";
4
4
 
5
5
  import { UseSocketStateType, initialSocketState, UseSocketStateProps } from "helpers";
6
6
 
@@ -1,6 +1,6 @@
1
1
  import { useRef } from "react";
2
- import { useDidUpdate, useForceUpdate } from "@better-typed/react-lifecycle-hooks";
3
- import { ExtractError, CacheValueType, ExtractResponse, CommandInstance } from "@hyper-fetch/core";
2
+ import { useDidUpdate, useForceUpdate } from "@better-hooks/lifecycle";
3
+ import { ExtractErrorType, CacheValueType, ExtractResponseType, RequestInstance } from "@hyper-fetch/core";
4
4
 
5
5
  import { isEqual } from "utils";
6
6
  import {
@@ -13,26 +13,26 @@ import { getDetailsState, getInitialState, isStaleCacheData } from "./use-tracke
13
13
 
14
14
  /**
15
15
  *
16
- * @param command
16
+ * @param request
17
17
  * @param initialData
18
18
  * @param dispatcher
19
19
  * @param dependencies
20
20
  * @internal
21
21
  */
22
- export const useTrackedState = <T extends CommandInstance>({
23
- command,
22
+ export const useTrackedState = <T extends RequestInstance>({
23
+ request,
24
24
  dispatcher,
25
25
  initialData,
26
26
  deepCompare,
27
27
  dependencyTracking,
28
28
  defaultCacheEmitting = true,
29
29
  }: UseTrackedStateProps<T>): UseTrackedStateReturn<T> => {
30
- const { builder, cacheKey, queueKey, cacheTime } = command;
31
- const { cache, commandManager } = builder;
30
+ const { client, cacheKey, queueKey, cacheTime } = request;
31
+ const { cache, requestManager } = client;
32
32
 
33
33
  const forceUpdate = useForceUpdate();
34
34
 
35
- const state = useRef<UseTrackedStateType<T>>(getInitialState(initialData, dispatcher, command));
35
+ const state = useRef<UseTrackedStateType<T>>(getInitialState(initialData, dispatcher, request));
36
36
  const renderKeys = useRef<Array<keyof UseTrackedStateType<T>>>([]);
37
37
 
38
38
  // ******************
@@ -70,7 +70,7 @@ export const useTrackedState = <T extends CommandInstance>({
70
70
  state.current.loading = dispatcher.hasRunningRequests(queueKey);
71
71
 
72
72
  // Get cache state
73
- const newState = getInitialState(initialData, dispatcher, command);
73
+ const newState = getInitialState(initialData, dispatcher, request);
74
74
 
75
75
  const hasInitialState = initialData?.[0] === state.current.data;
76
76
  const hasState = !!(state.current.data || state.current.error) && !hasInitialState;
@@ -119,7 +119,7 @@ export const useTrackedState = <T extends CommandInstance>({
119
119
  return false;
120
120
  };
121
121
 
122
- const setCacheData = async (cacheData: CacheValueType<ExtractResponse<T>, ExtractError<T>>) => {
122
+ const setCacheData = async (cacheData: CacheValueType<ExtractResponseType<T>, ExtractErrorType<T>>) => {
123
123
  const newStateValues: UseTrackedStateType<T> = {
124
124
  data: cacheData.data[0],
125
125
  error: cacheData.data[1],
@@ -153,7 +153,7 @@ export const useTrackedState = <T extends CommandInstance>({
153
153
  setData: (data, emitToCache = defaultCacheEmitting) => {
154
154
  if (emitToCache) {
155
155
  const currentState = state.current;
156
- cache.set(command, [data, currentState.error, currentState.status], getDetailsState(state.current));
156
+ cache.set(request, [data, currentState.error, currentState.status], getDetailsState(state.current));
157
157
  } else {
158
158
  state.current.data = data;
159
159
  renderKeyTrigger(["data"]);
@@ -163,7 +163,7 @@ export const useTrackedState = <T extends CommandInstance>({
163
163
  if (emitToCache) {
164
164
  const currentState = state.current;
165
165
  cache.set(
166
- command,
166
+ request,
167
167
  [currentState.data, error, currentState.status],
168
168
  getDetailsState(state.current, { isFailed: !!error }),
169
169
  );
@@ -174,7 +174,7 @@ export const useTrackedState = <T extends CommandInstance>({
174
174
  },
175
175
  setLoading: (loading, emitToHooks = true) => {
176
176
  if (emitToHooks) {
177
- commandManager.events.emitLoading(queueKey, "", {
177
+ requestManager.events.emitLoading(queueKey, "", {
178
178
  queueKey,
179
179
  requestId: "",
180
180
  loading,
@@ -189,7 +189,7 @@ export const useTrackedState = <T extends CommandInstance>({
189
189
  setStatus: (status, emitToCache = defaultCacheEmitting) => {
190
190
  if (emitToCache) {
191
191
  const currentState = state.current;
192
- cache.set(command, [currentState.data, currentState.error, status], getDetailsState(state.current));
192
+ cache.set(request, [currentState.data, currentState.error, status], getDetailsState(state.current));
193
193
  } else {
194
194
  state.current.status = status;
195
195
  renderKeyTrigger(["status"]);
@@ -199,7 +199,7 @@ export const useTrackedState = <T extends CommandInstance>({
199
199
  if (emitToCache) {
200
200
  const currentState = state.current;
201
201
  cache.set(
202
- command,
202
+ request,
203
203
  [currentState.data, currentState.error, currentState.status],
204
204
  getDetailsState(state.current, { retries }),
205
205
  );
@@ -212,7 +212,7 @@ export const useTrackedState = <T extends CommandInstance>({
212
212
  if (emitToCache) {
213
213
  const currentState = state.current;
214
214
  cache.set(
215
- command,
215
+ request,
216
216
  [currentState.data, currentState.error, currentState.status],
217
217
  getDetailsState(state.current, { timestamp: +timestamp }),
218
218
  );
@@ -1,44 +1,44 @@
1
1
  import {
2
2
  Dispatcher,
3
- ExtractError,
3
+ ExtractErrorType,
4
4
  CacheValueType,
5
- ExtractResponse,
6
- ClientResponseType,
7
- CommandInstance,
5
+ ExtractResponseType,
6
+ ResponseType,
7
+ RequestInstance,
8
8
  LoggerType,
9
9
  } from "@hyper-fetch/core";
10
10
 
11
11
  import { isEqual } from "utils";
12
12
 
13
- export type UseTrackedStateProps<T extends CommandInstance> = {
14
- command: T;
13
+ export type UseTrackedStateProps<T extends RequestInstance> = {
14
+ request: T;
15
15
  logger: LoggerType;
16
- initialData: ClientResponseType<ExtractResponse<T>, ExtractError<T>> | null;
16
+ initialData: ResponseType<ExtractResponseType<T>, ExtractErrorType<T>> | null;
17
17
  dispatcher: Dispatcher;
18
18
  dependencyTracking: boolean;
19
19
  defaultCacheEmitting?: boolean;
20
20
  deepCompare: boolean | typeof isEqual;
21
21
  };
22
22
 
23
- export type UseTrackedStateReturn<T extends CommandInstance> = [
23
+ export type UseTrackedStateReturn<T extends RequestInstance> = [
24
24
  UseTrackedStateType<T>,
25
25
  UseTrackedStateActions<T>,
26
26
  {
27
27
  setRenderKey: (renderKey: keyof UseTrackedStateType<T>) => void;
28
- setCacheData: (cacheData: CacheValueType<ExtractResponse<T>, ExtractError<T>>) => void;
28
+ setCacheData: (cacheData: CacheValueType<ExtractResponseType<T>, ExtractErrorType<T>>) => void;
29
29
  getStaleStatus: () => boolean;
30
30
  },
31
31
  ];
32
32
 
33
- export type UseTrackedStateType<T extends CommandInstance = CommandInstance> = {
33
+ export type UseTrackedStateType<T extends RequestInstance = RequestInstance> = {
34
34
  /**
35
35
  * Request response data
36
36
  */
37
- data: null | ExtractResponse<T>;
37
+ data: null | ExtractResponseType<T>;
38
38
  /**
39
39
  * Request response error
40
40
  */
41
- error: null | ExtractError<T>;
41
+ error: null | ExtractErrorType<T>;
42
42
  /**
43
43
  * Request loading state
44
44
  */
@@ -57,15 +57,15 @@ export type UseTrackedStateType<T extends CommandInstance = CommandInstance> = {
57
57
  timestamp: null | Date;
58
58
  };
59
59
 
60
- export type UseTrackedStateActions<T extends CommandInstance> = {
60
+ export type UseTrackedStateActions<T extends RequestInstance> = {
61
61
  /**
62
62
  * Action to set custom data. We can do it locally(inside hook state) or in cache(all related sources) with 'emitToCache' option.
63
63
  */
64
- setData: (data: ExtractResponse<T>, emitToCache?: boolean) => void;
64
+ setData: (data: ExtractResponseType<T>, emitToCache?: boolean) => void;
65
65
  /**
66
66
  * Action to set custom error. We can do it locally(inside hook state) or in all hooks with 'emitToCache' option.
67
67
  */
68
- setError: (error: ExtractError<T>, emitToCache?: boolean) => void;
68
+ setError: (error: ExtractErrorType<T>, emitToCache?: boolean) => void;
69
69
  /**
70
70
  * Action to set custom loading. We can do it locally(inside hook state) or in cache(all related sources) with 'emitToCache' option
71
71
  */
@@ -1,18 +1,18 @@
1
1
  import {
2
2
  CacheValueType,
3
3
  NullableType,
4
- CommandInstance,
5
- ClientResponseType,
6
- ExtractResponse,
7
- ExtractError,
8
- ExtractClientReturnType,
4
+ RequestInstance,
5
+ ResponseType,
6
+ ExtractResponseType,
7
+ ExtractErrorType,
8
+ ExtractAdapterReturnType,
9
9
  Dispatcher,
10
10
  } from "@hyper-fetch/core";
11
11
 
12
12
  import { initialState, UseTrackedStateType } from "helpers";
13
13
 
14
14
  export const getDetailsState = (
15
- state?: UseTrackedStateType<CommandInstance>,
15
+ state?: UseTrackedStateType<RequestInstance>,
16
16
  details?: Partial<CacheValueType<unknown, unknown>["details"]>,
17
17
  ): CacheValueType<unknown, unknown>["details"] => {
18
18
  return {
@@ -30,12 +30,12 @@ export const isStaleCacheData = (cacheTime: number, cacheTimestamp: NullableType
30
30
  return +new Date() > +cacheTimestamp + cacheTime;
31
31
  };
32
32
 
33
- export const getValidCacheData = <T extends CommandInstance>(
34
- command: T,
35
- initialData: NullableType<ExtractClientReturnType<T>>,
36
- cacheData: NullableType<CacheValueType<ExtractResponse<T>, ExtractError<T>>>,
37
- ): CacheValueType<ExtractResponse<T>, ExtractError<T>> | null => {
38
- const isStale = isStaleCacheData(command.cacheTime, cacheData?.details.timestamp);
33
+ export const getValidCacheData = <T extends RequestInstance>(
34
+ request: T,
35
+ initialData: NullableType<ExtractAdapterReturnType<T>>,
36
+ cacheData: NullableType<CacheValueType<ExtractResponseType<T>, ExtractErrorType<T>>>,
37
+ ): CacheValueType<ExtractResponseType<T>, ExtractErrorType<T>> | null => {
38
+ const isStale = isStaleCacheData(request.cacheTime, cacheData?.details.timestamp);
39
39
 
40
40
  if (!isStale && cacheData) {
41
41
  return cacheData;
@@ -46,8 +46,8 @@ export const getValidCacheData = <T extends CommandInstance>(
46
46
  data: initialData,
47
47
  details: getDetailsState(),
48
48
  cacheTime: 1000,
49
- clearKey: command.builder.cache.clearKey,
50
- garbageCollection: command.garbageCollection,
49
+ clearKey: request.client.cache.clearKey,
50
+ garbageCollection: request.garbageCollection,
51
51
  };
52
52
  }
53
53
 
@@ -58,18 +58,18 @@ export const getTimestamp = (timestamp?: NullableType<number | Date>) => {
58
58
  return timestamp ? new Date(timestamp) : null;
59
59
  };
60
60
 
61
- export const getInitialState = <T extends CommandInstance>(
62
- initialData: ClientResponseType<ExtractResponse<T>, ExtractError<T>> | null,
61
+ export const getInitialState = <T extends RequestInstance>(
62
+ initialData: ResponseType<ExtractResponseType<T>, ExtractErrorType<T>> | null,
63
63
  dispatcher: Dispatcher,
64
- command: T,
64
+ request: T,
65
65
  ): UseTrackedStateType<T> => {
66
- const { builder, cacheKey } = command;
67
- const { cache } = builder;
66
+ const { client, cacheKey } = request;
67
+ const { cache } = client;
68
68
 
69
- const cacheData = cache.get<ExtractResponse<T>, ExtractError<T>>(cacheKey);
70
- const cacheState = getValidCacheData<T>(command, initialData, cacheData);
69
+ const cacheData = cache.get<ExtractResponseType<T>, ExtractErrorType<T>>(cacheKey);
70
+ const cacheState = getValidCacheData<T>(request, initialData, cacheData);
71
71
 
72
- const initialLoading = dispatcher.hasRunningRequests(command.queueKey);
72
+ const initialLoading = dispatcher.hasRunningRequests(request.queueKey);
73
73
 
74
74
  return {
75
75
  ...initialState,