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