@emeryld/rrroutes-client 2.1.10 → 2.1.12

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 CHANGED
@@ -251,18 +251,42 @@ function createRouteClient(opts) {
251
251
  emit({ type: "setData", key: k });
252
252
  return next;
253
253
  };
254
- const fetchGet = async (...tuple) => {
254
+ const buildOnReceive = rqOpts?.onReceive;
255
+ const fetchEndpoint = async (tuple, options) => {
255
256
  const a = extractArgs(tuple);
256
257
  const params = a?.params;
257
- const query = a?.query;
258
+ const query = options?.queryOverride ?? a?.query;
258
259
  const { url, normalizedQuery, normalizedParams } = buildUrl(leaf, baseUrl, params, query);
260
+ let payload;
261
+ const acceptsBody = Boolean(leaf.cfg.bodySchema);
262
+ const requiresBody = options?.requireBody ?? (!isGet && acceptsBody);
263
+ if (typeof options?.body !== "undefined") {
264
+ const normalizedBody = zParse(options.body, leaf.cfg.bodySchema);
265
+ const isMultipart = Array.isArray(leaf.cfg.bodyFiles) && leaf.cfg.bodyFiles.length > 0;
266
+ payload = isMultipart ? toFormData(normalizedBody) : normalizedBody;
267
+ } else if (requiresBody) {
268
+ throw new Error("Body is required when invoking a mutation fetch.");
269
+ }
259
270
  const startedAt = Date.now();
260
271
  const detail = isVerboseDebug ? { params: normalizedParams, query: normalizedQuery } : void 0;
261
- emit(decorateDebugEvent({ type: "fetch", stage: "start", method, url, leaf: leafLabel }, detail));
272
+ emit(
273
+ decorateDebugEvent(
274
+ {
275
+ type: "fetch",
276
+ stage: "start",
277
+ method,
278
+ url,
279
+ leaf: leafLabel,
280
+ ...payload !== void 0 ? { body: payload } : {}
281
+ },
282
+ detail
283
+ )
284
+ );
262
285
  try {
263
- const out = await fetcher({ url, method });
286
+ const out = await fetcher(
287
+ payload === void 0 ? { url, method } : { url, method, body: payload }
288
+ );
264
289
  const parsed = zParse(out, leaf.cfg.outputSchema);
265
- rqOpts?.onReceive?.(parsed);
266
290
  emit(
267
291
  decorateDebugEvent(
268
292
  {
@@ -276,6 +300,7 @@ function createRouteClient(opts) {
276
300
  isVerboseDebug ? { params: normalizedParams, query: normalizedQuery, output: parsed } : void 0
277
301
  )
278
302
  );
303
+ options?.onReceive?.(parsed);
279
304
  return parsed;
280
305
  } catch (error) {
281
306
  emit(
@@ -287,6 +312,7 @@ function createRouteClient(opts) {
287
312
  url,
288
313
  leaf: leafLabel,
289
314
  durationMs: Date.now() - startedAt,
315
+ ...payload !== void 0 ? { body: payload } : {},
290
316
  error
291
317
  },
292
318
  detail
@@ -295,6 +321,15 @@ function createRouteClient(opts) {
295
321
  throw error;
296
322
  }
297
323
  };
324
+ const fetchGet = (...tupleWithBody) => {
325
+ const acceptsBody = Boolean(leaf.cfg.bodySchema);
326
+ const tupleLength = tupleWithBody.length;
327
+ const maybeBodyIndex = expectsArgs ? 1 : 0;
328
+ const hasBodyCandidate = acceptsBody && tupleLength > maybeBodyIndex;
329
+ const body = hasBodyCandidate ? tupleWithBody[tupleLength - 1] : void 0;
330
+ const tuple = hasBodyCandidate ? tupleWithBody.slice(0, tupleLength - 1) : tupleWithBody;
331
+ return fetchEndpoint(tuple, { body, onReceive: buildOnReceive, requireBody: false });
332
+ };
298
333
  if (isGet && isFeed) {
299
334
  const useEndpoint2 = (...useArgs) => {
300
335
  const { args, options } = splitUseEndpointArgs(useArgs, expectsArgs);
@@ -310,56 +345,18 @@ function createRouteClient(opts) {
310
345
  initialPageParam: void 0,
311
346
  getNextPageParam: (lastPage) => getNextCursor(lastPage),
312
347
  placeholderData: import_react_query.keepPreviousData,
313
- queryFn: async ({ pageParam }) => {
348
+ queryFn: ({ pageParam }) => {
314
349
  const pageQuery = {
315
350
  ...normalizedQuery,
316
351
  ...pageParam ? { [cursorParam]: pageParam } : {}
317
352
  };
318
- const { url } = buildUrl(leaf, baseUrl, params, pageQuery);
319
- const startedAt = Date.now();
320
- const detail = isVerboseDebug ? { ...normalizedParams ? { params: normalizedParams } : {}, ...Object.keys(pageQuery).length > 0 ? { query: pageQuery } : {} } : void 0;
321
- emit(
322
- decorateDebugEvent(
323
- { type: "fetch", stage: "start", method, url, leaf: leafLabel },
324
- detail
325
- )
326
- );
327
- try {
328
- const out = await fetcher({ url, method });
329
- const parsed = zParse(out, leaf.cfg.outputSchema);
330
- buildOptions?.onReceive?.(parsed);
331
- options?.onReceive?.(parsed);
332
- emit(
333
- decorateDebugEvent(
334
- {
335
- type: "fetch",
336
- stage: "success",
337
- method,
338
- url,
339
- leaf: leafLabel,
340
- durationMs: Date.now() - startedAt
341
- },
342
- isVerboseDebug ? { params: normalizedParams, query: pageQuery, output: parsed } : void 0
343
- )
344
- );
345
- return parsed;
346
- } catch (error) {
347
- emit(
348
- decorateDebugEvent(
349
- {
350
- type: "fetch",
351
- stage: "error",
352
- method,
353
- url,
354
- leaf: leafLabel,
355
- durationMs: Date.now() - startedAt,
356
- error
357
- },
358
- detail
359
- )
360
- );
361
- throw error;
362
- }
353
+ return fetchEndpoint(tuple, {
354
+ queryOverride: pageQuery,
355
+ onReceive: (data) => {
356
+ buildOptions?.onReceive?.(data);
357
+ options?.onReceive?.(data);
358
+ }
359
+ });
363
360
  }
364
361
  // NOTE: TData is InfiniteData<T>, so we don't need a select here.
365
362
  }, queryClient);
@@ -380,57 +377,17 @@ function createRouteClient(opts) {
380
377
  const params = args?.params;
381
378
  const query = args?.query;
382
379
  const buildOptions = rqOpts ?? {};
383
- const { url, normalizedQuery, normalizedParams } = buildUrl(leaf, baseUrl, params, query);
380
+ buildUrl(leaf, baseUrl, params, query);
384
381
  return (0, import_react_query.useQuery)({
385
382
  ...buildOptions,
386
383
  queryKey: getQueryKeys(...tuple),
387
384
  placeholderData: import_react_query.keepPreviousData,
388
- queryFn: async () => {
389
- const startedAt = Date.now();
390
- const detail = isVerboseDebug ? { params: normalizedParams, query: normalizedQuery } : void 0;
391
- emit(
392
- decorateDebugEvent(
393
- { type: "fetch", stage: "start", method, url, leaf: leafLabel },
394
- detail
395
- )
396
- );
397
- try {
398
- const out = await fetcher({ url, method });
399
- const parsed = zParse(out, leaf.cfg.outputSchema);
400
- buildOptions?.onReceive?.(parsed);
401
- options?.onReceive?.(parsed);
402
- emit(
403
- decorateDebugEvent(
404
- {
405
- type: "fetch",
406
- stage: "success",
407
- method,
408
- url,
409
- leaf: leafLabel,
410
- durationMs: Date.now() - startedAt
411
- },
412
- isVerboseDebug ? { params: normalizedParams, query: normalizedQuery, output: parsed } : void 0
413
- )
414
- );
415
- return parsed;
416
- } catch (error) {
417
- emit(
418
- decorateDebugEvent(
419
- {
420
- type: "fetch",
421
- stage: "error",
422
- method,
423
- url,
424
- leaf: leafLabel,
425
- durationMs: Date.now() - startedAt,
426
- error
427
- },
428
- detail
429
- )
430
- );
431
- throw error;
385
+ queryFn: () => fetchEndpoint(tuple, {
386
+ onReceive: (data) => {
387
+ buildOptions?.onReceive?.(data);
388
+ options?.onReceive?.(data);
432
389
  }
433
- }
390
+ })
434
391
  }, queryClient);
435
392
  };
436
393
  return {
@@ -442,71 +399,19 @@ function createRouteClient(opts) {
442
399
  };
443
400
  }
444
401
  const mutationBuildOptions = rqOpts ?? {};
445
- const fetchEndpoint = async (...tupleWithBody) => {
402
+ const fetchMutation = async (...tupleWithBody) => {
446
403
  if (tupleWithBody.length === 0) {
447
404
  throw new Error("Body is required when invoking a mutation fetch.");
448
405
  }
449
406
  const bodyIndex = tupleWithBody.length - 1;
450
407
  const tuple = tupleWithBody.slice(0, bodyIndex);
451
408
  const body = tupleWithBody[bodyIndex];
452
- const args = extractArgs(tuple);
453
- const params = args?.params;
454
- const query = args?.query;
455
- const { url, normalizedQuery, normalizedParams } = buildUrl(leaf, baseUrl, params, query);
456
- const normalizedBody = zParse(body, leaf.cfg.bodySchema);
457
- const isMultipart = Array.isArray(leaf.cfg.bodyFiles) && leaf.cfg.bodyFiles.length > 0;
458
- const payload = isMultipart ? toFormData(normalizedBody) : normalizedBody;
459
- const startedAt = Date.now();
460
- const detail = isVerboseDebug ? { params: normalizedParams, query: normalizedQuery } : void 0;
461
- emit(
462
- decorateDebugEvent(
463
- {
464
- type: "fetch",
465
- stage: "start",
466
- method,
467
- url,
468
- leaf: leafLabel,
469
- body: payload
470
- },
471
- detail
472
- )
473
- );
474
- try {
475
- const out = await fetcher({ url, method, body: payload });
476
- const parsed = zParse(out, leaf.cfg.outputSchema);
477
- mutationBuildOptions?.onReceive?.(parsed);
478
- emit(
479
- decorateDebugEvent(
480
- {
481
- type: "fetch",
482
- stage: "success",
483
- method,
484
- url,
485
- leaf: leafLabel,
486
- durationMs: Date.now() - startedAt
487
- },
488
- isVerboseDebug ? { params: normalizedParams, query: normalizedQuery, output: parsed } : void 0
489
- )
490
- );
491
- return parsed;
492
- } catch (error) {
493
- emit(
494
- decorateDebugEvent(
495
- {
496
- type: "fetch",
497
- stage: "error",
498
- method,
499
- url,
500
- leaf: leafLabel,
501
- durationMs: Date.now() - startedAt,
502
- body: payload,
503
- error
504
- },
505
- detail
506
- )
507
- );
508
- throw error;
509
- }
409
+ const result = await fetchEndpoint(tuple, {
410
+ body,
411
+ onReceive: (data) => mutationBuildOptions?.onReceive?.(data),
412
+ requireBody: true
413
+ });
414
+ return result;
510
415
  };
511
416
  const useEndpoint = (...useArgs) => {
512
417
  const { args, options } = splitUseEndpointArgs(useArgs, expectsArgs);
@@ -516,7 +421,7 @@ function createRouteClient(opts) {
516
421
  ...mutationBuildOptions,
517
422
  mutationKey: getQueryKeys(...tuple),
518
423
  mutationFn: async (body) => {
519
- const result = await fetchEndpoint(...[...tuple, body]);
424
+ const result = await fetchMutation(...[...tuple, body]);
520
425
  options?.onReceive?.(result);
521
426
  return result;
522
427
  }
@@ -527,7 +432,7 @@ function createRouteClient(opts) {
527
432
  invalidate: invalidateExact,
528
433
  setData,
529
434
  useEndpoint,
530
- fetch: fetchEndpoint
435
+ fetch: fetchMutation
531
436
  };
532
437
  }
533
438
  return {
@@ -776,7 +681,7 @@ function useSocketConnection(args) {
776
681
  if (autoLeave && normalizedRooms.length > 0) client.leaveRooms(normalizedRooms, args.leaveMeta);
777
682
  if (onCleanup) onCleanup();
778
683
  };
779
- }, args.deps ?? [client, event, onMessage, autoJoin, autoLeave, ...normalizedRooms]);
684
+ }, [client, event, onMessage, autoJoin, autoLeave, ...normalizedRooms]);
780
685
  }
781
686
 
782
687
  // src/sockets/socket.client.index.ts
@@ -1145,7 +1050,8 @@ var SocketClient = class {
1145
1050
  rooms: this.toArray(rooms),
1146
1051
  err: "sys:room_join handler aborted join"
1147
1052
  });
1148
- return;
1053
+ return async () => {
1054
+ };
1149
1055
  }
1150
1056
  const list = this.toArray(rooms);
1151
1057
  const toJoin = [];
@@ -1168,13 +1074,17 @@ var SocketClient = class {
1168
1074
  err: "payload validation failed",
1169
1075
  details: this.getValidationDetails(payloadResult.error)
1170
1076
  });
1171
- return;
1077
+ return async () => {
1078
+ };
1172
1079
  }
1173
1080
  const payload = payloadResult.data;
1174
1081
  const normalizedRooms = this.toArray(payload.rooms);
1175
1082
  this.socket.emit("sys:room_join", payload);
1176
1083
  this.dbg({ type: "room", phase: "join", rooms: normalizedRooms });
1177
1084
  }
1085
+ return async () => {
1086
+ await this.leaveRooms(rooms, meta);
1087
+ };
1178
1088
  }
1179
1089
  async leaveRooms(rooms, meta) {
1180
1090
  if (!this.socket) {