@trpc/tanstack-react-query 11.6.1-canary.5 → 11.7.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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@trpc/tanstack-react-query",
3
3
  "type": "module",
4
- "version": "11.6.1-canary.5+4c9efbb58",
4
+ "version": "11.7.0",
5
5
  "description": "TanStack React Query Integration for tRPC",
6
6
  "author": "juliusmarminge",
7
7
  "license": "MIT",
@@ -53,16 +53,16 @@
53
53
  },
54
54
  "peerDependencies": {
55
55
  "@tanstack/react-query": "^5.80.3",
56
- "@trpc/client": "11.6.1-canary.5+4c9efbb58",
57
- "@trpc/server": "11.6.1-canary.5+4c9efbb58",
56
+ "@trpc/client": "11.7.0",
57
+ "@trpc/server": "11.7.0",
58
58
  "react": ">=18.2.0",
59
59
  "react-dom": ">=18.2.0",
60
60
  "typescript": ">=5.7.2"
61
61
  },
62
62
  "devDependencies": {
63
63
  "@tanstack/react-query": "^5.80.3",
64
- "@trpc/client": "11.6.1-canary.5+4c9efbb58",
65
- "@trpc/server": "11.6.1-canary.5+4c9efbb58",
64
+ "@trpc/client": "11.7.0",
65
+ "@trpc/server": "11.7.0",
66
66
  "@types/node": "^22.13.5",
67
67
  "@types/react": "^19.1.0",
68
68
  "eslint": "^9.26.0",
@@ -82,5 +82,5 @@
82
82
  "funding": [
83
83
  "https://trpc.io/sponsor"
84
84
  ],
85
- "gitHead": "4c9efbb587f6bc327c5d92dbe95c82690002aa72"
85
+ "gitHead": "5961a63c0161c24c0b21e47503a357d3b7c4410a"
86
86
  }
@@ -4,16 +4,32 @@ import type { AnyTRPCRouter } from '@trpc/server';
4
4
  import * as React from 'react';
5
5
  import type { TRPCOptionsProxy } from './createOptionsProxy';
6
6
  import { createTRPCOptionsProxy } from './createOptionsProxy';
7
+ import type {
8
+ DefaultFeatureFlags,
9
+ FeatureFlags,
10
+ KeyPrefixOptions,
11
+ } from './types';
7
12
 
