@hyper-fetch/react 2.0.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 (58) hide show
  1. package/README.md +16 -15
  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 +2 -2
  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.d.ts +3 -0
  27. package/dist/index.esm.js +1 -1
  28. package/dist/index.esm.js.map +4 -4
  29. package/dist/types/invalidation.types.d.ts +2 -2
  30. package/package.json +3 -3
  31. package/src/config-provider/config-provider.types.ts +11 -5
  32. package/src/helpers/index.ts +1 -1
  33. package/src/helpers/use-request-events/index.ts +2 -0
  34. package/src/helpers/{use-command-events/use-command-events.hooks.ts → use-request-events/use-request-events.hooks.ts} +56 -53
  35. package/src/helpers/{use-command-events/use-command-events.types.ts → use-request-events/use-request-events.types.ts} +35 -35
  36. package/src/helpers/use-socket-state/use-socket-state.hooks.ts +1 -1
  37. package/src/helpers/use-tracked-state/use-tracked-state.hooks.ts +16 -16
  38. package/src/helpers/use-tracked-state/use-tracked-state.types.ts +15 -15
  39. package/src/helpers/use-tracked-state/use-tracked-state.utils.ts +22 -22
  40. package/src/hooks/use-app-manager/use-app-manager.hooks.ts +11 -11
  41. package/src/hooks/use-cache/use-cache.hooks.ts +13 -13
  42. package/src/hooks/use-cache/use-cache.types.ts +6 -6
  43. package/src/hooks/use-emitter/use-emitter.hooks.ts +9 -5
  44. package/src/hooks/use-event-messages/use-event-messages.hooks.ts +6 -2
  45. package/src/hooks/use-fetch/use-fetch.constants.ts +3 -2
  46. package/src/hooks/use-fetch/use-fetch.hooks.ts +26 -25
  47. package/src/hooks/use-fetch/use-fetch.types.ts +11 -7
  48. package/src/hooks/use-listener/use-listener.hooks.ts +9 -5
  49. package/src/hooks/use-queue/use-queue.hooks.ts +23 -23
  50. package/src/hooks/use-queue/use-queue.types.ts +7 -7
  51. package/src/hooks/use-submit/use-submit.constants.ts +3 -2
  52. package/src/hooks/use-submit/use-submit.hooks.ts +42 -41
  53. package/src/hooks/use-submit/use-submit.types.ts +28 -18
  54. package/src/index.ts +3 -0
  55. package/src/types/invalidation.types.ts +2 -2
  56. package/dist/helpers/use-command-events/index.d.ts +0 -2
  57. package/dist/helpers/use-command-events/use-command-events.hooks.d.ts +0 -8
  58. package/src/helpers/use-command-events/index.ts +0 -2
@@ -1,18 +1,18 @@
1
- import { FetchProgressType, CommandInstance, DispatcherDumpValueType } from "@hyper-fetch/core";
1
+ import { ProgressType, RequestInstance, DispatcherStorageValueType } from "@hyper-fetch/core";
2
2
 
3
3
  export type UseQueueOptionsType = {
4
4
  queueType?: "auto" | "fetch" | "submit";
5
5
  };
6
6
 
