@trpc/react-query 11.1.2 → 11.1.3-alpha-tmp-tsdown.23

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 (161) hide show
  1. package/dist/_virtual/rolldown_runtime.js +30 -0
  2. package/dist/createTRPCQueryUtils.d.mts +12 -0
  3. package/dist/createTRPCQueryUtils.d.mts.map +1 -0
  4. package/dist/createTRPCQueryUtils.d.ts +11 -3
  5. package/dist/createTRPCQueryUtils.d.ts.map +1 -1
  6. package/dist/createTRPCQueryUtils.js +7 -12
  7. package/dist/createTRPCQueryUtils.mjs +7 -9
  8. package/dist/createTRPCQueryUtils.mjs.map +1 -0
  9. package/dist/createTRPCReact.d.mts +143 -0
  10. package/dist/createTRPCReact.d.mts.map +1 -0
  11. package/dist/createTRPCReact.d.ts +92 -96
  12. package/dist/createTRPCReact.d.ts.map +1 -1
  13. package/dist/createTRPCReact.js +26 -54
  14. package/dist/createTRPCReact.mjs +26 -32
  15. package/dist/createTRPCReact.mjs.map +1 -0
  16. package/dist/index.d.mts +6 -0
  17. package/dist/index.d.ts +6 -6
  18. package/dist/index.js +11 -15
  19. package/dist/index.mjs +7 -4
  20. package/dist/internals/context.d.mts +164 -0
  21. package/dist/internals/context.d.mts.map +1 -0
  22. package/dist/internals/context.d.ts +146 -139
  23. package/dist/internals/context.d.ts.map +1 -1
  24. package/dist/internals/context.js +10 -28
  25. package/dist/internals/context.mjs +8 -5
  26. package/dist/internals/context.mjs.map +1 -0
  27. package/dist/internals/getClientArgs.d.mts +19 -0
  28. package/dist/internals/getClientArgs.d.mts.map +1 -0
  29. package/dist/internals/getClientArgs.d.ts +14 -4
  30. package/dist/internals/getClientArgs.d.ts.map +1 -1
  31. package/dist/internals/getClientArgs.js +18 -20
  32. package/dist/internals/getClientArgs.mjs +18 -18
  33. package/dist/internals/getClientArgs.mjs.map +1 -0
  34. package/dist/internals/getQueryKey.d.mts +45 -0
  35. package/dist/internals/getQueryKey.d.mts.map +1 -0
  36. package/dist/internals/getQueryKey.d.ts +22 -26
  37. package/dist/internals/getQueryKey.d.ts.map +1 -1
  38. package/dist/internals/getQueryKey.js +43 -62
  39. package/dist/internals/getQueryKey.mjs +42 -59
  40. package/dist/internals/getQueryKey.mjs.map +1 -0
  41. package/dist/internals/trpcResult.js +27 -53
  42. package/dist/internals/trpcResult.mjs +26 -31
  43. package/dist/internals/trpcResult.mjs.map +1 -0
  44. package/dist/internals/useQueries.d.mts +57 -0
  45. package/dist/internals/useQueries.d.mts.map +1 -0
  46. package/dist/internals/useQueries.d.ts +23 -24
  47. package/dist/internals/useQueries.d.ts.map +1 -1
  48. package/dist/rsc.d.mts +81 -0
  49. package/dist/rsc.d.mts.map +1 -0
  50. package/dist/rsc.d.ts +57 -56
  51. package/dist/rsc.d.ts.map +1 -1
  52. package/dist/rsc.js +66 -126
  53. package/dist/rsc.mjs +65 -104
  54. package/dist/rsc.mjs.map +1 -0
  55. package/dist/server/index.d.mts +2 -0
  56. package/dist/server/index.d.ts +2 -2
  57. package/dist/server/index.js +2 -6
  58. package/dist/server/index.mjs +3 -1
  59. package/dist/server/ssgProxy.d.mts +33 -0
  60. package/dist/server/ssgProxy.d.mts.map +1 -0
  61. package/dist/server/ssgProxy.d.ts +18 -16
  62. package/dist/server/ssgProxy.d.ts.map +1 -1
  63. package/dist/server/ssgProxy.js +130 -137
  64. package/dist/server/ssgProxy.mjs +129 -134
  65. package/dist/server/ssgProxy.mjs.map +1 -0
  66. package/dist/shared/hooks/createHooksInternal.d.mts +42 -0
  67. package/dist/shared/hooks/createHooksInternal.d.mts.map +1 -0
  68. package/dist/shared/hooks/createHooksInternal.d.ts +33 -22
  69. package/dist/shared/hooks/createHooksInternal.d.ts.map +1 -1
  70. package/dist/shared/hooks/createHooksInternal.js +427 -537
  71. package/dist/shared/hooks/createHooksInternal.mjs +426 -515
  72. package/dist/shared/hooks/createHooksInternal.mjs.map +1 -0
  73. package/dist/shared/hooks/createRootHooks.js +1 -0
  74. package/dist/shared/hooks/createRootHooks.mjs +1 -0
  75. package/dist/shared/hooks/types.d.mts +154 -0
  76. package/dist/shared/hooks/types.d.mts.map +1 -0
  77. package/dist/shared/hooks/types.d.ts +122 -125
  78. package/dist/shared/hooks/types.d.ts.map +1 -1
  79. package/dist/shared/index.d.mts +16 -0
  80. package/dist/shared/index.d.ts +16 -22
  81. package/dist/shared/index.js +17 -21
  82. package/dist/shared/index.mjs +9 -7
  83. package/dist/shared/polymorphism/mutationLike.d.mts +27 -0
  84. package/dist/shared/polymorphism/mutationLike.d.mts.map +1 -0
  85. package/dist/shared/polymorphism/mutationLike.d.ts +16 -6
  86. package/dist/shared/polymorphism/mutationLike.d.ts.map +1 -1
  87. package/dist/shared/polymorphism/queryLike.d.mts +31 -0
  88. package/dist/shared/polymorphism/queryLike.d.mts.map +1 -0
  89. package/dist/shared/polymorphism/queryLike.d.ts +20 -10
  90. package/dist/shared/polymorphism/queryLike.d.ts.map +1 -1
  91. package/dist/shared/polymorphism/routerLike.d.mts +19 -0
  92. package/dist/shared/polymorphism/routerLike.d.mts.map +1 -0
  93. package/dist/shared/polymorphism/routerLike.d.ts +15 -7
  94. package/dist/shared/polymorphism/routerLike.d.ts.map +1 -1
  95. package/dist/shared/polymorphism/utilsLike.d.mts +17 -0
  96. package/dist/shared/polymorphism/utilsLike.d.mts.map +1 -0
  97. package/dist/shared/polymorphism/utilsLike.d.ts +13 -3
  98. package/dist/shared/polymorphism/utilsLike.d.ts.map +1 -1
  99. package/dist/shared/proxy/decorationProxy.d.mts +19 -0
  100. package/dist/shared/proxy/decorationProxy.d.mts.map +1 -0
  101. package/dist/shared/proxy/decorationProxy.d.ts +14 -3
  102. package/dist/shared/proxy/decorationProxy.d.ts.map +1 -1
  103. package/dist/shared/proxy/decorationProxy.js +18 -26
  104. package/dist/shared/proxy/decorationProxy.mjs +17 -23
  105. package/dist/shared/proxy/decorationProxy.mjs.map +1 -0
  106. package/dist/shared/proxy/useQueriesProxy.d.mts +24 -0
  107. package/dist/shared/proxy/useQueriesProxy.d.mts.map +1 -0
  108. package/dist/shared/proxy/useQueriesProxy.d.ts +11 -12
  109. package/dist/shared/proxy/useQueriesProxy.d.ts.map +1 -1
  110. package/dist/shared/proxy/useQueriesProxy.js +25 -23
  111. package/dist/shared/proxy/useQueriesProxy.mjs +24 -20
  112. package/dist/shared/proxy/useQueriesProxy.mjs.map +1 -0
  113. package/dist/shared/proxy/utilsProxy.d.mts +143 -0
  114. package/dist/shared/proxy/utilsProxy.d.mts.map +1 -0
  115. package/dist/shared/proxy/utilsProxy.d.ts +123 -121
  116. package/dist/shared/proxy/utilsProxy.d.ts.map +1 -1
  117. package/dist/shared/proxy/utilsProxy.js +82 -95
  118. package/dist/shared/proxy/utilsProxy.mjs +81 -92
  119. package/dist/shared/proxy/utilsProxy.mjs.map +1 -0
  120. package/dist/shared/queryClient.d.mts +26 -0
  121. package/dist/shared/queryClient.d.mts.map +1 -0
  122. package/dist/shared/queryClient.d.ts +17 -8
  123. package/dist/shared/queryClient.d.ts.map +1 -1
  124. package/dist/shared/queryClient.js +8 -6
  125. package/dist/shared/queryClient.mjs +7 -3
  126. package/dist/shared/queryClient.mjs.map +1 -0
  127. package/dist/shared/types.d.mts +99 -0
  128. package/dist/shared/types.d.mts.map +1 -0
  129. package/dist/shared/types.d.ts +72 -71
  130. package/dist/shared/types.d.ts.map +1 -1
  131. package/dist/utils/createUtilityFunctions.d.mts +24 -0
  132. package/dist/utils/createUtilityFunctions.d.mts.map +1 -0
  133. package/dist/utils/createUtilityFunctions.d.ts +16 -15
  134. package/dist/utils/createUtilityFunctions.d.ts.map +1 -1
  135. package/dist/utils/createUtilityFunctions.js +166 -192
  136. package/dist/utils/createUtilityFunctions.mjs +165 -189
  137. package/dist/utils/createUtilityFunctions.mjs.map +1 -0
  138. package/dist/utils/inferReactQueryProcedure.d.mts +30 -0
  139. package/dist/utils/inferReactQueryProcedure.d.mts.map +1 -0
  140. package/dist/utils/inferReactQueryProcedure.d.ts +17 -12
  141. package/dist/utils/inferReactQueryProcedure.d.ts.map +1 -1
  142. package/package.json +48 -28
  143. package/rsc/package.json +1 -0
  144. package/server/index/package.json +1 -0
  145. package/server/package.json +1 -0
  146. package/shared/index/package.json +1 -0
  147. package/shared/package.json +1 -0
  148. package/src/createTRPCReact.tsx +0 -4
  149. package/src/shared/hooks/createHooksInternal.tsx +2 -0
  150. package/src/shared/hooks/types.ts +0 -2
  151. package/src/shared/proxy/decorationProxy.ts +1 -1
  152. package/src/shared/types.ts +2 -2
  153. package/dist/index.d.ts.map +0 -1
  154. package/dist/internals/trpcResult.d.ts +0 -18
  155. package/dist/internals/trpcResult.d.ts.map +0 -1
  156. package/dist/server/index.d.ts.map +0 -1
  157. package/dist/shared/hooks/createRootHooks.d.ts +0 -2
  158. package/dist/shared/hooks/createRootHooks.d.ts.map +0 -1
  159. package/dist/shared/index.d.ts.map +0 -1
  160. package/dist/shared/polymorphism/index.d.ts +0 -5
  161. package/dist/shared/polymorphism/index.d.ts.map +0 -1
