@trpc/react-query 10.43.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
@@ -0,0 +1,435 @@
1
+ import { createRecursiveProxy } from '@trpc/server/shared';
2
+ import { useQuery, useSuspenseQuery, useQueryClient, useMutation, hashKey, useInfiniteQuery, useSuspenseInfiniteQuery, useQueries } from '@tanstack/react-query';
3
+ import { createTRPCUntypedClient } from '@trpc/client';
4
+ import React, { useRef, useState, useEffect, useCallback, useMemo } from 'react';
5
+ import { a as getQueryKeyInternal, T as TRPCContext } from './utilsProxy-7e396600.js';
6
+
7
+ /**
8
+ * Create proxy for decorating procedures
9
+ * @internal
10
+ */
11
+ function createReactDecoration(name, hooks) {
12
+ return createRecursiveProxy(({ path, args }) => {
13
+ const pathCopy = [name, ...path];
14
+ // The last arg is for instance `.useMutation` or `.useQuery()`
15
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
16
+ const lastArg = pathCopy.pop();
17
+ if (lastArg === 'useMutation') {
18
+ return hooks[lastArg](pathCopy, ...args);
19
+ }
20
+ if (lastArg === '_def') {
21
+ return {
22
+ path: pathCopy,
23
+ };
24
+ }
25
+ const [input, ...rest] = args;
26
+ const opts = rest[0] || {};
27
+ return hooks[lastArg](pathCopy, input, opts);
28
+ });
29
+ }
30
+
31
+ /**
32
+ * Create proxy for `useQueries` options
33
+ * @internal
34
+ */
35
+ function createUseQueries(client) {
36
+ return createRecursiveProxy((opts) => {
37
+ const arrayPath = opts.path;
38
+ const dotPath = arrayPath.join('.');
39
+ const [input, _opts] = opts.args;
40
+ const options = {
41
+ queryKey: getQueryKeyInternal(arrayPath, input, 'query'),
42
+ queryFn: () => {
43
+ return client.query(dotPath, input, _opts?.trpc);
44
+ },
45
+ ..._opts,
46
+ };
47
+ return options;
48
+ });
49
+ }
50
+
51
+ function getClientArgs(queryKey, opts, pageParam) {
52
+ const path = queryKey[0];
53
+ const input = queryKey[1]?.input;
54
+ if (pageParam)
55
+ input.cursor = pageParam;
56
+ return [path.join('.'), input, opts?.trpc];
57
+ }
58
+
59
+ /**
60
+ * Makes a stable reference of the `trpc` prop
61
+ */
62
+ function useHookResult(value) {
63
+ const ref = useRef(value);
64
+ ref.current.path = value.path;
65
+ return ref.current;
66
+ }
67
+
68
+ /**
69
+ * @internal
70
+ */
71
+ function createRootHooks(config) {
72
+ const mutationSuccessOverride = config?.overrides?.useMutation?.onSuccess ??
73
+ ((options) => options.originalFn());
74
+ const Context = (config?.context ??
75
+ TRPCContext);
76
+ const createClient = (opts) => {
77
+ return createTRPCUntypedClient(opts);
78
+ };
79
+ const TRPCProvider = (props) => {
80
+ const { abortOnUnmount = false, client, queryClient, ssrContext } = props;
81
+ const [ssrState, setSSRState] = useState(props.ssrState ?? false);
82
+ useEffect(() => {
83
+ // Only updating state to `mounted` if we are using SSR.
84
+ // This makes it so we don't have an unnecessary re-render when opting out of SSR.
85
+ setSSRState((state) => (state ? 'mounted' : false));
86
+ }, []);
87
+ return (React.createElement(Context.Provider, { value: {
88
+ abortOnUnmount,
89
+ queryClient,
90
+ client,
91
+ ssrContext: ssrContext ?? null,
92
+ ssrState,
93
+ fetchQuery: useCallback((queryKey, opts) => {
94
+ return queryClient.fetchQuery({
95
+ ...opts,
96
+ queryKey,
97
+ queryFn: () => client.query(...getClientArgs(queryKey, opts)),
98
+ });
99
+ }, [client, queryClient]),
100
+ fetchInfiniteQuery: useCallback((queryKey, opts) => {
101
+ return queryClient.fetchInfiniteQuery({
102
+ ...opts,
103
+ queryKey,
104
+ queryFn: ({ pageParam }) => {
105
+ return client.query(...getClientArgs(queryKey, opts, pageParam));
106
+ },
107
+ initialPageParam: opts?.initialCursor ?? null,
108
+ });
109
+ }, [client, queryClient]),
110
+ prefetchQuery: useCallback((queryKey, opts) => {
111
+ return queryClient.prefetchQuery({
112
+ ...opts,
113
+ queryKey,
114
+ queryFn: () => client.query(...getClientArgs(queryKey, opts)),
115
+ });
116
+ }, [client, queryClient]),
117
+ prefetchInfiniteQuery: useCallback((queryKey, opts) => {
118
+ return queryClient.prefetchInfiniteQuery({
119
+ ...opts,
120
+ queryKey,
121
+ queryFn: ({ pageParam }) => {
122
+ return client.query(...getClientArgs(queryKey, opts, pageParam));
123
+ },
124
+ initialPageParam: opts?.initialCursor ?? null,
125
+ });
126
+ }, [client, queryClient]),
127
+ ensureQueryData: useCallback((queryKey, opts) => {
128
+ return queryClient.ensureQueryData({
129
+ ...opts,
130
+ queryKey,
131
+ queryFn: () => client.query(...getClientArgs(queryKey, opts)),
132
+ });
133
+ }, [client, queryClient]),
134
+ invalidateQueries: useCallback((queryKey, filters, options) => {
135
+ return queryClient.invalidateQueries({
136
+ ...filters,
137
+ queryKey,
138
+ }, options);
139
+ }, [queryClient]),
140
+ resetQueries: useCallback((queryKey, filters, options) => {
141
+ return queryClient.resetQueries({
142
+ ...filters,
143
+ queryKey,
144
+ }, options);
145
+ }, [queryClient]),
146
+ refetchQueries: useCallback((queryKey, filters, options) => {
147
+ return queryClient.refetchQueries({
148
+ ...filters,
149
+ queryKey,
150
+ }, options);
151
+ }, [queryClient]),
152
+ cancelQuery: useCallback((queryKey, options) => {
153
+ return queryClient.cancelQueries({
154
+ queryKey,
155
+ }, options);
156
+ }, [queryClient]),
157
+ setQueryData: useCallback((queryKey, updater, options) => {
158
+ return queryClient.setQueryData(queryKey, updater, options);
159
+ }, [queryClient]),
160
+ getQueryData: useCallback((queryKey) => {
161
+ return queryClient.getQueryData(queryKey);
162
+ }, [queryClient]),
163
+ setInfiniteQueryData: useCallback((queryKey, updater, options) => {
164
+ return queryClient.setQueryData(queryKey, updater, options);
165
+ }, [queryClient]),
166
+ getInfiniteQueryData: useCallback((queryKey) => {
167
+ return queryClient.getQueryData(queryKey);
168
+ }, [queryClient]),
169
+ } }, props.children));
170
+ };
171
+ function useContext() {
172
+ const context = React.useContext(Context);
173
+ if (!context) {
174
+ throw new Error('Unable to find tRPC Context. Did you forget to wrap your App inside `withTRPC` HoC?');
175
+ }
176
+ return context;
177
+ }
178
+ /**
179
+ * Hack to make sure errors return `status`='error` when doing SSR
180
+ * @link https://github.com/trpc/trpc/pull/1645
181
+ */
182
+ function useSSRQueryOptionsIfNeeded(queryKey, opts) {
183
+ const { queryClient, ssrState } = useContext();
184
+ return ssrState &&
185
+ ssrState !== 'mounted' &&
186
+ queryClient.getQueryCache().find({ queryKey })?.state.status === 'error'
187
+ ? {
188
+ retryOnMount: false,
189
+ ...opts,
190
+ }
191
+ : opts;
192
+ }
193
+ function useQuery$1(path, input, opts) {
194
+ const context = useContext();
195
+ const { abortOnUnmount, client, ssrState, queryClient, prefetchQuery } = context;
196
+ const queryKey = getQueryKeyInternal(path, input, 'query');
197
+ const defaultOpts = queryClient.getQueryDefaults(queryKey);
198
+ if (typeof window === 'undefined' &&
199
+ ssrState === 'prepass' &&
200
+ opts?.trpc?.ssr !== false &&
201
+ (opts?.enabled ?? defaultOpts?.enabled) !== false &&
202
+ !queryClient.getQueryCache().find({ queryKey })) {
203
+ void prefetchQuery(queryKey, opts);
204
+ }
205
+ const ssrOpts = useSSRQueryOptionsIfNeeded(queryKey, {
206
+ ...defaultOpts,
207
+ ...opts,
208
+ });
209
+ const shouldAbortOnUnmount = opts?.trpc?.abortOnUnmount ?? config?.abortOnUnmount ?? abortOnUnmount;
210
+ const hook = useQuery({
211
+ ...ssrOpts,
212
+ queryKey: queryKey,
213
+ queryFn: (queryFunctionContext) => {
214
+ const actualOpts = {
215
+ ...ssrOpts,
216
+ trpc: {
217
+ ...ssrOpts?.trpc,
218
+ ...(shouldAbortOnUnmount
219
+ ? { signal: queryFunctionContext.signal }
220
+ : {}),
221
+ },
222
+ };
223
+ return client.query(...getClientArgs(queryKey, actualOpts));
224
+ },
225
+ }, queryClient);
226
+ hook.trpc = useHookResult({
227
+ path: path.join('.'),
228
+ });
229
+ return hook;
230
+ }
231
+ function useSuspenseQuery$1(path, input, opts) {
232
+ const context = useContext();
233
+ const queryKey = getQueryKeyInternal(path, input, 'query');
234
+ const shouldAbortOnUnmount = opts?.trpc?.abortOnUnmount ??
235
+ config?.abortOnUnmount ??
236
+ context.abortOnUnmount;
237
+ const hook = useSuspenseQuery({
238
+ ...opts,
239
+ queryKey: queryKey,
240
+ queryFn: (queryFunctionContext) => {
241
+ const actualOpts = {
242
+ trpc: {
243
+ ...(shouldAbortOnUnmount
244
+ ? { signal: queryFunctionContext.signal }
245
+ : {}),
246
+ },
247
+ };
248
+ return context.client.query(...getClientArgs(queryKey, actualOpts));
249
+ },
250
+ }, context.queryClient);
251
+ hook.trpc = useHookResult({
252
+ path: path.join('.'),
253
+ });
254
+ return [hook.data, hook];
255
+ }
256
+ function useMutation$1(path, opts) {
257
+ const { client } = useContext();
258
+ const queryClient = useQueryClient();
259
+ const mutationKey = [path];
260
+ const defaultOpts = queryClient.getMutationDefaults(mutationKey);
261
+ const hook = useMutation({
262
+ ...opts,
263
+ mutationKey: mutationKey,
264
+ mutationFn: (input) => {
265
+ return client.mutation(...getClientArgs([path, { input }], opts));
266
+ },
267
+ onSuccess(...args) {
268
+ const originalFn = () => opts?.onSuccess?.(...args) ?? defaultOpts?.onSuccess?.(...args);
269
+ return mutationSuccessOverride({
270
+ originalFn,
271
+ queryClient,
272
+ meta: opts?.meta ?? defaultOpts?.meta ?? {},
273
+ });
274
+ },
275
+ }, queryClient);
276
+ hook.trpc = useHookResult({
277
+ path: path.join('.'),
278
+ });
279
+ return hook;
280
+ }
281
+ /* istanbul ignore next -- @preserve */
282
+ function useSubscription(path, input, opts) {
283
+ const enabled = opts?.enabled ?? true;
284
+ const queryKey = hashKey(getQueryKeyInternal(path, input, 'any'));
285
+ const { client } = useContext();
286
+ const optsRef = useRef(opts);
287
+ optsRef.current = opts;
288
+ useEffect(() => {
289
+ if (!enabled) {
290
+ return;
291
+ }
292
+ let isStopped = false;
293
+ const subscription = client.subscription(path.join('.'), input ?? undefined, {
294
+ onStarted: () => {
295
+ if (!isStopped) {
296
+ optsRef.current.onStarted?.();
297
+ }
298
+ },
299
+ onData: (data) => {
300
+ if (!isStopped) {
301
+ opts.onData(data);
302
+ }
303
+ },
304
+ onError: (err) => {
305
+ if (!isStopped) {
306
+ optsRef.current.onError?.(err);
307
+ }
308
+ },
309
+ });
310
+ return () => {
311
+ isStopped = true;
312
+ subscription.unsubscribe();
313
+ };
314
+ // eslint-disable-next-line react-hooks/exhaustive-deps
315
+ }, [queryKey, enabled]);
316
+ }
317
+ function useInfiniteQuery$1(path, input, opts) {
318
+ const { client, ssrState, prefetchInfiniteQuery, queryClient, abortOnUnmount, } = useContext();
319
+ const queryKey = getQueryKeyInternal(path, input, 'infinite');
320
+ const defaultOpts = queryClient.getQueryDefaults(queryKey);
321
+ if (typeof window === 'undefined' &&
322
+ ssrState === 'prepass' &&
323
+ opts?.trpc?.ssr !== false &&
324
+ (opts?.enabled ?? defaultOpts?.enabled) !== false &&
325
+ !queryClient.getQueryCache().find({ queryKey })) {
326
+ void prefetchInfiniteQuery(queryKey, { ...defaultOpts, ...opts });
327
+ }
328
+ const ssrOpts = useSSRQueryOptionsIfNeeded(queryKey, {
329
+ ...defaultOpts,
330
+ ...opts,
331
+ });
332
+ // request option should take priority over global
333
+ const shouldAbortOnUnmount = opts?.trpc?.abortOnUnmount ?? abortOnUnmount;
334
+ const hook = useInfiniteQuery({
335
+ ...ssrOpts,
336
+ initialPageParam: opts.initialCursor ?? null,
337
+ persister: opts.persister,
338
+ queryKey: queryKey,
339
+ queryFn: (queryFunctionContext) => {
340
+ const actualOpts = {
341
+ ...ssrOpts,
342
+ trpc: {
343
+ ...ssrOpts?.trpc,
344
+ ...(shouldAbortOnUnmount
345
+ ? { signal: queryFunctionContext.signal }
346
+ : {}),
347
+ },
348
+ };
349
+ return client.query(...getClientArgs(queryKey, actualOpts, queryFunctionContext.pageParam ?? opts.initialCursor));
350
+ },
351
+ }, queryClient);
352
+ hook.trpc = useHookResult({
353
+ path: path.join('.'),
354
+ });
355
+ return hook;
356
+ }
357
+ function useSuspenseInfiniteQuery$1(path, input, opts) {
358
+ const context = useContext();
359
+ const queryKey = getQueryKeyInternal(path, input, 'infinite');
360
+ const defaultOpts = context.queryClient.getQueryDefaults(queryKey);
361
+ const ssrOpts = useSSRQueryOptionsIfNeeded(queryKey, {
362
+ ...defaultOpts,
363
+ ...opts,
364
+ });
365
+ // request option should take priority over global
366
+ const shouldAbortOnUnmount = opts?.trpc?.abortOnUnmount ?? context.abortOnUnmount;
367
+ const hook = useSuspenseInfiniteQuery({
368
+ ...opts,
369
+ initialPageParam: opts.initialCursor ?? null,
370
+ queryKey,
371
+ queryFn: (queryFunctionContext) => {
372
+ const actualOpts = {
373
+ ...ssrOpts,
374
+ trpc: {
375
+ ...ssrOpts?.trpc,
376
+ ...(shouldAbortOnUnmount
377
+ ? { signal: queryFunctionContext.signal }
378
+ : {}),
379
+ },
380
+ };
381
+ return context.client.query(...getClientArgs(queryKey, actualOpts, queryFunctionContext.pageParam ?? opts.initialCursor));
382
+ },
383
+ }, context.queryClient);
384
+ hook.trpc = useHookResult({
385
+ path: path.join('.'),
386
+ });
387
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
388
+ return [hook.data, hook];
389
+ }
390
+ const useQueries$1 = (queriesCallback) => {
391
+ const { ssrState, queryClient, prefetchQuery, client } = useContext();
392
+ const proxy = createUseQueries(client);
393
+ const queries = queriesCallback(proxy);
394
+ if (typeof window === 'undefined' && ssrState === 'prepass') {
395
+ for (const query of queries) {
396
+ const queryOption = query;
397
+ if (queryOption.trpc?.ssr !== false &&
398
+ !queryClient.getQueryCache().find({ queryKey: queryOption.queryKey })) {
399
+ void prefetchQuery(queryOption.queryKey, queryOption);
400
+ }
401
+ }
402
+ }
403
+ return useQueries({
404
+ queries: queries.map((query) => ({
405
+ ...query,
406
+ queryKey: query.queryKey,
407
+ })),
408
+ }, queryClient);
409
+ };
410
+ const useDehydratedState = (client, trpcState) => {
411
+ const transformed = useMemo(() => {
412
+ if (!trpcState) {
413
+ return trpcState;
414
+ }
415
+ return client.runtime.transformer.deserialize(trpcState);
416
+ }, [trpcState, client]);
417
+ return transformed;
418
+ };
419
+ return {
420
+ Provider: TRPCProvider,
421
+ createClient,
422
+ useContext,
423
+ useUtils: useContext,
424
+ useQuery: useQuery$1,
425
+ useSuspenseQuery: useSuspenseQuery$1,
426
+ useQueries: useQueries$1,
427
+ useMutation: useMutation$1,
428
+ useSubscription,
429
+ useDehydratedState,
430
+ useInfiniteQuery: useInfiniteQuery$1,
431
+ useSuspenseInfiniteQuery: useSuspenseInfiniteQuery$1,
432
+ };
433
+ }
434
+
435
+ export { createReactDecoration as a, createUseQueries as b, createRootHooks as c, getClientArgs as g };