@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
@@ -1,641 +0,0 @@
1
- /* eslint-disable @typescript-eslint/no-non-null-assertion */
2
- import {
3
- useInfiniteQuery as __useInfiniteQuery,
4
- useMutation as __useMutation,
5
- useQueries as __useQueries,
6
- useQuery as __useQuery,
7
- DehydratedState,
8
- hashQueryKey,
9
- useQueryClient,
10
- } from '@tanstack/react-query';
11
- import { createTRPCClient, TRPCClientErrorLike } from '@trpc/client';
12
- import type {
13
- AnyRouter,
14
- inferHandlerInput,
15
- inferProcedureClientError,
16
- inferProcedureInput,
17
- inferProcedureOutput,
18
- inferSubscriptionOutput,
19
- ProcedureRecord,
20
- } from '@trpc/server';
21
- import { inferObservableValue } from '@trpc/server/observable';
22
- import { inferTransformedProcedureOutput } from '@trpc/server/shared';
23
- import React, { useCallback, useEffect, useMemo, useState } from 'react';
24
- import {
25
- SSRState,
26
- TRPCContext,
27
- TRPCContextState,
28
- } from '../../../internals/context';
29
- import {
30
- getArrayQueryKey,
31
- QueryType,
32
- } from '../../../internals/getArrayQueryKey';
33
- import { getClientArgs } from '../../../internals/getClientArgs';
34
- import { useHookResult } from '../../../internals/useHookResult';
35
- import { TRPCUseQueries } from '../../../internals/useQueries';
36
- import { createUseQueriesProxy } from '../../proxy/useQueriesProxy';
37
- import { CreateTRPCReactOptions, UseMutationOverride } from '../../types';
38
- import { createRootHooks } from '../createHooksInternal';
39
- import {
40
- CreateClient,
41
- TRPCProvider,
42
- TRPCQueryOptions,
43
- UseDehydratedState,
44
- UseTRPCInfiniteQueryOptions,
45
- UseTRPCInfiniteQueryResult,
46
- UseTRPCMutationOptions,
47
- UseTRPCMutationResult,
48
- UseTRPCQueryOptions,
49
- UseTRPCQueryResult,
50
- UseTRPCSubscriptionOptions,
51
- } from '../types';
52
-
53
- type inferInfiniteQueryNames<TObj extends ProcedureRecord> = {
54
- [TPath in keyof TObj]: inferProcedureInput<TObj[TPath]> extends {
55
- cursor?: any;
56
- }
57
- ? TPath
58
- : never;
59
- }[keyof TObj];
60
-
61
- type inferProcedures<TObj extends ProcedureRecord> = {
62
- [TPath in keyof TObj]: {
63
- input: inferProcedureInput<TObj[TPath]>;
64
- output: inferTransformedProcedureOutput<TObj[TPath]>;
65
- };
66
- };
67
-
68
- /* istanbul ignore next */
69
- /**
70
- * This isn't used anymore - only exists for type inference
71
- * @internal
72
- * @deprecated
73
- */
74
- function __createHooksInternal<
75
- TRouter extends AnyRouter,
76
- TSSRContext = unknown,
77
- >(config?: CreateTRPCReactOptions<TRouter>) {
78
- const mutationSuccessOverride: UseMutationOverride['onSuccess'] =
79
- config?.overrides?.useMutation?.onSuccess ??
80
- config?.unstable_overrides?.useMutation?.onSuccess ??
81
- ((options) => options.originalFn());
82
-
83
- type TQueries = TRouter['_def']['queries'];
84
- type TSubscriptions = TRouter['_def']['subscriptions'];
85
- type TMutations = TRouter['_def']['mutations'];
86
-
87
- type TError = TRPCClientErrorLike<TRouter>;
88
- type TInfiniteQueryNames = inferInfiniteQueryNames<TQueries>;
89
-
90
- type TQueryValues = inferProcedures<TQueries>;
91
- type TMutationValues = inferProcedures<TMutations>;
92
-
93
- type ProviderContext = TRPCContextState<TRouter, TSSRContext>;
94
-
95
- const Context = (config?.context ??
96
- TRPCContext) as React.Context<ProviderContext>;
97
- const ReactQueryContext = config?.reactQueryContext;
98
-
99
- const createClient: CreateClient<TRouter> = (opts) => {
100
- return createTRPCClient(opts);
101
- };
102
-
103
- const TRPCProvider: TRPCProvider<TRouter, TSSRContext> = (props) => {
104
- const { abortOnUnmount = false, client, queryClient, ssrContext } = props;
105
- const [ssrState, setSSRState] = useState<SSRState>(props.ssrState ?? false);
106
- useEffect(() => {
107
- // Only updating state to `mounted` if we are using SSR.
108
- // This makes it so we don't have an unnecessary re-render when opting out of SSR.
109
- setSSRState((state) => (state ? 'mounted' : false));
110
- }, []);
111
- return (
112
- <Context.Provider
113
- value={{
114
- abortOnUnmount,
115
- queryClient,
116
- client,
117
- ssrContext: ssrContext ?? null,
118
- ssrState,
119
- fetchQuery: useCallback(
120
- (pathAndInput, opts) => {
121
- return queryClient.fetchQuery(
122
- getArrayQueryKey(pathAndInput, 'query'),
123
- () =>
124
- (client as any).query(...getClientArgs(pathAndInput, opts)),
125
- opts,
126
- );
127
- },
128
- [client, queryClient],
129
- ),
130
- fetchInfiniteQuery: useCallback(
131
- (pathAndInput, opts) => {
132
- return queryClient.fetchInfiniteQuery(
133
- getArrayQueryKey(pathAndInput, 'infinite'),
134
- ({ pageParam }) => {
135
- const [path, input] = pathAndInput;
136
- const actualInput = { ...(input as any), cursor: pageParam };
137
- return (client as any).query(
138
- ...getClientArgs([path, actualInput], opts),
139
- );
140
- },
141
- opts,
142
- );
143
- },
144
- [client, queryClient],
145
- ),
146
- prefetchQuery: useCallback(
147
- (pathAndInput, opts) => {
148
- return queryClient.prefetchQuery(
149
- getArrayQueryKey(pathAndInput, 'query'),
150
- () =>
151
- (client as any).query(...getClientArgs(pathAndInput, opts)),
152
- opts,
153
- );
154
- },
155
- [client, queryClient],
156
- ),
157
- prefetchInfiniteQuery: useCallback(
158
- (pathAndInput, opts) => {
159
- return queryClient.prefetchInfiniteQuery(
160
- getArrayQueryKey(pathAndInput, 'infinite'),
161
- ({ pageParam }) => {
162
- const [path, input] = pathAndInput;
163
- const actualInput = { ...(input as any), cursor: pageParam };
164
- return (client as any).query(
165
- ...getClientArgs([path, actualInput], opts),
166
- );
167
- },
168
- opts,
169
- );
170
- },
171
- [client, queryClient],
172
- ),
173
- ensureQueryData: useCallback(
174
- (pathAndInput, opts) => {
175
- return queryClient.ensureQueryData(
176
- getArrayQueryKey(pathAndInput, 'query'),
177
- () =>
178
- (client as any).query(...getClientArgs(pathAndInput, opts)),
179
- opts,
180
- );
181
- },
182
- [client, queryClient],
183
- ),
184
- invalidateQueries: useCallback(
185
- (...args: any[]) => {
186
- const [queryKey, ...rest] = args;
187
-
188
- return queryClient.invalidateQueries(
189
- getArrayQueryKey(queryKey, 'any'),
190
- ...rest,
191
- );
192
- },
193
- [queryClient],
194
- ),
195
- resetQueries: useCallback(
196
- (...args: any[]) => {
197
- const [queryKey, ...rest] = args;
198
-
199
- return queryClient.resetQueries(
200
- getArrayQueryKey(queryKey, 'any'),
201
- ...rest,
202
- );
203
- },
204
- [queryClient],
205
- ),
206
- refetchQueries: useCallback(
207
- (...args: any[]) => {
208
- const [queryKey, ...rest] = args;
209
-
210
- return queryClient.refetchQueries(
211
- getArrayQueryKey(queryKey, 'any'),
212
- ...rest,
213
- );
214
- },
215
- [queryClient],
216
- ),
217
- cancelQuery: useCallback(
218
- (pathAndInput) => {
219
- return queryClient.cancelQueries(
220
- getArrayQueryKey(pathAndInput, 'any'),
221
- );
222
- },
223
- [queryClient],
224
- ),
225
- setQueryData: useCallback(
226
- (...args) => {
227
- const [queryKey, ...rest] = args;
228
- return queryClient.setQueryData(
229
- getArrayQueryKey(queryKey, 'query'),
230
- ...rest,
231
- );
232
- },
233
- [queryClient],
234
- ),
235
- getQueryData: useCallback(
236
- (...args) => {
237
- const [queryKey, ...rest] = args;
238
-
239
- return queryClient.getQueryData(
240
- getArrayQueryKey(queryKey, 'query'),
241
- ...rest,
242
- );
243
- },
244
- [queryClient],
245
- ),
246
- setInfiniteQueryData: useCallback(
247
- (...args) => {
248
- const [queryKey, ...rest] = args;
249
-
250
- return queryClient.setQueryData(
251
- getArrayQueryKey(queryKey, 'infinite'),
252
- ...rest,
253
- );
254
- },
255
- [queryClient],
256
- ),
257
- getInfiniteQueryData: useCallback(
258
- (...args) => {
259
- const [queryKey, ...rest] = args;
260
-
261
- return queryClient.getQueryData(
262
- getArrayQueryKey(queryKey, 'infinite'),
263
- ...rest,
264
- );
265
- },
266
- [queryClient],
267
- ),
268
- }}
269
- >
270
- {props.children}
271
- </Context.Provider>
272
- );
273
- };
274
-
275
- function useContext() {
276
- return React.useContext(Context);
277
- }
278
-
279
- /**
280
- * Hack to make sure errors return `status`='error` when doing SSR
281
- * @link https://github.com/trpc/trpc/pull/1645
282
- */
283
- function useSSRQueryOptionsIfNeeded<
284
- TOptions extends { retryOnMount?: boolean } | undefined,
285
- >(
286
- pathAndInput: unknown[],
287
- type: Exclude<QueryType, 'any'>,
288
- opts: TOptions,
289
- ): TOptions {
290
- const { queryClient, ssrState } = useContext();
291
- return ssrState &&
292
- ssrState !== 'mounted' &&
293
- queryClient.getQueryCache().find(getArrayQueryKey(pathAndInput, type))
294
- ?.state.status === 'error'
295
- ? {
296
- retryOnMount: false,
297
- ...opts,
298
- }
299
- : opts;
300
- }
301
-
302
- function useQuery<
303
- TPath extends string & keyof TQueryValues,
304
- TQueryFnData = TQueryValues[TPath]['output'],
305
- TData = TQueryValues[TPath]['output'],
306
- >(
307
- pathAndInput: [path: TPath, ...args: inferHandlerInput<TQueries[TPath]>],
308
- opts?: UseTRPCQueryOptions<
309
- TPath,
310
- TQueryValues[TPath]['input'],
311
- TQueryFnData,
312
- TData,
313
- TError
314
- >,
315
- ): UseTRPCQueryResult<TData, TError> {
316
- const { abortOnUnmount, client, ssrState, queryClient, prefetchQuery } =
317
- useContext();
318
-
319
- if (
320
- typeof window === 'undefined' &&
321
- ssrState === 'prepass' &&
322
- opts?.trpc?.ssr !== false &&
323
- opts?.enabled !== false &&
324
- !queryClient.getQueryCache().find(getArrayQueryKey(pathAndInput, 'query'))
325
- ) {
326
- void prefetchQuery(pathAndInput as any, opts as any);
327
- }
328
- const ssrOpts = useSSRQueryOptionsIfNeeded(pathAndInput, 'query', opts);
329
- // request option should take priority over global
330
- const shouldAbortOnUnmount = opts?.trpc?.abortOnUnmount ?? abortOnUnmount;
331
-
332
- const hook = __useQuery(
333
- getArrayQueryKey(pathAndInput, 'query') as any,
334
- (queryFunctionContext) => {
335
- const actualOpts = {
336
- ...ssrOpts,
337
- trpc: {
338
- ...ssrOpts?.trpc,
339
- ...(shouldAbortOnUnmount
340
- ? { signal: queryFunctionContext.signal }
341
- : {}),
342
- },
343
- };
344
-
345
- return (client as any).query(
346
- ...getClientArgs(pathAndInput, actualOpts),
347
- );
348
- },
349
- { context: ReactQueryContext, ...ssrOpts },
350
- ) as UseTRPCQueryResult<TData, TError>;
351
- hook.trpc = useHookResult({
352
- path: pathAndInput[0],
353
- });
354
-
355
- return hook;
356
- }
357
-
358
- function useMutation<
359
- TPath extends string & keyof TMutationValues,
360
- TContext = unknown,
361
- >(
362
- path: TPath | [TPath],
363
- opts?: UseTRPCMutationOptions<
364
- TMutationValues[TPath]['input'],
365
- TError,
366
- TMutationValues[TPath]['output'],
367
- TContext
368
- >,
369
- ): UseTRPCMutationResult<
370
- TMutationValues[TPath]['output'],
371
- TError,
372
- TMutationValues[TPath]['input'],
373
- TContext
374
- > {
375
- const { client } = useContext();
376
- const queryClient = useQueryClient({ context: ReactQueryContext });
377
-
378
- const hook = __useMutation(
379
- (input) => {
380
- const actualPath = Array.isArray(path) ? path[0] : path;
381
-
382
- return (client.mutation as any)(
383
- ...getClientArgs([actualPath, input], opts),
384
- );
385
- },
386
- {
387
- context: ReactQueryContext,
388
- ...opts,
389
- onSuccess(...args) {
390
- const originalFn = () => opts?.onSuccess?.(...args);
391
-
392
- return mutationSuccessOverride({
393
- originalFn,
394
- queryClient,
395
- meta: opts?.meta ?? {},
396
- });
397
- },
398
- },
399
- ) as UseTRPCMutationResult<
400
- TMutationValues[TPath]['output'],
401
- TError,
402
- TMutationValues[TPath]['input'],
403
- TContext
404
- >;
405
-
406
- hook.trpc = useHookResult({
407
- path: Array.isArray(path) ? path[0] : path,
408
- });
409
-
410
- return hook;
411
- }
412
-
413
- /* istanbul ignore next */
414
- /**
415
- * ⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️
416
- * **Experimental.** API might change without major version bump
417
- * ⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠
418
- */
419
- function useSubscription<
420
- TPath extends string & keyof TSubscriptions,
421
- TOutput extends inferSubscriptionOutput<TRouter, TPath>,
422
- >(
423
- pathAndInput: [
424
- path: TPath,
425
- ...args: inferHandlerInput<TSubscriptions[TPath]>,
426
- ],
427
- opts: UseTRPCSubscriptionOptions<
428
- inferObservableValue<inferProcedureOutput<TSubscriptions[TPath]>>,
429
- inferProcedureClientError<TSubscriptions[TPath]>
430
- >,
431
- ) {
432
- const enabled = opts?.enabled ?? true;
433
- const queryKey = hashQueryKey(pathAndInput);
434
- const { client } = useContext();
435
-
436
- useEffect(() => {
437
- if (!enabled) {
438
- return;
439
- }
440
- const [path, input] = pathAndInput;
441
- let isStopped = false;
442
- const subscription = client.subscription<
443
- TRouter['_def']['subscriptions'],
444
- TPath,
445
- TOutput,
446
- inferProcedureInput<TRouter['_def']['subscriptions'][TPath]>
447
- >(path, (input ?? undefined) as any, {
448
- onStarted: () => {
449
- if (!isStopped) {
450
- opts.onStarted?.();
451
- }
452
- },
453
- onData: (data) => {
454
- if (!isStopped) {
455
- opts.onData(data);
456
- }
457
- },
458
- onError: (err) => {
459
- if (!isStopped) {
460
- opts.onError?.(err);
461
- }
462
- },
463
- });
464
- return () => {
465
- isStopped = true;
466
- subscription.unsubscribe();
467
- };
468
- // eslint-disable-next-line react-hooks/exhaustive-deps
469
- }, [queryKey, enabled]);
470
- }
471
-
472
- function useInfiniteQuery<TPath extends TInfiniteQueryNames & string>(
473
- pathAndInput: [
474
- path: TPath,
475
- input: Omit<TQueryValues[TPath]['input'], 'cursor'>,
476
- ],
477
- opts?: UseTRPCInfiniteQueryOptions<
478
- TPath,
479
- TQueryValues[TPath]['input'],
480
- TQueryValues[TPath]['output'],
481
- TError
482
- >,
483
- ): UseTRPCInfiniteQueryResult<TQueryValues[TPath]['output'], TError> {
484
- const [path, input] = pathAndInput;
485
- const {
486
- client,
487
- ssrState,
488
- prefetchInfiniteQuery,
489
- queryClient,
490
- abortOnUnmount,
491
- } = useContext();
492
-
493
- if (
494
- typeof window === 'undefined' &&
495
- ssrState === 'prepass' &&
496
- opts?.trpc?.ssr !== false &&
497
- opts?.enabled !== false &&
498
- !queryClient
499
- .getQueryCache()
500
- .find(getArrayQueryKey(pathAndInput, 'infinite'))
501
- ) {
502
- void prefetchInfiniteQuery(pathAndInput as any, opts as any);
503
- }
504
-
505
- const ssrOpts = useSSRQueryOptionsIfNeeded(pathAndInput, 'infinite', opts);
506
-
507
- // request option should take priority over global
508
- const shouldAbortOnUnmount = opts?.trpc?.abortOnUnmount ?? abortOnUnmount;
509
-
510
- const hook = __useInfiniteQuery(
511
- getArrayQueryKey(pathAndInput, 'infinite') as any,
512
- (queryFunctionContext) => {
513
- const actualOpts = {
514
- ...ssrOpts,
515
- trpc: {
516
- ...ssrOpts?.trpc,
517
- ...(shouldAbortOnUnmount
518
- ? { signal: queryFunctionContext.signal }
519
- : {}),
520
- },
521
- };
522
-
523
- const actualInput = {
524
- ...((input as any) ?? {}),
525
- cursor: queryFunctionContext.pageParam ?? opts?.initialCursor,
526
- };
527
-
528
- return (client as any).query(
529
- ...getClientArgs([path, actualInput], actualOpts),
530
- );
531
- },
532
- { context: ReactQueryContext, ...ssrOpts },
533
- ) as UseTRPCInfiniteQueryResult<TQueryValues[TPath]['output'], TError>;
534
-
535
- hook.trpc = useHookResult({
536
- path,
537
- });
538
- return hook;
539
- }
540
-
541
- const useQueries: TRPCUseQueries<TRouter> = (queriesCallback, context) => {
542
- const { ssrState, queryClient, prefetchQuery, client } = useContext();
543
-
544
- const proxy = createUseQueriesProxy(client);
545
-
546
- const queries = queriesCallback(proxy);
547
-
548
- if (typeof window === 'undefined' && ssrState === 'prepass') {
549
- for (const query of queries) {
550
- const queryOption = query as TRPCQueryOptions<any, any, any, any>;
551
- if (
552
- queryOption.trpc?.ssr !== false &&
553
- !queryClient
554
- .getQueryCache()
555
- .find(getArrayQueryKey(queryOption.queryKey!, 'query'))
556
- ) {
557
- void prefetchQuery(queryOption.queryKey as any, queryOption as any);
558
- }
559
- }
560
- }
561
-
562
- return __useQueries({
563
- queries: queries.map((query) => ({
564
- ...query,
565
- queryKey: getArrayQueryKey(query.queryKey, 'query'),
566
- })),
567
- context,
568
- }) as any;
569
- };
570
-
571
- const useDehydratedState: UseDehydratedState<TRouter> = (
572
- client,
573
- trpcState,
574
- ) => {
575
- const transformed: DehydratedState | undefined = useMemo(() => {
576
- if (!trpcState) {
577
- return trpcState;
578
- }
579
-
580
- return client.runtime.transformer.deserialize(trpcState);
581
- }, [trpcState, client]);
582
- return transformed;
583
- };
584
-
585
- return {
586
- Provider: TRPCProvider,
587
- createClient,
588
- useContext,
589
- useUtils: useContext,
590
- useQuery,
591
- useQueries,
592
- useMutation,
593
- useSubscription,
594
- useDehydratedState,
595
- useInfiniteQuery,
596
- };
597
- }
598
-
599
- /**
600
- * Hack to infer the type of `createReactQueryHooks`
601
- * @link https://stackoverflow.com/a/59072991
602
- */
603
- class GnClass<TRouter extends AnyRouter, TSSRContext = unknown> {
604
- fn() {
605
- return __createHooksInternal<TRouter, TSSRContext>();
606
- }
607
- }
608
-
609
- type returnTypeInferer<TType> = TType extends (
610
- a: Record<string, string>,
611
- ) => infer U
612
- ? U
613
- : never;
614
- type fooType<TRouter extends AnyRouter, TSSRContext = unknown> = GnClass<
615
- TRouter,
616
- TSSRContext
617
- >['fn'];
618
-
619
- /**
620
- * Infer the type of a `createReactQueryHooks` function
621
- * @internal
622
- */
623
- export type CreateReactQueryHooks<
624
- TRouter extends AnyRouter,
625
- TSSRContext = unknown,
626
- > = returnTypeInferer<fooType<TRouter, TSSRContext>>;
627
-
628
- /**
629
- * Create strongly typed react hooks
630
- * @internal
631
- * @deprecated
632
- */
633
- export function createHooksInternal<
634
- TRouter extends AnyRouter,
635
- TSSRContext = unknown,
636
- >(config?: CreateTRPCReactOptions<TRouter>) {
637
- return createRootHooks(config) as unknown as CreateReactQueryHooks<
638
- TRouter,
639
- TSSRContext
640
- >;
641
- }
package/src/ssg/index.tsx DELETED
@@ -1,16 +0,0 @@
1
- export {
2
- /**
3
- * @deprecated use `@trpc/react-query/server` instead
4
- */
5
- type CreateSSGHelpersOptions,
6
- /**
7
- * @deprecated use `@trpc/react-query/server` instead
8
- */
9
- type DecoratedProcedureSSGRecord,
10
- /**
11
- * @deprecated use `import { createServerSideHelpers } from "@trpc/react-query/server"`
12
- */
13
- createServerSideHelpers as createProxySSGHelpers,
14
- } from '../server';
15
-
16
- export { createSSGHelpers } from './ssg';