8
- export interface CreateTRPCContextResult<TRouter extends AnyTRPCRouter> {
9
- TRPCProvider: React.FC<{
13
+ type TRPCProviderType<
14
+ TRouter extends AnyTRPCRouter,
15
+ TFeatureFlags extends FeatureFlags = DefaultFeatureFlags,
16
+ > = React.FC<
17
+ {
10
18
  children: React.ReactNode;
11
19
  queryClient: QueryClient;
12
20
  trpcClient: TRPCClient<TRouter>;
13
- }>;
14
- useTRPC: () => TRPCOptionsProxy<TRouter>;
21
+ } & KeyPrefixOptions<TFeatureFlags>
22
+ >;
23
+
24
+ export interface CreateTRPCContextResult<
25
+ TRouter extends AnyTRPCRouter,
26
+ TFeatureFlags extends FeatureFlags = DefaultFeatureFlags,
27
+ > {
28
+ TRPCProvider: TRPCProviderType<TRouter, TFeatureFlags>;
29
+ useTRPC: () => TRPCOptionsProxy<TRouter, TFeatureFlags>;
15
30
  useTRPCClient: () => TRPCClient<TRouter>;
16
31
  }
32
+
17
33
  /**
18
34
  * Create a set of type-safe provider-consumers
19
35
  *
@@ -21,28 +37,25 @@ export interface CreateTRPCContextResult<TRouter extends AnyTRPCRouter> {
21
37
  */
22
38
  export function createTRPCContext<
23
39
  TRouter extends AnyTRPCRouter,
24
- >(): CreateTRPCContextResult<TRouter> {
40
+ TFeatureFlags extends FeatureFlags = DefaultFeatureFlags,
41
+ >(): CreateTRPCContextResult<TRouter, TFeatureFlags> {
25
42
  const TRPCClientContext = React.createContext<TRPCClient<TRouter> | null>(
26
43
  null,
27
44
  );
28
- const TRPCContext = React.createContext<TRPCOptionsProxy<TRouter> | null>(
29
- null,
30
- );
45
+ const TRPCContext = React.createContext<TRPCOptionsProxy<
46
+ TRouter,
47
+ TFeatureFlags
48
+ > | null>(null);
31
49
 
32
- function TRPCProvider(
33
- props: Readonly<{
34
- children: React.ReactNode;
35
- queryClient: QueryClient;
36
- trpcClient: TRPCClient<TRouter>;
37
- }>,
38
- ) {
50
+ const TRPCProvider: TRPCProviderType<TRouter, TFeatureFlags> = (props) => {
39
51
  const value = React.useMemo(
40
52
  () =>
41
- createTRPCOptionsProxy({
53
+ createTRPCOptionsProxy<TRouter, TFeatureFlags>({
42
54
  client: props.trpcClient,
43
55
  queryClient: props.queryClient,
56
+ keyPrefix: props.keyPrefix as any,
44
57
  }),
45
- [props.trpcClient, props.queryClient],
58
+ [props.trpcClient, props.queryClient, props.keyPrefix],
46
59
  );
47
60
  return (
48
61
  <TRPCClientContext.Provider value={props.trpcClient}>
@@ -51,7 +64,8 @@ export function createTRPCContext<
51
64
  </TRPCContext.Provider>
52
65
  </TRPCClientContext.Provider>
53
66
  );
54
- }
67
+ };
68
+ TRPCProvider.displayName = 'TRPCProvider';
55
69
 
56
70
  function useTRPC() {
57
71
  const utils = React.useContext(TRPCContext);
@@ -71,5 +85,8 @@ export function createTRPCContext<
71
85
  return client;
72
86
  }
73
87
 
74
- return { TRPCProvider, useTRPC, useTRPCClient };
88
+ return { TRPCProvider, useTRPC, useTRPCClient } as CreateTRPCContextResult<
89
+ TRouter,
90
+ TFeatureFlags
91
+ >;
75
92
  }
@@ -32,6 +32,9 @@ import {
32
32
  type TRPCSubscriptionOptions,
33
33
  } from './subscriptionOptions';
34
34
  import type {
35
+ DefaultFeatureFlags,
36
+ FeatureFlags,
37
+ KeyPrefixOptions,
35
38
  OptionalCursorInput,
36
39
  ResolverDef,
37
40
  TRPCInfiniteData,
@@ -45,14 +48,14 @@ import {
45
48
  unwrapLazyArg,
46
49
  } from './utils';
47
50
 
48
- export interface DecorateRouterKeyable {
51
+ export interface DecorateRouterKeyable<TFeatureFlags extends FeatureFlags> {
49
52
  /**
50
53
  * Calculate the TanStack Query Key for any path, could be used to invalidate every procedure beneath this path
51
54
  *
52
55
  * @see https://tanstack.com/query/latest/docs/framework/react/guides/query-keys
53
56
  * @see https://trpc.io/docs/client/tanstack-react-query/usage#queryKey
54
57
  */
55
- pathKey: () => TRPCQueryKey;
58
+ pathKey: () => TRPCQueryKey<TFeatureFlags['keyPrefix']>;
56
59
 
57
60
  /**
58
61
  * Calculate a TanStack Query Filter for any path, could be used to manipulate every procedure beneath this path
@@ -61,8 +64,11 @@ export interface DecorateRouterKeyable {
61
64
  * @see https://trpc.io/docs/client/tanstack-react-query/usage#queryFilter
62
65
  */
63
66
  pathFilter: (
64
- filters?: QueryFilters<TRPCQueryKey>,
65
- ) => WithRequired<QueryFilters<TRPCQueryKey>, 'queryKey'>;
67
+ filters?: QueryFilters<TRPCQueryKey<TFeatureFlags['keyPrefix']>>,
68
+ ) => WithRequired<
69
+ QueryFilters<TRPCQueryKey<TFeatureFlags['keyPrefix']>>,
70
+ 'queryKey'
71
+ >;
66
72
  }
67
73
 
68
74
  interface TypeHelper<TDef extends ResolverDef> {
@@ -107,7 +113,7 @@ export interface DecorateInfiniteQueryProcedure<TDef extends ResolverDef>
107
113
  * @see https://trpc.io/docs/client/tanstack-react-query/usage#queryKey
108
114
  */
109
115
  infiniteQueryKey: (input?: Partial<TDef['input']>) => DataTag<
110
- TRPCQueryKey,
116
+ TRPCQueryKey<TDef['featureFlags']['keyPrefix']>,
111
117
  TRPCInfiniteData<TDef['input'], TDef['output']>,
112
118
  TRPCClientErrorLike<{
113
119
  transformer: TDef['transformer'];
@@ -125,7 +131,7 @@ export interface DecorateInfiniteQueryProcedure<TDef extends ResolverDef>
125
131
  input?: Partial<TDef['input']>,
126
132
  filters?: QueryFilters<
127
133
  DataTag<
128
- TRPCQueryKey,
134
+ TRPCQueryKey<TDef['featureFlags']['keyPrefix']>,
129
135
  TRPCInfiniteData<TDef['input'], TDef['output']>,
130
136
  TRPCClientErrorLike<{
131
137
  transformer: TDef['transformer'];
@@ -136,7 +142,7 @@ export interface DecorateInfiniteQueryProcedure<TDef extends ResolverDef>
136
142
  ) => WithRequired<
137
143
  QueryFilters<
138
144
  DataTag<
139
- TRPCQueryKey,
145
+ TRPCQueryKey<TDef['featureFlags']['keyPrefix']>,
140
146
  TRPCInfiniteData<TDef['input'], TDef['output']>,
141
147
  TRPCClientErrorLike<{
142
148
  transformer: TDef['transformer'];
@@ -149,7 +155,7 @@ export interface DecorateInfiniteQueryProcedure<TDef extends ResolverDef>
149
155
  }
150
156
  export interface DecorateQueryProcedure<TDef extends ResolverDef>
151
157
  extends TypeHelper<TDef>,
152
- DecorateRouterKeyable {
158
+ DecorateRouterKeyable<TDef['featureFlags']> {
153
159
  /**
154
160
  * Create a set of type-safe query options that can be passed to `useQuery`, `prefetchQuery` etc.
155
161
  *
@@ -165,7 +171,7 @@ export interface DecorateQueryProcedure<TDef extends ResolverDef>
165
171
  * @see https://trpc.io/docs/client/tanstack-react-query/usage#queryKey
166
172
  */
167
173
  queryKey: (input?: Partial<TDef['input']>) => DataTag<
168
- TRPCQueryKey,
174
+ TRPCQueryKey<TDef['featureFlags']['keyPrefix']>,
169
175
  TDef['output'],
170
176
  TRPCClientErrorLike<{
171
177
  transformer: TDef['transformer'];
@@ -183,7 +189,7 @@ export interface DecorateQueryProcedure<TDef extends ResolverDef>
183
189
  input?: Partial<TDef['input']>,
184
190
  filters?: QueryFilters<
185
191
  DataTag<
186
- TRPCQueryKey,
192
+ TRPCQueryKey<TDef['featureFlags']['keyPrefix']>,
187
193
  TDef['output'],
188
194
  TRPCClientErrorLike<{
189
195
  transformer: TDef['transformer'];
@@ -194,7 +200,7 @@ export interface DecorateQueryProcedure<TDef extends ResolverDef>
194
200
  ) => WithRequired<
195
201
  QueryFilters<
196
202
  DataTag<
197
- TRPCQueryKey,
203
+ TRPCQueryKey<TDef['featureFlags']['keyPrefix']>,
198
204
  TDef['output'],
199
205
  TRPCClientErrorLike<{
200
206
  transformer: TDef['transformer'];
@@ -220,10 +226,11 @@ export interface DecorateMutationProcedure<TDef extends ResolverDef>
220
226
  *
221
227
  * @see https://trpc.io/docs/client/tanstack-react-query/usage#mutationKey
222
228
  */
223
- mutationKey: () => TRPCMutationKey;
229
+ mutationKey: () => TRPCMutationKey<TDef['featureFlags']['keyPrefix']>;
224
230
  }
225
231
 
226
- export interface DecorateSubscriptionProcedure<TDef extends ResolverDef> {
232
+ export interface DecorateSubscriptionProcedure<TDef extends ResolverDef>
233
+ extends TypeHelper<TDef> {
227
234
  /**
228
235
  * Create a set of type-safe subscription options that can be passed to `useSubscription`
229
236
  *
@@ -252,10 +259,12 @@ export type DecorateProcedure<
252
259
  export type DecoratedRouterRecord<
253
260
  TRoot extends AnyTRPCRootTypes,
254
261
  TRecord extends TRPCRouterRecord,
262
+ TFeatureFlags extends FeatureFlags = DefaultFeatureFlags,
255
263
  > = {
256
264
  [TKey in keyof TRecord]: TRecord[TKey] extends infer $Value
257
265
  ? $Value extends TRPCRouterRecord
258
- ? DecoratedRouterRecord<TRoot, $Value> & DecorateRouterKeyable
266
+ ? DecoratedRouterRecord<TRoot, $Value, TFeatureFlags> &
267
+ DecorateRouterKeyable<TFeatureFlags>
259
268
  : $Value extends AnyTRPCProcedure
260
269
  ? DecorateProcedure<
261
270
  $Value['_def']['type'],
@@ -264,25 +273,31 @@ export type DecoratedRouterRecord<
264
273
  output: inferTransformedProcedureOutput<TRoot, $Value>;
265
274
  transformer: TRoot['transformer'];
266
275
  errorShape: TRoot['errorShape'];
276
+ featureFlags: TFeatureFlags;
267
277
  }
268
278
  >
269
279
  : never
270
280
  : never;
271
281
  };
272
282
 
273
- export type TRPCOptionsProxy<TRouter extends AnyTRPCRouter> =
274
- DecoratedRouterRecord<
275
- TRouter['_def']['_config']['$types'],
276
- TRouter['_def']['record']
277
- > &
278
- DecorateRouterKeyable;
279
-
280
- export interface TRPCOptionsProxyOptionsBase {
283
+ export type TRPCOptionsProxy<
284
+ TRouter extends AnyTRPCRouter,
285
+ TFeatureFlags extends FeatureFlags = DefaultFeatureFlags,
286
+ > = DecoratedRouterRecord<
287
+ TRouter['_def']['_config']['$types'],
288
+ TRouter['_def']['record'],
289
+ TFeatureFlags
290
+ > &
291
+ DecorateRouterKeyable<TFeatureFlags>;
292
+
293
+ export type TRPCOptionsProxyOptionsBase<
294
+ TFeatureFlags extends FeatureFlags = DefaultFeatureFlags,
295
+ > = {
281
296
  queryClient: QueryClient | (() => QueryClient);
282
297
  overrides?: {
283
298
  mutations?: MutationOptionsOverride;
284
299
  };
285
- }
300
+ } & KeyPrefixOptions<TFeatureFlags>;
286
301
 
287
302
  export interface TRPCOptionsProxyOptionsInternal<
288
303
  TRouter extends AnyTRPCRouter,
@@ -299,19 +314,20 @@ export interface TRPCOptionsProxyOptionsExternal<
299
314
  client: TRPCUntypedClient<TRouter> | TRPCClient<TRouter>;
300
315
  }
301
316
 
302
- export type TRPCOptionsProxyOptions<TRouter extends AnyTRPCRouter> =
303
- TRPCOptionsProxyOptionsBase &
304
- (
305
- | TRPCOptionsProxyOptionsInternal<TRouter>
306
- | TRPCOptionsProxyOptionsExternal<TRouter>
307
- );
317
+ export type TRPCOptionsProxyOptions<
318
+ TRouter extends AnyTRPCRouter,
319
+ TFeatureFlags extends FeatureFlags = DefaultFeatureFlags,
320
+ > = TRPCOptionsProxyOptionsBase<TFeatureFlags> &
321
+ (
322
+ | TRPCOptionsProxyOptionsInternal<TRouter>
323
+ | TRPCOptionsProxyOptionsExternal<TRouter>
324
+ );
308
325
 
309
326
  type UtilsMethods =
310
327
  | keyof DecorateQueryProcedure<any>
311
328
  | keyof DecorateInfiniteQueryProcedure<any>
312
329
  | keyof DecorateMutationProcedure<any>
313
- | keyof DecorateSubscriptionProcedure<any>
314
- | keyof DecorateRouterKeyable;
330
+ | keyof DecorateSubscriptionProcedure<any>;
315
331
 
316
332
  /**
317
333
  * Create a typed proxy from your router types. Can also be used on the server.
@@ -319,9 +335,14 @@ type UtilsMethods =
319
335
  * @see https://trpc.io/docs/client/tanstack-react-query/setup#3b-setup-without-react-context
320
336
  * @see https://trpc.io/docs/client/tanstack-react-query/server-components#5-create-a-trpc-caller-for-server-components
321
337
  */
322
- export function createTRPCOptionsProxy<TRouter extends AnyTRPCRouter>(
323
- opts: TRPCOptionsProxyOptions<TRouter>,
324
- ): TRPCOptionsProxy<TRouter> {
338
+ export function createTRPCOptionsProxy<
339
+ TRouter extends AnyTRPCRouter,
340
+ TFeatureFlags extends FeatureFlags = DefaultFeatureFlags,
341
+ >(
342
+ opts: TRPCOptionsProxyOptions<TRouter, TFeatureFlags>,
343
+ ): TRPCOptionsProxy<TRouter, TFeatureFlags> {
344
+ const prefix = opts.keyPrefix;
345
+
325
346
  const callIt = (type: TRPCProcedureType): any => {
326
347
  return (path: string, input: unknown, trpcOpts: TRPCRequestOptions) => {
327
348
  if ('router' in opts) {
@@ -355,12 +376,20 @@ export function createTRPCOptionsProxy<TRouter extends AnyTRPCRouter>(
355
376
  '~types': undefined as any,
356
377
 
357
378
  pathKey: () => {
358
- return getQueryKeyInternal(path);
379
+ return getQueryKeyInternal({
380
+ path,
381
+ type: 'any',
382
+ prefix,
383
+ });
359
384
  },
360
385
  pathFilter: (): QueryFilters => {
361
386
  return {
362
387
  ...arg1,
363
- queryKey: getQueryKeyInternal(path),
388
+ queryKey: getQueryKeyInternal({
389
+ path,
390
+ type: 'any',
391
+ prefix,
392
+ }),
364
393
  };
365
394
  },
366
395
 
@@ -370,17 +399,32 @@ export function createTRPCOptionsProxy<TRouter extends AnyTRPCRouter>(
370
399
  opts: arg2,
371
400
  path,
372
401
  queryClient: opts.queryClient,
373
- queryKey: getQueryKeyInternal(path, arg1, 'query'),
402
+ queryKey: getQueryKeyInternal({
403
+ path,
404
+ input: arg1,
405
+ type: 'query',
406
+ prefix,
407
+ }),
374
408
  query: callIt('query'),
375
409
  });
376
410
  },
377
411
  queryKey: () => {
378
- return getQueryKeyInternal(path, arg1, 'query');
412
+ return getQueryKeyInternal({
413
+ path,
414
+ input: arg1,
415
+ type: 'query',
416
+ prefix,
417
+ });
379
418
  },
380
419
  queryFilter: (): QueryFilters => {
381
420
  return {
382
421
  ...arg2,
383
- queryKey: getQueryKeyInternal(path, arg1, 'query'),
422
+ queryKey: getQueryKeyInternal({
423
+ path,
424
+ input: arg1,
425
+ type: 'query',
426
+ prefix,
427
+ }),
384
428
  };
385
429
  },
386
430
 
@@ -390,17 +434,32 @@ export function createTRPCOptionsProxy<TRouter extends AnyTRPCRouter>(
390
434
  opts: arg2,
391
435
  path,
392
436
  queryClient: opts.queryClient,
393
- queryKey: getQueryKeyInternal(path, arg1, 'infinite'),
437
+ queryKey: getQueryKeyInternal({
438
+ path,
439
+ input: arg1,
440
+ type: 'infinite',
441
+ prefix,
442
+ }),
394
443
  query: callIt('query'),
395
444
  });
396
445
  },
397
446
  infiniteQueryKey: () => {
398
- return getQueryKeyInternal(path, arg1, 'infinite');
447
+ return getQueryKeyInternal({
448
+ path,
449
+ input: arg1,
450
+ type: 'infinite',
451
+ prefix,
452
+ });
399
453
  },
400
454
  infiniteQueryFilter: (): QueryFilters => {
401
455
  return {
402
456
  ...arg2,
403
- queryKey: getQueryKeyInternal(path, arg1, 'infinite'),
457
+ queryKey: getQueryKeyInternal({
458
+ path,
459
+ input: arg1,
460
+ type: 'infinite',
461
+ prefix,
462
+ }),
404
463
  };
405
464
  },
406
465
 
@@ -414,14 +473,22 @@ export function createTRPCOptionsProxy<TRouter extends AnyTRPCRouter>(
414
473
  });
415
474
  },
416
475
  mutationKey: () => {
417
- return getMutationKeyInternal(path);
476
+ return getMutationKeyInternal({
477
+ path,
478
+ prefix,
479
+ });
418
480
  },
419
481
 
420
482
  subscriptionOptions: () => {
421
483
  return trpcSubscriptionOptions({
422
484
  opts: arg2,
423
485
  path,
424
- queryKey: getQueryKeyInternal(path, arg1, 'any'),
486
+ queryKey: getQueryKeyInternal({
487
+ path,
488
+ input: arg1,
489
+ type: 'any',
490
+ prefix,
491
+ }),
425
492
  subscribe: callIt('subscription'),
426
493
  });
427
494
  },