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