@trpc/react-query 10.42.0 → 11.0.0-next.91

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.
Files changed (97) hide show
  1. package/dist/bundle-analysis.json +208 -0
  2. package/dist/createHooksInternal-3d8ebfcf.mjs +481 -0
  3. package/dist/createHooksInternal-dce6e141.js +435 -0
  4. package/dist/createHooksInternal-e2034194.js +490 -0
  5. package/dist/createTRPCReact.d.ts +46 -45
  6. package/dist/createTRPCReact.d.ts.map +1 -1
  7. package/dist/index.d.ts +0 -1
  8. package/dist/index.d.ts.map +1 -1
  9. package/dist/index.js +8 -21
  10. package/dist/index.mjs +9 -21
  11. package/dist/internals/context.d.ts +42 -54
  12. package/dist/internals/context.d.ts.map +1 -1
  13. package/dist/internals/getClientArgs.d.ts +2 -1
  14. package/dist/internals/getClientArgs.d.ts.map +1 -1
  15. package/dist/internals/getQueryKey.d.ts +28 -13
  16. package/dist/internals/getQueryKey.d.ts.map +1 -1
  17. package/dist/internals/useQueries.d.ts +4 -4
  18. package/dist/internals/useQueries.d.ts.map +1 -1
  19. package/dist/server/index.d.ts +0 -6
  20. package/dist/server/index.d.ts.map +1 -1
  21. package/dist/server/index.js +52 -69
  22. package/dist/server/index.mjs +52 -69
  23. package/dist/server/ssgProxy.d.ts +23 -13
  24. package/dist/server/ssgProxy.d.ts.map +1 -1
  25. package/dist/shared/hooks/createHooksInternal.d.ts +35 -12
  26. package/dist/shared/hooks/createHooksInternal.d.ts.map +1 -1
  27. package/dist/shared/hooks/createRootHooks.d.ts +0 -5
  28. package/dist/shared/hooks/createRootHooks.d.ts.map +1 -1
  29. package/dist/shared/hooks/types.d.ts +33 -18
  30. package/dist/shared/hooks/types.d.ts.map +1 -1
  31. package/dist/shared/index.js +9 -9
  32. package/dist/shared/index.mjs +3 -3
  33. package/dist/shared/polymorphism/mutationLike.d.ts +5 -5
  34. package/dist/shared/polymorphism/mutationLike.d.ts.map +1 -1
  35. package/dist/shared/polymorphism/queryLike.d.ts +5 -5
  36. package/dist/shared/polymorphism/queryLike.d.ts.map +1 -1
  37. package/dist/shared/polymorphism/routerLike.d.ts +4 -3
  38. package/dist/shared/polymorphism/routerLike.d.ts.map +1 -1
  39. package/dist/shared/proxy/decorationProxy.d.ts +2 -2
  40. package/dist/shared/proxy/decorationProxy.d.ts.map +1 -1
  41. package/dist/shared/proxy/useQueriesProxy.d.ts +6 -6
  42. package/dist/shared/proxy/useQueriesProxy.d.ts.map +1 -1
  43. package/dist/shared/proxy/utilsProxy.d.ts +34 -31
  44. package/dist/shared/proxy/utilsProxy.d.ts.map +1 -1
  45. package/dist/shared/types.d.ts +0 -12
  46. package/dist/shared/types.d.ts.map +1 -1
  47. package/dist/utils/inferReactQueryProcedure.d.ts +7 -7
  48. package/dist/utils/inferReactQueryProcedure.d.ts.map +1 -1
  49. package/dist/utilsProxy-00894da2.mjs +121 -0
  50. package/dist/utilsProxy-27bd93c1.js +128 -0
  51. package/dist/utilsProxy-7e396600.js +114 -0
  52. package/package.json +8 -14
  53. package/src/createTRPCReact.tsx +156 -135
  54. package/src/index.ts +0 -1
  55. package/src/internals/context.tsx +93 -164
  56. package/src/internals/getClientArgs.ts +9 -4
  57. package/src/internals/getQueryKey.ts +53 -24
  58. package/src/internals/useQueries.ts +7 -10
  59. package/src/server/index.ts +0 -8
  60. package/src/server/ssgProxy.ts +179 -34
  61. package/src/shared/hooks/createHooksInternal.tsx +298 -207
  62. package/src/shared/hooks/createRootHooks.tsx +0 -6
  63. package/src/shared/hooks/types.ts +87 -36
  64. package/src/shared/polymorphism/mutationLike.ts +11 -8
  65. package/src/shared/polymorphism/queryLike.ts +17 -12
  66. package/src/shared/polymorphism/routerLike.ts +15 -9
  67. package/src/shared/proxy/decorationProxy.ts +9 -31
  68. package/src/shared/proxy/useQueriesProxy.ts +23 -37
  69. package/src/shared/proxy/utilsProxy.ts +121 -95
  70. package/src/shared/types.ts +0 -12
  71. package/src/utils/inferReactQueryProcedure.ts +28 -30
  72. package/dist/context-4557b3d3.js +0 -38
  73. package/dist/createHooksInternal-2e69c447.js +0 -582
  74. package/dist/createHooksInternal-37b068e0.js +0 -510
  75. package/dist/createHooksInternal-bdff7171.mjs +0 -568
  76. package/dist/getArrayQueryKey-4bdb5cc2.js +0 -36
  77. package/dist/getArrayQueryKey-86134f8b.mjs +0 -34
  78. package/dist/internals/getArrayQueryKey.d.ts +0 -25
  79. package/dist/internals/getArrayQueryKey.d.ts.map +0 -1
  80. package/dist/interop.d.ts +0 -11
  81. package/dist/interop.d.ts.map +0 -1
  82. package/dist/server/types.d.ts +0 -14
  83. package/dist/server/types.d.ts.map +0 -1
  84. package/dist/shared/hooks/deprecated/createHooksInternal.d.ts +0 -63
  85. package/dist/shared/hooks/deprecated/createHooksInternal.d.ts.map +0 -1
  86. package/dist/ssg/index.d.ts +0 -15
  87. package/dist/ssg/index.d.ts.map +0 -1
  88. package/dist/ssg/ssg.d.ts +0 -16
  89. package/dist/ssg/ssg.d.ts.map +0 -1
  90. package/src/internals/getArrayQueryKey.ts +0 -46
  91. package/src/interop.ts +0 -29
  92. package/src/server/types.ts +0 -29
  93. package/src/shared/hooks/deprecated/createHooksInternal.tsx +0 -641
  94. package/src/ssg/index.tsx +0 -16
  95. package/src/ssg/ssg.ts +0 -139
  96. package/ssg/index.d.ts +0 -1
  97. package/ssg/index.js +0 -1