@@ -1,521 +1,432 @@
1
- import { useSuspenseInfiniteQuery, usePrefetchInfiniteQuery, useInfiniteQuery, skipToken, hashKey, useMutation, useSuspenseQueries, useQueries, useSuspenseQuery, usePrefetchQuery, useQuery } from '@tanstack/react-query';
2
- import { createTRPCClient, TRPCUntypedClient, getUntypedClient } from '@trpc/client';
3
- import { isAsyncIterable } from '@trpc/server/unstable-core-do-not-import';
4
- import * as React from 'react';
5
- import { TRPCContext } from '../../internals/context.mjs';
6
- import { getClientArgs } from '../../internals/getClientArgs.mjs';
7
- import { getQueryKeyInternal, getMutationKeyInternal } from '../../internals/getQueryKey.mjs';
8
- import { useHookResult, buildQueryFromAsyncIterable } from '../../internals/trpcResult.mjs';
9
- import { createUtilityFunctions } from '../../utils/createUtilityFunctions.mjs';
10
- import { createUseQueries } from '../proxy/useQueriesProxy.mjs';
1
+ import { getMutationKeyInternal, getQueryKeyInternal } from "../../internals/getQueryKey.mjs";
2
+ import { TRPCContext } from "../../internals/context.mjs";
3
+ import { createUseQueries } from "../proxy/useQueriesProxy.mjs";
4
+ import { getClientArgs } from "../../internals/getClientArgs.mjs";
5
+ import { buildQueryFromAsyncIterable, useHookResult } from "../../internals/trpcResult.mjs";
6
+ import { createUtilityFunctions } from "../../utils/createUtilityFunctions.mjs";
7
+ import { TRPCUntypedClient, createTRPCClient, getUntypedClient } from "@trpc/client";
8
+ import { hashKey, skipToken, useInfiniteQuery, useMutation, usePrefetchInfiniteQuery, usePrefetchQuery, useQueries, useQuery, useSuspenseInfiniteQuery, useSuspenseQueries, useSuspenseQuery } from "@tanstack/react-query";
9
+ import { isAsyncIterable } from "@trpc/server/unstable-core-do-not-import";
10
+ import * as React from "react";
11
+ import { jsx } from "react/jsx-runtime";
11
12
 
