@trpc/react-query 11.3.1 → 11.3.2-canary.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 (129) hide show
  1. package/dist/getQueryKey-CP8onRYT.cjs +104 -0
  2. package/dist/getQueryKey-Dekty44G.mjs +53 -0
  3. package/dist/getQueryKey-Dekty44G.mjs.map +1 -0
  4. package/dist/getQueryKey.d-C_PnqPni.d.cts +892 -0
  5. package/dist/getQueryKey.d-C_PnqPni.d.cts.map +1 -0
  6. package/dist/getQueryKey.d-CruH3ncI.d.mts +892 -0
  7. package/dist/getQueryKey.d-CruH3ncI.d.mts.map +1 -0
  8. package/dist/index.cjs +47 -0
  9. package/dist/index.d.cts +31 -0
  10. package/dist/index.d.cts.map +1 -0
  11. package/dist/index.d.mts +31 -0
  12. package/dist/index.d.mts.map +1 -0
  13. package/dist/index.mjs +40 -4
  14. package/dist/index.mjs.map +1 -0
  15. package/dist/rsc.cjs +69 -0
  16. package/dist/rsc.d.cts +80 -0
  17. package/dist/rsc.d.cts.map +1 -0
  18. package/dist/rsc.d.mts +80 -0
  19. package/dist/rsc.d.mts.map +1 -0
  20. package/dist/rsc.mjs +65 -104
  21. package/dist/rsc.mjs.map +1 -0
  22. package/dist/server/index.cjs +132 -0
  23. package/dist/server/index.d.cts +31 -0
  24. package/dist/server/index.d.cts.map +1 -0
  25. package/dist/server/index.d.mts +31 -0
  26. package/dist/server/index.d.mts.map +1 -0
  27. package/dist/server/index.mjs +133 -1
  28. package/dist/server/index.mjs.map +1 -0
  29. package/dist/shared/index.cjs +13 -0
  30. package/dist/shared/index.d.cts +2 -0
  31. package/dist/shared/index.d.mts +2 -0
  32. package/dist/shared/index.mjs +4 -7
  33. package/dist/shared-CMPBsqju.mjs +784 -0
  34. package/dist/shared-CMPBsqju.mjs.map +1 -0
  35. package/dist/shared-Doe-Gsbv.cjs +846 -0
  36. package/package.json +45 -26
  37. package/rsc/package.json +1 -0
  38. package/server/package.json +1 -0
  39. package/shared/package.json +1 -0
  40. package/dist/createTRPCQueryUtils.d.ts +0 -4
  41. package/dist/createTRPCQueryUtils.d.ts.map +0 -1
  42. package/dist/createTRPCQueryUtils.js +0 -16
  43. package/dist/createTRPCQueryUtils.mjs +0 -14
  44. package/dist/createTRPCReact.d.ts +0 -147
  45. package/dist/createTRPCReact.d.ts.map +0 -1
  46. package/dist/createTRPCReact.js +0 -60
  47. package/dist/createTRPCReact.mjs +0 -38
  48. package/dist/index.d.ts +0 -6
  49. package/dist/index.d.ts.map +0 -1
  50. package/dist/index.js +0 -19
  51. package/dist/internals/context.d.ts +0 -157
  52. package/dist/internals/context.d.ts.map +0 -1
  53. package/dist/internals/context.js +0 -33
  54. package/dist/internals/context.mjs +0 -11
  55. package/dist/internals/getClientArgs.d.ts +0 -9
  56. package/dist/internals/getClientArgs.d.ts.map +0 -1
  57. package/dist/internals/getClientArgs.js +0 -24
  58. package/dist/internals/getClientArgs.mjs +0 -22
  59. package/dist/internals/getQueryKey.d.ts +0 -49
  60. package/dist/internals/getQueryKey.d.ts.map +0 -1
  61. package/dist/internals/getQueryKey.js +0 -75
  62. package/dist/internals/getQueryKey.mjs +0 -70
  63. package/dist/internals/trpcResult.d.ts +0 -18
  64. package/dist/internals/trpcResult.d.ts.map +0 -1
  65. package/dist/internals/trpcResult.js +0 -63
  66. package/dist/internals/trpcResult.mjs +0 -40
  67. package/dist/internals/useQueries.d.ts +0 -58
  68. package/dist/internals/useQueries.d.ts.map +0 -1
  69. package/dist/rsc.d.ts +0 -80
  70. package/dist/rsc.d.ts.map +0 -1
  71. package/dist/rsc.js +0 -130
  72. package/dist/server/index.d.ts +0 -2
  73. package/dist/server/index.d.ts.map +0 -1
  74. package/dist/server/index.js +0 -7
  75. package/dist/server/ssgProxy.d.ts +0 -31
  76. package/dist/server/ssgProxy.d.ts.map +0 -1
  77. package/dist/server/ssgProxy.js +0 -141
  78. package/dist/server/ssgProxy.mjs +0 -139
  79. package/dist/shared/hooks/createHooksInternal.d.ts +0 -31
  80. package/dist/shared/hooks/createHooksInternal.d.ts.map +0 -1
  81. package/dist/shared/hooks/createHooksInternal.js +0 -543
  82. package/dist/shared/hooks/createHooksInternal.mjs +0 -522
  83. package/dist/shared/hooks/createRootHooks.d.ts +0 -2
  84. package/dist/shared/hooks/createRootHooks.d.ts.map +0 -1
  85. package/dist/shared/hooks/types.d.ts +0 -157
  86. package/dist/shared/hooks/types.d.ts.map +0 -1
  87. package/dist/shared/index.d.ts +0 -22
  88. package/dist/shared/index.d.ts.map +0 -1
  89. package/dist/shared/index.js +0 -22
  90. package/dist/shared/polymorphism/index.d.ts +0 -5
  91. package/dist/shared/polymorphism/index.d.ts.map +0 -1
  92. package/dist/shared/polymorphism/mutationLike.d.ts +0 -17
  93. package/dist/shared/polymorphism/mutationLike.d.ts.map +0 -1
  94. package/dist/shared/polymorphism/queryLike.d.ts +0 -21
  95. package/dist/shared/polymorphism/queryLike.d.ts.map +0 -1
  96. package/dist/shared/polymorphism/routerLike.d.ts +0 -11
  97. package/dist/shared/polymorphism/routerLike.d.ts.map +0 -1
  98. package/dist/shared/polymorphism/utilsLike.d.ts +0 -7
  99. package/dist/shared/polymorphism/utilsLike.d.ts.map +0 -1
  100. package/dist/shared/proxy/decorationProxy.d.ts +0 -8
  101. package/dist/shared/proxy/decorationProxy.d.ts.map +0 -1
  102. package/dist/shared/proxy/decorationProxy.js +0 -30
  103. package/dist/shared/proxy/decorationProxy.mjs +0 -28
  104. package/dist/shared/proxy/useQueriesProxy.d.ts +0 -25
  105. package/dist/shared/proxy/useQueriesProxy.d.ts.map +0 -1
  106. package/dist/shared/proxy/useQueriesProxy.js +0 -27
  107. package/dist/shared/proxy/useQueriesProxy.mjs +0 -25
  108. package/dist/shared/proxy/utilsProxy.d.ts +0 -141
  109. package/dist/shared/proxy/utilsProxy.d.ts.map +0 -1
  110. package/dist/shared/proxy/utilsProxy.js +0 -106
  111. package/dist/shared/proxy/utilsProxy.mjs +0 -102
  112. package/dist/shared/queryClient.d.ts +0 -17
  113. package/dist/shared/queryClient.d.ts.map +0 -1
  114. package/dist/shared/queryClient.js +0 -9
  115. package/dist/shared/queryClient.mjs +0 -7
  116. package/dist/shared/types.d.ts +0 -98
  117. package/dist/shared/types.d.ts.map +0 -1
  118. package/dist/utils/createUtilityFunctions.d.ts +0 -23
  119. package/dist/utils/createUtilityFunctions.d.ts.map +0 -1
  120. package/dist/utils/createUtilityFunctions.js +0 -196
  121. package/dist/utils/createUtilityFunctions.mjs +0 -194
  122. package/dist/utils/inferReactQueryProcedure.d.ts +0 -25
  123. package/dist/utils/inferReactQueryProcedure.d.ts.map +0 -1
  124. package/rsc/index.d.ts +0 -1
  125. package/rsc/index.js +0 -1
  126. package/server/index.d.ts +0 -1
  127. package/server/index.js +0 -1
  128. package/shared/index.d.ts +0 -1
  129. package/shared/index.js +0 -1
