@trpc/react-query 10.43.0 → 11.0.0-next.92

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,582 +0,0 @@
1
- 'use strict';
2
-
3
- var shared = require('@trpc/server/shared');
4
- var getArrayQueryKey = require('./getArrayQueryKey-4bdb5cc2.js');
5
- var client = require('@trpc/client');
6
- var reactQuery = require('@tanstack/react-query');
7
- var React = require('react');
8
-
9
- function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
10
-
11
- var React__default = /*#__PURE__*/_interopDefaultLegacy(React);
12
-
13
- /**
14
- * We treat `undefined` as an input the same as omitting an `input`
15
- * https://github.com/trpc/trpc/issues/2290
16
- */ function getQueryKeyInternal(path, input) {
17
- if (path.length) return input === undefined ? [
18
- path
19
- ] : [
20
- path,
21
- input
22
- ];
23
- return [];
24
- }
25
- /**
26
- * Method to extract the query key for a procedure
27
- * @param procedureOrRouter - procedure or AnyRouter
28
- * @param input - input to procedureOrRouter
29
- * @param type - defaults to `any`
30
- * @link https://trpc.io/docs/getQueryKey
31
- */ function getQueryKey(..._params) {
32
- const [procedureOrRouter, input, type] = _params;
33
- // @ts-expect-error - we don't expose _def on the type layer
34
- const path = procedureOrRouter._def().path;
35
- const dotPath = path.join('.');
36
- const queryKey = getArrayQueryKey.getArrayQueryKey(getQueryKeyInternal(dotPath, input), type ?? 'any');
37
- return queryKey;
38
- }
39
-
40
- /**
41
- * Create proxy for decorating procedures
42
- * @internal
43
- */ function createReactProxyDecoration(name, hooks) {
44
- return shared.createRecursiveProxy((opts)=>{
45
- const args = opts.args;
46
- const pathCopy = [
47
- name,
48
- ...opts.path
49
- ];
50
- // The last arg is for instance `.useMutation` or `.useQuery()`
51
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
52
- const lastArg = pathCopy.pop();
53
- // The `path` ends up being something like `post.byId`
54
- const path = pathCopy.join('.');
55
- if (lastArg === 'useMutation') {
56
- return hooks[lastArg](path, ...args);
57
- }
58
- const [input, ...rest] = args;
59
- const queryKey = getQueryKeyInternal(path, input);
60
- // Expose queryKey helper
61
- if (lastArg === 'getQueryKey') {
62
- return getArrayQueryKey.getArrayQueryKey(queryKey, rest[0] ?? 'any');
63
- }
64
- if (lastArg === '_def') {
65
- return {
66
- path: pathCopy
67
- };
68
- }
69
- if (lastArg.startsWith('useSuspense')) {
70
- const opts1 = rest[0] || {};
71
- const fn = lastArg === 'useSuspenseQuery' ? 'useQuery' : 'useInfiniteQuery';
72
- const result = hooks[fn](queryKey, {
73
- ...opts1,
74
- suspense: true,
75
- enabled: true
76
- });
77
- return [
78
- result.data,
79
- result
80
- ];
81
- }
82
- return hooks[lastArg](queryKey, ...rest);
83
- });
84
- }
85
-
86
- const contextProps = [
87
- 'client',
88
- 'ssrContext',
89
- 'ssrState',
90
- 'abortOnUnmount'
91
- ];
92
- const TRPCContext = /*#__PURE__*/ React.createContext(null);
93
-
94
- /**
95
- * @internal
96
- */ function createReactQueryUtilsProxy(context) {
97
- return shared.createFlatProxy((key)=>{
98
- const contextName = key;
99
- if (contextName === 'client') {
100
- return client.createTRPCClientProxy(context.client);
101
- }
102
- if (contextProps.includes(contextName)) {
103
- return context[contextName];
104
- }
105
- return shared.createRecursiveProxy(({ path , args })=>{
106
- const pathCopy = [
107
- key,
108
- ...path
109
- ];
110
- const utilName = pathCopy.pop();
111
- const fullPath = pathCopy.join('.');
112
- const getOpts = (name)=>{
113
- if ([
114
- 'setData',
115
- 'setInfiniteData'
116
- ].includes(name)) {
117
- const [input, updater, ...rest] = args;
118
- const queryKey = getQueryKeyInternal(fullPath, input);
119
- return {
120
- queryKey,
121
- updater,
122
- rest
123
- };
124
- }
125
- const [input1, ...rest1] = args;
126
- const queryKey1 = getQueryKeyInternal(fullPath, input1);
127
- return {
128
- queryKey: queryKey1,
129
- rest: rest1
130
- };
131
- };
132
- const { queryKey , rest , updater } = getOpts(utilName);
133
- const contextMap = {
134
- fetch: ()=>context.fetchQuery(queryKey, ...rest),
135
- fetchInfinite: ()=>context.fetchInfiniteQuery(queryKey, ...rest),
136
- prefetch: ()=>context.prefetchQuery(queryKey, ...rest),
137
- prefetchInfinite: ()=>context.prefetchInfiniteQuery(queryKey, ...rest),
138
- ensureData: ()=>context.ensureQueryData(queryKey, ...rest),
139
- invalidate: ()=>context.invalidateQueries(queryKey, ...rest),
140
- reset: ()=>context.resetQueries(queryKey, ...rest),
141
- refetch: ()=>context.refetchQueries(queryKey, ...rest),
142
- cancel: ()=>context.cancelQuery(queryKey, ...rest),
143
- setData: ()=>{
144
- context.setQueryData(queryKey, updater, ...rest);
145
- },
146
- setInfiniteData: ()=>{
147
- context.setInfiniteQueryData(queryKey, updater, ...rest);
148
- },
149
- getData: ()=>context.getQueryData(queryKey),
150
- getInfiniteData: ()=>context.getInfiniteQueryData(queryKey)
151
- };
152
- return contextMap[utilName]();
153
- });
154
- });
155
- }
156
-
157
- /**
158
- * Create proxy for `useQueries` options
159
- * @internal
160
- */ function createUseQueriesProxy(client) {
161
- return shared.createRecursiveProxy((opts)=>{
162
- const path = opts.path.join('.');
163
- const [input, _opts] = opts.args;
164
- const queryKey = getQueryKeyInternal(path, input);
165
- const options = {
166
- queryKey,
167
- queryFn: ()=>{
168
- return client.query(path, input, _opts?.trpc);
169
- },
170
- ..._opts
171
- };
172
- return options;
173
- });
174
- }
175
-
176
- function getClientArgs(pathAndInput, opts) {
177
- const [path, input] = pathAndInput;
178
- return [
179
- path,
180
- input,
181
- opts?.trpc
182
- ];
183
- }
184
-
185
- /**
186
- * Makes a stable reference of the `trpc` prop
187
- */ function useHookResult(value) {
188
- const ref = React.useRef(value);
189
- ref.current.path = value.path;
190
- return ref.current;
191
- }
192
-
193
- /**
194
- * @internal
195
- */ function createRootHooks(config) {
196
- const mutationSuccessOverride = (config?.overrides ?? config?.unstable_overrides)?.useMutation?.onSuccess ?? ((options)=>options.originalFn());
197
- const Context = config?.context ?? TRPCContext;
198
- const ReactQueryContext = config?.reactQueryContext;
199
- const createClient = (opts)=>{
200
- return client.createTRPCClient(opts);
201
- };
202
- const TRPCProvider = (props)=>{
203
- const { abortOnUnmount =false , client , queryClient , ssrContext } = props;
204
- const [ssrState, setSSRState] = React.useState(props.ssrState ?? false);
205
- React.useEffect(()=>{
206
- // Only updating state to `mounted` if we are using SSR.
207
- // This makes it so we don't have an unnecessary re-render when opting out of SSR.
208
- setSSRState((state)=>state ? 'mounted' : false);
209
- }, []);
210
- return /*#__PURE__*/ React__default["default"].createElement(Context.Provider, {
211
- value: {
212
- abortOnUnmount,
213
- queryClient,
214
- client,
215
- ssrContext: ssrContext ?? null,
216
- ssrState,
217
- fetchQuery: React.useCallback((pathAndInput, opts)=>{
218
- return queryClient.fetchQuery({
219
- ...opts,
220
- queryKey: getArrayQueryKey.getArrayQueryKey(pathAndInput, 'query'),
221
- queryFn: ()=>client.query(...getClientArgs(pathAndInput, opts))
222
- });
223
- }, [
224
- client,
225
- queryClient
226
- ]),
227
- fetchInfiniteQuery: React.useCallback((pathAndInput, opts)=>{
228
- return queryClient.fetchInfiniteQuery({
229
- ...opts,
230
- queryKey: getArrayQueryKey.getArrayQueryKey(pathAndInput, 'infinite'),
231
- queryFn: ({ pageParam })=>{
232
- const [path, input] = pathAndInput;
233
- const actualInput = {
234
- ...input,
235
- cursor: pageParam
236
- };
237
- return client.query(...getClientArgs([
238
- path,
239
- actualInput
240
- ], opts));
241
- }
242
- });
243
- }, [
244
- client,
245
- queryClient
246
- ]),
247
- prefetchQuery: React.useCallback((pathAndInput, opts)=>{
248
- return queryClient.prefetchQuery({
249
- ...opts,
250
- queryKey: getArrayQueryKey.getArrayQueryKey(pathAndInput, 'query'),
251
- queryFn: ()=>client.query(...getClientArgs(pathAndInput, opts))
252
- });
253
- }, [
254
- client,
255
- queryClient
256
- ]),
257
- prefetchInfiniteQuery: React.useCallback((pathAndInput, opts)=>{
258
- return queryClient.prefetchInfiniteQuery({
259
- ...opts,
260
- queryKey: getArrayQueryKey.getArrayQueryKey(pathAndInput, 'infinite'),
261
- queryFn: ({ pageParam })=>{
262
- const [path, input] = pathAndInput;
263
- const actualInput = {
264
- ...input,
265
- cursor: pageParam
266
- };
267
- return client.query(...getClientArgs([
268
- path,
269
- actualInput
270
- ], opts));
271
- }
272
- });
273
- }, [
274
- client,
275
- queryClient
276
- ]),
277
- ensureQueryData: React.useCallback((pathAndInput, opts)=>{
278
- return queryClient.ensureQueryData({
279
- ...opts,
280
- queryKey: getArrayQueryKey.getArrayQueryKey(pathAndInput, 'query'),
281
- queryFn: ()=>client.query(...getClientArgs(pathAndInput, opts))
282
- });
283
- }, [
284
- client,
285
- queryClient
286
- ]),
287
- invalidateQueries: React.useCallback((queryKey, filters, options)=>{
288
- return queryClient.invalidateQueries({
289
- ...filters,
290
- queryKey: getArrayQueryKey.getArrayQueryKey(queryKey, 'any')
291
- }, options);
292
- }, [
293
- queryClient
294
- ]),
295
- resetQueries: React.useCallback((...args)=>{
296
- const [queryKey, filters, options] = args;
297
- return queryClient.resetQueries({
298
- ...filters,
299
- queryKey: getArrayQueryKey.getArrayQueryKey(queryKey, 'any')
300
- }, options);
301
- }, [
302
- queryClient
303
- ]),
304
- refetchQueries: React.useCallback((...args)=>{
305
- const [queryKey, filters, options] = args;
306
- return queryClient.refetchQueries({
307
- ...filters,
308
- queryKey: getArrayQueryKey.getArrayQueryKey(queryKey, 'any')
309
- }, options);
310
- }, [
311
- queryClient
312
- ]),
313
- cancelQuery: React.useCallback((pathAndInput)=>{
314
- return queryClient.cancelQueries({
315
- queryKey: getArrayQueryKey.getArrayQueryKey(pathAndInput, 'any')
316
- });
317
- }, [
318
- queryClient
319
- ]),
320
- setQueryData: React.useCallback((...args)=>{
321
- const [queryKey, ...rest] = args;
322
- return queryClient.setQueryData(getArrayQueryKey.getArrayQueryKey(queryKey, 'query'), ...rest);
323
- }, [
324
- queryClient
325
- ]),
326
- getQueryData: React.useCallback((...args)=>{
327
- const [queryKey, ...rest] = args;
328
- return queryClient.getQueryData(getArrayQueryKey.getArrayQueryKey(queryKey, 'query'), ...rest);
329
- }, [
330
- queryClient
331
- ]),
332
- setInfiniteQueryData: React.useCallback((...args)=>{
333
- const [queryKey, ...rest] = args;
334
- return queryClient.setQueryData(getArrayQueryKey.getArrayQueryKey(queryKey, 'infinite'), ...rest);
335
- }, [
336
- queryClient
337
- ]),
338
- getInfiniteQueryData: React.useCallback((...args)=>{
339
- const [queryKey, ...rest] = args;
340
- return queryClient.getQueryData(getArrayQueryKey.getArrayQueryKey(queryKey, 'infinite'), ...rest);
341
- }, [
342
- queryClient
343
- ])
344
- }
345
- }, props.children);
346
- };
347
- function useContext() {
348
- return React__default["default"].useContext(Context);
349
- }
350
- /**
351
- * Hack to make sure errors return `status`='error` when doing SSR
352
- * @link https://github.com/trpc/trpc/pull/1645
353
- */ function useSSRQueryOptionsIfNeeded(pathAndInput, type, opts) {
354
- const { queryClient , ssrState } = useContext();
355
- return ssrState && ssrState !== 'mounted' && queryClient.getQueryCache().find(getArrayQueryKey.getArrayQueryKey(pathAndInput, type))?.state.status === 'error' ? {
356
- retryOnMount: false,
357
- ...opts
358
- } : opts;
359
- }
360
- function useQuery(// FIXME path should be a tuple in next major
361
- pathAndInput, opts) {
362
- const context = useContext();
363
- if (!context) {
364
- throw new Error('Unable to retrieve application context. Did you forget to wrap your App inside `withTRPC` HoC?');
365
- }
366
- const { abortOnUnmount , client , ssrState , queryClient , prefetchQuery } = context;
367
- const defaultOpts = queryClient.getQueryDefaults(getArrayQueryKey.getArrayQueryKey(pathAndInput, 'query'));
368
- if (typeof window === 'undefined' && ssrState === 'prepass' && opts?.trpc?.ssr !== false && (opts?.enabled ?? defaultOpts?.enabled) !== false && !queryClient.getQueryCache().find(getArrayQueryKey.getArrayQueryKey(pathAndInput, 'query'))) {
369
- void prefetchQuery(pathAndInput, opts);
370
- }
371
- const ssrOpts = useSSRQueryOptionsIfNeeded(pathAndInput, 'query', {
372
- ...defaultOpts,
373
- ...opts
374
- });
375
- const shouldAbortOnUnmount = opts?.trpc?.abortOnUnmount ?? config?.abortOnUnmount ?? abortOnUnmount;
376
- const hook = reactQuery.useQuery({
377
- ...ssrOpts,
378
- queryKey: getArrayQueryKey.getArrayQueryKey(pathAndInput, 'query'),
379
- queryFn: (queryFunctionContext)=>{
380
- const actualOpts = {
381
- ...ssrOpts,
382
- trpc: {
383
- ...ssrOpts?.trpc,
384
- ...shouldAbortOnUnmount ? {
385
- signal: queryFunctionContext.signal
386
- } : {}
387
- }
388
- };
389
- return client.query(...getClientArgs(pathAndInput, actualOpts));
390
- },
391
- context: ReactQueryContext
392
- });
393
- hook.trpc = useHookResult({
394
- path: pathAndInput[0]
395
- });
396
- return hook;
397
- }
398
- function useMutation(// FIXME: this should only be a tuple path in next major
399
- path, opts) {
400
- const { client } = useContext();
401
- const queryClient = reactQuery.useQueryClient({
402
- context: ReactQueryContext
403
- });
404
- const actualPath = Array.isArray(path) ? path[0] : path;
405
- const defaultOpts = queryClient.getMutationDefaults([
406
- actualPath.split('.')
407
- ]);
408
- const hook = reactQuery.useMutation({
409
- ...opts,
410
- mutationKey: [
411
- actualPath.split('.')
412
- ],
413
- mutationFn: (input)=>{
414
- return client.mutation(...getClientArgs([
415
- actualPath,
416
- input
417
- ], opts));
418
- },
419
- context: ReactQueryContext,
420
- onSuccess (...args) {
421
- const originalFn = ()=>opts?.onSuccess?.(...args) ?? defaultOpts?.onSuccess?.(...args);
422
- return mutationSuccessOverride({
423
- originalFn,
424
- queryClient,
425
- meta: opts?.meta ?? defaultOpts?.meta ?? {}
426
- });
427
- }
428
- });
429
- hook.trpc = useHookResult({
430
- path: actualPath
431
- });
432
- return hook;
433
- }
434
- /* istanbul ignore next -- @preserve */ function useSubscription(pathAndInput, opts) {
435
- const enabled = opts?.enabled ?? true;
436
- const queryKey = reactQuery.hashQueryKey(pathAndInput);
437
- const { client } = useContext();
438
- const optsRef = React.useRef(opts);
439
- optsRef.current = opts;
440
- React.useEffect(()=>{
441
- if (!enabled) {
442
- return;
443
- }
444
- const [path, input] = pathAndInput;
445
- let isStopped = false;
446
- const subscription = client.subscription(path, input ?? undefined, {
447
- onStarted: ()=>{
448
- if (!isStopped) {
449
- optsRef.current.onStarted?.();
450
- }
451
- },
452
- onData: (data)=>{
453
- if (!isStopped) {
454
- // FIXME this shouldn't be needed as both should be `unknown` in next major
455
- optsRef.current.onData(data);
456
- }
457
- },
458
- onError: (err)=>{
459
- if (!isStopped) {
460
- optsRef.current.onError?.(err);
461
- }
462
- }
463
- });
464
- return ()=>{
465
- isStopped = true;
466
- subscription.unsubscribe();
467
- };
468
- // eslint-disable-next-line react-hooks/exhaustive-deps
469
- }, [
470
- queryKey,
471
- enabled
472
- ]);
473
- }
474
- function useInfiniteQuery(pathAndInput, opts) {
475
- const [path, input] = pathAndInput;
476
- const { client , ssrState , prefetchInfiniteQuery , queryClient , abortOnUnmount , } = useContext();
477
- const defaultOpts = queryClient.getQueryDefaults(getArrayQueryKey.getArrayQueryKey(pathAndInput, 'infinite'));
478
- if (typeof window === 'undefined' && ssrState === 'prepass' && opts?.trpc?.ssr !== false && (opts?.enabled ?? defaultOpts?.enabled) !== false && !queryClient.getQueryCache().find(getArrayQueryKey.getArrayQueryKey(pathAndInput, 'infinite'))) {
479
- void prefetchInfiniteQuery(pathAndInput, {
480
- ...defaultOpts,
481
- ...opts
482
- });
483
- }
484
- const ssrOpts = useSSRQueryOptionsIfNeeded(pathAndInput, 'infinite', {
485
- ...defaultOpts,
486
- ...opts
487
- });
488
- // request option should take priority over global
489
- const shouldAbortOnUnmount = opts?.trpc?.abortOnUnmount ?? abortOnUnmount;
490
- const hook = reactQuery.useInfiniteQuery({
491
- ...ssrOpts,
492
- queryKey: getArrayQueryKey.getArrayQueryKey(pathAndInput, 'infinite'),
493
- queryFn: (queryFunctionContext)=>{
494
- const actualOpts = {
495
- ...ssrOpts,
496
- trpc: {
497
- ...ssrOpts?.trpc,
498
- ...shouldAbortOnUnmount ? {
499
- signal: queryFunctionContext.signal
500
- } : {}
501
- }
502
- };
503
- const actualInput = {
504
- ...input ?? {},
505
- cursor: queryFunctionContext.pageParam ?? opts?.initialCursor
506
- };
507
- // FIXME as any shouldn't be needed as client should be untyped too
508
- return client.query(...getClientArgs([
509
- path,
510
- actualInput
511
- ], actualOpts));
512
- },
513
- context: ReactQueryContext
514
- });
515
- hook.trpc = useHookResult({
516
- path
517
- });
518
- return hook;
519
- }
520
- const useQueries = (queriesCallback, context)=>{
521
- const { ssrState , queryClient , prefetchQuery , client } = useContext();
522
- const proxy = createUseQueriesProxy(client);
523
- const queries = queriesCallback(proxy);
524
- if (typeof window === 'undefined' && ssrState === 'prepass') {
525
- for (const query of queries){
526
- const queryOption = query;
527
- if (queryOption.trpc?.ssr !== false && !queryClient.getQueryCache().find(getArrayQueryKey.getArrayQueryKey(queryOption.queryKey, 'query'))) {
528
- void prefetchQuery(queryOption.queryKey, queryOption);
529
- }
530
- }
531
- }
532
- return reactQuery.useQueries({
533
- queries: queries.map((query)=>({
534
- ...query,
535
- queryKey: getArrayQueryKey.getArrayQueryKey(query.queryKey, 'query')
536
- })),
537
- context
538
- });
539
- };
540
- const useDehydratedState = (client, trpcState)=>{
541
- const transformed = React.useMemo(()=>{
542
- if (!trpcState) {
543
- return trpcState;
544
- }
545
- return client.runtime.transformer.deserialize(trpcState);
546
- }, [
547
- trpcState,
548
- client
549
- ]);
550
- return transformed;
551
- };
552
- return {
553
- Provider: TRPCProvider,
554
- createClient,
555
- useContext,
556
- useUtils: useContext,
557
- useQuery,
558
- useQueries,
559
- useMutation,
560
- useSubscription,
561
- useDehydratedState,
562
- useInfiniteQuery
563
- };
564
- }
565
-
566
- /**
567
- * Create strongly typed react hooks
568
- * @internal
569
- * @deprecated
570
- */ function createHooksInternal(config) {
571
- return createRootHooks(config);
572
- }
573
-
574
- exports.TRPCContext = TRPCContext;
575
- exports.contextProps = contextProps;
576
- exports.createHooksInternal = createHooksInternal;
577
- exports.createReactProxyDecoration = createReactProxyDecoration;
578
- exports.createReactQueryUtilsProxy = createReactQueryUtilsProxy;
579
- exports.createRootHooks = createRootHooks;
580
- exports.createUseQueriesProxy = createUseQueriesProxy;
581
- exports.getClientArgs = getClientArgs;
582
- exports.getQueryKey = getQueryKey;