@trpc/react-query 10.28.0 → 10.28.2

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