@@ -6,40 +6,53 @@ import {
6
6
  InvalidateOptions,
7
7
  InvalidateQueryFilters,
8
8
  QueryClient,
9
+ QueryFilters,
9
10
  RefetchOptions,
10
11
  RefetchQueryFilters,
11
12
  ResetOptions,
12
- ResetQueryFilters,
13
13
  SetDataOptions,
14
14
  Updater,
15
15
  } from '@tanstack/react-query';
16
16
  import {
17
- CreateTRPCProxyClient,
18
- TRPCClient,
17
+ CreateTRPCClient,
19
18
  TRPCClientError,
20
19
  TRPCRequestOptions,
20
+ TRPCUntypedClient,
21
21
  } from '@trpc/client';
22
- import type { AnyRouter } from '@trpc/server';
23
- import { inferHandlerInput, inferProcedureInput } from '@trpc/server';
24
- import { inferTransformedProcedureOutput } from '@trpc/server/shared';
22
+ import type { AnyRouter, DistributiveOmit } from '@trpc/server';
25
23
  import { createContext } from 'react';
26
-
27
- export interface TRPCFetchQueryOptions<TInput, TError, TOutput>
28
- extends FetchQueryOptions<TInput, TError, TOutput>,
29
- TRPCRequestOptions {}
30
-
31
- export interface TRPCFetchInfiniteQueryOptions<TInput, TError, TOutput>
32
- extends FetchInfiniteQueryOptions<TInput, TError, TOutput>,
33
- TRPCRequestOptions {}
24
+ import { ExtractCursorType } from '../shared';
25
+ import { TRPCQueryKey } from './getQueryKey';
26
+
27
+ export type TRPCFetchQueryOptions<TOutput, TError> = DistributiveOmit<
28
+ FetchQueryOptions<TOutput, TError>,
29
+ 'queryKey'
30
+ > &
31
+ TRPCRequestOptions;
32
+
33
+ export type TRPCFetchInfiniteQueryOptions<TInput, TOutput, TError> =
34
+ DistributiveOmit<
35
+ FetchInfiniteQueryOptions<
36
+ TOutput,
37
+ TError,
38
+ TOutput,
39
+ TRPCQueryKey,
40
+ ExtractCursorType<TInput>
41
+ >,
42
+ 'queryKey' | 'initialPageParam'
43
+ > &
44
+ TRPCRequestOptions & {
45
+ initialCursor?: ExtractCursorType<TInput>;
46
+ };
34
47
 
