@hyper-fetch/react 5.5.0 → 5.7.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.
- package/dist/index.cjs.js +16 -23
- package/dist/index.cjs.js.map +2 -2
- package/dist/index.d.ts +1 -1
- package/dist/index.esm.js +16 -23
- package/dist/index.esm.js.map +2 -2
- package/package.json +1 -1
- package/src/helpers/use-request-events/use-request-events.hooks.ts +11 -2
- package/src/helpers/use-tracked-state/use-tracked-state.hooks.ts +3 -23
- package/src/hooks/use-fetch/use-fetch.constants.ts +1 -1
- package/src/hooks/use-fetch/use-fetch.hooks.ts +2 -2
- package/src/hooks/use-fetch/use-fetch.types.ts +1 -1
|
@@ -125,26 +125,6 @@ export const useTrackedState = <T extends RequestInstance>({
|
|
|
125
125
|
return false;
|
|
126
126
|
};
|
|
127
127
|
|
|
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
128
|
const handleCacheData = (
|
|
149
129
|
cacheData: CacheValueType<ExtractResponseType<T>, ExtractErrorType<T>, ExtractAdapterType<T>>,
|
|
150
130
|
) => {
|
|
@@ -178,15 +158,15 @@ export const useTrackedState = <T extends RequestInstance>({
|
|
|
178
158
|
const setCacheData = (
|
|
179
159
|
cacheData: CacheValueType<ExtractResponseType<T>, ExtractErrorType<T>, ExtractAdapterType<T>>,
|
|
180
160
|
): Promise<void> | void => {
|
|
181
|
-
const data =
|
|
161
|
+
const data = responseMapper ? responseMapper(cacheData) : cacheData;
|
|
182
162
|
|
|
183
163
|
if (data instanceof Promise) {
|
|
184
164
|
return (async () => {
|
|
185
165
|
const promiseData = await data;
|
|
186
|
-
handleCacheData(promiseData);
|
|
166
|
+
handleCacheData({ ...cacheData, ...promiseData });
|
|
187
167
|
})();
|
|
188
168
|
}
|
|
189
|
-
return handleCacheData(data);
|
|
169
|
+
return handleCacheData({ ...cacheData, ...data });
|
|
190
170
|
};
|
|
191
171
|
|
|
192
172
|
// ******************
|
|
@@ -25,7 +25,7 @@ export const useFetch = <RequestType extends RequestInstance>(
|
|
|
25
25
|
dependencies = useFetchDefaultOptions.dependencies,
|
|
26
26
|
disabled = useFetchDefaultOptions.disabled,
|
|
27
27
|
dependencyTracking = useFetchDefaultOptions.dependencyTracking,
|
|
28
|
-
|
|
28
|
+
revalidate = useFetchDefaultOptions.revalidate,
|
|
29
29
|
initialData = useFetchDefaultOptions.initialData,
|
|
30
30
|
refresh = useFetchDefaultOptions.refresh,
|
|
31
31
|
refreshTime = useFetchDefaultOptions.refreshTime,
|
|
@@ -153,7 +153,7 @@ export const useFetch = <RequestType extends RequestInstance>(
|
|
|
153
153
|
const initialFetchData = () => {
|
|
154
154
|
const hasStaleData = getStaleStatus();
|
|
155
155
|
const isFetching = getIsFetchingIdentity();
|
|
156
|
-
if ((
|
|
156
|
+
if ((revalidate || hasStaleData) && !isFetching) {
|
|
157
157
|
handleFetch();
|
|
158
158
|
}
|
|
159
159
|
};
|
|
@@ -20,7 +20,7 @@ export type UseFetchOptionsType<T extends RequestInstance> = {
|
|
|
20
20
|
/**
|
|
21
21
|
* If `true` it will refetch data in background no matter if we have it from cache.
|
|
22
22
|
*/
|
|
23
|
-
|
|
23
|
+
revalidate?: boolean;
|
|
24
24
|
/**
|
|
25
25
|
* If cache is empty we can use placeholder data.
|
|
26
26
|
*/
|