@trpc/tanstack-react-query 11.6.1-canary.5 → 11.6.1-canary.6

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
@@ -207,19 +207,36 @@ function createTRPCOptionsResult(value) {
207
207
  const path = value.path.join(".");
208
208
  return { path };
209
209
  }
210
+ function isPrefixedQueryKey(queryKey) {
211
+ return queryKey.length >= 3;
212
+ }
213
+ function readQueryKey(queryKey) {
214
+ if (isPrefixedQueryKey(queryKey)) return {
215
+ type: "prefixed",
216
+ prefix: queryKey[0],
217
+ path: queryKey[1],
218
+ args: queryKey[2]
219
+ };
220
+ else return {
221
+ type: "unprefixed",
222
+ prefix: void 0,
223
+ path: queryKey[0],
224
+ args: queryKey[1]
225
+ };
226
+ }
210
227
  /**
211
228
  * @internal
212
229
  */
213
230
  function getClientArgs(queryKey, opts, infiniteParams) {
214
- var _queryKey$;
215
- const path = queryKey[0];
216
- let input = (_queryKey$ = queryKey[1]) === null || _queryKey$ === void 0 ? void 0 : _queryKey$.input;
231
+ var _queryKeyData$args;
232
+ const queryKeyData = readQueryKey(queryKey);
233
+ let input = (_queryKeyData$args = queryKeyData.args) === null || _queryKeyData$args === void 0 ? void 0 : _queryKeyData$args.input;
217
234
  if (infiniteParams) {
218
- var _input;
219
- input = (0, import_objectSpread2$5.default)((0, import_objectSpread2$5.default)((0, import_objectSpread2$5.default)({}, (_input = input) !== null && _input !== void 0 ? _input : {}), infiniteParams.pageParam !== void 0 ? { cursor: infiniteParams.pageParam } : {}), {}, { direction: infiniteParams.direction });
235
+ var _queryKeyData$args$in, _queryKeyData$args2;
236
+ input = (0, import_objectSpread2$5.default)((0, import_objectSpread2$5.default)((0, import_objectSpread2$5.default)({}, (_queryKeyData$args$in = (_queryKeyData$args2 = queryKeyData.args) === null || _queryKeyData$args2 === void 0 ? void 0 : _queryKeyData$args2.input) !== null && _queryKeyData$args$in !== void 0 ? _queryKeyData$args$in : {}), infiniteParams.pageParam !== void 0 ? { cursor: infiniteParams.pageParam } : {}), {}, { direction: infiniteParams.direction });
220
237
  }
221
238
  return [
222
- path.join("."),
239
+ queryKeyData.path.join("."),
223
240
  input,
224
241
  opts === null || opts === void 0 ? void 0 : opts.trpc
225
242
  ];
@@ -265,24 +282,30 @@ async function buildQueryFromAsyncIterable(asyncIterable, queryClient, queryKey)
265
282
  *
266
283
  * @internal
267
284
  */
268
- function getQueryKeyInternal(path, input, type) {
269
- const splitPath = path.flatMap((part) => part.split("."));
270
- if (!input && (!type || type === "any")) return splitPath.length ? [splitPath] : [];
271
- if (type === "infinite" && (0, __trpc_server_unstable_core_do_not_import.isObject)(input) && ("direction" in input || "cursor" in input)) {
272
- const { cursor: _, direction: __ } = input, inputWithoutCursorAndDirection = (0, import_objectWithoutProperties.default)(input, _excluded);
273
- return [splitPath, {
274
- input: inputWithoutCursorAndDirection,
275
- type: "infinite"
276
- }];
277
- }
278
- return [splitPath, (0, import_objectSpread2$5.default)((0, import_objectSpread2$5.default)({}, typeof input !== "undefined" && input !== __tanstack_react_query.skipToken && { input }), type && type !== "any" && { type })];
285
+ function getQueryKeyInternal(opts) {
286
+ const key = (0, __trpc_server_unstable_core_do_not_import.run)(() => {
287
+ const { input, type } = opts;
288
+ const splitPath = opts.path.flatMap((part) => part.split("."));
289
+ if (!input && type === "any") return splitPath.length ? [splitPath] : [];
290
+ if (type === "infinite" && (0, __trpc_server_unstable_core_do_not_import.isObject)(input) && ("direction" in input || "cursor" in input)) {
291
+ const { cursor: _, direction: __ } = input, inputWithoutCursorAndDirection = (0, import_objectWithoutProperties.default)(input, _excluded);
292
+ return [splitPath, {
293
+ input: inputWithoutCursorAndDirection,
294
+ type: "infinite"
295
+ }];
296
+ }
297
+ return [splitPath, (0, import_objectSpread2$5.default)((0, import_objectSpread2$5.default)({}, typeof input !== "undefined" && input !== __tanstack_react_query.skipToken && { input }), type && type !== "any" && { type })];
298
+ });
299
+ if (opts.prefix) key.unshift([opts.prefix]);
300
+ return key;
279
301
  }
280
302
  /**
281
303
  * @internal
282
304
  */
283
- function getMutationKeyInternal(path) {
284
- const splitPath = path.flatMap((part) => part.split("."));
285
- return splitPath.length ? [splitPath] : [];
305
+ function getMutationKeyInternal(opts) {
306
+ const key = [opts.path.flatMap((part) => part.split("."))];
307
+ if (opts.prefix) key.unshift([opts.prefix]);
308
+ return key;
286
309
  }
287
310
  /**
288
311
  * @internal
@@ -324,11 +347,14 @@ function trpcMutationOptions(args) {
324
347
  var _overrides$onSuccess;
325
348
  const { mutate, path, opts, overrides } = args;
326
349
  const queryClient = unwrapLazyArg(args.queryClient);
327
- const mutationKey = getMutationKeyInternal(path);
350
+ const mutationKey = getMutationKeyInternal({
351
+ path,
352
+ prefix: opts.keyPrefix
353
+ });
328
354
  const defaultOpts = queryClient.defaultMutationOptions(queryClient.getMutationDefaults(mutationKey));
329
355
  const mutationSuccessOverride = (_overrides$onSuccess = overrides === null || overrides === void 0 ? void 0 : overrides.onSuccess) !== null && _overrides$onSuccess !== void 0 ? _overrides$onSuccess : (options) => options.originalFn();
330
356
  const mutationFn = async (input) => {
331
- const result = await mutate(...getClientArgs([path, { input }], opts));
357
+ const result = await mutate(...getClientArgs([...mutationKey, { input }], opts));
332
358
  return result;
333
359
  };
334
360
  return (0, import_objectSpread2$3.default)((0, import_objectSpread2$3.default)({}, opts), {}, {
@@ -381,9 +407,9 @@ var import_objectSpread2$1 = __toESM(require_objectSpread2(), 1);
381
407
  * @internal
382
408
  */
383
409
  const trpcSubscriptionOptions = (args) => {
384
- var _queryKey$;
410
+ var _readQueryKey;
385
411
  const { subscribe, path, queryKey, opts = {} } = args;
386
- const input = (_queryKey$ = queryKey[1]) === null || _queryKey$ === void 0 ? void 0 : _queryKey$.input;
412
+ const input = (_readQueryKey = readQueryKey(queryKey)) === null || _readQueryKey === void 0 || (_readQueryKey = _readQueryKey.args) === null || _readQueryKey === void 0 ? void 0 : _readQueryKey.input;
387
413
  const enabled = "enabled" in opts ? !!opts.enabled : input !== __tanstack_react_query.skipToken;
388
414
  const _subscribe = (innerOpts) => {
389
415
  return subscribe(path.join("."), input !== null && input !== void 0 ? input : void 0, innerOpts);
@@ -511,6 +537,7 @@ var import_objectSpread2 = __toESM(require_objectSpread2(), 1);
511
537
  * @see https://trpc.io/docs/client/tanstack-react-query/server-components#5-create-a-trpc-caller-for-server-components
512
538
  */
513
539
  function createTRPCOptionsProxy(opts) {
540
+ const prefix = opts.keyPrefix;
514
541
  const callIt = (type) => {
515
542
  return (path, input, trpcOpts) => {
516
543
  if ("router" in opts) return Promise.resolve(unwrapLazyArg(opts.ctx)).then((ctx) => (0, __trpc_server.callTRPCProcedure)({
@@ -532,10 +559,18 @@ function createTRPCOptionsProxy(opts) {
532
559
  const contextMap = {
533
560
  "~types": void 0,
534
561
  pathKey: () => {
535
- return getQueryKeyInternal(path);
562
+ return getQueryKeyInternal({
563
+ path,
564
+ type: "any",
565
+ prefix
566
+ });
536
567
  },
537
568
  pathFilter: () => {
538
- return (0, import_objectSpread2.default)((0, import_objectSpread2.default)({}, arg1), {}, { queryKey: getQueryKeyInternal(path) });
569
+ return (0, import_objectSpread2.default)((0, import_objectSpread2.default)({}, arg1), {}, { queryKey: getQueryKeyInternal({
570
+ path,
571
+ type: "any",
572
+ prefix
573
+ }) });
539
574
  },
540
575
  queryOptions: () => {
541
576
  return trpcQueryOptions({
@@ -543,15 +578,30 @@ function createTRPCOptionsProxy(opts) {
543
578
  opts: arg2,
544
579
  path,
545
580
  queryClient: opts.queryClient,
546
- queryKey: getQueryKeyInternal(path, arg1, "query"),
581
+ queryKey: getQueryKeyInternal({
582
+ path,
583
+ input: arg1,
584
+ type: "query",
585
+ prefix
586
+ }),
547
587
  query: callIt("query")
548
588
  });
549
589
  },
550
590
  queryKey: () => {
551
- return getQueryKeyInternal(path, arg1, "query");
591
+ return getQueryKeyInternal({
592
+ path,
593
+ input: arg1,
594
+ type: "query",
595
+ prefix
596
+ });
552
597
  },
553
598
  queryFilter: () => {
554
- return (0, import_objectSpread2.default)((0, import_objectSpread2.default)({}, arg2), {}, { queryKey: getQueryKeyInternal(path, arg1, "query") });
599
+ return (0, import_objectSpread2.default)((0, import_objectSpread2.default)({}, arg2), {}, { queryKey: getQueryKeyInternal({
600
+ path,
601
+ input: arg1,
602
+ type: "query",
603
+ prefix
604
+ }) });
555
605
  },
556
606
  infiniteQueryOptions: () => {
557
607
  return trpcInfiniteQueryOptions({
@@ -559,15 +609,30 @@ function createTRPCOptionsProxy(opts) {
559
609
  opts: arg2,
560
610
  path,
561
611
  queryClient: opts.queryClient,
562
- queryKey: getQueryKeyInternal(path, arg1, "infinite"),
612
+ queryKey: getQueryKeyInternal({
613
+ path,
614
+ input: arg1,
615
+ type: "infinite",
616
+ prefix
617
+ }),
563
618
  query: callIt("query")
564
619
  });
565
620
  },
566
621
  infiniteQueryKey: () => {
567
- return getQueryKeyInternal(path, arg1, "infinite");
622
+ return getQueryKeyInternal({
623
+ path,
624
+ input: arg1,
625
+ type: "infinite",
626
+ prefix
627
+ });
568
628
  },
569
629
  infiniteQueryFilter: () => {
570
- return (0, import_objectSpread2.default)((0, import_objectSpread2.default)({}, arg2), {}, { queryKey: getQueryKeyInternal(path, arg1, "infinite") });
630
+ return (0, import_objectSpread2.default)((0, import_objectSpread2.default)({}, arg2), {}, { queryKey: getQueryKeyInternal({
631
+ path,
632
+ input: arg1,
633
+ type: "infinite",
634
+ prefix
635
+ }) });
571
636
  },
572
637
  mutationOptions: () => {
573
638
  var _opts$overrides;
@@ -580,13 +645,21 @@ function createTRPCOptionsProxy(opts) {
580
645
  });
581
646
  },
582
647
  mutationKey: () => {
583
- return getMutationKeyInternal(path);
648
+ return getMutationKeyInternal({
649
+ path,
650
+ prefix
651
+ });
584
652
  },
585
653
  subscriptionOptions: () => {
586
654
  return trpcSubscriptionOptions({
587
655
  opts: arg2,
588
656
  path,
589
- queryKey: getQueryKeyInternal(path, arg1, "any"),
657
+ queryKey: getQueryKeyInternal({
658
+ path,
659
+ input: arg1,
660
+ type: "any",
661
+ prefix
662
+ }),
590
663
  subscribe: callIt("subscription")
591
664
  });
592
665
  }
@@ -605,11 +678,16 @@ function createTRPCOptionsProxy(opts) {
605
678
  function createTRPCContext() {
606
679
  const TRPCClientContext = react.createContext(null);
607
680
  const TRPCContext = react.createContext(null);
608
- function TRPCProvider(props) {
681
+ const TRPCProvider = (props) => {
609
682
  const value = react.useMemo(() => createTRPCOptionsProxy({
610
683
  client: props.trpcClient,
611
- queryClient: props.queryClient
612
- }), [props.trpcClient, props.queryClient]);
684
+ queryClient: props.queryClient,
685
+ keyPrefix: props.keyPrefix
686
+ }), [
687
+ props.trpcClient,
688
+ props.queryClient,
689
+ props.keyPrefix
690
+ ]);
613
691
  return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(TRPCClientContext.Provider, {
614
692
  value: props.trpcClient,
615
693
  children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(TRPCContext.Provider, {
@@ -617,7 +695,8 @@ function createTRPCContext() {
617
695
  children: props.children
618
696
  })
619
697
  });
620
- }
698
+ };
699
+ TRPCProvider.displayName = "TRPCProvider";
621
700
  function useTRPC() {
622
701
  const utils = react.useContext(TRPCContext);
623
702
  if (!utils) throw new Error("useTRPC() can only be used inside of a <TRPCProvider>");