35
48
  /** @internal */
36
49
  export type SSRState = 'mounted' | 'mounting' | 'prepass' | false;
37
50
 
38
- export interface ProxyTRPCContextProps<TRouter extends AnyRouter, TSSRContext> {
51
+ export interface TRPCContextPropsBase<TRouter extends AnyRouter, TSSRContext> {
39
52
  /**
40
53
  * The `TRPCClient`
41
54
  */
42
- client: TRPCClient<TRouter>;
55
+ client: TRPCUntypedClient<TRouter>;
43
56
  /**
44
57
  * The SSR context when server-side rendering
45
58
  * @default null
@@ -65,230 +78,146 @@ export interface ProxyTRPCContextProps<TRouter extends AnyRouter, TSSRContext> {
65
78
  /**
66
79
  * @internal
67
80
  */
68
- export type DecoratedProxyTRPCContextProps<
81
+ export type DecoratedTRPCContextProps<
69
82
  TRouter extends AnyRouter,
70
83
  TSSRContext,
71
- > = ProxyTRPCContextProps<TRouter, TSSRContext> & {
72
- client: CreateTRPCProxyClient<TRouter>;
84
+ > = TRPCContextPropsBase<TRouter, TSSRContext> & {
85
+ client: CreateTRPCClient<TRouter>;
73
86
  };
74
87
 
75
88
  export interface TRPCContextProps<TRouter extends AnyRouter, TSSRContext>
76
- extends ProxyTRPCContextProps<TRouter, TSSRContext> {
89
+ extends TRPCContextPropsBase<TRouter, TSSRContext> {
77
90
  /**
78
91
  * The react-query `QueryClient`
79
92
  */
80
93
  queryClient: QueryClient;
81
94
  }
82
95
 
83
- export const contextProps: (keyof ProxyTRPCContextProps<any, any>)[] = [
96
+ export const contextProps: (keyof TRPCContextPropsBase<any, any>)[] = [
84
97
  'client',
85
98
  'ssrContext',
86
99
  'ssrState',
87
100
  'abortOnUnmount',
88
101
  ];
89
102
 
90
- /** @internal */
91
- type TRPCContextResetQueries<TRouter extends AnyRouter> =
92
- /**
93
- * @link https://tanstack.com/query/v4/docs/react/reference/QueryClient#queryclientresetqueries
94
- */
95
- ((filters?: ResetQueryFilters, options?: ResetOptions) => Promise<void>) &
96
- (<
97
- TPath extends string & keyof TRouter['_def']['queries'],
98
- TInput extends inferProcedureInput<TRouter['_def']['queries'][TPath]>,
99
- >(
100
- pathAndInput?: TPath | [TPath, TInput?],
101
- filters?: ResetQueryFilters,
102
- options?: ResetOptions,
103
- ) => Promise<void>);
104
-
105
103
  /**
106
- * @deprecated
107
104
  * @internal
108
- **/
105
+ */
109
106
  export interface TRPCContextState<
110
107
  TRouter extends AnyRouter,
111
108
  TSSRContext = undefined,
112
109
  > extends Required<TRPCContextProps<TRouter, TSSRContext>> {
113
110
  /**
114
- * @link https://tanstack.com/query/v4/docs/reference/QueryClient#queryclientfetchquery
111
+ * @link https://tanstack.com/query/v5/docs/reference/QueryClient#queryclientfetchquery
115
112
  */
116
- fetchQuery: <
117
- TPath extends string & keyof TRouter['_def']['queries'],
118
- TProcedure extends TRouter['_def']['queries'][TPath],
119
- TOutput extends inferTransformedProcedureOutput<TProcedure>,
120
- TInput extends inferProcedureInput<TProcedure>,
121
- >(
122
- pathAndInput: [path: TPath, ...args: inferHandlerInput<TProcedure>],
123
- opts?: TRPCFetchQueryOptions<TInput, TRPCClientError<TProcedure>, TOutput>,
124
- ) => Promise<TOutput>;
125
-
113
+ fetchQuery: (
114
+ queryKey: TRPCQueryKey,
115
+ opts?: TRPCFetchQueryOptions<unknown, TRPCClientError<TRouter>>,
116
+ ) => Promise<unknown>;
126
117
  /**
127
- * @link https://tanstack.com/query/v4/docs/reference/QueryClient#queryclientfetchinfinitequery
118
+ * @link https://tanstack.com/query/v5/docs/reference/QueryClient#queryclientfetchinfinitequery
128
119
  */
129
- fetchInfiniteQuery: <
130
- TPath extends string & keyof TRouter['_def']['queries'],
131
- TProcedure extends TRouter['_def']['queries'][TPath],
132
- TOutput extends inferTransformedProcedureOutput<TProcedure>,
133
- TInput extends inferProcedureInput<TProcedure>,
134
- >(
135
- pathAndInput: [path: TPath, ...args: inferHandlerInput<TProcedure>],
120
+ fetchInfiniteQuery: (
121
+ queryKey: TRPCQueryKey,
136
122
  opts?: TRPCFetchInfiniteQueryOptions<
137
- TInput,
138
- TRPCClientError<TProcedure>,
139
- TOutput
123
+ unknown,
124
+ unknown,
125
+ TRPCClientError<TRouter>
140
126
  >,
141
- ) => Promise<InfiniteData<TOutput>>;
142
-
127
+ ) => Promise<InfiniteData<unknown, unknown>>;
143
128
  /**
144
- * @link https://tanstack.com/query/v4/docs/react/guides/prefetching
129
+ * @link https://tanstack.com/query/v5/docs/react/guides/prefetching
145
130
  */
146
- prefetchQuery: <
147
- TPath extends string & keyof TRouter['_def']['queries'],
148
- TProcedure extends TRouter['_def']['queries'][TPath],
149
- TOutput extends inferTransformedProcedureOutput<TProcedure>,
150
- TInput extends inferProcedureInput<TProcedure>,
151
- >(
152
- pathAndInput: [path: TPath, ...args: inferHandlerInput<TProcedure>],
153
- opts?: TRPCFetchQueryOptions<TInput, TRPCClientError<TProcedure>, TOutput>,
131
+ prefetchQuery: (
132
+ queryKey: TRPCQueryKey,
133
+ opts?: TRPCFetchQueryOptions<unknown, TRPCClientError<TRouter>>,
154
134
  ) => Promise<void>;
155
135
 
156
136
  /**
157
- * @link https://tanstack.com/query/v4/docs/reference/QueryClient#queryclientprefetchinfinitequery
137
+ * @link https://tanstack.com/query/v5/docs/reference/QueryClient#queryclientprefetchinfinitequery
158
138
  */
159
- prefetchInfiniteQuery: <
160
- TPath extends string & keyof TRouter['_def']['queries'],
161
- TProcedure extends TRouter['_def']['queries'][TPath],
162
- TOutput extends inferTransformedProcedureOutput<TProcedure>,
163
- TInput extends inferProcedureInput<TProcedure>,
164
- >(
165
- pathAndInput: [path: TPath, ...args: inferHandlerInput<TProcedure>],
139
+ prefetchInfiniteQuery: (
140
+ queryKey: TRPCQueryKey,
166
141
  opts?: TRPCFetchInfiniteQueryOptions<
167
- TInput,
168
- TRPCClientError<TProcedure>,
169
- TOutput
142
+ unknown,
143
+ unknown,
144
+ TRPCClientError<TRouter>
170
145
  >,
171
146
  ) => Promise<void>;
172
147
 
173
148
  /**
174
- * @link https://tanstack.com/query/v4/docs/react/reference/QueryClient#queryclientensurequerydata
149
+ * @link https://tanstack.com/query/v5/docs/react/reference/QueryClient#queryclientensurequerydata
175
150
  */
176
- ensureQueryData: <
177
- TPath extends string & keyof TRouter['_def']['queries'],
178
- TProcedure extends TRouter['_def']['queries'][TPath],
179
- TOutput extends inferTransformedProcedureOutput<TProcedure>,
180
- TInput extends inferProcedureInput<TProcedure>,
181
- >(
182
- pathAndInput: [path: TPath, ...args: inferHandlerInput<TProcedure>],
183
- opts?: TRPCFetchQueryOptions<TInput, TRPCClientError<TProcedure>, TOutput>,
184
- ) => Promise<TOutput>;
151
+ ensureQueryData: (
152
+ queryKey: TRPCQueryKey,
153
+ opts?: TRPCFetchQueryOptions<unknown, TRPCClientError<TRouter>>,
154
+ ) => Promise<unknown>;
185
155
 
186
156
  /**
187
- * @link https://tanstack.com/query/v4/docs/react/guides/query-invalidation
157
+ * @link https://tanstack.com/query/v5/docs/react/guides/query-invalidation
188
158
  */
189
- invalidateQueries: <
190
- TPath extends string & keyof TRouter['_def']['queries'],
191
- TInput extends inferProcedureInput<TRouter['_def']['queries'][TPath]>,
192
- >(
193
- pathAndInput?: TPath | [TPath, TInput?],
159
+ invalidateQueries: (
160
+ queryKey: TRPCQueryKey,
194
161
  filters?: InvalidateQueryFilters,
195
162
  options?: InvalidateOptions,
196
163
  ) => Promise<void>;
197
164
 
198
165
  /**
199
- * @link https://tanstack.com/query/v4/docs/react/reference/QueryClient#queryclientresetqueries
166
+ * @link https://tanstack.com/query/v5/docs/react/reference/QueryClient#queryclientresetqueries
200
167
  */
201
- resetQueries: TRPCContextResetQueries<TRouter>;
202
-
203
- /**
204
- * @link https://tanstack.com/query/v4/docs/react/reference/QueryClient#queryclientrefetchqueries
205
- */
206
- refetchQueries<
207
- TPath extends string & keyof TRouter['_def']['queries'],
208
- TInput extends inferProcedureInput<TRouter['_def']['queries'][TPath]>,
209
- >(
210
- pathAndInput: [TPath, TInput?],
211
- filters?: RefetchQueryFilters,
212
- options?: RefetchOptions,
213
- ): Promise<void>;
168
+ resetQueries: (
169
+ queryKey: TRPCQueryKey,
170
+ filters?: QueryFilters,
171
+ options?: ResetOptions,
172
+ ) => Promise<void>;
214
173
 
215
174
  /**
216
- * @link https://tanstack.com/query/v4/docs/react/reference/QueryClient#queryclientrefetchqueries
175
+ * @link https://tanstack.com/query/v5/docs/react/reference/QueryClient#queryclientrefetchqueries
217
176
  */
218
- refetchQueries(
177
+ refetchQueries: (
178
+ queryKey: TRPCQueryKey,
219
179
  filters?: RefetchQueryFilters,
220
180
  options?: RefetchOptions,
221
- ): Promise<void>;
181
+ ) => Promise<void>;
222
182
 
223
183
  /**
224
- * @link https://tanstack.com/query/v4/docs/react/guides/query-cancellation
184
+ * @link https://tanstack.com/query/v5/docs/react/guides/query-cancellation
225
185
  */
226
- cancelQuery: <
227
- TPath extends string & keyof TRouter['_def']['queries'],
228
- TInput extends inferProcedureInput<TRouter['_def']['queries'][TPath]>,
229
- >(
230
- pathAndInput: [TPath, TInput?],
186
+ cancelQuery: (
187
+ queryKey: TRPCQueryKey,
231
188
  options?: CancelOptions,
232
189
  ) => Promise<void>;
233
190
 
234
191
  /**
235
- * @link https://tanstack.com/query/v4/docs/react/reference/QueryClient#queryclientsetquerydata
192
+ * @link https://tanstack.com/query/v5/docs/react/reference/QueryClient#queryclientsetquerydata
236
193
  */
237
- setQueryData: <
238
- TPath extends string & keyof TRouter['_def']['queries'],
239
- TInput extends inferProcedureInput<TRouter['_def']['queries'][TPath]>,
240
- TOutput extends inferTransformedProcedureOutput<
241
- TRouter['_def']['queries'][TPath]
242
- >,
243
- >(
244
- pathAndInput: [TPath, TInput?],
245
- updater: Updater<TOutput | undefined, TOutput | undefined>,
194
+ setQueryData: (
195
+ queryKey: TRPCQueryKey,
196
+ updater: Updater<unknown, unknown>,
246
197
  options?: SetDataOptions,
247
198
  ) => void;
248
199
 
249
200
  /**
250
- * @link https://tanstack.com/query/v4/docs/react/reference/QueryClient#queryclientgetquerydata
201
+ * @link https://tanstack.com/query/v5/docs/react/reference/QueryClient#queryclientgetquerydata
251
202
  */
252
- getQueryData: <
253
- TPath extends string & keyof TRouter['_def']['queries'],
254
- TInput extends inferProcedureInput<TRouter['_def']['queries'][TPath]>,
255
- TOutput extends inferTransformedProcedureOutput<
256
- TRouter['_def']['queries'][TPath]
257
- >,
258
- >(
259
- pathAndInput: [TPath, TInput?],
260
- ) => TOutput | undefined;
261
-
203
+ getQueryData: (queryKey: TRPCQueryKey) => unknown;
262
204
  /**
263
- * @link https://tanstack.com/query/v4/docs/react/reference/QueryClient#queryclientsetquerydata
205
+ * @link https://tanstack.com/query/v5/docs/react/reference/QueryClient#queryclientsetquerydata
264
206
  */
265
- setInfiniteQueryData: <
266
- TPath extends string & keyof TRouter['_def']['queries'],
267
- TInput extends inferProcedureInput<TRouter['_def']['queries'][TPath]>,
268
- TOutput extends inferTransformedProcedureOutput<
269
- TRouter['_def']['queries'][TPath]
270
- >,
271
- >(
272
- pathAndInput: [TPath, TInput?],
207
+ setInfiniteQueryData: (
208
+ queryKey: TRPCQueryKey,
273
209
  updater: Updater<
274
- InfiniteData<TOutput> | undefined,
275
- InfiniteData<TOutput> | undefined
210
+ InfiniteData<unknown> | undefined,
211
+ InfiniteData<unknown> | undefined
276
212
  >,
277
213
  options?: SetDataOptions,
278
214
  ) => void;
279
215
 
280
216
  /**
281
- * @link https://tanstack.com/query/v4/docs/react/reference/QueryClient#queryclientgetquerydata
217
+ * @link https://tanstack.com/query/v5/docs/react/reference/QueryClient#queryclientgetquerydata
282
218
  */
283
- getInfiniteQueryData: <
284
- TPath extends string & keyof TRouter['_def']['queries'],
285
- TInput extends inferProcedureInput<TRouter['_def']['queries'][TPath]>,
286
- TOutput extends inferTransformedProcedureOutput<
287
- TRouter['_def']['queries'][TPath]
288
- >,
289
- >(
290
- pathAndInput: [TPath, TInput?],
291
- ) => InfiniteData<TOutput> | undefined;
219
+ getInfiniteQueryData: (
220
+ queryKey: TRPCQueryKey,
221
+ ) => InfiniteData<unknown> | undefined;
292
222
  }
293
-
294
223
  export const TRPCContext = createContext(null as any);
@@ -1,7 +1,12 @@
1
- export function getClientArgs<TPathAndInput extends unknown[], TOptions>(
2
- pathAndInput: TPathAndInput,
1
+ import { TRPCQueryKey } from './getQueryKey';
2
+
3
+ export function getClientArgs<TOptions>(
4
+ queryKey: TRPCQueryKey,
3
5
  opts: TOptions,
6
+ pageParam?: any,
4
7
  ) {
5
- const [path, input] = pathAndInput;
6
- return [path, input, (opts as any)?.trpc] as const;
8
+ const path = queryKey[0];
9
+ const input = queryKey[1]?.input;
10
+ if (pageParam) (input as any).cursor = pageParam;
11
+ return [path.join('.'), input, (opts as any)?.trpc] as const;
7
12
  }
@@ -1,23 +1,48 @@
1
1
  import {
2
2
  AnyMutationProcedure,
3
3
  AnyQueryProcedure,
4
+ AnyRootConfig,
4
5
  AnyRouter,
5
6
  DeepPartial,
6
7
  inferProcedureInput,
7
8
  } from '@trpc/server';
8
- import { getArrayQueryKey, QueryType } from '../internals/getArrayQueryKey';
9
9
  import { DecoratedProcedureRecord, DecorateProcedure } from '../shared';
10
10
 
11
+ export type QueryType = 'any' | 'infinite' | 'query';
12
+
13
+ export type TRPCQueryKey = [
14
+ string[],
15
+ { input?: unknown; type?: Exclude<QueryType, 'any'> }?,
16
+ ];
17
+
11
18
  /**
12
- * We treat `undefined` as an input the same as omitting an `input`
13
- * https://github.com/trpc/trpc/issues/2290
14
- */
19
+ * To allow easy interactions with groups of related queries, such as
20
+ * invalidating all queries of a router, we use an array as the path when
21
+ * storing in tanstack query.
22
+ **/
15
23
  export function getQueryKeyInternal(
16
- path: string,
24
+ path: string[],
17
25
  input: unknown,
18
- ): [string, unknown] | [string] {
19
- if (path.length) return input === undefined ? [path] : [path, input];
20
- return [] as unknown as [string];
26
+ type: QueryType,
27
+ ): TRPCQueryKey {
28
+ // Construct a query key that is easy to destructure and flexible for
29
+ // partial selecting etc.
30
+ // https://github.com/trpc/trpc/issues/3128
31
+
32
+ // some parts of the path may be dot-separated, split them up
33
+ const splitPath = path.flatMap((part) => part.split('.'));
34
+
35
+ if (!input && (!type || type === 'any'))
36
+ // for `utils.invalidate()` to match all queries (including vanilla react-query)
37
+ // we don't want nested array if path is empty, i.e. `[]` instead of `[[]]`
38
+ return splitPath.length ? [splitPath] : ([] as unknown as TRPCQueryKey);
39
+ return [
40
+ splitPath,
41
+ {
42
+ ...(typeof input !== 'undefined' && { input: input }),
43
+ ...(type && type !== 'any' && { type: type }),
44
+ },
45
+ ];
21
46
  }
22
47
 
23
48
  type GetInfiniteQueryInput<
@@ -42,35 +67,37 @@ type GetQueryParams<
42
67
  : [input?: GetQueryProcedureInput<TProcedureInput>, type?: QueryType];
43
68
 
44
69
  type GetParams<
70
+ TConfig extends AnyRootConfig,
45
71
  TProcedureOrRouter extends
46
72
  | AnyMutationProcedure
47
73
  | AnyQueryProcedure
48
74
  | AnyRouter,
49
- TPath extends string,
50
75
  TFlags,
51
76
  > = TProcedureOrRouter extends AnyQueryProcedure
52
77
  ? [
53
- procedureOrRouter: DecorateProcedure<TProcedureOrRouter, TFlags, TPath>,
78
+ procedureOrRouter: DecorateProcedure<TConfig, TProcedureOrRouter, TFlags>,
54
79
  ..._params: GetQueryParams<TProcedureOrRouter>,
55
80
  ]
56
81
  : TProcedureOrRouter extends AnyMutationProcedure
57
- ? [procedureOrRouter: DecorateProcedure<TProcedureOrRouter, TFlags, TPath>]
58
- : [
82
+ ? [procedureOrRouter: DecorateProcedure<TConfig, TProcedureOrRouter, TFlags>]
83
+ : TProcedureOrRouter extends AnyRouter
84
+ ? [
59
85
  procedureOrRouter: DecoratedProcedureRecord<
86
+ TConfig,
60
87
  TProcedureOrRouter['_def']['record'],
61
- TFlags,
62
- any
88
+ TFlags
63
89
  >,
64
- ];
90
+ ]
91
+ : never;
65
92
 
66
93
  type GetQueryKeyParams<
94
+ TConfig extends AnyRootConfig,
67
95
  TProcedureOrRouter extends
68
96
  | AnyMutationProcedure
69
97
  | AnyQueryProcedure
70
98
  | AnyRouter,
71
- TPath extends string,
72
99
  TFlags,
73
- > = GetParams<TProcedureOrRouter, TPath, TFlags>;
100
+ > = GetParams<TConfig, TProcedureOrRouter, TFlags>;
74
101
 
75
102
  /**
76
103
  * Method to extract the query key for a procedure
@@ -80,20 +107,22 @@ type GetQueryKeyParams<
80
107
  * @link https://trpc.io/docs/getQueryKey
81
108
  */
82
109
  export function getQueryKey<
110
+ TConfig extends AnyRootConfig,
83
111
  TProcedureOrRouter extends
84
112
  | AnyMutationProcedure
85
113
  | AnyQueryProcedure
86
114
  | AnyRouter,
87
- TPath extends string,
88
115
  TFlags,
89
- >(..._params: GetQueryKeyParams<TProcedureOrRouter, TPath, TFlags>) {
116
+ >(..._params: GetQueryKeyParams<TConfig, TProcedureOrRouter, TFlags>) {
90
117
  const [procedureOrRouter, input, type] = _params;
91
118
  // @ts-expect-error - we don't expose _def on the type layer
92
119
  const path = procedureOrRouter._def().path as string[];
93
- const dotPath = path.join('.');
94
- const queryKey = getArrayQueryKey(
95
- getQueryKeyInternal(dotPath, input),
96
- type ?? 'any',
97
- );
120
+ const queryKey = getQueryKeyInternal(path, input, type ?? 'any');
98
121
  return queryKey;
99
122
  }
123
+
124
+ // TODO: look over if we can't use a single type
125
+ export type QueryKeyKnown<TInput, TType extends Exclude<QueryType, 'any'>> = [
126
+ string[],
127
+ { input?: GetQueryProcedureInput<TInput>; type: TType }?,
128
+ ];
@@ -1,5 +1,5 @@
1
1
  import { QueryKey, UseQueryOptions } from '@tanstack/react-query';
2
- import { AnyRouter } from '@trpc/server';
2
+ import { AnyRouter, DistributiveOmit } from '@trpc/server';
3
3
  import {
4
4
  UseQueriesProcedureRecord,
5
5
  UseTRPCQueryOptions,
@@ -14,18 +14,16 @@ export type UseQueryOptionsForUseQueries<
14
14
  TError = unknown,
15
15
  TData = TQueryFnData,
16
16
  TQueryKey extends QueryKey = QueryKey,
17
- > = Omit<UseQueryOptions<TQueryFnData, TError, TData, TQueryKey>, 'context'>;
17
+ > = DistributiveOmit<
18
+ UseQueryOptions<TQueryFnData, TError, TData, TQueryKey>,
19
+ 'queryKey'
20
+ >;
18
21
 
19
22
  /**
20
23
  * @internal
21
24
  */
22
- export type TrpcQueryOptionsForUseQueries<
23
- TPath,
24
- TInput,
25
- TOutput,
26
- TData,
27
- TError,
28
- > = Omit<UseTRPCQueryOptions<TPath, TInput, TOutput, TData, TError>, 'context'>;
25
+ export type TrpcQueryOptionsForUseQueries<TOutput, TData, TError> =
26
+ DistributiveOmit<UseTRPCQueryOptions<TOutput, TData, TError>, 'queryKey'>;
29
27
 
30
28
  /**
31
29
  * @internal
@@ -80,5 +78,4 @@ export type TRPCUseQueries<TRouter extends AnyRouter> = <
80
78
  queriesCallback: (
81
79
  t: UseQueriesProcedureRecord<TRouter>,
82
80
  ) => readonly [...QueriesOptions<TQueryOptions>],
83
- context?: UseQueryOptions['context'],
84
81
  ) => QueriesResults<TQueryOptions>;
@@ -1,9 +1 @@
1
1
  export { createServerSideHelpers } from './ssgProxy';
2
- export type {
3
- /**
4
- * @deprecated this exported is planned to be removed in the next major version
5
- */
6
- CreateServerSideHelpersOptions as CreateSSGHelpersOptions,
7
- } from './types';
8
-
9
- export type { DecoratedProcedureSSGRecord } from './ssgProxy';