@hyper-fetch/react 3.3.3 → 4.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.
@@ -4,10 +4,11 @@ import {
4
4
  ExtractResponseType,
5
5
  RequestInstance,
6
6
  ProgressType,
7
- ResponseType,
7
+ ResponseReturnType,
8
8
  RequestEventType,
9
9
  ResponseDetailsType,
10
10
  RequestLoadingEventType,
11
+ ExtractAdapterType,
11
12
  } from "@hyper-fetch/core";
12
13
  import { useWillUnmount } from "@better-hooks/lifecycle";
13
14
 
@@ -80,17 +81,18 @@ export const useRequestEvents = <T extends RequestInstance>({
80
81
 
81
82
  const handleResponseCallbacks = (
82
83
  cmd: T,
83
- data: ResponseType<ExtractResponseType<T>, ExtractErrorType<T>>,
84
+ data: ResponseReturnType<ExtractResponseType<T>, ExtractErrorType<T>, ExtractAdapterType<T>>,
84
85
  details: ResponseDetailsType,
85
86
  ) => {
86
- const { isOffline, isFailed, isCanceled } = details;
87
- if (request.offline && isOffline && isFailed) {
87
+ const { success } = data;
88
+ const { isOffline, isCanceled } = details;
89
+ if (request.offline && isOffline && !success) {
88
90
  logger.debug("Performing offline error callback", { data, details });
89
91
  onOfflineErrorCallback.current?.({ response: data[1] as ExtractErrorType<T>, request: cmd, details });
90
92
  } else if (isCanceled) {
91
93
  logger.debug("Performing abort callback", { data, details });
92
94
  onAbortCallback.current?.({ response: data[1] as ExtractErrorType<T>, request: cmd, details });
93
- } else if (!isFailed) {
95
+ } else if (success) {
94
96
  logger.debug("Performing success callback", { data, details });
95
97
  onSuccessCallback.current?.({ response: data[0] as ExtractResponseType<T>, request: cmd, details });
96
98
  } else {
@@ -132,9 +134,12 @@ export const useRequestEvents = <T extends RequestInstance>({
132
134
  };
133
135
  };
134
136
 
135
- const handleResponse = (cmd: T) => {
136
- return (data: ResponseType<ExtractResponseType<T>, ExtractErrorType<T>>, details: ResponseDetailsType) => {
137
- handleResponseCallbacks(cmd, data, details);
137
+ const handleResponse = (req: T) => {
138
+ return (
139
+ data: ResponseReturnType<ExtractResponseType<T>, ExtractErrorType<T>, ExtractAdapterType<T>>,
140
+ details: ResponseDetailsType,
141
+ ) => {
142
+ handleResponseCallbacks(req, data, details);
138
143
  };
139
144
  };
140
145
 
@@ -151,11 +156,11 @@ export const useRequestEvents = <T extends RequestInstance>({
151
156
  dataEvents.current = null;
152
157
  };
153
158
 
154
- const addDataListener = (cmd: T) => {
159
+ const addDataListener = (req: T) => {
155
160
  // Data handlers
156
- const loadingUnmount = requestManager.events.onLoading(cmd.queueKey, handleGetLoadingEvent(cmd.queueKey));
157
- const getResponseUnmount = cache.events.onData<ExtractResponseType<T>, ExtractErrorType<T>>(
158
- cmd.cacheKey,
161
+ const loadingUnmount = requestManager.events.onLoading(req.queueKey, handleGetLoadingEvent(req.queueKey));
162
+ const getResponseUnmount = cache.events.onData<ExtractResponseType<T>, ExtractErrorType<T>, ExtractAdapterType<T>>(
163
+ req.cacheKey,
159
164
  setCacheData,
160
165
  );
161
166
 
@@ -174,7 +179,7 @@ export const useRequestEvents = <T extends RequestInstance>({
174
179
  // Lifecycle Listeners
175
180
  // ******************
176
181
 
177
- const addLifecycleListeners = (cmd: T, requestId?: string) => {
182
+ const addLifecycleListeners = (req: T, requestId?: string) => {
178
183
  /**
179
184
  * useFetch handles requesting by general keys
180
185
  * This makes it possible to deduplicate requests from different places and share data
@@ -183,12 +188,12 @@ export const useRequestEvents = <T extends RequestInstance>({
183
188
  // It's important to clear previously attached listeners to not cause some additional response/request
184
189
  // events to be triggered during lifecycle
185
190
  clearLifecycleListeners();
186
- const { queueKey, cacheKey } = cmd;
187
- const requestStartUnmount = requestManager.events.onRequestStart(queueKey, handleRequestStart(cmd));
188
- const responseStartUnmount = requestManager.events.onResponseStart(queueKey, handleResponseStart(cmd));
191
+ const { queueKey, cacheKey } = req;
192
+ const requestStartUnmount = requestManager.events.onRequestStart(queueKey, handleRequestStart(req));
193
+ const responseStartUnmount = requestManager.events.onResponseStart(queueKey, handleResponseStart(req));
189
194
  const uploadUnmount = requestManager.events.onUploadProgress(queueKey, handleUploadProgress);
190
195
  const downloadUnmount = requestManager.events.onDownloadProgress(queueKey, handleDownloadProgress);
191
- const responseUnmount = requestManager.events.onResponse(cacheKey, handleResponse(cmd));
196
+ const responseUnmount = requestManager.events.onResponse(cacheKey, handleResponse(req));
192
197
 
193
198
  const unmount = () => {
194
199
  downloadUnmount();
@@ -206,9 +211,9 @@ export const useRequestEvents = <T extends RequestInstance>({
206
211
  * useSubmit handles requesting by requestIds, this makes it possible to track single requests
207
212
  */
208
213
  const requestRemove = requestManager.events.onRemoveById(requestId, handleRemove);
209
- const requestStartUnmount = requestManager.events.onRequestStartById(requestId, handleRequestStart(cmd));
210
- const responseStartUnmount = requestManager.events.onResponseStartById(requestId, handleResponseStart(cmd));
211
- const responseUnmount = requestManager.events.onResponseById(requestId, handleResponse(cmd));
214
+ const requestStartUnmount = requestManager.events.onRequestStartById(requestId, handleRequestStart(req));
215
+ const responseStartUnmount = requestManager.events.onResponseStartById(requestId, handleResponseStart(req));
216
+ const responseUnmount = requestManager.events.onResponseById(requestId, handleResponse(req));
212
217
  const uploadUnmount = requestManager.events.onUploadProgressById(requestId, handleUploadProgress);
213
218
  const downloadUnmount = requestManager.events.onDownloadProgressById(requestId, handleDownloadProgress);
214
219
 
@@ -3,8 +3,10 @@ import { UseTrackedStateType } from "./use-tracked-state.types";
3
3
  export const initialState: UseTrackedStateType = {
4
4
  data: null,
5
5
  error: null,
6
- loading: false,
7
6
  status: null,
7
+ extra: null,
8
+ success: null,
9
+ loading: false,
8
10
  retries: 0,
9
11
  timestamp: null,
10
12
  };
@@ -1,6 +1,12 @@
1
1
  import { useRef } from "react";
2
2
  import { useDidUpdate, useForceUpdate } from "@better-hooks/lifecycle";
3
- import { ExtractErrorType, CacheValueType, ExtractResponseType, RequestInstance } from "@hyper-fetch/core";
3
+ import {
4
+ ExtractErrorType,
5
+ CacheValueType,
6
+ ExtractResponseType,
7
+ RequestInstance,
8
+ ExtractAdapterType,
9
+ } from "@hyper-fetch/core";
4
10
 
5
11
  import { isEqual } from "utils";
6
12
  import {
@@ -27,7 +33,7 @@ export const useTrackedState = <T extends RequestInstance>({
27
33
  dependencyTracking,
28
34
  defaultCacheEmitting = true,
29
35
  }: UseTrackedStateProps<T>): UseTrackedStateReturn<T> => {
30
- const { client, cacheKey, queueKey, cacheTime } = request;
36
+ const { client, cacheKey, queueKey, cacheTime, responseMapper } = request;
31
37
  const { cache, requestManager } = client;
32
38
 
33
39
  const forceUpdate = useForceUpdate();
@@ -42,7 +48,7 @@ export const useTrackedState = <T extends RequestInstance>({
42
48
  const getStaleStatus = (): boolean => {
43
49
  const cacheData = cache.get(cacheKey);
44
50
 
45
- return isStaleCacheData(cacheTime, cacheData?.details.timestamp || state.current.timestamp);
51
+ return isStaleCacheData(cacheTime, cacheData?.timestamp || state.current.timestamp);
46
52
  };
47
53
 
48
54
  // ******************
@@ -72,9 +78,9 @@ export const useTrackedState = <T extends RequestInstance>({
72
78
  // Get cache state
73
79
  const newState = getInitialState(initialData, dispatcher, request);
74
80
 
75
- const hasInitialState = initialData?.[0] === state.current.data;
81
+ const hasInitialState = isEqual(initialData?.data, state.current.data);
76
82
  const hasState = !!(state.current.data || state.current.error) && !hasInitialState;
77
- const shouldLoadInitialCache = !hasState && state.current.data;
83
+ const shouldLoadInitialCache = !hasState && !!state.current.data;
78
84
  const shouldRemovePreviousData = hasState && !state.current.data;
79
85
 
80
86
  if (shouldLoadInitialCache || shouldRemovePreviousData) {
@@ -119,13 +125,37 @@ export const useTrackedState = <T extends RequestInstance>({
119
125
  return false;
120
126
  };
121
127
 
122
- const setCacheData = async (cacheData: CacheValueType<ExtractResponseType<T>, ExtractErrorType<T>>) => {
128
+ const mapResponseData = (
129
+ data: CacheValueType<ExtractResponseType<T>, ExtractErrorType<T>, ExtractAdapterType<T>>,
130
+ ):
131
+ | Promise<CacheValueType<ExtractResponseType<T>, ExtractErrorType<T>, ExtractAdapterType<T>>>
132
+ | CacheValueType<ExtractResponseType<T>, ExtractErrorType<T>, ExtractAdapterType<T>> => {
133
+ if (responseMapper) {
134
+ const response = responseMapper(data);
135
+ if (response instanceof Promise) {
136
+ return new Promise((resolve) => {
137
+ (async () => {
138
+ const newResponse = await response;
139
+ resolve({ ...data, ...newResponse });
140
+ })();
141
+ });
142
+ }
143
+ return { ...data, ...response };
144
+ }
145
+ return data;
146
+ };
147
+
148
+ const handleCacheData = (
149
+ cacheData: CacheValueType<ExtractResponseType<T>, ExtractErrorType<T>, ExtractAdapterType<T>>,
150
+ ) => {
123
151
  const newStateValues: UseTrackedStateType<T> = {
124
- data: cacheData.data[0],
125
- error: cacheData.data[1],
126
- status: cacheData.data[2],
127
- retries: cacheData.details.retries,
128
- timestamp: new Date(cacheData.details.timestamp),
152
+ data: cacheData.data,
153
+ error: cacheData.error,
154
+ status: cacheData.status,
155
+ success: cacheData.success,
156
+ extra: cacheData.extra,
157
+ retries: cacheData.retries,
158
+ timestamp: new Date(cacheData.timestamp),
129
159
  loading: state.current.loading,
130
160
  };
131
161
 
@@ -145,6 +175,20 @@ export const useTrackedState = <T extends RequestInstance>({
145
175
  renderKeyTrigger(changedKeys);
146
176
  };
147
177
 
178
+ const setCacheData = (
179
+ cacheData: CacheValueType<ExtractResponseType<T>, ExtractErrorType<T>, ExtractAdapterType<T>>,
180
+ ): Promise<void> | void => {
181
+ const data = mapResponseData(cacheData);
182
+
183
+ if (data instanceof Promise) {
184
+ return (async () => {
185
+ const promiseData = await data;
186
+ handleCacheData(promiseData);
187
+ })();
188
+ }
189
+ return handleCacheData(data);
190
+ };
191
+
148
192
  // ******************
149
193
  // Actions
150
194
  // ******************
@@ -153,7 +197,7 @@ export const useTrackedState = <T extends RequestInstance>({
153
197
  setData: (data, emitToCache = defaultCacheEmitting) => {
154
198
  if (emitToCache) {
155
199
  const currentState = state.current;
156
- cache.set(request, [data, currentState.error, currentState.status], getDetailsState(state.current));
200
+ cache.set(request, { ...currentState, ...getDetailsState(state.current), success: true, data });
157
201
  } else {
158
202
  state.current.data = data;
159
203
  renderKeyTrigger(["data"]);
@@ -162,11 +206,7 @@ export const useTrackedState = <T extends RequestInstance>({
162
206
  setError: (error, emitToCache = defaultCacheEmitting) => {
163
207
  if (emitToCache) {
164
208
  const currentState = state.current;
165
- cache.set(
166
- request,
167
- [currentState.data, error, currentState.status],
168
- getDetailsState(state.current, { isFailed: !!error }),
169
- );
209
+ cache.set(request, { ...currentState, ...getDetailsState(state.current), success: false, error });
170
210
  } else {
171
211
  state.current.error = error;
172
212
  renderKeyTrigger(["error"]);
@@ -189,20 +229,34 @@ export const useTrackedState = <T extends RequestInstance>({
189
229
  setStatus: (status, emitToCache = defaultCacheEmitting) => {
190
230
  if (emitToCache) {
191
231
  const currentState = state.current;
192
- cache.set(request, [currentState.data, currentState.error, status], getDetailsState(state.current));
232
+ cache.set(request, { ...currentState, ...getDetailsState(state.current), status });
193
233
  } else {
194
234
  state.current.status = status;
195
235
  renderKeyTrigger(["status"]);
196
236
  }
197
237
  },
238
+ setSuccess: (success, emitToCache = defaultCacheEmitting) => {
239
+ if (emitToCache) {
240
+ const currentState = state.current;
241
+ cache.set(request, { ...currentState, ...getDetailsState(state.current), success });
242
+ } else {
243
+ state.current.success = success;
244
+ renderKeyTrigger(["success"]);
245
+ }
246
+ },
247
+ setExtra: (extra, emitToCache = defaultCacheEmitting) => {
248
+ if (emitToCache) {
249
+ const currentState = state.current;
250
+ cache.set(request, { ...currentState, ...getDetailsState(state.current), extra });
251
+ } else {
252
+ state.current.extra = extra;
253
+ renderKeyTrigger(["extra"]);
254
+ }
255
+ },
198
256
  setRetries: (retries, emitToCache = defaultCacheEmitting) => {
199
257
  if (emitToCache) {
200
258
  const currentState = state.current;
201
- cache.set(
202
- request,
203
- [currentState.data, currentState.error, currentState.status],
204
- getDetailsState(state.current, { retries }),
205
- );
259
+ cache.set(request, { ...currentState, ...getDetailsState(state.current, { retries }) });
206
260
  } else {
207
261
  state.current.retries = retries;
208
262
  renderKeyTrigger(["retries"]);
@@ -211,11 +265,7 @@ export const useTrackedState = <T extends RequestInstance>({
211
265
  setTimestamp: (timestamp, emitToCache = defaultCacheEmitting) => {
212
266
  if (emitToCache) {
213
267
  const currentState = state.current;
214
- cache.set(
215
- request,
216
- [currentState.data, currentState.error, currentState.status],
217
- getDetailsState(state.current, { timestamp: +timestamp }),
218
- );
268
+ cache.set(request, { ...currentState, ...getDetailsState(state.current, { timestamp: +timestamp }) });
219
269
  } else {
220
270
  state.current.timestamp = timestamp;
221
271
  renderKeyTrigger(["timestamp"]);
@@ -3,9 +3,12 @@ import {
3
3
  ExtractErrorType,
4
4
  CacheValueType,
5
5
  ExtractResponseType,
6
- ResponseType,
6
+ ResponseReturnType,
7
7
  RequestInstance,
8
8
  LoggerType,
9
+ ExtractAdapterType,
10
+ ExtractAdapterStatusType,
11
+ ExtractAdapterExtraType,
9
12
  } from "@hyper-fetch/core";
10
13
 
11
14
  import { isEqual } from "utils";
@@ -13,7 +16,7 @@ import { isEqual } from "utils";
13
16
  export type UseTrackedStateProps<T extends RequestInstance> = {
14
17
  request: T;
15
18
  logger: LoggerType;
16
- initialData: ResponseType<ExtractResponseType<T>, ExtractErrorType<T>> | null;
19
+ initialData: ResponseReturnType<ExtractResponseType<T>, ExtractErrorType<T>, ExtractAdapterType<T>> | null;
17
20
  dispatcher: Dispatcher;
18
21
  dependencyTracking: boolean;
19
22
  defaultCacheEmitting?: boolean;
@@ -44,9 +47,17 @@ export type UseTrackedStateType<T extends RequestInstance = RequestInstance> = {
44
47
  */
45
48
  loading: boolean;
46
49
  /**
47
- * Request http status code
50
+ * Request status
48
51
  */
49
- status: null | number;
52
+ status: ExtractAdapterStatusType<ExtractAdapterType<T>>;
53
+ /**
54
+ * Request additional response data
55
+ */
56
+ extra: ExtractAdapterExtraType<ExtractAdapterType<T>>;
57
+ /**
58
+ * Information whether request succeeded
59
+ */
60
+ success: boolean;
50
61
  /**
51
62
  * Request attempts
52
63
  */
@@ -73,7 +84,15 @@ export type UseTrackedStateActions<T extends RequestInstance> = {
73
84
  /**
74
85
  * Action to set custom status. We can do it locally(inside hook state) or in cache(all related sources) with 'emitToCache' option
75
86
  */
76
- setStatus: (status: number | null, emitToCache?: boolean) => void;
87
+ setStatus: (status: ExtractAdapterStatusType<ExtractAdapterType<T>>, emitToCache?: boolean) => void;
88
+ /**
89
+ * Action to set custom success. We can do it locally(inside hook state) or in cache(all related sources) with 'emitToCache' option
90
+ */
91
+ setSuccess: (success: boolean, emitToCache?: boolean) => void;
92
+ /**
93
+ * Action to set custom additional data. We can do it locally(inside hook state) or in cache(all related sources) with 'emitToCache' option
94
+ */
95
+ setExtra: (extra: ExtractAdapterExtraType<ExtractAdapterType<T>>, emitToCache?: boolean) => void;
77
96
  /**
78
97
  * Action to set custom retries count. We can do it locally(inside hook state) or in cache(all related sources) with 'emitToCache' option
79
98
  */
@@ -2,23 +2,25 @@ import {
2
2
  CacheValueType,
3
3
  NullableType,
4
4
  RequestInstance,
5
- ResponseType,
5
+ ResponseReturnType,
6
6
  ExtractResponseType,
7
7
  ExtractErrorType,
8
8
  ExtractAdapterReturnType,
9
9
  Dispatcher,
10
+ ExtractAdapterType,
11
+ ExtractAdapterExtraType,
12
+ ResponseDetailsType,
10
13
  } from "@hyper-fetch/core";
11
14
 
12
15
  import { initialState, UseTrackedStateType } from "helpers";
13
16
 
14
17
  export const getDetailsState = (
15
18
  state?: UseTrackedStateType<RequestInstance>,
16
- details?: Partial<CacheValueType<unknown, unknown>["details"]>,
17
- ): CacheValueType<unknown, unknown>["details"] => {
19
+ details?: Partial<ResponseDetailsType>,
20
+ ): ResponseDetailsType => {
18
21
  return {
19
22
  retries: state?.retries || 0,
20
23
  timestamp: +new Date(),
21
- isFailed: false,
22
24
  isCanceled: false,
23
25
  isOffline: false,
24
26
  ...details,
@@ -35,7 +37,7 @@ export const getValidCacheData = <T extends RequestInstance>(
35
37
  initialData: NullableType<ExtractAdapterReturnType<T>>,
36
38
  cacheData: NullableType<CacheValueType<ExtractResponseType<T>, ExtractErrorType<T>>>,
37
39
  ): CacheValueType<ExtractResponseType<T>, ExtractErrorType<T>> | null => {
38
- const isStale = isStaleCacheData(request.cacheTime, cacheData?.details.timestamp);
40
+ const isStale = isStaleCacheData(request.cacheTime, cacheData?.timestamp);
39
41
 
40
42
  if (!isStale && cacheData) {
41
43
  return cacheData;
@@ -43,8 +45,8 @@ export const getValidCacheData = <T extends RequestInstance>(
43
45
 
44
46
  if (initialData) {
45
47
  return {
46
- data: initialData,
47
- details: getDetailsState(),
48
+ ...initialData,
49
+ ...getDetailsState(),
48
50
  cacheTime: 1000,
49
51
  clearKey: request.client.cache.clearKey,
50
52
  garbageCollection: request.garbageCollection,
@@ -59,25 +61,42 @@ export const getTimestamp = (timestamp?: NullableType<number | Date>) => {
59
61
  };
60
62
 
61
63
  export const getInitialState = <T extends RequestInstance>(
62
- initialData: ResponseType<ExtractResponseType<T>, ExtractErrorType<T>> | null,
64
+ initialData: ResponseReturnType<ExtractResponseType<T>, ExtractErrorType<T>, ExtractAdapterType<T>> | null,
63
65
  dispatcher: Dispatcher,
64
66
  request: T,
65
67
  ): UseTrackedStateType<T> => {
66
68
  const { client, cacheKey } = request;
67
69
  const { cache } = client;
68
70
 
69
- const cacheData = cache.get<ExtractResponseType<T>, ExtractErrorType<T>>(cacheKey);
71
+ const cacheData = cache.get<
72
+ ExtractResponseType<T>,
73
+ ExtractErrorType<T>,
74
+ ExtractAdapterExtraType<ExtractAdapterType<T>>
75
+ >(cacheKey);
70
76
  const cacheState = getValidCacheData<T>(request, initialData, cacheData);
71
-
72
77
  const initialLoading = dispatcher.hasRunningRequests(request.queueKey);
73
78
 
79
+ if (cacheState) {
80
+ return {
81
+ data: cacheState.data,
82
+ error: cacheState.error,
83
+ status: cacheState.status,
84
+ success: cacheState.success,
85
+ extra: cacheState.extra,
86
+ retries: cacheState.retries,
87
+ timestamp: getTimestamp(cacheState.timestamp),
88
+ loading: initialLoading,
89
+ };
90
+ }
91
+
74
92
  return {
75
- ...initialState,
76
- data: cacheState?.data?.[0] || initialState.data,
77
- error: cacheState?.data?.[1] || initialState.error,
78
- status: cacheState?.data?.[2] || initialState.status,
79
- retries: cacheState?.details.retries || initialState.retries,
80
- timestamp: getTimestamp(cacheState?.details.timestamp || initialState.timestamp),
93
+ data: initialState.data,
94
+ error: initialState.error,
95
+ status: initialState.status,
96
+ success: initialState.success,
97
+ extra: request.client.defaultExtra,
98
+ retries: initialState.retries,
99
+ timestamp: getTimestamp(initialState.timestamp),
81
100
  loading: initialLoading,
82
101
  };
83
102
  };
@@ -73,6 +73,14 @@ export const useCache = <T extends RequestInstance>(
73
73
  setRenderKey("status");
74
74
  return state.status;
75
75
  },
76
+ get success() {
77
+ setRenderKey("success");
78
+ return state.success;
79
+ },
80
+ get extra() {
81
+ setRenderKey("extra");
82
+ return state.extra;
83
+ },
76
84
  get retries() {
77
85
  setRenderKey("retries");
78
86
  return state.retries;
@@ -5,8 +5,6 @@ import { UseEmitterOptionsType } from "hooks/use-emitter";
5
5
  import { useSocketState } from "helpers";
6
6
  import { useConfigProvider } from "config-provider";
7
7
 
8
- // Todo bounce
9
-
10
8
  export const useEmitter = <EmitterType extends EmitterInstance>(
11
9
  emitter: EmitterType,
12
10
  options: UseEmitterOptionsType,
@@ -21,6 +21,5 @@ export const useFetchDefaultOptions: DefaultOptionsType = {
21
21
  bounce: false,
22
22
  bounceType: "debounce",
23
23
  bounceTime: 400,
24
- bounceTimeout: 200,
25
24
  deepCompare: true,
26
25
  };
@@ -36,7 +36,7 @@ export const useFetch = <RequestType extends RequestInstance>(
36
36
  bounce = useFetchDefaultOptions.bounce,
37
37
  bounceType = useFetchDefaultOptions.bounceType,
38
38
  bounceTime = useFetchDefaultOptions.bounceTime,
39
- bounceTimeout = useFetchDefaultOptions.bounceTimeout,
39
+ bounceTimeout = useFetchDefaultOptions.bounceTime,
40
40
  deepCompare = useFetchDefaultOptions.deepCompare,
41
41
  } = {
42
42
  ...useFetchDefaultOptions,
@@ -44,7 +44,7 @@ export const useFetch = <RequestType extends RequestInstance>(
44
44
  ...options,
45
45
  };
46
46
 
47
- const updateKey = JSON.stringify(request.dump());
47
+ const updateKey = JSON.stringify(request.toJSON());
48
48
  const requestDebounce = useDebounce({ delay: bounceTime });
49
49
  const requestThrottle = useThrottle({ interval: bounceTime, timeout: bounceTimeout });
50
50
  const refreshDebounce = useDebounce({ delay: refreshTime });
@@ -57,7 +57,7 @@ export const useFetch = <RequestType extends RequestInstance>(
57
57
  const bounceFunction = bounceType === "throttle" ? requestThrottle.throttle : requestDebounce.debounce;
58
58
 
59
59
  /**
60
- * State handler with optimization for rerendering, that hooks into the cache state and dispatchers queues
60
+ * State handler with optimization for re-rendering, that hooks into the cache state and dispatchers queues
61
61
  */
62
62
  const [state, actions, { setRenderKey, setCacheData, getStaleStatus }] = useTrackedState<RequestType>({
63
63
  logger,
@@ -210,7 +210,7 @@ export const useFetch = <RequestType extends RequestInstance>(
210
210
 
211
211
  /**
212
212
  * Initialization of the events related to data exchange with cache and queue
213
- * This allow to share the state with other hooks and keep it related
213
+ * This allows to share the state with other hooks and keep it related
214
214
  */
215
215
  useDidUpdate(handleMountEvents, [updateKey], true);
216
216
 
@@ -246,6 +246,14 @@ export const useFetch = <RequestType extends RequestInstance>(
246
246
  setRenderKey("status");
247
247
  return state.status;
248
248
  },
249
+ get success() {
250
+ setRenderKey("success");
251
+ return state.success;
252
+ },
253
+ get extra() {
254
+ setRenderKey("extra");
255
+ return state.extra;
256
+ },
249
257
  get retries() {
250
258
  setRenderKey("retries");
251
259
  return state.retries;
@@ -53,23 +53,36 @@ export type UseFetchOptionsType<T extends RequestInstance> = {
53
53
  * Enable/disable debouncing for often changing keys or refreshing, to limit requests to server.
54
54
  */
55
55
  bounce?: boolean;
56
- /**
57
- * Possibility to choose between debounce and throttle approaches
58
- */
59
- bounceType?: "debounce" | "throttle";
60
- /**
61
- * How long it should bounce requests.
62
- */
63
- bounceTime?: number;
64
- /**
65
- * ONLY in throttle mode - options for handling last bounce event
66
- */
67
- bounceTimeout?: number;
68
56
  /**
69
57
  * Deep comparison function for hook to check for equality in incoming data, to limit rerenders.
70
58
  */
71
59
  deepCompare?: boolean | typeof isEqual;
72
- };
60
+ } & (
61
+ | {
62
+ /**
63
+ * Possibility to choose between debounce and throttle approaches
64
+ */
65
+ bounceType?: "debounce";
66
+ /**
67
+ * How long it should bounce requests.
68
+ */
69
+ bounceTime?: number;
70
+ }
71
+ | {
72
+ /**
73
+ * Possibility to choose between debounce and throttle approaches
74
+ */
75
+ bounceType: "throttle";
76
+ /**
77
+ * How long it should bounce requests.
78
+ */
79
+ bounceTime?: number;
80
+ /**
81
+ * ONLY in throttle mode - options for handling last bounce event
82
+ */
83
+ bounceTimeout?: number;
84
+ }
85
+ );
73
86
 
74
87
  export type UseFetchReturnType<T extends RequestInstance> = UseTrackedStateType<T> &
75
88
  UseTrackedStateActions<T> &
@@ -1,5 +1,5 @@
1
1
  import { useState, useRef } from "react";
2
- import { RequestInstance, getRequestDispatcher, DispatcherStorageValueType } from "@hyper-fetch/core";
2
+ import { RequestInstance, getRequestDispatcher, QueueElementType } from "@hyper-fetch/core";
3
3
  import { useDidMount, useDidUpdate } from "@better-hooks/lifecycle";
4
4
 
5
5
  import { UseQueueOptionsType, useQueueDefaultOptions, QueueRequest, UseQueueReturnType } from "hooks/use-queue";
@@ -37,7 +37,7 @@ export const useQueue = <Request extends RequestInstance>(
37
37
  // Mapping
38
38
  // ******************
39
39
 
40
- const createRequestsArray = (queueElements: DispatcherStorageValueType<Request>[]): QueueRequest<Request>[] => {
40
+ const createRequestsArray = (queueElements: QueueElementType<Request>[]): QueueRequest<Request>[] => {
41
41
  return queueElements.map<QueueRequest<Request>>((req) => ({
42
42
  ...req,
43
43
  stopRequest: () => dispatcher.stopRequest(queueKey, req.requestId),
@@ -1,10 +1,10 @@
1
- import { ProgressType, RequestInstance, DispatcherStorageValueType } from "@hyper-fetch/core";
1
+ import { ProgressType, RequestInstance, QueueElementType } 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<Request extends RequestInstance> = DispatcherStorageValueType<Request> & {
7
+ export type QueueRequest<Request extends RequestInstance> = QueueElementType<Request> & {
8
8
  /**
9
9
  * Uploading progress for given request
10
10
  */
@@ -13,6 +13,5 @@ export const useSubmitDefaultOptions: DefaultOptionsType = {
13
13
  bounce: false,
14
14
  bounceType: "debounce",
15
15
  bounceTime: 400,
16
- bounceTimeout: 200,
17
16
  deepCompare: true,
18
17
  };