@emeryld/rrroutes-client 2.1.7 → 2.1.8
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 +65 -14
- package/dist/index.cjs.map +1 -1
- package/dist/index.mjs +65 -14
- package/dist/index.mjs.map +1 -1
- package/dist/routesV3.client.types.d.ts +23 -3
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -202,33 +202,82 @@ function createRouteClient(opts) {
|
|
|
202
202
|
const debugName = meta?.name;
|
|
203
203
|
const emit = (event) => emitDebug(event, debugName);
|
|
204
204
|
emit({ type: "build", leaf: leafLabel });
|
|
205
|
-
const
|
|
205
|
+
const getQueryKeys = (...tuple) => {
|
|
206
206
|
const a = extractArgs(tuple);
|
|
207
207
|
const params = a?.params;
|
|
208
208
|
const query = a?.query;
|
|
209
209
|
const qForKey = isGet && isFeed ? stripKey(query, cursorParam) : query;
|
|
210
|
-
return (0, import_rrroutes_contract.buildCacheKey)({
|
|
210
|
+
return (0, import_rrroutes_contract.buildCacheKey)({
|
|
211
|
+
leaf,
|
|
212
|
+
params,
|
|
213
|
+
query: qForKey
|
|
214
|
+
});
|
|
211
215
|
};
|
|
212
216
|
const invalidateExact = async (...tuple) => {
|
|
213
|
-
const queryKey =
|
|
217
|
+
const queryKey = getQueryKeys(...tuple);
|
|
214
218
|
await queryClient.invalidateQueries({ queryKey, exact: true });
|
|
215
219
|
emit({ type: "invalidate", key: queryKey, exact: true });
|
|
216
220
|
};
|
|
217
221
|
const setData = (...args) => {
|
|
218
222
|
const [updater, ...rest] = args;
|
|
219
|
-
const k =
|
|
223
|
+
const k = getQueryKeys(...rest);
|
|
224
|
+
let next;
|
|
220
225
|
if (isGet && isFeed) {
|
|
221
|
-
queryClient.setQueryData(
|
|
226
|
+
next = queryClient.setQueryData(
|
|
222
227
|
k,
|
|
223
228
|
(prev) => typeof updater === "function" ? updater(prev) : updater
|
|
224
229
|
);
|
|
225
230
|
} else {
|
|
226
|
-
queryClient.setQueryData(
|
|
231
|
+
next = queryClient.setQueryData(
|
|
227
232
|
k,
|
|
228
233
|
(prev) => typeof updater === "function" ? updater(prev) : updater
|
|
229
234
|
);
|
|
230
235
|
}
|
|
231
236
|
emit({ type: "setData", key: k });
|
|
237
|
+
return next;
|
|
238
|
+
};
|
|
239
|
+
const fetchGet = async (...tuple) => {
|
|
240
|
+
const a = extractArgs(tuple);
|
|
241
|
+
const params = a?.params;
|
|
242
|
+
const query = a?.query;
|
|
243
|
+
const { url, normalizedQuery, normalizedParams } = buildUrl(leaf, baseUrl, params, query);
|
|
244
|
+
const startedAt = Date.now();
|
|
245
|
+
const detail = isVerboseDebug ? { params: normalizedParams, query: normalizedQuery } : void 0;
|
|
246
|
+
emit(decorateDebugEvent({ type: "fetch", stage: "start", method, url, leaf: leafLabel }, detail));
|
|
247
|
+
try {
|
|
248
|
+
const out = await fetcher({ url, method });
|
|
249
|
+
const parsed = zParse(out, leaf.cfg.outputSchema);
|
|
250
|
+
emit(
|
|
251
|
+
decorateDebugEvent(
|
|
252
|
+
{
|
|
253
|
+
type: "fetch",
|
|
254
|
+
stage: "success",
|
|
255
|
+
method,
|
|
256
|
+
url,
|
|
257
|
+
leaf: leafLabel,
|
|
258
|
+
durationMs: Date.now() - startedAt
|
|
259
|
+
},
|
|
260
|
+
isVerboseDebug ? { params: normalizedParams, query: normalizedQuery, output: parsed } : void 0
|
|
261
|
+
)
|
|
262
|
+
);
|
|
263
|
+
return parsed;
|
|
264
|
+
} catch (error) {
|
|
265
|
+
emit(
|
|
266
|
+
decorateDebugEvent(
|
|
267
|
+
{
|
|
268
|
+
type: "fetch",
|
|
269
|
+
stage: "error",
|
|
270
|
+
method,
|
|
271
|
+
url,
|
|
272
|
+
leaf: leafLabel,
|
|
273
|
+
durationMs: Date.now() - startedAt,
|
|
274
|
+
error
|
|
275
|
+
},
|
|
276
|
+
detail
|
|
277
|
+
)
|
|
278
|
+
);
|
|
279
|
+
throw error;
|
|
280
|
+
}
|
|
232
281
|
};
|
|
233
282
|
if (isGet && isFeed) {
|
|
234
283
|
const useEndpoint2 = (...tuple) => {
|
|
@@ -239,7 +288,7 @@ function createRouteClient(opts) {
|
|
|
239
288
|
const { normalizedQuery, normalizedParams } = buildUrl(leaf, baseUrl, params, query);
|
|
240
289
|
return (0, import_react_query.useInfiniteQuery)({
|
|
241
290
|
...rqOpts,
|
|
242
|
-
queryKey:
|
|
291
|
+
queryKey: getQueryKeys(...tuple),
|
|
243
292
|
initialPageParam: void 0,
|
|
244
293
|
getNextPageParam: (lastPage) => getNextCursor(lastPage),
|
|
245
294
|
placeholderData: import_react_query.keepPreviousData,
|
|
@@ -297,10 +346,11 @@ function createRouteClient(opts) {
|
|
|
297
346
|
}, queryClient);
|
|
298
347
|
};
|
|
299
348
|
return {
|
|
300
|
-
|
|
349
|
+
getQueryKeys,
|
|
301
350
|
invalidate: invalidateExact,
|
|
302
351
|
setData,
|
|
303
|
-
useEndpoint: useEndpoint2
|
|
352
|
+
useEndpoint: useEndpoint2,
|
|
353
|
+
fetch: fetchGet
|
|
304
354
|
};
|
|
305
355
|
}
|
|
306
356
|
if (isGet) {
|
|
@@ -312,7 +362,7 @@ function createRouteClient(opts) {
|
|
|
312
362
|
const { url, normalizedQuery, normalizedParams } = buildUrl(leaf, baseUrl, params, query);
|
|
313
363
|
return (0, import_react_query.useQuery)({
|
|
314
364
|
...rqOpts,
|
|
315
|
-
queryKey:
|
|
365
|
+
queryKey: getQueryKeys(...tuple),
|
|
316
366
|
placeholderData: import_react_query.keepPreviousData,
|
|
317
367
|
queryFn: async () => {
|
|
318
368
|
const startedAt = Date.now();
|
|
@@ -362,10 +412,11 @@ function createRouteClient(opts) {
|
|
|
362
412
|
}, queryClient);
|
|
363
413
|
};
|
|
364
414
|
return {
|
|
365
|
-
|
|
415
|
+
getQueryKeys,
|
|
366
416
|
invalidate: invalidateExact,
|
|
367
417
|
setData,
|
|
368
|
-
useEndpoint: useEndpoint2
|
|
418
|
+
useEndpoint: useEndpoint2,
|
|
419
|
+
fetch: fetchGet
|
|
369
420
|
};
|
|
370
421
|
}
|
|
371
422
|
const fetchEndpoint = async (...tupleWithBody) => {
|
|
@@ -438,12 +489,12 @@ function createRouteClient(opts) {
|
|
|
438
489
|
emit({ type: "useEndpoint", leaf: leafLabel, variant: "mutation" });
|
|
439
490
|
return (0, import_react_query.useMutation)({
|
|
440
491
|
...rqOpts,
|
|
441
|
-
mutationKey:
|
|
492
|
+
mutationKey: getQueryKeys(...tuple),
|
|
442
493
|
mutationFn: (body) => fetchEndpoint(...[...tuple, body])
|
|
443
494
|
}, queryClient);
|
|
444
495
|
};
|
|
445
496
|
return {
|
|
446
|
-
|
|
497
|
+
getQueryKeys,
|
|
447
498
|
invalidate: invalidateExact,
|
|
448
499
|
setData,
|
|
449
500
|
useEndpoint,
|