@web-ts-toolkit/access-router-react 0.38.0 → 0.39.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/README.md +29 -13
- package/index.d.mts +42 -39
- package/index.d.ts +42 -39
- package/index.js +213 -167
- package/index.mjs +213 -167
- package/package.json +3 -3
package/index.js
CHANGED
|
@@ -32,43 +32,58 @@ var import_access_router_client = require("@web-ts-toolkit/access-router-client"
|
|
|
32
32
|
|
|
33
33
|
// src/fetch.ts
|
|
34
34
|
var import_react = require("react");
|
|
35
|
-
function composeAbortSignals(
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
} };
|
|
35
|
+
function composeAbortSignals(internalSignal, ...otherSignals) {
|
|
36
|
+
const uniqueSignals = [];
|
|
37
|
+
for (const signal of [internalSignal, ...otherSignals]) {
|
|
38
|
+
if (!signal || uniqueSignals.includes(signal)) {
|
|
39
|
+
continue;
|
|
40
|
+
}
|
|
41
|
+
if (signal.aborted) {
|
|
42
|
+
return { signal, release: () => {
|
|
43
|
+
} };
|
|
44
|
+
}
|
|
45
|
+
uniqueSignals.push(signal);
|
|
47
46
|
}
|
|
48
|
-
if (
|
|
49
|
-
return { signal:
|
|
47
|
+
if (uniqueSignals.length === 1) {
|
|
48
|
+
return { signal: uniqueSignals[0], release: () => {
|
|
50
49
|
} };
|
|
51
50
|
}
|
|
52
51
|
const composed = new AbortController();
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
52
|
+
let released = false;
|
|
53
|
+
const listeners = /* @__PURE__ */ new Map();
|
|
54
|
+
const release = () => {
|
|
55
|
+
if (released) {
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
released = true;
|
|
59
|
+
for (const [signal, onAbort] of listeners) {
|
|
60
|
+
signal.removeEventListener("abort", onAbort);
|
|
61
|
+
}
|
|
62
|
+
listeners.clear();
|
|
56
63
|
};
|
|
57
64
|
const settle = (source) => {
|
|
58
|
-
composed.
|
|
59
|
-
|
|
65
|
+
if (!composed.signal.aborted) {
|
|
66
|
+
composed.abort(source.reason);
|
|
67
|
+
}
|
|
68
|
+
release();
|
|
60
69
|
};
|
|
61
|
-
const
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
70
|
+
for (const signal of uniqueSignals) {
|
|
71
|
+
const onAbort = () => settle(signal);
|
|
72
|
+
listeners.set(signal, onAbort);
|
|
73
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
74
|
+
}
|
|
65
75
|
return {
|
|
66
76
|
signal: composed.signal,
|
|
67
|
-
|
|
68
|
-
// fired and detached on abort.
|
|
69
|
-
release: detach
|
|
77
|
+
release
|
|
70
78
|
};
|
|
71
79
|
}
|
|
80
|
+
function requestConfigKeyInput(requestConfig) {
|
|
81
|
+
if (!requestConfig) {
|
|
82
|
+
return void 0;
|
|
83
|
+
}
|
|
84
|
+
const { signal: _signal, ...rest } = requestConfig;
|
|
85
|
+
return Object.keys(rest).length === 0 ? void 0 : rest;
|
|
86
|
+
}
|
|
72
87
|
function mergeRequestConfig(requestConfig, signal) {
|
|
73
88
|
if (signal === void 0) {
|
|
74
89
|
return { ...requestConfig ?? {} };
|
|
@@ -99,23 +114,45 @@ var RequestKeyError = class extends Error {
|
|
|
99
114
|
this.name = "RequestKeyError";
|
|
100
115
|
}
|
|
101
116
|
};
|
|
117
|
+
var REQUEST_KEY_MAX_DEPTH = 64;
|
|
118
|
+
var REQUEST_KEY_MAX_NODES = 2e4;
|
|
119
|
+
var REQUEST_KEY_MAX_OUTPUT_LENGTH = 2e5;
|
|
102
120
|
function requestKeyFor(value) {
|
|
103
|
-
return requestKeyForImpl(value, { stack: /* @__PURE__ */ new WeakSet() });
|
|
121
|
+
return requestKeyForImpl(value, { stack: /* @__PURE__ */ new WeakSet(), cache: /* @__PURE__ */ new WeakMap(), nodesVisited: 0, outputLength: 0 }, 0);
|
|
104
122
|
}
|
|
105
|
-
function requestKeyForImpl(value, ctx) {
|
|
106
|
-
if (
|
|
107
|
-
|
|
123
|
+
function requestKeyForImpl(value, ctx, depth) {
|
|
124
|
+
if (depth > REQUEST_KEY_MAX_DEPTH) {
|
|
125
|
+
throw new RequestKeyError(
|
|
126
|
+
`requestKeyFor: request input exceeds the maximum depth of ${REQUEST_KEY_MAX_DEPTH} nested arrays/objects; flatten or normalize the structure before passing it to a query hook.`
|
|
127
|
+
);
|
|
128
|
+
}
|
|
129
|
+
if (value === null) {
|
|
130
|
+
noteRequestKeyNode(ctx);
|
|
131
|
+
return finalizePrimitiveRequestKey(ctx, "n:");
|
|
132
|
+
}
|
|
133
|
+
if (value === void 0) {
|
|
134
|
+
noteRequestKeyNode(ctx);
|
|
135
|
+
return finalizePrimitiveRequestKey(ctx, "u:");
|
|
136
|
+
}
|
|
137
|
+
if (typeof value === "object") {
|
|
138
|
+
const cached = ctx.cache.get(value);
|
|
139
|
+
if (cached !== void 0) {
|
|
140
|
+
reserveRequestKeyOutput(ctx, cached.length);
|
|
141
|
+
return cached;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
noteRequestKeyNode(ctx);
|
|
108
145
|
const t = typeof value;
|
|
109
146
|
switch (t) {
|
|
110
147
|
case "boolean":
|
|
111
|
-
return `b:${value}
|
|
148
|
+
return finalizePrimitiveRequestKey(ctx, `b:${value}`);
|
|
112
149
|
case "number": {
|
|
113
|
-
if (Number.isNaN(value)) return "n:NaN";
|
|
114
|
-
if (Object.is(value, -0)) return "n:-0";
|
|
115
|
-
return `n:${String(value)}
|
|
150
|
+
if (Number.isNaN(value)) return finalizePrimitiveRequestKey(ctx, "n:NaN");
|
|
151
|
+
if (Object.is(value, -0)) return finalizePrimitiveRequestKey(ctx, "n:-0");
|
|
152
|
+
return finalizePrimitiveRequestKey(ctx, `n:${String(value)}`);
|
|
116
153
|
}
|
|
117
154
|
case "string":
|
|
118
|
-
return `s:${JSON.stringify(value)}
|
|
155
|
+
return finalizePrimitiveRequestKey(ctx, `s:${JSON.stringify(value)}`);
|
|
119
156
|
case "bigint":
|
|
120
157
|
throw new RequestKeyError(
|
|
121
158
|
"requestKeyFor: bigint is not supported in request keys; convert to a number or string before passing to a query hook."
|
|
@@ -130,7 +167,7 @@ function requestKeyForImpl(value, ctx) {
|
|
|
130
167
|
);
|
|
131
168
|
case "object": {
|
|
132
169
|
if (value instanceof Date) {
|
|
133
|
-
return `d:${value.getTime()}
|
|
170
|
+
return finalizePrimitiveRequestKey(ctx, `d:${value.getTime()}`);
|
|
134
171
|
}
|
|
135
172
|
if (value instanceof RegExp || value instanceof Map || value instanceof Set || typeof URL !== "undefined" && value instanceof URL || value instanceof Error) {
|
|
136
173
|
throw new RequestKeyError(
|
|
@@ -142,13 +179,19 @@ function requestKeyForImpl(value, ctx) {
|
|
|
142
179
|
throw new RequestKeyError("requestKeyFor: cycle detected in array (request key).");
|
|
143
180
|
}
|
|
144
181
|
ctx.stack.add(value);
|
|
182
|
+
reserveRequestKeyOutput(ctx, 1);
|
|
145
183
|
let out2 = "[";
|
|
146
184
|
for (let i = 0; i < value.length; i++) {
|
|
147
|
-
if (i > 0)
|
|
148
|
-
|
|
185
|
+
if (i > 0) {
|
|
186
|
+
reserveRequestKeyOutput(ctx, 1);
|
|
187
|
+
out2 += ",";
|
|
188
|
+
}
|
|
189
|
+
out2 += requestKeyForImpl(value[i], ctx, depth + 1);
|
|
149
190
|
}
|
|
191
|
+
reserveRequestKeyOutput(ctx, 1);
|
|
150
192
|
out2 += "]";
|
|
151
193
|
ctx.stack.delete(value);
|
|
194
|
+
ctx.cache.set(value, out2);
|
|
152
195
|
return out2;
|
|
153
196
|
}
|
|
154
197
|
const proto = Object.getPrototypeOf(value);
|
|
@@ -168,9 +211,13 @@ function requestKeyForImpl(value, ctx) {
|
|
|
168
211
|
);
|
|
169
212
|
}
|
|
170
213
|
const keys = Object.keys(value).sort();
|
|
214
|
+
reserveRequestKeyOutput(ctx, 1);
|
|
171
215
|
let out = "{";
|
|
172
216
|
for (let i = 0; i < keys.length; i++) {
|
|
173
|
-
if (i > 0)
|
|
217
|
+
if (i > 0) {
|
|
218
|
+
reserveRequestKeyOutput(ctx, 1);
|
|
219
|
+
out += ",";
|
|
220
|
+
}
|
|
174
221
|
const k = keys[i];
|
|
175
222
|
const desc = Object.getOwnPropertyDescriptor(value, k);
|
|
176
223
|
if (desc === void 0) {
|
|
@@ -181,16 +228,40 @@ function requestKeyForImpl(value, ctx) {
|
|
|
181
228
|
`requestKeyFor: accessor property ${JSON.stringify(k)} is not supported in request keys; getters/setters must not run during dep-key construction.`
|
|
182
229
|
);
|
|
183
230
|
}
|
|
184
|
-
|
|
231
|
+
const keyText = JSON.stringify(k);
|
|
232
|
+
reserveRequestKeyOutput(ctx, keyText.length + 1);
|
|
233
|
+
out += `${keyText}:${requestKeyForImpl(desc.value, ctx, depth + 1)}`;
|
|
185
234
|
}
|
|
235
|
+
reserveRequestKeyOutput(ctx, 1);
|
|
186
236
|
out += "}";
|
|
187
237
|
ctx.stack.delete(value);
|
|
238
|
+
ctx.cache.set(value, out);
|
|
188
239
|
return out;
|
|
189
240
|
}
|
|
190
241
|
default:
|
|
191
242
|
throw new RequestKeyError(`requestKeyFor: unsupported value of type ${t}.`);
|
|
192
243
|
}
|
|
193
244
|
}
|
|
245
|
+
function noteRequestKeyNode(ctx) {
|
|
246
|
+
ctx.nodesVisited += 1;
|
|
247
|
+
if (ctx.nodesVisited > REQUEST_KEY_MAX_NODES) {
|
|
248
|
+
throw new RequestKeyError(
|
|
249
|
+
`requestKeyFor: request input exceeds the maximum traversal budget of ${REQUEST_KEY_MAX_NODES} nodes; reduce the filter, params, or requestConfig shape before passing it to a query hook.`
|
|
250
|
+
);
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
function finalizePrimitiveRequestKey(ctx, key) {
|
|
254
|
+
reserveRequestKeyOutput(ctx, key.length);
|
|
255
|
+
return key;
|
|
256
|
+
}
|
|
257
|
+
function reserveRequestKeyOutput(ctx, length) {
|
|
258
|
+
ctx.outputLength += length;
|
|
259
|
+
if (ctx.outputLength > REQUEST_KEY_MAX_OUTPUT_LENGTH) {
|
|
260
|
+
throw new RequestKeyError(
|
|
261
|
+
`requestKeyFor: request input exceeds the maximum serialized key length of ${REQUEST_KEY_MAX_OUTPUT_LENGTH} characters; reduce repeated or oversized request data before passing it to a query hook.`
|
|
262
|
+
);
|
|
263
|
+
}
|
|
264
|
+
}
|
|
194
265
|
function useMountRef() {
|
|
195
266
|
const ref = (0, import_react.useRef)(true);
|
|
196
267
|
(0, import_react.useEffect)(() => {
|
|
@@ -219,11 +290,33 @@ function useEventCallback(cb) {
|
|
|
219
290
|
}, []);
|
|
220
291
|
return invoker;
|
|
221
292
|
}
|
|
293
|
+
function useLatestRef(value) {
|
|
294
|
+
const ref = (0, import_react2.useRef)(value);
|
|
295
|
+
(0, import_react2.useLayoutEffect)(() => {
|
|
296
|
+
ref.current = value;
|
|
297
|
+
}, [value]);
|
|
298
|
+
return ref;
|
|
299
|
+
}
|
|
300
|
+
function reportObserverError(error) {
|
|
301
|
+
queueMicrotask(() => {
|
|
302
|
+
throw error;
|
|
303
|
+
});
|
|
304
|
+
}
|
|
305
|
+
function runObserverSequence(invocations) {
|
|
306
|
+
for (const invoke of invocations) {
|
|
307
|
+
try {
|
|
308
|
+
invoke();
|
|
309
|
+
} catch (error) {
|
|
310
|
+
reportObserverError(error);
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
}
|
|
222
314
|
function useAutoQuery({
|
|
223
315
|
doFetch,
|
|
224
316
|
applyResult,
|
|
225
317
|
shouldFetch,
|
|
226
318
|
deps,
|
|
319
|
+
getRequestSignal,
|
|
227
320
|
onSuccess,
|
|
228
321
|
onError,
|
|
229
322
|
onSettled,
|
|
@@ -238,37 +331,42 @@ function useAutoQuery({
|
|
|
238
331
|
const manager = useAbortManager();
|
|
239
332
|
const mountRef = useMountRef();
|
|
240
333
|
const ownerIdRef = (0, import_react2.useRef)(0);
|
|
334
|
+
const createAbortScope = (0, import_react2.useCallback)(
|
|
335
|
+
(callerSignal) => {
|
|
336
|
+
const controller = new AbortController();
|
|
337
|
+
manager.replace(controller);
|
|
338
|
+
const composed = composeAbortSignals(controller.signal, getRequestSignal?.(), callerSignal);
|
|
339
|
+
return {
|
|
340
|
+
controller,
|
|
341
|
+
effectiveSignal: composed.signal,
|
|
342
|
+
release: composed.release
|
|
343
|
+
};
|
|
344
|
+
},
|
|
345
|
+
[manager, getRequestSignal]
|
|
346
|
+
);
|
|
241
347
|
const fireCallbacksSafely = (0, import_react2.useCallback)(
|
|
242
348
|
(settle) => {
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
onSettled?.(settle.result, null);
|
|
247
|
-
} else {
|
|
248
|
-
onError?.(settle.error);
|
|
249
|
-
onSettled?.(null, settle.error);
|
|
250
|
-
}
|
|
251
|
-
} catch (cbErr) {
|
|
252
|
-
queueMicrotask(() => {
|
|
253
|
-
throw cbErr;
|
|
254
|
-
});
|
|
349
|
+
if ("result" in settle) {
|
|
350
|
+
runObserverSequence([() => onSuccess?.(settle.result), () => onSettled?.(settle.result, null)]);
|
|
351
|
+
return;
|
|
255
352
|
}
|
|
353
|
+
runObserverSequence([() => onError?.(settle.error), () => onSettled?.(null, settle.error)]);
|
|
256
354
|
},
|
|
257
355
|
[onSuccess, onError, onSettled]
|
|
258
356
|
);
|
|
259
357
|
const runWithCallbacks = (0, import_react2.useCallback)(
|
|
260
|
-
async (
|
|
261
|
-
const
|
|
358
|
+
async (abortScope, doFetchOverride) => {
|
|
359
|
+
const { effectiveSignal, release } = abortScope;
|
|
262
360
|
const myId = ++ownerIdRef.current;
|
|
263
361
|
setIsFetching(true);
|
|
264
362
|
setError(null);
|
|
265
363
|
if (!hasDataRef.current) setIsLoading(true);
|
|
266
364
|
try {
|
|
267
|
-
const res = await doFetchOverride(
|
|
365
|
+
const res = await doFetchOverride(effectiveSignal);
|
|
268
366
|
if (myId !== ownerIdRef.current) {
|
|
269
367
|
return res;
|
|
270
368
|
}
|
|
271
|
-
if (
|
|
369
|
+
if (effectiveSignal.aborted) {
|
|
272
370
|
if (mountRef.current) {
|
|
273
371
|
if (!hasDataRef.current) setIsLoading(false);
|
|
274
372
|
setIsFetching(false);
|
|
@@ -286,7 +384,7 @@ function useAutoQuery({
|
|
|
286
384
|
if (myId !== ownerIdRef.current) {
|
|
287
385
|
throw err;
|
|
288
386
|
}
|
|
289
|
-
if (
|
|
387
|
+
if (effectiveSignal.aborted) {
|
|
290
388
|
if (mountRef.current) {
|
|
291
389
|
if (!hasDataRef.current) setIsLoading(false);
|
|
292
390
|
setIsFetching(false);
|
|
@@ -300,6 +398,8 @@ function useAutoQuery({
|
|
|
300
398
|
fireCallbacksSafely({ error: err });
|
|
301
399
|
onFailed?.();
|
|
302
400
|
throw err;
|
|
401
|
+
} finally {
|
|
402
|
+
release();
|
|
303
403
|
}
|
|
304
404
|
},
|
|
305
405
|
[applyResult, fireCallbacksSafely, mountRef, onFailed, onAborted]
|
|
@@ -312,40 +412,35 @@ function useAutoQuery({
|
|
|
312
412
|
return;
|
|
313
413
|
}
|
|
314
414
|
setIsLoading(!hasDataRef.current);
|
|
315
|
-
const
|
|
316
|
-
|
|
317
|
-
runWithCallbacks(controller, doFetch).catch(() => {
|
|
415
|
+
const abortScope = createAbortScope();
|
|
416
|
+
runWithCallbacks(abortScope, doFetch).catch(() => {
|
|
318
417
|
});
|
|
319
418
|
return () => {
|
|
320
|
-
controller.abort();
|
|
419
|
+
abortScope.controller.abort();
|
|
321
420
|
};
|
|
322
421
|
}, deps);
|
|
323
422
|
const query = (0, import_react2.useCallback)(
|
|
324
423
|
(doFetchOverride, callerSignal) => {
|
|
325
|
-
const
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
} };
|
|
329
|
-
const effectiveSignal = composed.signal;
|
|
330
|
-
const p = runWithCallbacks(controller, () => doFetchOverride(effectiveSignal));
|
|
331
|
-
p.finally(() => composed.release()).catch(() => {
|
|
424
|
+
const abortScope = createAbortScope(callerSignal);
|
|
425
|
+
const p = runWithCallbacks(abortScope, doFetchOverride);
|
|
426
|
+
p.catch(() => {
|
|
332
427
|
});
|
|
333
428
|
return p;
|
|
334
429
|
},
|
|
335
|
-
[
|
|
430
|
+
[createAbortScope, runWithCallbacks]
|
|
336
431
|
);
|
|
337
432
|
const refetch = (0, import_react2.useCallback)(() => {
|
|
338
|
-
const
|
|
339
|
-
|
|
340
|
-
const p = runWithCallbacks(controller, doFetch);
|
|
433
|
+
const abortScope = createAbortScope();
|
|
434
|
+
const p = runWithCallbacks(abortScope, doFetch);
|
|
341
435
|
p.catch(() => {
|
|
342
436
|
});
|
|
343
437
|
return p;
|
|
344
|
-
}, [
|
|
438
|
+
}, [createAbortScope, runWithCallbacks, doFetch]);
|
|
345
439
|
const resetError = (0, import_react2.useCallback)(() => {
|
|
346
440
|
setError(null);
|
|
347
441
|
}, []);
|
|
348
442
|
const resetLoading = (0, import_react2.useCallback)(() => {
|
|
443
|
+
ownerIdRef.current += 1;
|
|
349
444
|
setIsLoading(false);
|
|
350
445
|
setIsFetching(false);
|
|
351
446
|
hasDataRef.current = false;
|
|
@@ -368,7 +463,7 @@ function useMutation(execute, applyData, options) {
|
|
|
368
463
|
const [isPending, setIsPending] = (0, import_react2.useState)(false);
|
|
369
464
|
const [error, setError] = (0, import_react2.useState)(null);
|
|
370
465
|
const mountRef = useMountRef();
|
|
371
|
-
const { onSuccess, onSettled } = options ?? {};
|
|
466
|
+
const { onSuccess, onError, onSettled } = options ?? {};
|
|
372
467
|
const activeCountRef = (0, import_react2.useRef)(0);
|
|
373
468
|
const latestIdRef = (0, import_react2.useRef)(0);
|
|
374
469
|
const executeMutate = (0, import_react2.useCallback)(
|
|
@@ -382,14 +477,7 @@ function useMutation(execute, applyData, options) {
|
|
|
382
477
|
try {
|
|
383
478
|
const result = await execute(...args);
|
|
384
479
|
if (mountRef.current) {
|
|
385
|
-
|
|
386
|
-
onSuccess?.(result);
|
|
387
|
-
onSettled?.(result, null);
|
|
388
|
-
} catch (cbErr) {
|
|
389
|
-
queueMicrotask(() => {
|
|
390
|
-
throw cbErr;
|
|
391
|
-
});
|
|
392
|
-
}
|
|
480
|
+
runObserverSequence([() => onSuccess?.(result), () => onSettled?.(result, null)]);
|
|
393
481
|
if (myId === latestIdRef.current) {
|
|
394
482
|
setData(applyData(result));
|
|
395
483
|
}
|
|
@@ -398,13 +486,7 @@ function useMutation(execute, applyData, options) {
|
|
|
398
486
|
} catch (err) {
|
|
399
487
|
const svcErr = err;
|
|
400
488
|
if (mountRef.current) {
|
|
401
|
-
|
|
402
|
-
onSettled?.(null, svcErr);
|
|
403
|
-
} catch (cbErr) {
|
|
404
|
-
queueMicrotask(() => {
|
|
405
|
-
throw cbErr;
|
|
406
|
-
});
|
|
407
|
-
}
|
|
489
|
+
runObserverSequence([() => onError?.(svcErr), () => onSettled?.(null, svcErr)]);
|
|
408
490
|
if (myId === latestIdRef.current) {
|
|
409
491
|
setError(svcErr);
|
|
410
492
|
}
|
|
@@ -417,7 +499,7 @@ function useMutation(execute, applyData, options) {
|
|
|
417
499
|
}
|
|
418
500
|
}
|
|
419
501
|
},
|
|
420
|
-
[execute, applyData, mountRef, onSuccess, onSettled]
|
|
502
|
+
[execute, applyData, mountRef, onSuccess, onError, onSettled]
|
|
421
503
|
);
|
|
422
504
|
const reset = (0, import_react2.useCallback)(() => {
|
|
423
505
|
latestIdRef.current += 1;
|
|
@@ -447,6 +529,7 @@ function createModelHooks(config) {
|
|
|
447
529
|
onSettled
|
|
448
530
|
} = options;
|
|
449
531
|
const [data, setData] = (0, import_react2.useState)(initialData);
|
|
532
|
+
const requestConfigRef = useLatestRef(requestConfig);
|
|
450
533
|
const applyResult = (0, import_react2.useCallback)((res) => {
|
|
451
534
|
setData(res.data);
|
|
452
535
|
}, []);
|
|
@@ -460,7 +543,7 @@ function createModelHooks(config) {
|
|
|
460
543
|
tasks,
|
|
461
544
|
basicOptions,
|
|
462
545
|
advancedOptions,
|
|
463
|
-
requestConfig
|
|
546
|
+
requestConfig: requestConfigKeyInput(requestConfig)
|
|
464
547
|
});
|
|
465
548
|
} catch (e) {
|
|
466
549
|
if (e instanceof RequestKeyError) {
|
|
@@ -470,7 +553,7 @@ function createModelHooks(config) {
|
|
|
470
553
|
}
|
|
471
554
|
const doFetchById = (0, import_react2.useCallback)(
|
|
472
555
|
async (targetId, signal) => {
|
|
473
|
-
const forwardedConfig = mergeRequestConfig(
|
|
556
|
+
const forwardedConfig = mergeRequestConfig(requestConfigRef.current, signal);
|
|
474
557
|
if (advanced) {
|
|
475
558
|
const raw2 = await modelService.readAdvanced(
|
|
476
559
|
targetId,
|
|
@@ -508,6 +591,7 @@ function createModelHooks(config) {
|
|
|
508
591
|
const onSuccessStable = useEventCallback(onSuccess);
|
|
509
592
|
const onErrorStable = useEventCallback(onError);
|
|
510
593
|
const onSettledStable = useEventCallback(onSettled);
|
|
594
|
+
const getRequestSignal = (0, import_react2.useCallback)(() => requestConfigRef.current?.signal, [requestConfigRef]);
|
|
511
595
|
const {
|
|
512
596
|
isLoading,
|
|
513
597
|
isFetching,
|
|
@@ -521,6 +605,7 @@ function createModelHooks(config) {
|
|
|
521
605
|
applyResult,
|
|
522
606
|
shouldFetch,
|
|
523
607
|
deps: [id, enabled, advanced, requestKey],
|
|
608
|
+
getRequestSignal,
|
|
524
609
|
onSuccess: onSuccessStable,
|
|
525
610
|
onError: onErrorStable,
|
|
526
611
|
onSettled: onSettledStable
|
|
@@ -557,6 +642,7 @@ function createModelHooks(config) {
|
|
|
557
642
|
onSettled
|
|
558
643
|
} = options;
|
|
559
644
|
const [data, setData] = (0, import_react2.useState)(initialData ?? []);
|
|
645
|
+
const requestConfigRef = useLatestRef(requestConfig);
|
|
560
646
|
const [previousData, setPreviousData] = (0, import_react2.useState)(void 0);
|
|
561
647
|
const [totalCount, setTotalCount] = (0, import_react2.useState)(0);
|
|
562
648
|
const latestDataRef = (0, import_react2.useRef)(data);
|
|
@@ -586,7 +672,7 @@ function createModelHooks(config) {
|
|
|
586
672
|
tasks,
|
|
587
673
|
basicOptions,
|
|
588
674
|
advancedOptions,
|
|
589
|
-
requestConfig
|
|
675
|
+
requestConfig: requestConfigKeyInput(requestConfig)
|
|
590
676
|
});
|
|
591
677
|
} catch (e) {
|
|
592
678
|
if (e instanceof RequestKeyError) {
|
|
@@ -599,18 +685,19 @@ function createModelHooks(config) {
|
|
|
599
685
|
if (keepPreviousData && hasSettledRef.current) {
|
|
600
686
|
setPreviousData(latestDataRef.current);
|
|
601
687
|
}
|
|
602
|
-
const forwardedConfig = mergeRequestConfig(
|
|
688
|
+
const forwardedConfig = mergeRequestConfig(requestConfigRef.current, signal);
|
|
689
|
+
const effectiveArgs = args ?? listParams;
|
|
603
690
|
if (advanced) {
|
|
604
691
|
const raw2 = await modelService.listAdvanced(
|
|
605
692
|
filter ?? {},
|
|
606
|
-
{ sort, select, populate, include, tasks, ...
|
|
693
|
+
{ sort, select, populate, include, tasks, ...effectiveArgs },
|
|
607
694
|
advancedOptions,
|
|
608
695
|
forwardedConfig
|
|
609
696
|
).exec();
|
|
610
697
|
assertSuccess(raw2);
|
|
611
698
|
return raw2;
|
|
612
699
|
}
|
|
613
|
-
const raw = await modelService.list(
|
|
700
|
+
const raw = await modelService.list(effectiveArgs, basicOptions, forwardedConfig).exec();
|
|
614
701
|
assertSuccess(raw);
|
|
615
702
|
return raw;
|
|
616
703
|
},
|
|
@@ -631,13 +718,14 @@ function createModelHooks(config) {
|
|
|
631
718
|
// cannot see that derivation, so the missing-deps warning is
|
|
632
719
|
// silenced here intentionally.
|
|
633
720
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
634
|
-
[modelService, advanced, filterKey, sortKey, requestKey, keepPreviousData, latestDataRef]
|
|
721
|
+
[modelService, advanced, listParamsKey, filterKey, sortKey, requestKey, keepPreviousData, latestDataRef]
|
|
635
722
|
);
|
|
636
723
|
const doFetch = (0, import_react2.useCallback)((signal) => baseFetch(listParams, signal), [baseFetch, listParamsKey]);
|
|
637
724
|
const shouldFetch = Boolean((listParams || advanced) && enabled);
|
|
638
725
|
const onSuccessStable = useEventCallback(onSuccess);
|
|
639
726
|
const onErrorStable = useEventCallback(onError);
|
|
640
727
|
const onSettledStable = useEventCallback(onSettled);
|
|
728
|
+
const getRequestSignal = (0, import_react2.useCallback)(() => requestConfigRef.current?.signal, [requestConfigRef]);
|
|
641
729
|
const {
|
|
642
730
|
isLoading,
|
|
643
731
|
isFetching,
|
|
@@ -651,6 +739,7 @@ function createModelHooks(config) {
|
|
|
651
739
|
applyResult,
|
|
652
740
|
shouldFetch,
|
|
653
741
|
deps: [listParamsKey, filterKey, advanced, enabled, sortKey, requestKey],
|
|
742
|
+
getRequestSignal,
|
|
654
743
|
onSuccess: onSuccessStable,
|
|
655
744
|
onError: onErrorStable,
|
|
656
745
|
onSettled: onSettledStable,
|
|
@@ -690,7 +779,6 @@ function createModelHooks(config) {
|
|
|
690
779
|
onError,
|
|
691
780
|
onSettled
|
|
692
781
|
} = options;
|
|
693
|
-
const mountRef = useMountRef();
|
|
694
782
|
const execute = (0, import_react2.useCallback)(
|
|
695
783
|
async (createData) => {
|
|
696
784
|
let res;
|
|
@@ -714,7 +802,7 @@ function createModelHooks(config) {
|
|
|
714
802
|
// documented pattern for `createModelHooks`. The same pattern
|
|
715
803
|
// is used in `useRead.useList.useCount.useDistinct` query hooks.
|
|
716
804
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
717
|
-
[modelService, advanced, select, populate, tasks, basicOptions, advancedOptions, requestConfig
|
|
805
|
+
[modelService, advanced, select, populate, tasks, basicOptions, advancedOptions, requestConfig]
|
|
718
806
|
);
|
|
719
807
|
const {
|
|
720
808
|
data,
|
|
@@ -725,23 +813,20 @@ function createModelHooks(config) {
|
|
|
725
813
|
} = useMutation(
|
|
726
814
|
execute,
|
|
727
815
|
(res) => res.data,
|
|
728
|
-
{ onSuccess, onSettled }
|
|
816
|
+
{ onSuccess, onError, onSettled }
|
|
729
817
|
);
|
|
730
818
|
const mutate = (0, import_react2.useCallback)(
|
|
731
819
|
async (createData) => {
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
throw err;
|
|
820
|
+
if (Array.isArray(createData)) {
|
|
821
|
+
throw new TypeError(
|
|
822
|
+
"useCreate.mutate is single-record-only. Array input is not supported by the hook; call modelService.create(...) directly for bulk create."
|
|
823
|
+
);
|
|
737
824
|
}
|
|
825
|
+
return executeMutate(createData);
|
|
738
826
|
},
|
|
739
|
-
[executeMutate
|
|
827
|
+
[executeMutate]
|
|
740
828
|
);
|
|
741
|
-
|
|
742
|
-
resetMutation();
|
|
743
|
-
}, [resetMutation]);
|
|
744
|
-
return { data, isPending, error, mutate, reset };
|
|
829
|
+
return { data, isPending, error, mutate, reset: resetMutation };
|
|
745
830
|
}
|
|
746
831
|
function useUpdate(options = {}) {
|
|
747
832
|
const {
|
|
@@ -756,7 +841,6 @@ function createModelHooks(config) {
|
|
|
756
841
|
onError,
|
|
757
842
|
onSettled
|
|
758
843
|
} = options;
|
|
759
|
-
const mountRef = useMountRef();
|
|
760
844
|
const execute = (0, import_react2.useCallback)(
|
|
761
845
|
async (updateId, updateData) => {
|
|
762
846
|
let res;
|
|
@@ -781,7 +865,7 @@ function createModelHooks(config) {
|
|
|
781
865
|
// documented pattern for `createModelHooks`. The same pattern
|
|
782
866
|
// is used in `useRead.useList.useCount.useDistinct` query hooks.
|
|
783
867
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
784
|
-
[modelService, advanced, select, populate, tasks, basicOptions, advancedOptions, requestConfig
|
|
868
|
+
[modelService, advanced, select, populate, tasks, basicOptions, advancedOptions, requestConfig]
|
|
785
869
|
);
|
|
786
870
|
const {
|
|
787
871
|
data,
|
|
@@ -792,23 +876,9 @@ function createModelHooks(config) {
|
|
|
792
876
|
} = useMutation(
|
|
793
877
|
execute,
|
|
794
878
|
(res) => res.data,
|
|
795
|
-
{ onSuccess, onSettled }
|
|
796
|
-
);
|
|
797
|
-
const mutate = (0, import_react2.useCallback)(
|
|
798
|
-
async (updateId, updateData) => {
|
|
799
|
-
try {
|
|
800
|
-
return await executeMutate(updateId, updateData);
|
|
801
|
-
} catch (err) {
|
|
802
|
-
if (mountRef.current) onError?.(err);
|
|
803
|
-
throw err;
|
|
804
|
-
}
|
|
805
|
-
},
|
|
806
|
-
[executeMutate, onError, mountRef]
|
|
879
|
+
{ onSuccess, onError, onSettled }
|
|
807
880
|
);
|
|
808
|
-
|
|
809
|
-
resetMutation();
|
|
810
|
-
}, [resetMutation]);
|
|
811
|
-
return { data, isPending, error, mutate, reset };
|
|
881
|
+
return { data, isPending, error, mutate: executeMutate, reset: resetMutation };
|
|
812
882
|
}
|
|
813
883
|
function useUpsert(options = {}) {
|
|
814
884
|
const {
|
|
@@ -823,7 +893,6 @@ function createModelHooks(config) {
|
|
|
823
893
|
onError,
|
|
824
894
|
onSettled
|
|
825
895
|
} = options;
|
|
826
|
-
const mountRef = useMountRef();
|
|
827
896
|
const execute = (0, import_react2.useCallback)(
|
|
828
897
|
async (upsertData) => {
|
|
829
898
|
let res;
|
|
@@ -847,7 +916,7 @@ function createModelHooks(config) {
|
|
|
847
916
|
// documented pattern for `createModelHooks`. The same pattern
|
|
848
917
|
// is used in `useRead.useList.useCount.useDistinct` query hooks.
|
|
849
918
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
850
|
-
[modelService, advanced, select, populate, tasks, basicOptions, advancedOptions, requestConfig
|
|
919
|
+
[modelService, advanced, select, populate, tasks, basicOptions, advancedOptions, requestConfig]
|
|
851
920
|
);
|
|
852
921
|
const {
|
|
853
922
|
data,
|
|
@@ -858,27 +927,12 @@ function createModelHooks(config) {
|
|
|
858
927
|
} = useMutation(
|
|
859
928
|
execute,
|
|
860
929
|
(res) => res.data,
|
|
861
|
-
{ onSuccess, onSettled }
|
|
930
|
+
{ onSuccess, onError, onSettled }
|
|
862
931
|
);
|
|
863
|
-
|
|
864
|
-
async (upsertData) => {
|
|
865
|
-
try {
|
|
866
|
-
return await executeMutate(upsertData);
|
|
867
|
-
} catch (err) {
|
|
868
|
-
if (mountRef.current) onError?.(err);
|
|
869
|
-
throw err;
|
|
870
|
-
}
|
|
871
|
-
},
|
|
872
|
-
[executeMutate, onError, mountRef]
|
|
873
|
-
);
|
|
874
|
-
const reset = (0, import_react2.useCallback)(() => {
|
|
875
|
-
resetMutation();
|
|
876
|
-
}, [resetMutation]);
|
|
877
|
-
return { data, isPending, error, mutate, reset };
|
|
932
|
+
return { data, isPending, error, mutate: executeMutate, reset: resetMutation };
|
|
878
933
|
}
|
|
879
934
|
function useDelete(options = {}) {
|
|
880
935
|
const { requestConfig, onSuccess, onError, onSettled } = options;
|
|
881
|
-
const mountRef = useMountRef();
|
|
882
936
|
const execute = (0, import_react2.useCallback)(
|
|
883
937
|
async (deleteId) => {
|
|
884
938
|
const res = await modelService.delete(deleteId, requestConfig).exec();
|
|
@@ -898,27 +952,14 @@ function createModelHooks(config) {
|
|
|
898
952
|
} = useMutation(
|
|
899
953
|
execute,
|
|
900
954
|
() => null,
|
|
901
|
-
{ onSuccess, onSettled }
|
|
955
|
+
{ onSuccess, onError, onSettled }
|
|
902
956
|
);
|
|
903
|
-
|
|
904
|
-
async (deleteId) => {
|
|
905
|
-
try {
|
|
906
|
-
return await executeMutate(deleteId);
|
|
907
|
-
} catch (err) {
|
|
908
|
-
if (mountRef.current) onError?.(err);
|
|
909
|
-
throw err;
|
|
910
|
-
}
|
|
911
|
-
},
|
|
912
|
-
[executeMutate, onError, mountRef]
|
|
913
|
-
);
|
|
914
|
-
const reset = (0, import_react2.useCallback)(() => {
|
|
915
|
-
resetMutation();
|
|
916
|
-
}, [resetMutation]);
|
|
917
|
-
return { isPending, error, mutate, reset };
|
|
957
|
+
return { isPending, error, mutate: executeMutate, reset: resetMutation };
|
|
918
958
|
}
|
|
919
959
|
function useCount(options = {}) {
|
|
920
960
|
const { advanced, filter, enabled = true, requestConfig, onSuccess, onError, onSettled } = options;
|
|
921
961
|
const [data, setData] = (0, import_react2.useState)(null);
|
|
962
|
+
const requestConfigRef = useLatestRef(requestConfig);
|
|
922
963
|
const applyResult = (0, import_react2.useCallback)((res) => {
|
|
923
964
|
setData(res.data);
|
|
924
965
|
}, []);
|
|
@@ -926,7 +967,7 @@ function createModelHooks(config) {
|
|
|
926
967
|
let requestKey;
|
|
927
968
|
try {
|
|
928
969
|
filterKey = requestKeyFor(filter);
|
|
929
|
-
requestKey = requestKeyFor(requestConfig);
|
|
970
|
+
requestKey = requestKeyFor(requestConfigKeyInput(requestConfig));
|
|
930
971
|
} catch (e) {
|
|
931
972
|
if (e instanceof RequestKeyError) {
|
|
932
973
|
throw new Error(`useCount: ${e.message}`, { cause: e });
|
|
@@ -935,7 +976,7 @@ function createModelHooks(config) {
|
|
|
935
976
|
}
|
|
936
977
|
const doFetch = (0, import_react2.useCallback)(
|
|
937
978
|
async (signal) => {
|
|
938
|
-
const forwardedConfig = mergeRequestConfig(
|
|
979
|
+
const forwardedConfig = mergeRequestConfig(requestConfigRef.current, signal);
|
|
939
980
|
if (advanced) {
|
|
940
981
|
const raw2 = await modelService.countAdvanced(filter ?? {}, forwardedConfig).exec();
|
|
941
982
|
assertSuccess(raw2);
|
|
@@ -954,6 +995,7 @@ function createModelHooks(config) {
|
|
|
954
995
|
const onSuccessStable = useEventCallback(onSuccess);
|
|
955
996
|
const onErrorStable = useEventCallback(onError);
|
|
956
997
|
const onSettledStable = useEventCallback(onSettled);
|
|
998
|
+
const getRequestSignal = (0, import_react2.useCallback)(() => requestConfigRef.current?.signal, [requestConfigRef]);
|
|
957
999
|
const {
|
|
958
1000
|
isLoading,
|
|
959
1001
|
isFetching,
|
|
@@ -967,6 +1009,7 @@ function createModelHooks(config) {
|
|
|
967
1009
|
applyResult,
|
|
968
1010
|
shouldFetch: enabled,
|
|
969
1011
|
deps: [enabled, advanced, filterKey, requestKey],
|
|
1012
|
+
getRequestSignal,
|
|
970
1013
|
onSuccess: onSuccessStable,
|
|
971
1014
|
onError: onErrorStable,
|
|
972
1015
|
onSettled: onSettledStable
|
|
@@ -985,6 +1028,7 @@ function createModelHooks(config) {
|
|
|
985
1028
|
function useDistinct(options) {
|
|
986
1029
|
const { field, conditions, enabled = true, requestConfig, onSuccess, onError, onSettled } = options;
|
|
987
1030
|
const [data, setData] = (0, import_react2.useState)(null);
|
|
1031
|
+
const requestConfigRef = useLatestRef(requestConfig);
|
|
988
1032
|
const applyResult = (0, import_react2.useCallback)((res) => {
|
|
989
1033
|
setData(res.data);
|
|
990
1034
|
}, []);
|
|
@@ -992,7 +1036,7 @@ function createModelHooks(config) {
|
|
|
992
1036
|
let requestKey;
|
|
993
1037
|
try {
|
|
994
1038
|
conditionsKey = requestKeyFor(conditions);
|
|
995
|
-
requestKey = requestKeyFor(requestConfig);
|
|
1039
|
+
requestKey = requestKeyFor(requestConfigKeyInput(requestConfig));
|
|
996
1040
|
} catch (e) {
|
|
997
1041
|
if (e instanceof RequestKeyError) {
|
|
998
1042
|
throw new Error(`useDistinct: ${e.message}`, { cause: e });
|
|
@@ -1001,7 +1045,7 @@ function createModelHooks(config) {
|
|
|
1001
1045
|
}
|
|
1002
1046
|
const doFetch = (0, import_react2.useCallback)(
|
|
1003
1047
|
async (signal) => {
|
|
1004
|
-
const forwardedConfig = mergeRequestConfig(
|
|
1048
|
+
const forwardedConfig = mergeRequestConfig(requestConfigRef.current, signal);
|
|
1005
1049
|
if (conditions && Object.keys(conditions).length > 0) {
|
|
1006
1050
|
const raw2 = await modelService.distinctAdvanced(field, conditions, forwardedConfig).exec();
|
|
1007
1051
|
assertSuccess(raw2);
|
|
@@ -1020,6 +1064,7 @@ function createModelHooks(config) {
|
|
|
1020
1064
|
const onSuccessStable = useEventCallback(onSuccess);
|
|
1021
1065
|
const onErrorStable = useEventCallback(onError);
|
|
1022
1066
|
const onSettledStable = useEventCallback(onSettled);
|
|
1067
|
+
const getRequestSignal = (0, import_react2.useCallback)(() => requestConfigRef.current?.signal, [requestConfigRef]);
|
|
1023
1068
|
const {
|
|
1024
1069
|
isLoading,
|
|
1025
1070
|
isFetching,
|
|
@@ -1033,6 +1078,7 @@ function createModelHooks(config) {
|
|
|
1033
1078
|
applyResult,
|
|
1034
1079
|
shouldFetch: enabled,
|
|
1035
1080
|
deps: [enabled, field, conditionsKey, requestKey],
|
|
1081
|
+
getRequestSignal,
|
|
1036
1082
|
onSuccess: onSuccessStable,
|
|
1037
1083
|
onError: onErrorStable,
|
|
1038
1084
|
onSettled: onSettledStable
|