7
- export type QueueRequest<Command extends CommandInstance> = DispatcherDumpValueType<Command> & {
7
+ export type QueueRequest<Request extends RequestInstance> = DispatcherStorageValueType<Request> & {
8
8
  /**
9
9
  * Uploading progress for given request
10
10
  */
11
- uploading?: FetchProgressType;
11
+ uploading?: ProgressType;
12
12
  /**
13
13
  * Downloading progress for given request
14
14
  */
15
- downloading?: FetchProgressType;
15
+ downloading?: ProgressType;
16
16
  /**
17
17
  * Callback which allow to start previously stopped request.
18
18
  */
@@ -27,13 +27,13 @@ export type QueueRequest<Command extends CommandInstance> = DispatcherDumpValueT
27
27
  deleteRequest: () => void;
28
28
  };
29
29
 
30
- export type UseQueueReturnType<T extends CommandInstance> = {
30
+ export type UseQueueReturnType<T extends RequestInstance> = {
31
31
  /**
32
- * Queue status for provided command
32
+ * Queue status for provided request
33
33
  */
34
34
  stopped: boolean;
35
35
  /**
36
- * List of requests for provided command
36
+ * List of requests for provided request
37
37
  */
38
38
  requests: QueueRequest<T>[];
39
39
  /**
@@ -1,8 +1,8 @@
1
- import { CommandInstance, RequiredKeys } from "@hyper-fetch/core";
1
+ import { RequestInstance, RequiredKeys } from "@hyper-fetch/core";
2
2
 
3
3
  import { UseSubmitOptionsType } from "./use-submit.types";
4
4
 
5
- type DefaultOptionsType = RequiredKeys<Omit<UseSubmitOptionsType<CommandInstance>, "initialData">> & {
5
+ type DefaultOptionsType = RequiredKeys<Omit<UseSubmitOptionsType<RequestInstance>, "initialData">> & {
6
6
  initialData: null;
7
7
  };
8
8
 
@@ -13,5 +13,6 @@ export const useSubmitDefaultOptions: DefaultOptionsType = {
13
13
  bounce: false,
14
14
  bounceType: "debounce",
15
15
  bounceTime: 400,
16
+ bounceTimeout: 200,
16
17
  deepCompare: true,
17
18
  };
@@ -1,35 +1,35 @@
1
1
  import { useMemo, useRef } from "react";
2
2
  import {
3
- Command,
4
- getCommandKey,
5
- ExtractClientReturnType,
6
- commandSendRequest,
7
- CommandInstance,
8
- ClientResponseType,
9
- ExtractResponse,
10
- ExtractError,
11
- FetchType,
12
- FetchMethodType,
3
+ Request,
4
+ getRequestKey,
5
+ ExtractAdapterReturnType,
6
+ requestSendRequest,
7
+ RequestInstance,
8
+ ResponseType,
9
+ ExtractResponseType,
10
+ ExtractErrorType,
11
+ RequestSendOptionsType,
12
+ RequestSendType,
13
13
  } from "@hyper-fetch/core";
14
- import { useDidMount } from "@better-typed/react-lifecycle-hooks";
15
- import { useDebounce, useThrottle } from "@better-typed/react-performance-hooks";
14
+ import { useDidMount } from "@better-hooks/lifecycle";
15
+ import { useDebounce, useThrottle } from "@better-hooks/performance";
16
16
 
17
17
  import { UseSubmitOptionsType, useSubmitDefaultOptions, UseSubmitReturnType } from "hooks/use-submit";
18
- import { useTrackedState, useCommandEvents } from "helpers";
18
+ import { useTrackedState, useRequestEvents } from "helpers";
19
19
  import { useConfigProvider } from "config-provider";
20
20
  import { getBounceData } from "utils";
21
21
  import { InvalidationKeyType } from "types";
22
22
 
23
23
  /**
24
24
  * This hooks aims to mutate data on the server.
25
- * @param command
25
+ * @param request
26
26
  * @param options
27
27
  * @returns
28
28
  */
29
- export const useSubmit = <CommandType extends CommandInstance>(
30
- command: CommandType,
31
- options: UseSubmitOptionsType<CommandType> = useSubmitDefaultOptions,
32
- ): UseSubmitReturnType<CommandType> => {
29
+ export const useSubmit = <RequestType extends RequestInstance>(
30
+ request: RequestType,
31
+ options: UseSubmitOptionsType<RequestType> = useSubmitDefaultOptions,
32
+ ): UseSubmitReturnType<RequestType> => {
33
33
  // Build the configuration options
34
34
  const [globalConfig] = useConfigProvider();
35
35
  const mergedOptions = useMemo(
@@ -40,20 +40,21 @@ export const useSubmit = <CommandType extends CommandInstance>(
40
40
  }),
41
41
  [JSON.stringify(globalConfig.useSubmitConfig), JSON.stringify(options)],
42
42
  );
43
- const { disabled, dependencyTracking, initialData, bounce, bounceType, bounceTime, deepCompare } = mergedOptions;
43
+ const { disabled, dependencyTracking, initialData, bounce, bounceType, bounceTime, bounceTimeout, deepCompare } =
44
+ mergedOptions;
44
45
 
45
46
  /**
46
- * Because of the dynamic cacheKey / queueKey signing within the command we need to store it's latest instance
47
+ * Because of the dynamic cacheKey / queueKey signing within the request we need to store it's latest instance
47
48
  * so the events got triggered properly and show the latest result without mixing it up
48
49
  */
49
- const { builder } = command;
50
- const { cache, submitDispatcher: dispatcher, loggerManager } = builder;
50
+ const { client } = request;
51
+ const { cache, submitDispatcher: dispatcher, loggerManager } = client;
51
52
 
52
53
  const logger = useRef(loggerManager.init("useSubmit")).current;
53
- const requestDebounce = useDebounce(bounceTime);
54
- const requestThrottle = useThrottle(bounceTime);
54
+ const requestDebounce = useDebounce({ delay: bounceTime });
55
+ const requestThrottle = useThrottle({ interval: bounceTime, timeout: bounceTimeout });
55
56
  const bounceResolver = useRef<
56
- (value: ClientResponseType<ExtractResponse<CommandType>, ExtractError<CommandType>>) => void
57
+ (value: ResponseType<ExtractResponseType<RequestType>, ExtractErrorType<RequestType>>) => void
57
58
  >(() => null);
58
59
 
59
60
  const bounceData = bounceType === "throttle" ? requestThrottle : requestDebounce;
@@ -62,9 +63,9 @@ export const useSubmit = <CommandType extends CommandInstance>(
62
63
  /**
63
64
  * State handler with optimization for rerendering, that hooks into the cache state and dispatchers queues
64
65
  */
65
- const [state, actions, { setRenderKey, setCacheData }] = useTrackedState<CommandType>({
66
+ const [state, actions, { setRenderKey, setCacheData }] = useTrackedState<RequestType>({
66
67
  logger,
67
- command,
68
+ request,
68
69
  dispatcher,
69
70
  initialData,
70
71
  deepCompare,
@@ -74,10 +75,10 @@ export const useSubmit = <CommandType extends CommandInstance>(
74
75
  /**
75
76
  * Handles the data exchange with the core logic - responses, loading, downloading etc
76
77
  */
77
- const [callbacks, listeners] = useCommandEvents({
78
+ const [callbacks, listeners] = useRequestEvents({
78
79
  logger,
79
80
  actions,
80
- command,
81
+ request,
81
82
  dispatcher,
82
83
  setCacheData,
83
84
  });
@@ -88,29 +89,29 @@ export const useSubmit = <CommandType extends CommandInstance>(
88
89
  // Submitting
89
90
  // ******************
90
91
 
91
- const handleSubmit: FetchMethodType<CommandType> = (submitOptions?: FetchType<CommandType>) => {
92
- const commandClone = command.clone(submitOptions as any) as CommandType;
92
+ const handleSubmit: RequestSendType<RequestType> = (submitOptions?: RequestSendOptionsType<RequestType>) => {
93
+ const requestClone = request.clone(submitOptions as any) as RequestType;
93
94
 
94
95
  if (disabled) {
95
96
  logger.warning(`Cannot submit request`, { disabled, submitOptions });
96
97
  return Promise.resolve([null, new Error("Cannot submit request. Option 'disabled' is enabled"), 0]) as Promise<
97
- ClientResponseType<ExtractResponse<CommandType>, ExtractError<CommandType>>
98
+ ResponseType<ExtractResponseType<RequestType>, ExtractErrorType<RequestType>>
98
99
  >;
99
100
  }
100
101
 
101
102
  const triggerRequest = () => {
102
- addDataListener(commandClone);
103
- return commandSendRequest(commandClone, {
103
+ addDataListener(requestClone);
104
+ return requestSendRequest(requestClone, {
104
105
  dispatcherType: "submit",
105
106
  ...submitOptions,
106
107
  onSettle: (requestId, cmd) => {
107
- addLifecycleListeners(commandClone, requestId);
108
+ addLifecycleListeners(requestClone, requestId);
108
109
  submitOptions?.onSettle?.(requestId, cmd);
109
110
  },
110
111
  });
111
112
  };
112
113
 
113
- return new Promise<ExtractClientReturnType<CommandType>>((resolve) => {
114
+ return new Promise<ExtractAdapterReturnType<RequestType>>((resolve) => {
114
115
  const performSubmit = async () => {
115
116
  logger.debug(`Submitting request`, { disabled, submitOptions });
116
117
  if (bounce) {
@@ -119,7 +120,7 @@ export const useSubmit = <CommandType extends CommandInstance>(
119
120
  // By default bounce method will prevent function to be triggered, but returned promise will still await to be resolved.
120
121
  // This way we can close previous promise, making sure our logic will not stuck in memory.
121
122
  bounceResolver.current = (
122
- value: ClientResponseType<ExtractResponse<CommandType>, ExtractError<CommandType>>,
123
+ value: ResponseType<ExtractResponseType<RequestType>, ExtractErrorType<RequestType>>,
123
124
  ) => {
124
125
  // Trigger previous awaiting calls to resolve together in bounced batches
125
126
  bouncedResolve(value);
@@ -151,9 +152,9 @@ export const useSubmit = <CommandType extends CommandInstance>(
151
152
  // ******************
152
153
 
153
154
  const handleRevalidation = (invalidateKey: InvalidationKeyType) => {
154
- if (invalidateKey && invalidateKey instanceof Command) {
155
- cache.revalidate(getCommandKey(invalidateKey));
156
- } else if (invalidateKey && !(invalidateKey instanceof Command)) {
155
+ if (invalidateKey && invalidateKey instanceof Request) {
156
+ cache.revalidate(getRequestKey(invalidateKey));
157
+ } else if (invalidateKey && !(invalidateKey instanceof Request)) {
157
158
  cache.revalidate(invalidateKey);
158
159
  }
159
160
  };
@@ -189,7 +190,7 @@ export const useSubmit = <CommandType extends CommandInstance>(
189
190
  // ******************
190
191
 
191
192
  useDidMount(() => {
192
- addDataListener(command);
193
+ addDataListener(request);
193
194
  });
194
195
 
195
196
  return {
@@ -1,4 +1,10 @@
1
- import { CacheValueType, ExtractError, ExtractResponse, CommandInstance, FetchMethodType } from "@hyper-fetch/core";
1
+ import {
2
+ CacheValueType,
3
+ ExtractErrorType,
4
+ ExtractResponseType,
5
+ RequestInstance,
6
+ RequestSendType,
7
+ } from "@hyper-fetch/core";
2
8
 
3
9
  import { isEqual } from "utils";
4
10
  import {
@@ -12,7 +18,7 @@ import {
12
18
  } from "helpers";
13
19
  import { InvalidationKeyType } from "types";
14
20
 
15
- export type UseSubmitOptionsType<T extends CommandInstance> = {
21
+ export type UseSubmitOptionsType<T extends RequestInstance> = {
16
22
  /**
17
23
  * Disable fetching
18
24
  */
@@ -20,7 +26,7 @@ export type UseSubmitOptionsType<T extends CommandInstance> = {
20
26
  /**
21
27
  * If cache is empty we can use placeholder data.
22
28
  */
23
- initialData?: CacheValueType<ExtractResponse<T>, ExtractError<T>>["data"] | null;
29
+ initialData?: CacheValueType<ExtractResponseType<T>, ExtractErrorType<T>>["data"] | null;
24
30
  /**
25
31
  * Enable/disable debouncing for often changing keys or refreshing, to limit requests to server.
26
32
  */
@@ -33,6 +39,10 @@ export type UseSubmitOptionsType<T extends CommandInstance> = {
33
39
  * How long it should bounce requests.
34
40
  */
35
41
  bounceTime?: number;
42
+ /**
43
+ * ONLY in throttle mode - options for handling last bounce event
44
+ */
45
+ bounceTimeout?: number;
36
46
  /**
37
47
  * If `true` it will rerender only when values used by our component gets changed. Otherwise it will rerender on any change.
38
48
  */
@@ -43,11 +53,11 @@ export type UseSubmitOptionsType<T extends CommandInstance> = {
43
53
  deepCompare?: boolean | typeof isEqual;
44
54
  };
45
55
 
46
- export type UseSubmitReturnType<CommandType extends CommandInstance> = Omit<
47
- UseTrackedStateType<CommandType>,
56
+ export type UseSubmitReturnType<RequestType extends RequestInstance> = Omit<
57
+ UseTrackedStateType<RequestType>,
48
58
  "loading"
49
59
  > &
50
- UseTrackedStateActions<CommandType> & {
60
+ UseTrackedStateActions<RequestType> & {
51
61
  /**
52
62
  * Callback which allows to cancel ongoing requests from given queueKey.
53
63
  */
@@ -55,43 +65,43 @@ export type UseSubmitReturnType<CommandType extends CommandInstance> = Omit<
55
65
  /**
56
66
  * Helper hook listening on success response.
57
67
  */
58
- onSubmitSuccess: (callback: OnSuccessCallbackType<CommandType>) => void;
68
+ onSubmitSuccess: (callback: OnSuccessCallbackType<RequestType>) => void;
59
69
  /**
60
70
  * Helper hook listening on error response.
61
71
  */
62
- onSubmitError: (callback: OnErrorCallbackType<CommandType>) => void;
72
+ onSubmitError: (callback: OnErrorCallbackType<RequestType>) => void;
63
73
  /**
64
74
  * Helper hook listening on any response.
65
75
  */
66
- onSubmitFinished: (callback: OnFinishedCallbackType<CommandType>) => void;
76
+ onSubmitFinished: (callback: OnFinishedCallbackType<RequestType>) => void;
67
77
  /**
68
78
  * Helper hook listening on request start.
69
79
  */
70
- onSubmitRequestStart: (callback: OnStartCallbackType<CommandType>) => void;
80
+ onSubmitRequestStart: (callback: OnStartCallbackType<RequestType>) => void;
71
81
  /**
72
82
  * Helper hook listening on response start(before we receive all data from server).
73
83
  */
74
- onSubmitResponseStart: (callback: OnStartCallbackType<CommandType>) => void;
84
+ onSubmitResponseStart: (callback: OnStartCallbackType<RequestType>) => void;
75
85
  /**
76
- * 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.
86
+ * 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.
77
87
  */
78
88
  onSubmitDownloadProgress: (callback: OnProgressCallbackType) => void;
79
89
  /**
80
- * 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.
90
+ * 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.
81
91
  */
82
92
  onSubmitUploadProgress: (callback: OnProgressCallbackType) => void;
83
93
  /**
84
94
  * Helper hook listening on aborting of requests. Abort events are not triggering onError callbacks.
85
95
  */
86
- onSubmitAbort: (callback: OnErrorCallbackType<CommandType>) => void;
96
+ onSubmitAbort: (callback: OnErrorCallbackType<RequestType>) => void;
87
97
  /**
88
- * 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.
98
+ * 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.
89
99
  */
90
- onSubmitOfflineError: (callback: OnErrorCallbackType<CommandType>) => void;
100
+ onSubmitOfflineError: (callback: OnErrorCallbackType<RequestType>) => void;
91
101
  /**
92
102
  * Method responsible for triggering requests. It return Promise which will be resolved with the request.
93
103
  */
94
- submit: FetchMethodType<CommandType>;
104
+ submit: RequestSendType<RequestType>;
95
105
  /**
96
106
  * Request loading state
97
107
  */
@@ -110,7 +120,7 @@ export type UseSubmitReturnType<CommandType extends CommandInstance> = Omit<
110
120
  reset: () => void;
111
121
  };
112
122
  /**
113
- * Revalidate current command resource or pass custom key to trigger it by invalidationKey(Regex / cacheKey).
123
+ * Revalidate current request resource or pass custom key to trigger it by invalidationKey(Regex / cacheKey).
114
124
  */
115
125
  revalidate: (invalidateKey: InvalidationKeyType | InvalidationKeyType[]) => void;
116
126
  };
package/src/index.ts CHANGED
@@ -2,6 +2,9 @@ export * from "./hooks/use-fetch";
2
2
  export * from "./hooks/use-submit";
3
3
  export * from "./hooks/use-queue";
4
4
  export * from "./hooks/use-cache";
5
+ export * from "./hooks/use-listener";
6
+ export * from "./hooks/use-emitter";
7
+ export * from "./hooks/use-event-messages";
5
8
  export * from "./hooks/use-app-manager";
6
9
  export * from "./helpers";
7
10
  export * from "./types";
@@ -1,3 +1,3 @@
1
- import { CommandInstance } from "@hyper-fetch/core";
1
+ import { RequestInstance } from "@hyper-fetch/core";
2
2
 
3
- export type InvalidationKeyType = string | CommandInstance | RegExp;
3
+ export type InvalidationKeyType = string | RequestInstance | RegExp;
@@ -1,2 +0,0 @@
1
- export * from "./use-command-events.hooks";
2
- export * from "./use-command-events.types";
@@ -1,8 +0,0 @@
1
- import { UseCommandEventsReturnType, UseCommandEventsPropsType } from "helpers";
2
- /**
3
- * This is helper hook that handles main Hyper-Fetch event/data flow
4
- * @internal
5
- * @param options
6
- * @returns
7
- */
8
- export declare const useCommandEvents: <T extends Command<any, any, any, any, any, any, any, any, any, any, any>>({ command, dispatcher, logger, actions, setCacheData, }: UseCommandEventsPropsType<T>) => UseCommandEventsReturnType<T>;
@@ -1,2 +0,0 @@
1
- export * from "./use-command-events.hooks";
2
- export * from "./use-command-events.types";