@@ -0,0 +1,784 @@
1
+ import { getMutationKeyInternal, getQueryKeyInternal } from "./getQueryKey-Dekty44G.mjs";
2
+ import { TRPCUntypedClient, createTRPCClient, createTRPCClientProxy, getUntypedClient } from "@trpc/client";
3
+ import { QueryClient, hashKey, infiniteQueryOptions, queryOptions, skipToken, useInfiniteQuery, useMutation, usePrefetchInfiniteQuery, usePrefetchQuery, useQueries, useQuery, useSuspenseInfiniteQuery, useSuspenseQueries, useSuspenseQuery } from "@tanstack/react-query";
4
+ import { createFlatProxy, createRecursiveProxy, isAsyncIterable } from "@trpc/server/unstable-core-do-not-import";
5
+ import * as React$2 from "react";
6
+ import * as React$1 from "react";
7
+ import * as React from "react";
8
+ import { jsx } from "react/jsx-runtime";
9
+
10
+ //#region src/shared/proxy/decorationProxy.ts
11
+ /**
12
+ * Create proxy for decorating procedures
13
+ * @internal
14
+ */
15
+ function createReactDecoration(hooks) {
16
+ return createRecursiveProxy(({ path, args }) => {
17
+ const pathCopy = [...path];
18
+ const lastArg = pathCopy.pop();
19
+ if (lastArg === "useMutation") return hooks[lastArg](pathCopy, ...args);
20
+ if (lastArg === "_def") return { path: pathCopy };
21
+ const [input, ...rest] = args;
22
+ const opts = rest[0] ?? {};
23
+ return hooks[lastArg](pathCopy, input, opts);
24
+ });
25
+ }
26
+
27
+ //#endregion
28
+ //#region src/internals/context.tsx
29
+ const contextProps = [
30
+ "client",
31
+ "ssrContext",
32
+ "ssrState",
33
+ "abortOnUnmount"
34
+ ];
35
+ const TRPCContext = React$2.createContext?.(null);
36
+
37
+ //#endregion
38
+ //#region src/shared/proxy/utilsProxy.ts
39
+ const getQueryType = (utilName) => {
40
+ switch (utilName) {
41
+ case "queryOptions":
42
+ case "fetch":
43
+ case "ensureData":
44
+ case "prefetch":
45
+ case "getData":
46
+ case "setData":
47
+ case "setQueriesData": return "query";
48
+ case "infiniteQueryOptions":
49
+ case "fetchInfinite":
50
+ case "prefetchInfinite":
51
+ case "getInfiniteData":
52
+ case "setInfiniteData": return "infinite";
53
+ case "setMutationDefaults":
54
+ case "getMutationDefaults":
55
+ case "isMutating":
56
+ case "cancel":
57
+ case "invalidate":
58
+ case "refetch":
59
+ case "reset": return "any";
60
+ }
61
+ };
62
+ /**
63
+ * @internal
64
+ */
65
+ function createRecursiveUtilsProxy(context) {
66
+ return createRecursiveProxy((opts) => {
67
+ const path = [...opts.path];
68
+ const utilName = path.pop();
69
+ const args = [...opts.args];
70
+ const input = args.shift();
71
+ const queryType = getQueryType(utilName);
72
+ const queryKey = getQueryKeyInternal(path, input, queryType);
73
+ const contextMap = {
74
+ infiniteQueryOptions: () => context.infiniteQueryOptions(path, queryKey, args[0]),
75
+ queryOptions: () => context.queryOptions(path, queryKey, ...args),
76
+ fetch: () => context.fetchQuery(queryKey, ...args),
77
+ fetchInfinite: () => context.fetchInfiniteQuery(queryKey, args[0]),
78
+ prefetch: () => context.prefetchQuery(queryKey, ...args),
79
+ prefetchInfinite: () => context.prefetchInfiniteQuery(queryKey, args[0]),
80
+ ensureData: () => context.ensureQueryData(queryKey, ...args),
81
+ invalidate: () => context.invalidateQueries(queryKey, ...args),
82
+ reset: () => context.resetQueries(queryKey, ...args),
83
+ refetch: () => context.refetchQueries(queryKey, ...args),
84
+ cancel: () => context.cancelQuery(queryKey, ...args),
85
+ setData: () => {
86
+ context.setQueryData(queryKey, args[0], args[1]);
87
+ },
88
+ setQueriesData: () => context.setQueriesData(queryKey, args[0], args[1], args[2]),
89
+ setInfiniteData: () => {
90
+ context.setInfiniteQueryData(queryKey, args[0], args[1]);
91
+ },
92
+ getData: () => context.getQueryData(queryKey),
93
+ getInfiniteData: () => context.getInfiniteQueryData(queryKey),
94
+ setMutationDefaults: () => context.setMutationDefaults(getMutationKeyInternal(path), input),
95
+ getMutationDefaults: () => context.getMutationDefaults(getMutationKeyInternal(path)),
96
+ isMutating: () => context.isMutating({ mutationKey: getMutationKeyInternal(path) })
97
+ };
98
+ return contextMap[utilName]();
99
+ });
100
+ }
101
+ /**
102
+ * @internal
103
+ */
104
+ function createReactQueryUtils(context) {
105
+ const clientProxy = createTRPCClientProxy(context.client);
106
+ const proxy = createRecursiveUtilsProxy(context);
107
+ return createFlatProxy((key) => {
108
+ const contextName = key;
109
+ if (contextName === "client") return clientProxy;
110
+ if (contextProps.includes(contextName)) return context[contextName];
111
+ return proxy[key];
112
+ });
113
+ }
114
+ /**
115
+ * @internal
116
+ */
117
+ function createQueryUtilsProxy(context) {
118
+ return createRecursiveUtilsProxy(context);
119
+ }
120
+
121
+ //#endregion
122
+ //#region src/shared/proxy/useQueriesProxy.ts
123
+ /**
124
+ * Create proxy for `useQueries` options
125
+ * @internal
126
+ */
127
+ function createUseQueries(client) {
128
+ const untypedClient = client instanceof TRPCUntypedClient ? client : getUntypedClient(client);
129
+ return createRecursiveProxy((opts) => {
130
+ const arrayPath = opts.path;
131
+ const dotPath = arrayPath.join(".");
132
+ const [input, _opts] = opts.args;
133
+ const options = {
134
+ queryKey: getQueryKeyInternal(arrayPath, input, "query"),
135
+ queryFn: () => {
136
+ return untypedClient.query(dotPath, input, _opts?.trpc);
137
+ },
138
+ ..._opts
139
+ };
140
+ return options;
141
+ });
142
+ }
143
+
144
+ //#endregion
145
+ //#region src/internals/getClientArgs.ts
146
+ /**
147
+ * @internal
148
+ */
149
+ function getClientArgs(queryKey, opts, infiniteParams) {
150
+ const path = queryKey[0];
151
+ let input = queryKey[1]?.input;
152
+ if (infiniteParams) input = {
153
+ ...input ?? {},
154
+ ...infiniteParams.pageParam ? { cursor: infiniteParams.pageParam } : {},
155
+ direction: infiniteParams.direction
156
+ };
157
+ return [
158
+ path.join("."),
159
+ input,
160
+ opts?.trpc
161
+ ];
162
+ }
163
+
164
+ //#endregion
165
+ //#region src/internals/trpcResult.ts
166
+ function createTRPCOptionsResult(value) {
167
+ const path = value.path.join(".");
168
+ return { path };
169
+ }
170
+ /**
171
+ * Makes a stable reference of the `trpc` prop
172
+ */
173
+ function useHookResult(value) {
174
+ const result = createTRPCOptionsResult(value);
175
+ return React$1.useMemo(() => result, [result]);
176
+ }
177
+ /**
178
+ * @internal
179
+ */
180
+ async function buildQueryFromAsyncIterable(asyncIterable, queryClient, queryKey) {
181
+ const queryCache = queryClient.getQueryCache();
182
+ const query = queryCache.build(queryClient, { queryKey });
183
+ query.setState({
184
+ data: [],
185
+ status: "success"
186
+ });
187
+ const aggregate = [];
188
+ for await (const value of asyncIterable) {
189
+ aggregate.push(value);
190
+ query.setState({ data: [...aggregate] });
191
+ }
192
+ return aggregate;
193
+ }
194
+
195
+ //#endregion
196
+ //#region src/utils/createUtilityFunctions.ts
197
+ /**
198
+ * Creates a set of utility functions that can be used to interact with `react-query`
199
+ * @param opts the `TRPCClient` and `QueryClient` to use
200
+ * @returns a set of utility functions that can be used to interact with `react-query`
201
+ * @internal
202
+ */
203
+ function createUtilityFunctions(opts) {
204
+ const { client, queryClient } = opts;
205
+ const untypedClient = client instanceof TRPCUntypedClient ? client : getUntypedClient(client);
206
+ return {
207
+ infiniteQueryOptions: (path, queryKey, opts$1) => {
208
+ const inputIsSkipToken = queryKey[1]?.input === skipToken;
209
+ const queryFn = async (queryFnContext) => {
210
+ const actualOpts = {
211
+ ...opts$1,
212
+ trpc: {
213
+ ...opts$1?.trpc,
214
+ ...opts$1?.trpc?.abortOnUnmount ? { signal: queryFnContext.signal } : { signal: null }
215
+ }
216
+ };
217
+ const result = await untypedClient.query(...getClientArgs(queryKey, actualOpts, {
218
+ direction: queryFnContext.direction,
219
+ pageParam: queryFnContext.pageParam
220
+ }));
221
+ return result;
222
+ };
223
+ return Object.assign(infiniteQueryOptions({
224
+ ...opts$1,
225
+ initialData: opts$1?.initialData,
226
+ queryKey,
227
+ queryFn: inputIsSkipToken ? skipToken : queryFn,
228
+ initialPageParam: opts$1?.initialCursor ?? null
229
+ }), { trpc: createTRPCOptionsResult({ path }) });
230
+ },
231
+ queryOptions: (path, queryKey, opts$1) => {
232
+ const inputIsSkipToken = queryKey[1]?.input === skipToken;
233
+ const queryFn = async (queryFnContext) => {
234
+ const actualOpts = {
235
+ ...opts$1,
236
+ trpc: {
237
+ ...opts$1?.trpc,
238
+ ...opts$1?.trpc?.abortOnUnmount ? { signal: queryFnContext.signal } : { signal: null }
239
+ }
240
+ };
241
+ const result = await untypedClient.query(...getClientArgs(queryKey, actualOpts));
242
+ if (isAsyncIterable(result)) return buildQueryFromAsyncIterable(result, queryClient, queryKey);
243
+ return result;
244
+ };
245
+ return Object.assign(queryOptions({
246
+ ...opts$1,
247
+ initialData: opts$1?.initialData,
248
+ queryKey,
249
+ queryFn: inputIsSkipToken ? skipToken : queryFn
250
+ }), { trpc: createTRPCOptionsResult({ path }) });
251
+ },
252
+ fetchQuery: (queryKey, opts$1) => {
253
+ return queryClient.fetchQuery({
254
+ ...opts$1,
255
+ queryKey,
256
+ queryFn: () => untypedClient.query(...getClientArgs(queryKey, opts$1))
257
+ });
258
+ },
259
+ fetchInfiniteQuery: (queryKey, opts$1) => {
260
+ return queryClient.fetchInfiniteQuery({
261
+ ...opts$1,
262
+ queryKey,
263
+ queryFn: ({ pageParam, direction }) => {
264
+ return untypedClient.query(...getClientArgs(queryKey, opts$1, {
265
+ pageParam,
266
+ direction
267
+ }));
268
+ },
269
+ initialPageParam: opts$1?.initialCursor ?? null
270
+ });
271
+ },
272
+ prefetchQuery: (queryKey, opts$1) => {
273
+ return queryClient.prefetchQuery({
274
+ ...opts$1,
275
+ queryKey,
276
+ queryFn: () => untypedClient.query(...getClientArgs(queryKey, opts$1))
277
+ });
278
+ },
279
+ prefetchInfiniteQuery: (queryKey, opts$1) => {
280
+ return queryClient.prefetchInfiniteQuery({
281
+ ...opts$1,
282
+ queryKey,
283
+ queryFn: ({ pageParam, direction }) => {
284
+ return untypedClient.query(...getClientArgs(queryKey, opts$1, {
285
+ pageParam,
286
+ direction
287
+ }));
288
+ },
289
+ initialPageParam: opts$1?.initialCursor ?? null
290
+ });
291
+ },
292
+ ensureQueryData: (queryKey, opts$1) => {
293
+ return queryClient.ensureQueryData({
294
+ ...opts$1,
295
+ queryKey,
296
+ queryFn: () => untypedClient.query(...getClientArgs(queryKey, opts$1))
297
+ });
298
+ },
299
+ invalidateQueries: (queryKey, filters, options) => {
300
+ return queryClient.invalidateQueries({
301
+ ...filters,
302
+ queryKey
303
+ }, options);
304
+ },
305
+ resetQueries: (queryKey, filters, options) => {
306
+ return queryClient.resetQueries({
307
+ ...filters,
308
+ queryKey
309
+ }, options);
310
+ },
311
+ refetchQueries: (queryKey, filters, options) => {
312
+ return queryClient.refetchQueries({
313
+ ...filters,
314
+ queryKey
315
+ }, options);
316
+ },
317
+ cancelQuery: (queryKey, options) => {
318
+ return queryClient.cancelQueries({ queryKey }, options);
319
+ },
320
+ setQueryData: (queryKey, updater, options) => {
321
+ return queryClient.setQueryData(queryKey, updater, options);
322
+ },
323
+ setQueriesData: (queryKey, filters, updater, options) => {
324
+ return queryClient.setQueriesData({
325
+ ...filters,
326
+ queryKey
327
+ }, updater, options);
328
+ },
329
+ getQueryData: (queryKey) => {
330
+ return queryClient.getQueryData(queryKey);
331
+ },
332
+ setInfiniteQueryData: (queryKey, updater, options) => {
333
+ return queryClient.setQueryData(queryKey, updater, options);
334
+ },
335
+ getInfiniteQueryData: (queryKey) => {
336
+ return queryClient.getQueryData(queryKey);
337
+ },
338
+ setMutationDefaults: (mutationKey, options) => {
339
+ const path = mutationKey[0];
340
+ const canonicalMutationFn = (input) => {
341
+ return untypedClient.mutation(...getClientArgs([path, { input }], opts));
342
+ };
343
+ return queryClient.setMutationDefaults(mutationKey, typeof options === "function" ? options({ canonicalMutationFn }) : options);
344
+ },
345
+ getMutationDefaults: (mutationKey) => {
346
+ return queryClient.getMutationDefaults(mutationKey);
347
+ },
348
+ isMutating: (filters) => {
349
+ return queryClient.isMutating({
350
+ ...filters,
351
+ exact: true
352
+ });
353
+ }
354
+ };
355
+ }
356
+
357
+ //#endregion
358
+ //#region src/shared/hooks/createHooksInternal.tsx
359
+ const trackResult = (result, onTrackResult) => {
360
+ const trackedResult = new Proxy(result, { get(target, prop) {
361
+ onTrackResult(prop);
362
+ return target[prop];
363
+ } });
364
+ return trackedResult;
365
+ };
366
+ /**
367
+ * @internal
368
+ */
369
+ function createRootHooks(config) {
370
+ const mutationSuccessOverride = config?.overrides?.useMutation?.onSuccess ?? ((options) => options.originalFn());
371
+ const Context = config?.context ?? TRPCContext;
372
+ const createClient = createTRPCClient;
373
+ const TRPCProvider = (props) => {
374
+ const { abortOnUnmount = false, queryClient, ssrContext } = props;
375
+ const [ssrState, setSSRState] = React.useState(props.ssrState ?? false);
376
+ const client = props.client instanceof TRPCUntypedClient ? props.client : getUntypedClient(props.client);
377
+ const fns = React.useMemo(() => createUtilityFunctions({
378
+ client,
379
+ queryClient
380
+ }), [client, queryClient]);
381
+ const contextValue = React.useMemo(() => ({
382
+ abortOnUnmount,
383
+ queryClient,
384
+ client,
385
+ ssrContext: ssrContext ?? null,
386
+ ssrState,
387
+ ...fns
388
+ }), [
389
+ abortOnUnmount,
390
+ client,
391
+ fns,
392
+ queryClient,
393
+ ssrContext,
394
+ ssrState
395
+ ]);
396
+ React.useEffect(() => {
397
+ setSSRState((state) => state ? "mounted" : false);
398
+ }, []);
399
+ return /* @__PURE__ */ jsx(Context.Provider, {
400
+ value: contextValue,
401
+ children: props.children
402
+ });
403
+ };
404
+ function useContext() {
405
+ const context = React.useContext(Context);
406
+ if (!context) throw new Error("Unable to find tRPC Context. Did you forget to wrap your App inside `withTRPC` HoC?");
407
+ return context;
408
+ }
409
+ /**
410
+ * Hack to make sure errors return `status`='error` when doing SSR
411
+ * @see https://github.com/trpc/trpc/pull/1645
412
+ */
413
+ function useSSRQueryOptionsIfNeeded(queryKey, opts) {
414
+ const { queryClient, ssrState } = useContext();
415
+ return ssrState && ssrState !== "mounted" && queryClient.getQueryCache().find({ queryKey })?.state.status === "error" ? {
416
+ retryOnMount: false,
417
+ ...opts
418
+ } : opts;
419
+ }
420
+ function useQuery$1(path, input, opts) {
421
+ const context = useContext();
422
+ const { abortOnUnmount, client, ssrState, queryClient, prefetchQuery } = context;
423
+ const queryKey = getQueryKeyInternal(path, input, "query");
424
+ const defaultOpts = queryClient.getQueryDefaults(queryKey);
425
+ const isInputSkipToken = input === skipToken;
426
+ if (typeof window === "undefined" && ssrState === "prepass" && opts?.trpc?.ssr !== false && (opts?.enabled ?? defaultOpts?.enabled) !== false && !isInputSkipToken && !queryClient.getQueryCache().find({ queryKey })) prefetchQuery(queryKey, opts);
427
+ const ssrOpts = useSSRQueryOptionsIfNeeded(queryKey, {
428
+ ...defaultOpts,
429
+ ...opts
430
+ });
431
+ const shouldAbortOnUnmount = opts?.trpc?.abortOnUnmount ?? config?.abortOnUnmount ?? abortOnUnmount;
432
+ const hook = useQuery({
433
+ ...ssrOpts,
434
+ queryKey,
435
+ queryFn: isInputSkipToken ? input : async (queryFunctionContext) => {
436
+ const actualOpts = {
437
+ ...ssrOpts,
438
+ trpc: {
439
+ ...ssrOpts?.trpc,
440
+ ...shouldAbortOnUnmount ? { signal: queryFunctionContext.signal } : { signal: null }
441
+ }
442
+ };
443
+ const result = await client.query(...getClientArgs(queryKey, actualOpts));
444
+ if (isAsyncIterable(result)) return buildQueryFromAsyncIterable(result, queryClient, queryKey);
445
+ return result;
446
+ }
447
+ }, queryClient);
448
+ hook.trpc = useHookResult({ path });
449
+ return hook;
450
+ }
451
+ function usePrefetchQuery$1(path, input, opts) {
452
+ const context = useContext();
453
+ const queryKey = getQueryKeyInternal(path, input, "query");
454
+ const isInputSkipToken = input === skipToken;
455
+ const shouldAbortOnUnmount = opts?.trpc?.abortOnUnmount ?? config?.abortOnUnmount ?? context.abortOnUnmount;
456
+ usePrefetchQuery({
457
+ ...opts,
458
+ queryKey,
459
+ queryFn: isInputSkipToken ? input : (queryFunctionContext) => {
460
+ const actualOpts = { trpc: {
461
+ ...opts?.trpc,
462
+ ...shouldAbortOnUnmount ? { signal: queryFunctionContext.signal } : {}
463
+ } };
464
+ return context.client.query(...getClientArgs(queryKey, actualOpts));
465
+ }
466
+ });
467
+ }
468
+ function useSuspenseQuery$1(path, input, opts) {
469
+ const context = useContext();
470
+ const queryKey = getQueryKeyInternal(path, input, "query");
471
+ const shouldAbortOnUnmount = opts?.trpc?.abortOnUnmount ?? config?.abortOnUnmount ?? context.abortOnUnmount;
472
+ const hook = useSuspenseQuery({
473
+ ...opts,
474
+ queryKey,
475
+ queryFn: (queryFunctionContext) => {
476
+ const actualOpts = {
477
+ ...opts,
478
+ trpc: {
479
+ ...opts?.trpc,
480
+ ...shouldAbortOnUnmount ? { signal: queryFunctionContext.signal } : { signal: null }
481
+ }
482
+ };
483
+ return context.client.query(...getClientArgs(queryKey, actualOpts));
484
+ }
485
+ }, context.queryClient);
486
+ hook.trpc = useHookResult({ path });
487
+ return [hook.data, hook];
488
+ }
489
+ function useMutation$1(path, opts) {
490
+ const { client, queryClient } = useContext();
491
+ const mutationKey = getMutationKeyInternal(path);
492
+ const defaultOpts = queryClient.defaultMutationOptions(queryClient.getMutationDefaults(mutationKey));
493
+ const hook = useMutation({
494
+ ...opts,
495
+ mutationKey,
496
+ mutationFn: (input) => {
497
+ return client.mutation(...getClientArgs([path, { input }], opts));
498
+ },
499
+ onSuccess(...args) {
500
+ const originalFn = () => opts?.onSuccess?.(...args) ?? defaultOpts?.onSuccess?.(...args);
501
+ return mutationSuccessOverride({
502
+ originalFn,
503
+ queryClient,
504
+ meta: opts?.meta ?? defaultOpts?.meta ?? {}
505
+ });
506
+ }
507
+ }, queryClient);
508
+ hook.trpc = useHookResult({ path });
509
+ return hook;
510
+ }
511
+ const initialStateIdle = {
512
+ data: void 0,
513
+ error: null,
514
+ status: "idle"
515
+ };
516
+ const initialStateConnecting = {
517
+ data: void 0,
518
+ error: null,
519
+ status: "connecting"
520
+ };
521
+ /* istanbul ignore next -- @preserve */
522
+ function useSubscription(path, input, opts) {
523
+ const enabled = opts?.enabled ?? input !== skipToken;
524
+ const queryKey = hashKey(getQueryKeyInternal(path, input, "any"));
525
+ const { client } = useContext();
526
+ const optsRef = React.useRef(opts);
527
+ React.useEffect(() => {
528
+ optsRef.current = opts;
529
+ });
530
+ const [trackedProps] = React.useState(new Set([]));
531
+ const addTrackedProp = React.useCallback((key) => {
532
+ trackedProps.add(key);
533
+ }, [trackedProps]);
534
+ const currentSubscriptionRef = React.useRef(null);
535
+ const updateState = React.useCallback((callback) => {
536
+ const prev = resultRef.current;
537
+ const next = resultRef.current = callback(prev);
538
+ let shouldUpdate = false;
539
+ for (const key of trackedProps) if (prev[key] !== next[key]) {
540
+ shouldUpdate = true;
541
+ break;
542
+ }
543
+ if (shouldUpdate) setState(trackResult(next, addTrackedProp));
544
+ }, [addTrackedProp, trackedProps]);
545
+ const reset = React.useCallback(() => {
546
+ currentSubscriptionRef.current?.unsubscribe();
547
+ if (!enabled) {
548
+ updateState(() => ({
549
+ ...initialStateIdle,
550
+ reset
551
+ }));
552
+ return;
553
+ }
554
+ updateState(() => ({
555
+ ...initialStateConnecting,
556
+ reset
557
+ }));
558
+ const subscription = client.subscription(path.join("."), input ?? void 0, {
559
+ onStarted: () => {
560
+ optsRef.current.onStarted?.();
561
+ updateState((prev) => ({
562
+ ...prev,
563
+ status: "pending",
564
+ error: null
565
+ }));
566
+ },
567
+ onData: (data) => {
568
+ optsRef.current.onData?.(data);
569
+ updateState((prev) => ({
570
+ ...prev,
571
+ status: "pending",
572
+ data,
573
+ error: null
574
+ }));
575
+ },
576
+ onError: (error) => {
577
+ optsRef.current.onError?.(error);
578
+ updateState((prev) => ({
579
+ ...prev,
580
+ status: "error",
581
+ error
582
+ }));
583
+ },
584
+ onConnectionStateChange: (result) => {
585
+ updateState((prev) => {
586
+ switch (result.state) {
587
+ case "idle": return {
588
+ ...prev,
589
+ status: result.state,
590
+ error: null,
591
+ data: void 0
592
+ };
593
+ case "connecting": return {
594
+ ...prev,
595
+ error: result.error,
596
+ status: result.state
597
+ };
598
+ case "pending": return prev;
599
+ }
600
+ });
601
+ },
602
+ onComplete: () => {
603
+ optsRef.current.onComplete?.();
604
+ updateState((prev) => ({
605
+ ...prev,
606
+ status: "idle",
607
+ error: null,
608
+ data: void 0
609
+ }));
610
+ }
611
+ });
612
+ currentSubscriptionRef.current = subscription;
613
+ }, [
614
+ client,
615
+ queryKey,
616
+ enabled,
617
+ updateState
618
+ ]);
619
+ React.useEffect(() => {
620
+ reset();
621
+ return () => {
622
+ currentSubscriptionRef.current?.unsubscribe();
623
+ };
624
+ }, [reset]);
625
+ const resultRef = React.useRef(enabled ? {
626
+ ...initialStateConnecting,
627
+ reset
628
+ } : {
629
+ ...initialStateIdle,
630
+ reset
631
+ });
632
+ const [state, setState] = React.useState(trackResult(resultRef.current, addTrackedProp));
633
+ return state;
634
+ }
635
+ function useInfiniteQuery$1(path, input, opts) {
636
+ const { client, ssrState, prefetchInfiniteQuery, queryClient, abortOnUnmount } = useContext();
637
+ const queryKey = getQueryKeyInternal(path, input, "infinite");
638
+ const defaultOpts = queryClient.getQueryDefaults(queryKey);
639
+ const isInputSkipToken = input === skipToken;
640
+ if (typeof window === "undefined" && ssrState === "prepass" && opts?.trpc?.ssr !== false && (opts?.enabled ?? defaultOpts?.enabled) !== false && !isInputSkipToken && !queryClient.getQueryCache().find({ queryKey })) prefetchInfiniteQuery(queryKey, {
641
+ ...defaultOpts,
642
+ ...opts
643
+ });
644
+ const ssrOpts = useSSRQueryOptionsIfNeeded(queryKey, {
645
+ ...defaultOpts,
646
+ ...opts
647
+ });
648
+ const shouldAbortOnUnmount = opts?.trpc?.abortOnUnmount ?? abortOnUnmount;
649
+ const hook = useInfiniteQuery({
650
+ ...ssrOpts,
651
+ initialPageParam: opts.initialCursor ?? null,
652
+ persister: opts.persister,
653
+ queryKey,
654
+ queryFn: isInputSkipToken ? input : (queryFunctionContext) => {
655
+ const actualOpts = {
656
+ ...ssrOpts,
657
+ trpc: {
658
+ ...ssrOpts?.trpc,
659
+ ...shouldAbortOnUnmount ? { signal: queryFunctionContext.signal } : { signal: null }
660
+ }
661
+ };
662
+ return client.query(...getClientArgs(queryKey, actualOpts, {
663
+ pageParam: queryFunctionContext.pageParam ?? opts.initialCursor,
664
+ direction: queryFunctionContext.direction
665
+ }));
666
+ }
667
+ }, queryClient);
668
+ hook.trpc = useHookResult({ path });
669
+ return hook;
670
+ }
671
+ function usePrefetchInfiniteQuery$1(path, input, opts) {
672
+ const context = useContext();
673
+ const queryKey = getQueryKeyInternal(path, input, "infinite");
674
+ const defaultOpts = context.queryClient.getQueryDefaults(queryKey);
675
+ const isInputSkipToken = input === skipToken;
676
+ const ssrOpts = useSSRQueryOptionsIfNeeded(queryKey, {
677
+ ...defaultOpts,
678
+ ...opts
679
+ });
680
+ const shouldAbortOnUnmount = opts?.trpc?.abortOnUnmount ?? context.abortOnUnmount;
681
+ usePrefetchInfiniteQuery({
682
+ ...opts,
683
+ initialPageParam: opts.initialCursor ?? null,
684
+ queryKey,
685
+ queryFn: isInputSkipToken ? input : (queryFunctionContext) => {
686
+ const actualOpts = {
687
+ ...ssrOpts,
688
+ trpc: {
689
+ ...ssrOpts?.trpc,
690
+ ...shouldAbortOnUnmount ? { signal: queryFunctionContext.signal } : {}
691
+ }
692
+ };
693
+ return context.client.query(...getClientArgs(queryKey, actualOpts, {
694
+ pageParam: queryFunctionContext.pageParam ?? opts.initialCursor,
695
+ direction: queryFunctionContext.direction
696
+ }));
697
+ }
698
+ });
699
+ }
700
+ function useSuspenseInfiniteQuery$1(path, input, opts) {
701
+ const context = useContext();
702
+ const queryKey = getQueryKeyInternal(path, input, "infinite");
703
+ const defaultOpts = context.queryClient.getQueryDefaults(queryKey);
704
+ const ssrOpts = useSSRQueryOptionsIfNeeded(queryKey, {
705
+ ...defaultOpts,
706
+ ...opts
707
+ });
708
+ const shouldAbortOnUnmount = opts?.trpc?.abortOnUnmount ?? context.abortOnUnmount;
709
+ const hook = useSuspenseInfiniteQuery({
710
+ ...opts,
711
+ initialPageParam: opts.initialCursor ?? null,
712
+ queryKey,
713
+ queryFn: (queryFunctionContext) => {
714
+ const actualOpts = {
715
+ ...ssrOpts,
716
+ trpc: {
717
+ ...ssrOpts?.trpc,
718
+ ...shouldAbortOnUnmount ? { signal: queryFunctionContext.signal } : {}
719
+ }
720
+ };
721
+ return context.client.query(...getClientArgs(queryKey, actualOpts, {
722
+ pageParam: queryFunctionContext.pageParam ?? opts.initialCursor,
723
+ direction: queryFunctionContext.direction
724
+ }));
725
+ }
726
+ }, context.queryClient);
727
+ hook.trpc = useHookResult({ path });
728
+ return [hook.data, hook];
729
+ }
730
+ const useQueries$1 = (queriesCallback, options) => {
731
+ const { ssrState, queryClient, prefetchQuery, client } = useContext();
732
+ const proxy = createUseQueries(client);
733
+ const queries = queriesCallback(proxy);
734
+ if (typeof window === "undefined" && ssrState === "prepass") for (const query of queries) {
735
+ const queryOption = query;
736
+ if (queryOption.trpc?.ssr !== false && !queryClient.getQueryCache().find({ queryKey: queryOption.queryKey })) prefetchQuery(queryOption.queryKey, queryOption);
737
+ }
738
+ return useQueries({
739
+ queries: queries.map((query) => ({
740
+ ...query,
741
+ queryKey: query.queryKey
742
+ })),
743
+ combine: options?.combine
744
+ }, queryClient);
745
+ };
746
+ const useSuspenseQueries$1 = (queriesCallback) => {
747
+ const { queryClient, client } = useContext();
748
+ const proxy = createUseQueries(client);
749
+ const queries = queriesCallback(proxy);
750
+ const hook = useSuspenseQueries({ queries: queries.map((query) => ({
751
+ ...query,
752
+ queryFn: query.queryFn,
753
+ queryKey: query.queryKey
754
+ })) }, queryClient);
755
+ return [hook.map((h) => h.data), hook];
756
+ };
757
+ return {
758
+ Provider: TRPCProvider,
759
+ createClient,
760
+ useContext,
761
+ useUtils: useContext,
762
+ useQuery: useQuery$1,
763
+ usePrefetchQuery: usePrefetchQuery$1,
764
+ useSuspenseQuery: useSuspenseQuery$1,
765
+ useQueries: useQueries$1,
766
+ useSuspenseQueries: useSuspenseQueries$1,
767
+ useMutation: useMutation$1,
768
+ useSubscription,
769
+ useInfiniteQuery: useInfiniteQuery$1,
770
+ usePrefetchInfiniteQuery: usePrefetchInfiniteQuery$1,
771
+ useSuspenseInfiniteQuery: useSuspenseInfiniteQuery$1
772
+ };
773
+ }
774
+
775
+ //#endregion
776
+ //#region src/shared/queryClient.ts
777
+ /**
778
+ * @internal
779
+ */
780
+ const getQueryClient = (config) => config.queryClient ?? new QueryClient(config.queryClientConfig);
781
+
782
+ //#endregion
783
+ export { TRPCContext, contextProps, createQueryUtilsProxy, createReactDecoration, createReactQueryUtils, createRootHooks, createUseQueries, createUtilityFunctions, getClientArgs, getQueryClient, getQueryType };
784
+ //# sourceMappingURL=shared-CMPBsqju.mjs.map