@hyper-fetch/react 9.5.0 → 9.5.2
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/__tests__/features/helpers/use-request-events/use-request-events.mapper-failure.spec.ts +215 -0
- package/__tests__/features/use-fetch/use-fetch.methods.spec.ts +9 -3
- package/__tests__/features/use-submit/use-submit.methods.spec.ts +9 -3
- package/__tests__/features/use-submit/use-submit.optimistic-settlement.spec.ts +625 -0
- package/__tests__/features/use-submit/use-submit.optimistic.spec.ts +12 -4
- package/dist/helpers/use-request-events/use-request-events.hooks.d.ts.map +1 -1
- package/dist/helpers/use-tracked-state/use-tracked-state.hooks.d.ts.map +1 -1
- package/dist/helpers/use-tracked-state/use-tracked-state.utils.d.ts.map +1 -1
- package/dist/index.mjs +25 -28
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -3
- package/src/helpers/use-request-events/use-request-events.hooks.ts +11 -33
- package/src/helpers/use-tracked-state/use-tracked-state.hooks.ts +3 -5
- package/src/helpers/use-tracked-state/use-tracked-state.utils.ts +4 -5
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hyper-fetch/react",
|
|
3
|
-
"version": "9.5.
|
|
3
|
+
"version": "9.5.2",
|
|
4
4
|
"description": "React hooks and utils for the hyper-fetch",
|
|
5
5
|
"author": "Maciej Pyrc <maciekpyrc@gmail.com>, Kacper Skawina <kacper.skawina@gmail.com>",
|
|
6
6
|
"homepage": "https://hyperfetch.bettertyped.com/",
|
|
@@ -28,8 +28,7 @@
|
|
|
28
28
|
"lint": "oxlint .",
|
|
29
29
|
"format": "oxfmt --write .",
|
|
30
30
|
"typecheck": "tsgo --noEmit --emitDeclarationOnly false && tsgo --noEmit -p __tests__/tsconfig.json",
|
|
31
|
-
"tests": "pnpm lint-staged"
|
|
32
|
-
"release": "pnpm semantic-release --extends ../../release.config.js -t 'react-hyper-fetch-v${version}'"
|
|
31
|
+
"tests": "pnpm lint-staged"
|
|
33
32
|
},
|
|
34
33
|
"peerDependencies": {
|
|
35
34
|
"@hyper-fetch/core": "*",
|
|
@@ -11,7 +11,7 @@ import type {
|
|
|
11
11
|
ResponseType,
|
|
12
12
|
OptimisticCallbackResult,
|
|
13
13
|
} from "@hyper-fetch/core";
|
|
14
|
-
import { scopeKey } from "@hyper-fetch/core";
|
|
14
|
+
import { runResponseMapper, scopeKey } from "@hyper-fetch/core";
|
|
15
15
|
import type {
|
|
16
16
|
OnErrorCallbackType,
|
|
17
17
|
OnStartCallbackType,
|
|
@@ -40,8 +40,6 @@ export const useRequestEvents = <R extends RequestInstance>({
|
|
|
40
40
|
setCacheData,
|
|
41
41
|
getIsDataProcessing,
|
|
42
42
|
}: UseRequestEventsPropsType<R>): UseRequestEventsReturnType<R> => {
|
|
43
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
44
|
-
const { unstable_responseMapper } = request;
|
|
45
43
|
const { cache, requestManager } = request.client;
|
|
46
44
|
|
|
47
45
|
// ******************
|
|
@@ -84,6 +82,7 @@ export const useRequestEvents = <R extends RequestInstance>({
|
|
|
84
82
|
// Optimistic state
|
|
85
83
|
// ******************
|
|
86
84
|
|
|
85
|
+
// Rollback and invalidation are settled by the core - here we only deliver the context to the callbacks
|
|
87
86
|
const optimisticResultsRef = useRef<Map<string, OptimisticCallbackResult<any>>>(new Map());
|
|
88
87
|
|
|
89
88
|
// ******************
|
|
@@ -95,37 +94,24 @@ export const useRequestEvents = <R extends RequestInstance>({
|
|
|
95
94
|
const { isOffline, isCanceled, willRetry } = values.details;
|
|
96
95
|
const opt = optimisticResultsRef.current.get(values.requestId);
|
|
97
96
|
const paramsWithContext = { ...values, mutationContext: opt?.context } as any;
|
|
97
|
+
const isOfflineError = request.offline && isOffline && !success;
|
|
98
98
|
|
|
99
|
-
if (
|
|
99
|
+
if (isOfflineError) {
|
|
100
100
|
logger.debug({ title: "Performing offline error callback", type: "system", extra: values });
|
|
101
101
|
onOfflineErrorCallback.current?.(paramsWithContext);
|
|
102
102
|
} else if (isCanceled) {
|
|
103
103
|
logger.debug({ title: "Performing abort callback", type: "system", extra: values });
|
|
104
|
-
try {
|
|
105
|
-
opt?.rollback?.();
|
|
106
|
-
} catch {
|
|
107
|
-
// rollback threw — onAbort still fires
|
|
108
|
-
}
|
|
109
104
|
onAbortCallback.current?.(paramsWithContext);
|
|
110
105
|
} else if (success) {
|
|
111
106
|
logger.debug({ title: "Performing success callback", type: "system", extra: values });
|
|
112
|
-
if (opt?.invalidate) {
|
|
113
|
-
opt.invalidate.forEach((req) => cache.invalidate(req));
|
|
114
|
-
}
|
|
115
107
|
onSuccessCallback.current?.(paramsWithContext);
|
|
116
108
|
} else {
|
|
117
109
|
logger.debug({ title: "Performing error callback", type: "system", extra: values });
|
|
118
|
-
if (!willRetry) {
|
|
119
|
-
try {
|
|
120
|
-
opt?.rollback?.();
|
|
121
|
-
} catch {
|
|
122
|
-
// rollback threw — onError still fires
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
110
|
onErrorCallback.current?.(paramsWithContext);
|
|
126
111
|
}
|
|
127
112
|
onFinishedCallback.current?.(paramsWithContext);
|
|
128
|
-
|
|
113
|
+
// Retried and offline requests respond again, their callbacks still need the context
|
|
114
|
+
if (!willRetry && (!isOfflineError || isCanceled)) {
|
|
129
115
|
optimisticResultsRef.current.delete(values.requestId);
|
|
130
116
|
}
|
|
131
117
|
};
|
|
@@ -145,7 +131,9 @@ export const useRequestEvents = <R extends RequestInstance>({
|
|
|
145
131
|
|
|
146
132
|
// When we process the cache data, we don't want to change the loading state during it
|
|
147
133
|
// This prevents the UI from flickering with { data: null, loading: false }
|
|
148
|
-
if (isProcessing) {
|
|
134
|
+
if (isProcessing) {
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
149
137
|
|
|
150
138
|
const canDisableLoading = !loading && !dispatcher.hasRunningRequests(scopeKey(req.queryKey, req.scope));
|
|
151
139
|
if (loading || canDisableLoading) {
|
|
@@ -175,9 +163,7 @@ export const useRequestEvents = <R extends RequestInstance>({
|
|
|
175
163
|
|
|
176
164
|
const handleResponse = () => {
|
|
177
165
|
return (values: RequestResponseEventType<R>) => {
|
|
178
|
-
const data =
|
|
179
|
-
? unstable_responseMapper(values.response as ResponseType<any, any, ExtractAdapterType<R>>)
|
|
180
|
-
: values.response;
|
|
166
|
+
const data = runResponseMapper(request, values.response);
|
|
181
167
|
|
|
182
168
|
if (data instanceof Promise) {
|
|
183
169
|
return (async () => {
|
|
@@ -192,15 +178,7 @@ export const useRequestEvents = <R extends RequestInstance>({
|
|
|
192
178
|
};
|
|
193
179
|
|
|
194
180
|
const handleRemove = ({ requestId }: RequestEventType<R>) => {
|
|
195
|
-
|
|
196
|
-
if (opt) {
|
|
197
|
-
try {
|
|
198
|
-
opt.rollback?.();
|
|
199
|
-
} catch {
|
|
200
|
-
// swallow — queue removal rollback failure
|
|
201
|
-
}
|
|
202
|
-
optimisticResultsRef.current.delete(requestId);
|
|
203
|
-
}
|
|
181
|
+
optimisticResultsRef.current.delete(requestId);
|
|
204
182
|
removeLifecycleListener(requestId);
|
|
205
183
|
};
|
|
206
184
|
|
|
@@ -9,7 +9,7 @@ import type {
|
|
|
9
9
|
ExtractAdapterExtraType,
|
|
10
10
|
ResponseType,
|
|
11
11
|
} from "@hyper-fetch/core";
|
|
12
|
-
import { replaceEqualDeep, scopeKey } from "@hyper-fetch/core";
|
|
12
|
+
import { replaceEqualDeep, runResponseMapper, scopeKey } from "@hyper-fetch/core";
|
|
13
13
|
import { useHydration } from "hydrate/hydrate";
|
|
14
14
|
import { useCallback, useRef, useSyncExternalStore } from "react";
|
|
15
15
|
import { isEqual } from "utils";
|
|
@@ -49,7 +49,7 @@ export const useTrackedState = <T extends RequestInstance, TData = ExtractRespon
|
|
|
49
49
|
select,
|
|
50
50
|
structuralSharing = true,
|
|
51
51
|
}: UseTrackedStateProps<T, TData>): UseTrackedStateReturn<T, TData> => {
|
|
52
|
-
const { client, cacheKey, queryKey, staleTime
|
|
52
|
+
const { client, cacheKey, queryKey, staleTime } = request;
|
|
53
53
|
const { cache } = client;
|
|
54
54
|
const hydration = useHydration();
|
|
55
55
|
|
|
@@ -302,9 +302,7 @@ export const useTrackedState = <T extends RequestInstance, TData = ExtractRespon
|
|
|
302
302
|
cacheData: CacheValueType<ExtractResponseType<T>, ExtractErrorType<T>, ExtractAdapterType<T>>,
|
|
303
303
|
): Promise<void> | void => {
|
|
304
304
|
setIsDataProcessing({ processingCacheKey: cacheKey, isProcessing: true });
|
|
305
|
-
const data =
|
|
306
|
-
? unstable_responseMapper(cacheData as ResponseType<any, any, ExtractAdapterType<T>>)
|
|
307
|
-
: cacheData;
|
|
305
|
+
const data = runResponseMapper(request, cacheData as ResponseType<any, any, ExtractAdapterType<T>>) || cacheData;
|
|
308
306
|
|
|
309
307
|
if (data instanceof Promise) {
|
|
310
308
|
return (async () => {
|
|
@@ -13,7 +13,7 @@ import type {
|
|
|
13
13
|
ExtractAdapterStatusType,
|
|
14
14
|
ResponseType,
|
|
15
15
|
} from "@hyper-fetch/core";
|
|
16
|
-
import { scopeKey } from "@hyper-fetch/core";
|
|
16
|
+
import { runResponseMapper, scopeKey } from "@hyper-fetch/core";
|
|
17
17
|
import type { UseTrackedStateType } from "helpers";
|
|
18
18
|
import { initialState } from "helpers";
|
|
19
19
|
import type { HydratedEntry } from "hydrate/hydrate.types";
|
|
@@ -147,7 +147,7 @@ export const getInitialState = <T extends RequestInstance>({
|
|
|
147
147
|
/** Data from the closest `Hydrate` that was not written to the cache yet */
|
|
148
148
|
hydratedEntry?: NullableType<HydratedEntry>;
|
|
149
149
|
}): UseTrackedStateType<T> => {
|
|
150
|
-
const { client, cacheKey
|
|
150
|
+
const { client, cacheKey } = request;
|
|
151
151
|
const { cache } = client;
|
|
152
152
|
|
|
153
153
|
const cacheData = pickInitialCacheData(
|
|
@@ -165,9 +165,8 @@ export const getInitialState = <T extends RequestInstance>({
|
|
|
165
165
|
});
|
|
166
166
|
|
|
167
167
|
if (cacheState) {
|
|
168
|
-
const mappedData =
|
|
169
|
-
|
|
170
|
-
: cacheState;
|
|
168
|
+
const mappedData =
|
|
169
|
+
runResponseMapper(request, cacheState as ResponseType<any, any, ExtractAdapterType<T>>) || cacheState;
|
|
171
170
|
if (mappedData instanceof Promise) {
|
|
172
171
|
// For the async mapper we cannot return async values
|
|
173
172
|
// So we have return the initial state instead
|