12
- const trackResult = (result, onTrackResult)=>{
13
- const trackedResult = new Proxy(result, {
14
- get (target, prop) {
15
- onTrackResult(prop);
16
- return target[prop];
17
- }
18
- });
19
- return trackedResult;
13
+ //#region src/shared/hooks/createHooksInternal.tsx
14
+ const trackResult = (result, onTrackResult) => {
15
+ const trackedResult = new Proxy(result, { get(target, prop) {
16
+ onTrackResult(prop);
17
+ return target[prop];
18
+ } });
19
+ return trackedResult;
20
20
  };
21
21
  /**
22
- * @internal
23
- */ function createRootHooks(config) {
24
- const mutationSuccessOverride = config?.overrides?.useMutation?.onSuccess ?? ((options)=>options.originalFn());
25
- const Context = config?.context ?? TRPCContext;
26
- const createClient = createTRPCClient;
27
- const TRPCProvider = (props)=>{
28
- const { abortOnUnmount = false, queryClient, ssrContext } = props;
29
- const [ssrState, setSSRState] = React.useState(props.ssrState ?? false);
30
- const client = props.client instanceof TRPCUntypedClient ? props.client : getUntypedClient(props.client);
31
- const fns = React.useMemo(()=>createUtilityFunctions({
32
- client,
33
- queryClient
34
- }), [
35
- client,
36
- queryClient
37
- ]);
38
- const contextValue = React.useMemo(()=>({
39
- abortOnUnmount,
40
- queryClient,
41
- client,
42
- ssrContext: ssrContext ?? null,
43
- ssrState,
44
- ...fns
45
- }), [
46
- abortOnUnmount,
47
- client,
48
- fns,
49
- queryClient,
50
- ssrContext,
51
- ssrState
52
- ]);
53
- React.useEffect(()=>{
54
- // Only updating state to `mounted` if we are using SSR.
55
- // This makes it so we don't have an unnecessary re-render when opting out of SSR.
56
- setSSRState((state)=>state ? 'mounted' : false);
57
- }, []);
58
- return /*#__PURE__*/ React.createElement(Context.Provider, {
59
- value: contextValue
60
- }, props.children);
61
- };
62
- function useContext() {
63
- const context = React.useContext(Context);
64
- if (!context) {
65
- throw new Error('Unable to find tRPC Context. Did you forget to wrap your App inside `withTRPC` HoC?');
66
- }
67
- return context;
68
- }
69
- /**
70
- * Hack to make sure errors return `status`='error` when doing SSR
71
- * @see https://github.com/trpc/trpc/pull/1645
72
- */ function useSSRQueryOptionsIfNeeded(queryKey, opts) {
73
- const { queryClient, ssrState } = useContext();
74
- return ssrState && ssrState !== 'mounted' && queryClient.getQueryCache().find({
75
- queryKey
76
- })?.state.status === 'error' ? {
77
- retryOnMount: false,
78
- ...opts
79
- } : opts;
80
- }
81
- function useQuery$1(path, input, opts) {
82
- const context = useContext();
83
- const { abortOnUnmount, client, ssrState, queryClient, prefetchQuery } = context;
84
- const queryKey = getQueryKeyInternal(path, input, 'query');
85
- const defaultOpts = queryClient.getQueryDefaults(queryKey);
86
- const isInputSkipToken = input === skipToken;
87
- if (typeof window === 'undefined' && ssrState === 'prepass' && opts?.trpc?.ssr !== false && (opts?.enabled ?? defaultOpts?.enabled) !== false && !isInputSkipToken && !queryClient.getQueryCache().find({
88
- queryKey
89
- })) {
90
- void prefetchQuery(queryKey, opts);
91
- }
92
- const ssrOpts = useSSRQueryOptionsIfNeeded(queryKey, {
93
- ...defaultOpts,
94
- ...opts
95
- });
96
- const shouldAbortOnUnmount = opts?.trpc?.abortOnUnmount ?? config?.abortOnUnmount ?? abortOnUnmount;
97
- const hook = useQuery({
98
- ...ssrOpts,
99
- queryKey: queryKey,
100
- queryFn: isInputSkipToken ? input : async (queryFunctionContext)=>{
101
- const actualOpts = {
102
- ...ssrOpts,
103
- trpc: {
104
- ...ssrOpts?.trpc,
105
- ...shouldAbortOnUnmount ? {
106
- signal: queryFunctionContext.signal
107
- } : {
108
- signal: null
109
- }
110
- }
111
- };
112
- const result = await client.query(...getClientArgs(queryKey, actualOpts));
113
- if (isAsyncIterable(result)) {
114
- return buildQueryFromAsyncIterable(result, queryClient, queryKey);
115
- }
116
- return result;
117
- }
118
- }, queryClient);
119
- hook.trpc = useHookResult({
120
- path
121
- });
122
- return hook;
123
- }
124
- function usePrefetchQuery$1(path, input, opts) {
125
- const context = useContext();
126
- const queryKey = getQueryKeyInternal(path, input, 'query');
127
- const isInputSkipToken = input === skipToken;
128
- const shouldAbortOnUnmount = opts?.trpc?.abortOnUnmount ?? config?.abortOnUnmount ?? context.abortOnUnmount;
129
- usePrefetchQuery({
130
- ...opts,
131
- queryKey: queryKey,
132
- queryFn: isInputSkipToken ? input : (queryFunctionContext)=>{
133
- const actualOpts = {
134
- trpc: {
135
- ...opts?.trpc,
136
- ...shouldAbortOnUnmount ? {
137
- signal: queryFunctionContext.signal
138
- } : {}
139
- }
140
- };
141
- return context.client.query(...getClientArgs(queryKey, actualOpts));
142
- }
143
- });
144
- }
145
- function useSuspenseQuery$1(path, input, opts) {
146
- const context = useContext();
147
- const queryKey = getQueryKeyInternal(path, input, 'query');
148
- const shouldAbortOnUnmount = opts?.trpc?.abortOnUnmount ?? config?.abortOnUnmount ?? context.abortOnUnmount;
149
- const hook = useSuspenseQuery({
150
- ...opts,
151
- queryKey: queryKey,
152
- queryFn: (queryFunctionContext)=>{
153
- const actualOpts = {
154
- ...opts,
155
- trpc: {
156
- ...opts?.trpc,
157
- ...shouldAbortOnUnmount ? {
158
- signal: queryFunctionContext.signal
159
- } : {
160
- signal: null
161
- }
162
- }
163
- };
164
- return context.client.query(...getClientArgs(queryKey, actualOpts));
165
- }
166
- }, context.queryClient);
167
- hook.trpc = useHookResult({
168
- path
169
- });
170
- return [
171
- hook.data,
172
- hook
173
- ];
174
- }
175
- function useMutation$1(path, opts) {
176
- const { client, queryClient } = useContext();
177
- const mutationKey = getMutationKeyInternal(path);
178
- const defaultOpts = queryClient.defaultMutationOptions(queryClient.getMutationDefaults(mutationKey));
179
- const hook = useMutation({
180
- ...opts,
181
- mutationKey: mutationKey,
182
- mutationFn: (input)=>{
183
- return client.mutation(...getClientArgs([
184
- path,
185
- {
186
- input
187
- }
188
- ], opts));
189
- },
190
- onSuccess (...args) {
191
- const originalFn = ()=>opts?.onSuccess?.(...args) ?? defaultOpts?.onSuccess?.(...args);
192
- return mutationSuccessOverride({
193
- originalFn,
194
- queryClient,
195
- meta: opts?.meta ?? defaultOpts?.meta ?? {}
196
- });
197
- }
198
- }, queryClient);
199
- hook.trpc = useHookResult({
200
- path
201
- });
202
- return hook;
203
- }
204
- const initialStateIdle = {
205
- data: undefined,
206
- error: null,
207
- status: 'idle'
208
- };
209
- const initialStateConnecting = {
210
- data: undefined,
211
- error: null,
212
- status: 'connecting'
213
- };
214
- /* istanbul ignore next -- @preserve */ function useSubscription(path, input, opts) {
215
- const enabled = opts?.enabled ?? input !== skipToken;
216
- const queryKey = hashKey(getQueryKeyInternal(path, input, 'any'));
217
- const { client } = useContext();
218
- const optsRef = React.useRef(opts);
219
- React.useEffect(()=>{
220
- optsRef.current = opts;
221
- });
222
- const [trackedProps] = React.useState(new Set([]));
223
- const addTrackedProp = React.useCallback((key)=>{
224
- trackedProps.add(key);
225
- }, [
226
- trackedProps
227
- ]);
228
- const currentSubscriptionRef = React.useRef(null);
229
- const updateState = React.useCallback((callback)=>{
230
- const prev = resultRef.current;
231
- const next = resultRef.current = callback(prev);
232
- let shouldUpdate = false;
233
- for (const key of trackedProps){
234
- if (prev[key] !== next[key]) {
235
- shouldUpdate = true;
236
- break;
237
- }
238
- }
239
- if (shouldUpdate) {
240
- setState(trackResult(next, addTrackedProp));
241
- }
242
- }, [
243
- addTrackedProp,
244
- trackedProps
245
- ]);
246
- const reset = React.useCallback(()=>{
247
- // unsubscribe from the previous subscription
248
- currentSubscriptionRef.current?.unsubscribe();
249
- if (!enabled) {
250
- updateState(()=>({
251
- ...initialStateIdle,
252
- reset
253
- }));
254
- return;
255
- }
256
- updateState(()=>({
257
- ...initialStateConnecting,
258
- reset
259
- }));
260
- const subscription = client.subscription(path.join('.'), input ?? undefined, {
261
- onStarted: ()=>{
262
- optsRef.current.onStarted?.();
263
- updateState((prev)=>({
264
- ...prev,
265
- status: 'pending',
266
- error: null
267
- }));
268
- },
269
- onData: (data)=>{
270
- optsRef.current.onData?.(data);
271
- updateState((prev)=>({
272
- ...prev,
273
- status: 'pending',
274
- data,
275
- error: null
276
- }));
277
- },
278
- onError: (error)=>{
279
- optsRef.current.onError?.(error);
280
- updateState((prev)=>({
281
- ...prev,
282
- status: 'error',
283
- error
284
- }));
285
- },
286
- onConnectionStateChange: (result)=>{
287
- updateState((prev)=>{
288
- switch(result.state){
289
- case 'idle':
290
- return {
291
- ...prev,
292
- status: result.state,
293
- error: null,
294
- data: undefined
295
- };
296
- case 'connecting':
297
- return {
298
- ...prev,
299
- error: result.error,
300
- status: result.state
301
- };
302
- case 'pending':
303
- // handled when data is / onStarted
304
- return prev;
305
- }
306
- });
307
- },
308
- onComplete: ()=>{
309
- optsRef.current.onComplete?.();
310
- // In the case of WebSockets, the connection might not be idle so `onConnectionStateChange` will not be called until the connection is closed.
311
- // In this case, we need to set the state to idle manually.
312
- updateState((prev)=>({
313
- ...prev,
314
- status: 'idle',
315
- error: null,
316
- data: undefined
317
- }));
318
- // (We might want to add a `connectionState` to the state to track the connection state separately)
319
- }
320
- });
321
- currentSubscriptionRef.current = subscription;
322
- // eslint-disable-next-line react-hooks/exhaustive-deps
323
- }, [
324
- client,
325
- queryKey,
326
- enabled,
327
- updateState
328
- ]);
329
- React.useEffect(()=>{
330
- reset();
331
- return ()=>{
332
- currentSubscriptionRef.current?.unsubscribe();
333
- };
334
- }, [
335
- reset
336
- ]);
337
- const resultRef = React.useRef(enabled ? {
338
- ...initialStateConnecting,
339
- reset
340
- } : {
341
- ...initialStateIdle,
342
- reset
343
- });
344
- const [state, setState] = React.useState(trackResult(resultRef.current, addTrackedProp));
345
- return state;
346
- }
347
- function useInfiniteQuery$1(path, input, opts) {
348
- const { client, ssrState, prefetchInfiniteQuery, queryClient, abortOnUnmount } = useContext();
349
- const queryKey = getQueryKeyInternal(path, input, 'infinite');
350
- const defaultOpts = queryClient.getQueryDefaults(queryKey);
351
- const isInputSkipToken = input === skipToken;
352
- if (typeof window === 'undefined' && ssrState === 'prepass' && opts?.trpc?.ssr !== false && (opts?.enabled ?? defaultOpts?.enabled) !== false && !isInputSkipToken && !queryClient.getQueryCache().find({
353
- queryKey
354
- })) {
355
- void prefetchInfiniteQuery(queryKey, {
356
- ...defaultOpts,
357
- ...opts
358
- });
359
- }
360
- const ssrOpts = useSSRQueryOptionsIfNeeded(queryKey, {
361
- ...defaultOpts,
362
- ...opts
363
- });
364
- // request option should take priority over global
365
- const shouldAbortOnUnmount = opts?.trpc?.abortOnUnmount ?? abortOnUnmount;
366
- const hook = useInfiniteQuery({
367
- ...ssrOpts,
368
- initialPageParam: opts.initialCursor ?? null,
369
- persister: opts.persister,
370
- queryKey: queryKey,
371
- queryFn: isInputSkipToken ? input : (queryFunctionContext)=>{
372
- const actualOpts = {
373
- ...ssrOpts,
374
- trpc: {
375
- ...ssrOpts?.trpc,
376
- ...shouldAbortOnUnmount ? {
377
- signal: queryFunctionContext.signal
378
- } : {
379
- signal: null
380
- }
381
- }
382
- };
383
- return client.query(...getClientArgs(queryKey, actualOpts, {
384
- pageParam: queryFunctionContext.pageParam ?? opts.initialCursor,
385
- direction: queryFunctionContext.direction
386
- }));
387
- }
388
- }, queryClient);
389
- hook.trpc = useHookResult({
390
- path
391
- });
392
- return hook;
393
- }
394
- function usePrefetchInfiniteQuery$1(path, input, opts) {
395
- const context = useContext();
396
- const queryKey = getQueryKeyInternal(path, input, 'infinite');
397
- const defaultOpts = context.queryClient.getQueryDefaults(queryKey);
398
- const isInputSkipToken = input === skipToken;
399
- const ssrOpts = useSSRQueryOptionsIfNeeded(queryKey, {
400
- ...defaultOpts,
401
- ...opts
402
- });
403
- // request option should take priority over global
404
- const shouldAbortOnUnmount = opts?.trpc?.abortOnUnmount ?? context.abortOnUnmount;
405
- usePrefetchInfiniteQuery({
406
- ...opts,
407
- initialPageParam: opts.initialCursor ?? null,
408
- queryKey,
409
- queryFn: isInputSkipToken ? input : (queryFunctionContext)=>{
410
- const actualOpts = {
411
- ...ssrOpts,
412
- trpc: {
413
- ...ssrOpts?.trpc,
414
- ...shouldAbortOnUnmount ? {
415
- signal: queryFunctionContext.signal
416
- } : {}
417
- }
418
- };
419
- return context.client.query(...getClientArgs(queryKey, actualOpts, {
420
- pageParam: queryFunctionContext.pageParam ?? opts.initialCursor,
421
- direction: queryFunctionContext.direction
422
- }));
423
- }
424
- });
425
- }
426
- function useSuspenseInfiniteQuery$1(path, input, opts) {
427
- const context = useContext();
428
- const queryKey = getQueryKeyInternal(path, input, 'infinite');
429
- const defaultOpts = context.queryClient.getQueryDefaults(queryKey);
430
- const ssrOpts = useSSRQueryOptionsIfNeeded(queryKey, {
431
- ...defaultOpts,
432
- ...opts
433
- });
434
- // request option should take priority over global
435
- const shouldAbortOnUnmount = opts?.trpc?.abortOnUnmount ?? context.abortOnUnmount;
436
- const hook = useSuspenseInfiniteQuery({
437
- ...opts,
438
- initialPageParam: opts.initialCursor ?? null,
439
- queryKey,
440
- queryFn: (queryFunctionContext)=>{
441
- const actualOpts = {
442
- ...ssrOpts,
443
- trpc: {
444
- ...ssrOpts?.trpc,
445
- ...shouldAbortOnUnmount ? {
446
- signal: queryFunctionContext.signal
447
- } : {}
448
- }
449
- };
450
- return context.client.query(...getClientArgs(queryKey, actualOpts, {
451
- pageParam: queryFunctionContext.pageParam ?? opts.initialCursor,
452
- direction: queryFunctionContext.direction
453
- }));
454
- }
455
- }, context.queryClient);
456
- hook.trpc = useHookResult({
457
- path
458
- });
459
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
460
- return [
461
- hook.data,
462
- hook
463
- ];
464
- }
465
- const useQueries$1 = (queriesCallback, options)=>{
466
- const { ssrState, queryClient, prefetchQuery, client } = useContext();
467
- const proxy = createUseQueries(client);
468
- const queries = queriesCallback(proxy);
469
- if (typeof window === 'undefined' && ssrState === 'prepass') {
470
- for (const query of queries){
471
- const queryOption = query;
472
- if (queryOption.trpc?.ssr !== false && !queryClient.getQueryCache().find({
473
- queryKey: queryOption.queryKey
474
- })) {
475
- void prefetchQuery(queryOption.queryKey, queryOption);
476
- }
477
- }
478
- }
479
- return useQueries({
480
- queries: queries.map((query)=>({
481
- ...query,
482
- queryKey: query.queryKey
483
- })),
484
- combine: options?.combine
485
- }, queryClient);
486
- };
487
- const useSuspenseQueries$1 = (queriesCallback)=>{
488
- const { queryClient, client } = useContext();
489
- const proxy = createUseQueries(client);
490
- const queries = queriesCallback(proxy);
491
- const hook = useSuspenseQueries({
492
- queries: queries.map((query)=>({
493
- ...query,
494
- queryFn: query.queryFn,
495
- queryKey: query.queryKey
496
- }))
497
- }, queryClient);
498
- return [
499
- hook.map((h)=>h.data),
500
- hook
501
- ];
502
- };
503
- return {
504
- Provider: TRPCProvider,
505
- createClient,
506
- useContext,
507
- useUtils: useContext,
508
- useQuery: useQuery$1,
509
- usePrefetchQuery: usePrefetchQuery$1,
510
- useSuspenseQuery: useSuspenseQuery$1,
511
- useQueries: useQueries$1,
512
- useSuspenseQueries: useSuspenseQueries$1,
513
- useMutation: useMutation$1,
514
- useSubscription,
515
- useInfiniteQuery: useInfiniteQuery$1,
516
- usePrefetchInfiniteQuery: usePrefetchInfiniteQuery$1,
517
- useSuspenseInfiniteQuery: useSuspenseInfiniteQuery$1
518
- };
22
+ * @internal
23
+ */
24
+ function createRootHooks(config) {
25
+ const mutationSuccessOverride = config?.overrides?.useMutation?.onSuccess ?? ((options) => options.originalFn());
26
+ const Context = config?.context ?? TRPCContext;
27
+ const createClient = createTRPCClient;
28
+ const TRPCProvider = (props) => {
29
+ const { abortOnUnmount = false, queryClient, ssrContext } = props;
30
+ const [ssrState, setSSRState] = React.useState(props.ssrState ?? false);
31
+ const client = props.client instanceof TRPCUntypedClient ? props.client : getUntypedClient(props.client);
32
+ const fns = React.useMemo(() => createUtilityFunctions({
33
+ client,
34
+ queryClient
35
+ }), [client, queryClient]);
36
+ const contextValue = React.useMemo(() => ({
37
+ abortOnUnmount,
38
+ queryClient,
39
+ client,
40
+ ssrContext: ssrContext ?? null,
41
+ ssrState,
42
+ ...fns
43
+ }), [
44
+ abortOnUnmount,
45
+ client,
46
+ fns,
47
+ queryClient,
48
+ ssrContext,
49
+ ssrState
50
+ ]);
51
+ React.useEffect(() => {
52
+ setSSRState((state) => state ? "mounted" : false);
53
+ }, []);
54
+ return /* @__PURE__ */ jsx(Context.Provider, {
55
+ value: contextValue,
56
+ children: props.children
57
+ });
58
+ };
59
+ function useContext() {
60
+ const context = React.useContext(Context);
61
+ if (!context) throw new Error("Unable to find tRPC Context. Did you forget to wrap your App inside `withTRPC` HoC?");
62
+ return context;
63
+ }
64
+ /**
65
+ * Hack to make sure errors return `status`='error` when doing SSR
66
+ * @see https://github.com/trpc/trpc/pull/1645
67
+ */
68
+ function useSSRQueryOptionsIfNeeded(queryKey, opts) {
69
+ const { queryClient, ssrState } = useContext();
70
+ return ssrState && ssrState !== "mounted" && queryClient.getQueryCache().find({ queryKey })?.state.status === "error" ? {
71
+ retryOnMount: false,
72
+ ...opts
73
+ } : opts;
74
+ }
75
+ function useQuery$1(path, input, opts) {
76
+ const context = useContext();
77
+ const { abortOnUnmount, client, ssrState, queryClient, prefetchQuery } = context;
78
+ const queryKey = getQueryKeyInternal(path, input, "query");
79
+ const defaultOpts = queryClient.getQueryDefaults(queryKey);
80
+ const isInputSkipToken = input === skipToken;
81
+ if (typeof window === "undefined" && ssrState === "prepass" && opts?.trpc?.ssr !== false && (opts?.enabled ?? defaultOpts?.enabled) !== false && !isInputSkipToken && !queryClient.getQueryCache().find({ queryKey })) prefetchQuery(queryKey, opts);
82
+ const ssrOpts = useSSRQueryOptionsIfNeeded(queryKey, {
83
+ ...defaultOpts,
84
+ ...opts
85
+ });
86
+ const shouldAbortOnUnmount = opts?.trpc?.abortOnUnmount ?? config?.abortOnUnmount ?? abortOnUnmount;
87
+ const hook = useQuery({
88
+ ...ssrOpts,
89
+ queryKey,
90
+ queryFn: isInputSkipToken ? input : async (queryFunctionContext) => {
91
+ const actualOpts = {
92
+ ...ssrOpts,
93
+ trpc: {
94
+ ...ssrOpts?.trpc,
95
+ ...shouldAbortOnUnmount ? { signal: queryFunctionContext.signal } : { signal: null }
96
+ }
97
+ };
98
+ const result = await client.query(...getClientArgs(queryKey, actualOpts));
99
+ if (isAsyncIterable(result)) return buildQueryFromAsyncIterable(result, queryClient, queryKey);
100
+ return result;
101
+ }
102
+ }, queryClient);
103
+ hook.trpc = useHookResult({ path });
104
+ return hook;
105
+ }
106
+ function usePrefetchQuery$1(path, input, opts) {
107
+ const context = useContext();
108
+ const queryKey = getQueryKeyInternal(path, input, "query");
109
+ const isInputSkipToken = input === skipToken;
110
+ const shouldAbortOnUnmount = opts?.trpc?.abortOnUnmount ?? config?.abortOnUnmount ?? context.abortOnUnmount;
111
+ usePrefetchQuery({
112
+ ...opts,
113
+ queryKey,
114
+ queryFn: isInputSkipToken ? input : (queryFunctionContext) => {
115
+ const actualOpts = { trpc: {
116
+ ...opts?.trpc,
117
+ ...shouldAbortOnUnmount ? { signal: queryFunctionContext.signal } : {}
118
+ } };
119
+ return context.client.query(...getClientArgs(queryKey, actualOpts));
120
+ }
121
+ });
122
+ }
123
+ function useSuspenseQuery$1(path, input, opts) {
124
+ const context = useContext();
125
+ const queryKey = getQueryKeyInternal(path, input, "query");
126
+ const shouldAbortOnUnmount = opts?.trpc?.abortOnUnmount ?? config?.abortOnUnmount ?? context.abortOnUnmount;
127
+ const hook = useSuspenseQuery({
128
+ ...opts,
129
+ queryKey,
130
+ queryFn: (queryFunctionContext) => {
131
+ const actualOpts = {
132
+ ...opts,
133
+ trpc: {
134
+ ...opts?.trpc,
135
+ ...shouldAbortOnUnmount ? { signal: queryFunctionContext.signal } : { signal: null }
136
+ }
137
+ };
138
+ return context.client.query(...getClientArgs(queryKey, actualOpts));
139
+ }
140
+ }, context.queryClient);
141
+ hook.trpc = useHookResult({ path });
142
+ return [hook.data, hook];
143
+ }
144
+ function useMutation$1(path, opts) {
145
+ const { client, queryClient } = useContext();
146
+ const mutationKey = getMutationKeyInternal(path);
147
+ const defaultOpts = queryClient.defaultMutationOptions(queryClient.getMutationDefaults(mutationKey));
148
+ const hook = useMutation({
149
+ ...opts,
150
+ mutationKey,
151
+ mutationFn: (input) => {
152
+ return client.mutation(...getClientArgs([path, { input }], opts));
153
+ },
154
+ onSuccess(...args) {
155
+ const originalFn = () => opts?.onSuccess?.(...args) ?? defaultOpts?.onSuccess?.(...args);
156
+ return mutationSuccessOverride({
157
+ originalFn,
158
+ queryClient,
159
+ meta: opts?.meta ?? defaultOpts?.meta ?? {}
160
+ });
161
+ }
162
+ }, queryClient);
163
+ hook.trpc = useHookResult({ path });
164
+ return hook;
165
+ }
166
+ const initialStateIdle = {
167
+ data: void 0,
168
+ error: null,
169
+ status: "idle"
170
+ };
171
+ const initialStateConnecting = {
172
+ data: void 0,
173
+ error: null,
174
+ status: "connecting"
175
+ };
176
+ /* istanbul ignore next -- @preserve */
177
+ function useSubscription(path, input, opts) {
178
+ const enabled = opts?.enabled ?? input !== skipToken;
179
+ const queryKey = hashKey(getQueryKeyInternal(path, input, "any"));
180
+ const { client } = useContext();
181
+ const optsRef = React.useRef(opts);
182
+ React.useEffect(() => {
183
+ optsRef.current = opts;
184
+ });
185
+ const [trackedProps] = React.useState(new Set([]));
186
+ const addTrackedProp = React.useCallback((key) => {
187
+ trackedProps.add(key);
188
+ }, [trackedProps]);
189
+ const currentSubscriptionRef = React.useRef(null);
190
+ const updateState = React.useCallback((callback) => {
191
+ const prev = resultRef.current;
192
+ const next = resultRef.current = callback(prev);
193
+ let shouldUpdate = false;
194
+ for (const key of trackedProps) if (prev[key] !== next[key]) {
195
+ shouldUpdate = true;
196
+ break;
197
+ }
198
+ if (shouldUpdate) setState(trackResult(next, addTrackedProp));
199
+ }, [addTrackedProp, trackedProps]);
200
+ const reset = React.useCallback(() => {
201
+ currentSubscriptionRef.current?.unsubscribe();
202
+ if (!enabled) {
203
+ updateState(() => ({
204
+ ...initialStateIdle,
205
+ reset
206
+ }));
207
+ return;
208
+ }
209
+ updateState(() => ({
210
+ ...initialStateConnecting,
211
+ reset
212
+ }));
213
+ const subscription = client.subscription(path.join("."), input ?? void 0, {
214
+ onStarted: () => {
215
+ optsRef.current.onStarted?.();
216
+ updateState((prev) => ({
217
+ ...prev,
218
+ status: "pending",
219
+ error: null
220
+ }));
221
+ },
222
+ onData: (data) => {
223
+ optsRef.current.onData?.(data);
224
+ updateState((prev) => ({
225
+ ...prev,
226
+ status: "pending",
227
+ data,
228
+ error: null
229
+ }));
230
+ },
231
+ onError: (error) => {
232
+ optsRef.current.onError?.(error);
233
+ updateState((prev) => ({
234
+ ...prev,
235
+ status: "error",
236
+ error
237
+ }));
238
+ },
239
+ onConnectionStateChange: (result) => {
240
+ updateState((prev) => {
241
+ switch (result.state) {
242
+ case "idle": return {
243
+ ...prev,
244
+ status: result.state,
245
+ error: null,
246
+ data: void 0
247
+ };
248
+ case "connecting": return {
249
+ ...prev,
250
+ error: result.error,
251
+ status: result.state
252
+ };
253
+ case "pending": return prev;
254
+ }
255
+ });
256
+ },
257
+ onComplete: () => {
258
+ optsRef.current.onComplete?.();
259
+ updateState((prev) => ({
260
+ ...prev,
261
+ status: "idle",
262
+ error: null,
263
+ data: void 0
264
+ }));
265
+ }
266
+ });
267
+ currentSubscriptionRef.current = subscription;
268
+ }, [
269
+ client,
270
+ queryKey,
271
+ enabled,
272
+ updateState
273
+ ]);
274
+ React.useEffect(() => {
275
+ reset();
276
+ return () => {
277
+ currentSubscriptionRef.current?.unsubscribe();
278
+ };
279
+ }, [reset]);
280
+ const resultRef = React.useRef(enabled ? {
281
+ ...initialStateConnecting,
282
+ reset
283
+ } : {
284
+ ...initialStateIdle,
285
+ reset
286
+ });
287
+ const [state, setState] = React.useState(trackResult(resultRef.current, addTrackedProp));
288
+ return state;
289
+ }
290
+ function useInfiniteQuery$1(path, input, opts) {
291
+ const { client, ssrState, prefetchInfiniteQuery, queryClient, abortOnUnmount } = useContext();
292
+ const queryKey = getQueryKeyInternal(path, input, "infinite");
293
+ const defaultOpts = queryClient.getQueryDefaults(queryKey);
294
+ const isInputSkipToken = input === skipToken;
295
+ if (typeof window === "undefined" && ssrState === "prepass" && opts?.trpc?.ssr !== false && (opts?.enabled ?? defaultOpts?.enabled) !== false && !isInputSkipToken && !queryClient.getQueryCache().find({ queryKey })) prefetchInfiniteQuery(queryKey, {
296
+ ...defaultOpts,
297
+ ...opts
298
+ });
299
+ const ssrOpts = useSSRQueryOptionsIfNeeded(queryKey, {
300
+ ...defaultOpts,
301
+ ...opts
302
+ });
303
+ const shouldAbortOnUnmount = opts?.trpc?.abortOnUnmount ?? abortOnUnmount;
304
+ const hook = useInfiniteQuery({
305
+ ...ssrOpts,
306
+ initialPageParam: opts.initialCursor ?? null,
307
+ persister: opts.persister,
308
+ queryKey,
309
+ queryFn: isInputSkipToken ? input : (queryFunctionContext) => {
310
+ const actualOpts = {
311
+ ...ssrOpts,
312
+ trpc: {
313
+ ...ssrOpts?.trpc,
314
+ ...shouldAbortOnUnmount ? { signal: queryFunctionContext.signal } : { signal: null }
315
+ }
316
+ };
317
+ return client.query(...getClientArgs(queryKey, actualOpts, {
318
+ pageParam: queryFunctionContext.pageParam ?? opts.initialCursor,
319
+ direction: queryFunctionContext.direction
320
+ }));
321
+ }
322
+ }, queryClient);
323
+ hook.trpc = useHookResult({ path });
324
+ return hook;
325
+ }
326
+ function usePrefetchInfiniteQuery$1(path, input, opts) {
327
+ const context = useContext();
328
+ const queryKey = getQueryKeyInternal(path, input, "infinite");
329
+ const defaultOpts = context.queryClient.getQueryDefaults(queryKey);
330
+ const isInputSkipToken = input === skipToken;
331
+ const ssrOpts = useSSRQueryOptionsIfNeeded(queryKey, {
332
+ ...defaultOpts,
333
+ ...opts
334
+ });
335
+ const shouldAbortOnUnmount = opts?.trpc?.abortOnUnmount ?? context.abortOnUnmount;
336
+ usePrefetchInfiniteQuery({
337
+ ...opts,
338
+ initialPageParam: opts.initialCursor ?? null,
339
+ queryKey,
340
+ queryFn: isInputSkipToken ? input : (queryFunctionContext) => {
341
+ const actualOpts = {
342
+ ...ssrOpts,
343
+ trpc: {
344
+ ...ssrOpts?.trpc,
345
+ ...shouldAbortOnUnmount ? { signal: queryFunctionContext.signal } : {}
346
+ }
347
+ };
348
+ return context.client.query(...getClientArgs(queryKey, actualOpts, {
349
+ pageParam: queryFunctionContext.pageParam ?? opts.initialCursor,
350
+ direction: queryFunctionContext.direction
351
+ }));
352
+ }
353
+ });
354
+ }
355
+ function useSuspenseInfiniteQuery$1(path, input, opts) {
356
+ const context = useContext();
357
+ const queryKey = getQueryKeyInternal(path, input, "infinite");
358
+ const defaultOpts = context.queryClient.getQueryDefaults(queryKey);
359
+ const ssrOpts = useSSRQueryOptionsIfNeeded(queryKey, {
360
+ ...defaultOpts,
361
+ ...opts
362
+ });
363
+ const shouldAbortOnUnmount = opts?.trpc?.abortOnUnmount ?? context.abortOnUnmount;
364
+ const hook = useSuspenseInfiniteQuery({
365
+ ...opts,
366
+ initialPageParam: opts.initialCursor ?? null,
367
+ queryKey,
368
+ queryFn: (queryFunctionContext) => {
369
+ const actualOpts = {
370
+ ...ssrOpts,
371
+ trpc: {
372
+ ...ssrOpts?.trpc,
373
+ ...shouldAbortOnUnmount ? { signal: queryFunctionContext.signal } : {}
374
+ }
375
+ };
376
+ return context.client.query(...getClientArgs(queryKey, actualOpts, {
377
+ pageParam: queryFunctionContext.pageParam ?? opts.initialCursor,
378
+ direction: queryFunctionContext.direction
379
+ }));
380
+ }
381
+ }, context.queryClient);
382
+ hook.trpc = useHookResult({ path });
383
+ return [hook.data, hook];
384
+ }
385
+ const useQueries$1 = (queriesCallback, options) => {
386
+ const { ssrState, queryClient, prefetchQuery, client } = useContext();
387
+ const proxy = createUseQueries(client);
388
+ const queries = queriesCallback(proxy);
389
+ if (typeof window === "undefined" && ssrState === "prepass") for (const query of queries) {
390
+ const queryOption = query;
391
+ if (queryOption.trpc?.ssr !== false && !queryClient.getQueryCache().find({ queryKey: queryOption.queryKey })) prefetchQuery(queryOption.queryKey, queryOption);
392
+ }
393
+ return useQueries({
394
+ queries: queries.map((query) => ({
395
+ ...query,
396
+ queryKey: query.queryKey
397
+ })),
398
+ combine: options?.combine
399
+ }, queryClient);
400
+ };
401
+ const useSuspenseQueries$1 = (queriesCallback) => {
402
+ const { queryClient, client } = useContext();
403
+ const proxy = createUseQueries(client);
404
+ const queries = queriesCallback(proxy);
405
+ const hook = useSuspenseQueries({ queries: queries.map((query) => ({
406
+ ...query,
407
+ queryFn: query.queryFn,
408
+ queryKey: query.queryKey
409
+ })) }, queryClient);
410
+ return [hook.map((h) => h.data), hook];
411
+ };
412
+ return {
413
+ Provider: TRPCProvider,
414
+ createClient,
415
+ useContext,
416
+ useUtils: useContext,
417
+ useQuery: useQuery$1,
418
+ usePrefetchQuery: usePrefetchQuery$1,
419
+ useSuspenseQuery: useSuspenseQuery$1,
420
+ useQueries: useQueries$1,
421
+ useSuspenseQueries: useSuspenseQueries$1,
422
+ useMutation: useMutation$1,
423
+ useSubscription,
424
+ useInfiniteQuery: useInfiniteQuery$1,
425
+ usePrefetchInfiniteQuery: usePrefetchInfiniteQuery$1,
426
+ useSuspenseInfiniteQuery: useSuspenseInfiniteQuery$1
427
+ };
519
428
  }
520
429
 
430
+ //#endregion
521
431
  export { createRootHooks };
432
+ //# sourceMappingURL=createHooksInternal.mjs.map