@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/dist/index.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import React, { useCallback, useContext, useEffect, useLayoutEffect, useMemo, useRef, useState, useSyncExternalStore } from "react";
|
|
2
|
-
import { Request, Time, getRequestDispatcher, parseResponse, replaceEqualDeep, scopeKey, sendRequest } from "@hyper-fetch/core";
|
|
3
|
-
import {
|
|
2
|
+
import { Request, Time, getRequestDispatcher, parseResponse, replaceEqualDeep, runResponseMapper, scopeKey, sendRequest } from "@hyper-fetch/core";
|
|
3
|
+
import { jsxDEV } from "react/jsx-dev-runtime";
|
|
4
4
|
import { getDeliveryKey } from "@hyper-fetch/sockets";
|
|
5
5
|
//#region ../../node_modules/.pnpm/@better-hooks+lifecycle@2.0.0_react@19.2.5/node_modules/@better-hooks/lifecycle/dist/index.esm.js
|
|
6
6
|
var o = function(t) {
|
|
@@ -136,7 +136,6 @@ var isSameScope = (scope, otherScope) => {
|
|
|
136
136
|
* @returns
|
|
137
137
|
*/
|
|
138
138
|
var useRequestEvents = ({ request, dispatcher, logger, actions, setCacheData, getIsDataProcessing }) => {
|
|
139
|
-
const { unstable_responseMapper } = request;
|
|
140
139
|
const { cache, requestManager } = request.client;
|
|
141
140
|
const onSuccessCallback = useRef(null);
|
|
142
141
|
const onErrorCallback = useRef(null);
|
|
@@ -169,7 +168,8 @@ var useRequestEvents = ({ request, dispatcher, logger, actions, setCacheData, ge
|
|
|
169
168
|
...values,
|
|
170
169
|
mutationContext: opt?.context
|
|
171
170
|
};
|
|
172
|
-
|
|
171
|
+
const isOfflineError = request.offline && isOffline && !success;
|
|
172
|
+
if (isOfflineError) {
|
|
173
173
|
logger.debug({
|
|
174
174
|
title: "Performing offline error callback",
|
|
175
175
|
type: "system",
|
|
@@ -182,9 +182,6 @@ var useRequestEvents = ({ request, dispatcher, logger, actions, setCacheData, ge
|
|
|
182
182
|
type: "system",
|
|
183
183
|
extra: values
|
|
184
184
|
});
|
|
185
|
-
try {
|
|
186
|
-
opt?.rollback?.();
|
|
187
|
-
} catch {}
|
|
188
185
|
onAbortCallback.current?.(paramsWithContext);
|
|
189
186
|
} else if (success) {
|
|
190
187
|
logger.debug({
|
|
@@ -192,7 +189,6 @@ var useRequestEvents = ({ request, dispatcher, logger, actions, setCacheData, ge
|
|
|
192
189
|
type: "system",
|
|
193
190
|
extra: values
|
|
194
191
|
});
|
|
195
|
-
if (opt?.invalidate) opt.invalidate.forEach((req) => cache.invalidate(req));
|
|
196
192
|
onSuccessCallback.current?.(paramsWithContext);
|
|
197
193
|
} else {
|
|
198
194
|
logger.debug({
|
|
@@ -200,13 +196,10 @@ var useRequestEvents = ({ request, dispatcher, logger, actions, setCacheData, ge
|
|
|
200
196
|
type: "system",
|
|
201
197
|
extra: values
|
|
202
198
|
});
|
|
203
|
-
if (!willRetry) try {
|
|
204
|
-
opt?.rollback?.();
|
|
205
|
-
} catch {}
|
|
206
199
|
onErrorCallback.current?.(paramsWithContext);
|
|
207
200
|
}
|
|
208
201
|
onFinishedCallback.current?.(paramsWithContext);
|
|
209
|
-
if (!willRetry) optimisticResultsRef.current.delete(values.requestId);
|
|
202
|
+
if (!willRetry && (!isOfflineError || isCanceled)) optimisticResultsRef.current.delete(values.requestId);
|
|
210
203
|
};
|
|
211
204
|
const handleGetLoadingEvent = (req) => {
|
|
212
205
|
return ({ loading, request: eventRequest }) => {
|
|
@@ -234,7 +227,7 @@ var useRequestEvents = ({ request, dispatcher, logger, actions, setCacheData, ge
|
|
|
234
227
|
};
|
|
235
228
|
const handleResponse = () => {
|
|
236
229
|
return (values) => {
|
|
237
|
-
const data =
|
|
230
|
+
const data = runResponseMapper(request, values.response);
|
|
238
231
|
if (data instanceof Promise) return (async () => {
|
|
239
232
|
handleResponseCallbacks({
|
|
240
233
|
...values,
|
|
@@ -245,13 +238,7 @@ var useRequestEvents = ({ request, dispatcher, logger, actions, setCacheData, ge
|
|
|
245
238
|
};
|
|
246
239
|
};
|
|
247
240
|
const handleRemove = ({ requestId }) => {
|
|
248
|
-
|
|
249
|
-
if (opt) {
|
|
250
|
-
try {
|
|
251
|
-
opt.rollback?.();
|
|
252
|
-
} catch {}
|
|
253
|
-
optimisticResultsRef.current.delete(requestId);
|
|
254
|
-
}
|
|
241
|
+
optimisticResultsRef.current.delete(requestId);
|
|
255
242
|
removeLifecycleListener(requestId);
|
|
256
243
|
};
|
|
257
244
|
const clearCacheDataListener = () => {
|
|
@@ -480,6 +467,7 @@ var pickInitialCacheData = (cacheData, entry) => {
|
|
|
480
467
|
};
|
|
481
468
|
//#endregion
|
|
482
469
|
//#region src/hydrate/hydrate.tsx
|
|
470
|
+
var _jsxFileName$1 = "/home/runner/work/hyper-fetch/hyper-fetch/packages/react/src/hydrate/hydrate.tsx";
|
|
483
471
|
var HydrationContext = React.createContext(null);
|
|
484
472
|
var useIsomorphicLayoutEffect = typeof window !== "undefined" ? useLayoutEffect : useEffect;
|
|
485
473
|
/**
|
|
@@ -519,10 +507,14 @@ var Hydrate = ({ client, data, options, children }) => {
|
|
|
519
507
|
}
|
|
520
508
|
return parent?.get(targetClient, storageKey);
|
|
521
509
|
} }), [client, parent]);
|
|
522
|
-
return /* @__PURE__ */
|
|
510
|
+
return /* @__PURE__ */ jsxDEV(HydrationContext.Provider, {
|
|
523
511
|
value,
|
|
524
512
|
children
|
|
525
|
-
}
|
|
513
|
+
}, void 0, false, {
|
|
514
|
+
fileName: _jsxFileName$1,
|
|
515
|
+
lineNumber: 65,
|
|
516
|
+
columnNumber: 10
|
|
517
|
+
}, void 0);
|
|
526
518
|
};
|
|
527
519
|
/**
|
|
528
520
|
* Access to the closest hydration data
|
|
@@ -670,7 +662,7 @@ var getIsInitiallyLoading = ({ queryKey, dispatcher, hasState, revalidate, disab
|
|
|
670
662
|
return dispatcher.hasRunningRequests(queryKey) || !queue.stopped && disabled === false;
|
|
671
663
|
};
|
|
672
664
|
var getInitialState = ({ initialResponse, dispatcher, request, disabled, revalidate, hydratedEntry }) => {
|
|
673
|
-
const { client, cacheKey
|
|
665
|
+
const { client, cacheKey } = request;
|
|
674
666
|
const { cache } = client;
|
|
675
667
|
const cacheState = getValidCacheData(request, initialResponse, pickInitialCacheData(cache.get(scopeKey(cacheKey, request.scope)), hydratedEntry));
|
|
676
668
|
const initialLoading = getIsInitiallyLoading({
|
|
@@ -681,7 +673,7 @@ var getInitialState = ({ initialResponse, dispatcher, request, disabled, revalid
|
|
|
681
673
|
hasState: !!cacheState
|
|
682
674
|
});
|
|
683
675
|
if (cacheState) {
|
|
684
|
-
const mappedData =
|
|
676
|
+
const mappedData = runResponseMapper(request, cacheState) || cacheState;
|
|
685
677
|
if (mappedData instanceof Promise) return initialState;
|
|
686
678
|
return {
|
|
687
679
|
data: mappedData.data,
|
|
@@ -718,7 +710,7 @@ var getInitialState = ({ initialResponse, dispatcher, request, disabled, revalid
|
|
|
718
710
|
* @internal
|
|
719
711
|
*/
|
|
720
712
|
var useTrackedState = ({ request, dispatcher, initialResponse, deepCompare, dependencyTracking, keepPreviousData = "auto", disabled, revalidate, select, structuralSharing = true }) => {
|
|
721
|
-
const { client, cacheKey, queryKey, staleTime
|
|
713
|
+
const { client, cacheKey, queryKey, staleTime } = request;
|
|
722
714
|
const { cache } = client;
|
|
723
715
|
const hydration = useHydration();
|
|
724
716
|
const state = useRef(null);
|
|
@@ -875,7 +867,7 @@ var useTrackedState = ({ request, dispatcher, initialResponse, deepCompare, depe
|
|
|
875
867
|
processingCacheKey: cacheKey,
|
|
876
868
|
isProcessing: true
|
|
877
869
|
});
|
|
878
|
-
const data =
|
|
870
|
+
const data = runResponseMapper(request, cacheData) || cacheData;
|
|
879
871
|
if (data instanceof Promise) return (async () => {
|
|
880
872
|
const promiseData = await data;
|
|
881
873
|
handleCacheData({
|
|
@@ -1129,6 +1121,7 @@ var initialSocketState = {
|
|
|
1129
1121
|
};
|
|
1130
1122
|
//#endregion
|
|
1131
1123
|
//#region src/provider/provider.tsx
|
|
1124
|
+
var _jsxFileName = "/home/runner/work/hyper-fetch/hyper-fetch/packages/react/src/provider/provider.tsx";
|
|
1132
1125
|
var ConfigContext = React.createContext({
|
|
1133
1126
|
config: {},
|
|
1134
1127
|
setConfig: () => null
|
|
@@ -1146,10 +1139,14 @@ var Provider = ({ children, config }) => {
|
|
|
1146
1139
|
setConfig
|
|
1147
1140
|
};
|
|
1148
1141
|
}, [currentConfig]);
|
|
1149
|
-
return /* @__PURE__ */
|
|
1142
|
+
return /* @__PURE__ */ jsxDEV(ConfigContext.Provider, {
|
|
1150
1143
|
value,
|
|
1151
1144
|
children
|
|
1152
|
-
}
|
|
1145
|
+
}, void 0, false, {
|
|
1146
|
+
fileName: _jsxFileName,
|
|
1147
|
+
lineNumber: 23,
|
|
1148
|
+
columnNumber: 10
|
|
1149
|
+
}, void 0);
|
|
1153
1150
|
};
|
|
1154
1151
|
/**
|
|
1155
1152
|
* Hook to allow reading current context config
|