@orpc/tanstack-query 2.0.0-beta.49 → 2.0.0-beta.50

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.
@@ -1,411 +1,371 @@
1
- import { intercept, isAsyncIteratorObject, isNonPrimitive, resolveMaybeOptionalOptions, toArray, } from '@orpc/shared';
2
- import { skipToken } from '@tanstack/query-core';
3
- import { liveQuery } from './live-query.js';
4
- import { SharedUtils } from './shared-utils.js';
5
- import { serializableStreamedQuery } from './stream-query.js';
6
- import { createStructuralSharing } from './structural-sharing.js';
7
- import { OPERATION_CONTEXT_SYMBOL } from './types.js';
1
+ import { liveQuery } from "./live-query.js";
2
+ import { SharedUtils } from "./shared-utils.js";
3
+ import { serializableStreamedQuery } from "./stream-query.js";
4
+ import { createStructuralSharing } from "./structural-sharing.js";
5
+ import { OPERATION_CONTEXT_SYMBOL } from "./types.js";
6
+ import { intercept, isAsyncIteratorObject, isNonPrimitive, resolveMaybeOptionalOptions, toArray } from "@orpc/shared";
7
+ import { skipToken } from "@tanstack/query-core";
8
+ // src/procedure-utils.ts
8
9
  const PROCEDURE_UTILS_INTERCEPTOR_KEYS = [
9
- 'queryInterceptors',
10
- 'streamedInterceptors',
11
- 'liveInterceptors',
12
- 'infiniteInterceptors',
13
- 'mutationInterceptors',
10
+ "queryInterceptors",
11
+ "streamedInterceptors",
12
+ "liveInterceptors",
13
+ "infiniteInterceptors",
14
+ "mutationInterceptors"
14
15
  ];
15
16
  const PROCEDURE_UTILS_MODIFIER_KEYS = [
16
- 'queryKey',
17
- 'queryOptions',
18
- 'streamedKey',
19
- 'streamedOptions',
20
- 'liveKey',
21
- 'liveOptions',
22
- 'infiniteKey',
23
- 'infiniteOptions',
24
- 'mutationKey',
25
- 'mutationOptions',
17
+ "queryKey",
18
+ "queryOptions",
19
+ "streamedKey",
20
+ "streamedOptions",
21
+ "liveKey",
22
+ "liveOptions",
23
+ "infiniteKey",
24
+ "infiniteOptions",
25
+ "mutationKey",
26
+ "mutationOptions"
26
27
  ];
27
- export function isProcedureUtilsOptions(value) {
28
- if (!isNonPrimitive(value)) {
29
- return false;
30
- }
31
- for (const key of Object.keys(value)) {
32
- if (value[key] === undefined) {
33
- continue;
34
- }
35
- if (key === 'prefix') {
36
- if (typeof value[key] !== 'string') {
37
- return false;
38
- }
39
- }
40
- else if (key === 'serializer') {
41
- if (!isNonPrimitive(value[key]) || typeof value[key].serializeJson !== 'function') {
42
- return false;
43
- }
44
- }
45
- else if (PROCEDURE_UTILS_INTERCEPTOR_KEYS.includes(key)) {
46
- if (!Array.isArray(value[key]) || value[key].some((i) => typeof i !== 'function')) {
47
- return false;
48
- }
49
- }
50
- else if (PROCEDURE_UTILS_MODIFIER_KEYS.includes(key)) {
51
- if (!isNonPrimitive(value[key])) {
52
- return false;
53
- }
54
- }
55
- }
56
- return true;
28
+ function isProcedureUtilsOptions(value) {
29
+ if (!isNonPrimitive(value)) return false;
30
+ for (const key of Object.keys(value)) {
31
+ if (value[key] === void 0) continue;
32
+ if (key === "prefix") {
33
+ if (typeof value[key] !== "string") return false;
34
+ } else if (key === "serializer") {
35
+ if (!isNonPrimitive(value[key]) || typeof value[key].serializeJson !== "function") return false;
36
+ } else if (PROCEDURE_UTILS_INTERCEPTOR_KEYS.includes(key)) {
37
+ if (!Array.isArray(value[key]) || value[key].some((i) => typeof i !== "function")) return false;
38
+ } else if (PROCEDURE_UTILS_MODIFIER_KEYS.includes(key)) {
39
+ if (!isNonPrimitive(value[key])) return false;
40
+ }
41
+ }
42
+ return true;
57
43
  }
58
44
  function mergeProcedureUtilsModifier(base, override) {
59
- if (typeof override !== 'function') {
60
- if (typeof base !== 'function') {
61
- return { ...base, ...override };
62
- }
63
- return (options) => base({ ...override, ...options });
64
- }
65
- const applyBase = typeof base === 'function' ? base : (options) => ({ ...base, ...options });
66
- return (options) => override(applyBase(options));
45
+ if (typeof override !== "function") {
46
+ if (typeof base !== "function") return {
47
+ ...base,
48
+ ...override
49
+ };
50
+ return (options) => base({
51
+ ...override,
52
+ ...options
53
+ });
54
+ }
55
+ const applyBase = typeof base === "function" ? base : (options) => ({
56
+ ...base,
57
+ ...options
58
+ });
59
+ return (options) => override(applyBase(options));
67
60
  }
68
61
  /**
69
- * Merge two procedure utils options where `override` takes priority.
70
- * A key explicitly set to `undefined` in `override` resets the base value.
71
- * When both sides define a key, interceptors are concatenated (base ones run first) and
72
- * modifiers are combined so that, for object values, per-call options beat `override`,
73
- * which beats `base`. A function modifier receives the merged options and its result wins.
74
- */
75
- export function mergeProcedureUtilsOptions(base, override) {
76
- const merged = { ...base, ...override };
77
- for (const key of PROCEDURE_UTILS_INTERCEPTOR_KEYS) {
78
- const baseValue = base[key];
79
- const overrideValue = override[key];
80
- if (baseValue && overrideValue) {
81
- merged[key] = [...baseValue, ...overrideValue];
82
- }
83
- }
84
- for (const key of PROCEDURE_UTILS_MODIFIER_KEYS) {
85
- const baseValue = base[key];
86
- const overrideValue = override[key];
87
- if (baseValue && overrideValue) {
88
- merged[key] = mergeProcedureUtilsModifier(baseValue, overrideValue);
89
- }
90
- }
91
- return merged;
92
- }
93
- export class ProcedureUtils extends SharedUtils {
94
- /**
95
- * Calling corresponding procedure client
96
- */
97
- call;
98
- constructor(path, client, options = {}) {
99
- super(path, options);
100
- this.call = client;
101
- }
102
- /**
103
- * Generate a **full matching** key for [Query Options](https://orpc.dev/docs/integrations/tanstack-query#query-options).
104
- */
105
- queryKey(...rest) {
106
- let optionsIn = resolveMaybeOptionalOptions(rest);
107
- if (typeof this.options.queryKey === 'function') {
108
- optionsIn = this.options.queryKey(optionsIn);
109
- }
110
- else if (this.options.queryKey) {
111
- optionsIn = { ...this.options.queryKey, ...optionsIn };
112
- }
113
- const queryKey = optionsIn.queryKey ?? this.key({ type: 'query', input: optionsIn.input });
114
- return queryKey;
115
- }
116
- /**
117
- * Generate options used for useQuery/useSuspenseQuery/prefetchQuery/...
118
- */
119
- queryOptions(...rest) {
120
- let optionsIn = resolveMaybeOptionalOptions(rest);
121
- if (typeof this.options.queryOptions === 'function') {
122
- optionsIn = this.options.queryOptions(optionsIn);
123
- }
124
- else if (this.options.queryOptions) {
125
- optionsIn = { ...this.options.queryOptions, ...optionsIn };
126
- }
127
- const queryKey = this.queryKey(optionsIn);
128
- return {
129
- structuralSharing: createStructuralSharing(this.options.serializer),
130
- ...optionsIn,
131
- queryKey,
132
- queryFn: (fnContext) => {
133
- return intercept(this.options.queryInterceptors, {
134
- path: this.path,
135
- context: {
136
- [OPERATION_CONTEXT_SYMBOL]: {
137
- key: queryKey,
138
- type: 'query',
139
- },
140
- ...optionsIn.context,
141
- },
142
- input: optionsIn.input,
143
- fnContext,
144
- }, ({ context, input, fnContext }) => {
145
- if (input === skipToken || optionsIn.queryFn === skipToken) {
146
- throw new Error('queryFn should not be called when skipToken used for skipping');
147
- }
148
- if (optionsIn.queryFn) {
149
- return optionsIn.queryFn(fnContext);
150
- }
151
- return this.call(input, { signal: fnContext.signal, context });
152
- });
153
- },
154
- ...(optionsIn.input === skipToken || optionsIn.queryFn === skipToken
155
- ? { enabled: false }
156
- : {}),
157
- };
158
- }
159
- /**
160
- * Generate a **full matching** key for [Streamed Query Options](https://orpc.dev/docs/integrations/tanstack-query#streamed-query-options).
161
- */
162
- streamedKey(...rest) {
163
- let optionsIn = resolveMaybeOptionalOptions(rest);
164
- if (typeof this.options.streamedKey === 'function') {
165
- optionsIn = this.options.streamedKey(optionsIn);
166
- }
167
- else if (this.options.streamedKey) {
168
- optionsIn = { ...this.options.streamedKey, ...optionsIn };
169
- }
170
- const queryKey = optionsIn.queryKey ??
171
- this.key({
172
- type: 'streamed',
173
- input: optionsIn.input,
174
- fnOptions: optionsIn.queryFnOptions,
175
- });
176
- return queryKey;
177
- }
178
- /**
179
- * Configure queries for [AsyncIteratorObject](https://orpc.dev/docs/async-iterator-object).
180
- * This is built on [TanStack Query streamedQuery](https://tanstack.com/query/latest/docs/reference/streamedQuery)
181
- * and works with hooks like `useQuery`, `useSuspenseQuery`, or `prefetchQuery`.
182
- * The query never returns to pending once it has data.
183
- */
184
- streamedOptions(...rest) {
185
- let optionsIn = resolveMaybeOptionalOptions(rest);
186
- if (typeof this.options.streamedOptions === 'function') {
187
- optionsIn = this.options.streamedOptions(optionsIn);
188
- }
189
- else if (this.options.streamedOptions) {
190
- optionsIn = { ...this.options.streamedOptions, ...optionsIn };
191
- }
192
- const queryKey = this.streamedKey(optionsIn);
193
- const { settleOnFirstData, ...queryOptions } = optionsIn;
194
- return {
195
- ...queryOptions,
196
- queryKey,
197
- queryFn: (fnContext) => {
198
- return intercept(toArray(this.options.streamedInterceptors), {
199
- path: this.path,
200
- context: {
201
- [OPERATION_CONTEXT_SYMBOL]: {
202
- key: queryKey,
203
- type: 'streamed',
204
- },
205
- ...optionsIn.context,
206
- },
207
- input: optionsIn.input,
208
- fnContext,
209
- }, ({ context, fnContext, input }) => {
210
- if (input === skipToken || optionsIn.queryFn === skipToken) {
211
- throw new Error('queryFn should not be called when skipToken used for skipping');
212
- }
213
- if (optionsIn.queryFn) {
214
- return optionsIn.queryFn(fnContext);
215
- }
216
- return serializableStreamedQuery(async (queryContext) => {
217
- const output = await this.call(input, { signal: queryContext.signal, context });
218
- if (!isAsyncIteratorObject(output)) {
219
- throw new Error('streamedQuery requires an AsyncIteratorObject output');
220
- }
221
- return output;
222
- }, { ...optionsIn.queryFnOptions, settleOnFirstData })(fnContext);
223
- });
224
- },
225
- ...(optionsIn.input === skipToken || optionsIn.queryFn === skipToken
226
- ? { enabled: false }
227
- : {}),
228
- };
229
- }
230
- /**
231
- * Generate a **full matching** key for [Live Query Options](https://orpc.dev/docs/integrations/tanstack-query#live-query-options).
232
- */
233
- liveKey(...rest) {
234
- let optionsIn = resolveMaybeOptionalOptions(rest);
235
- if (typeof this.options.liveKey === 'function') {
236
- optionsIn = this.options.liveKey(optionsIn);
237
- }
238
- else if (this.options.liveKey) {
239
- optionsIn = { ...this.options.liveKey, ...optionsIn };
240
- }
241
- const queryKey = optionsIn.queryKey ?? this.key({ type: 'live', input: optionsIn.input });
242
- return queryKey;
243
- }
244
- /**
245
- * Configure live queries for [AsyncIteratorObject](https://orpc.dev/docs/async-iterator-object).
246
- * Unlike `.streamedOptions` which accumulates chunks, live queries replace the entire result with each new chunk received.
247
- * Works with hooks like `useQuery`, `useSuspenseQuery`, or `prefetchQuery`.
248
- */
249
- liveOptions(...rest) {
250
- let optionsIn = resolveMaybeOptionalOptions(rest);
251
- if (typeof this.options.liveOptions === 'function') {
252
- optionsIn = this.options.liveOptions(optionsIn);
253
- }
254
- else if (this.options.liveOptions) {
255
- optionsIn = { ...this.options.liveOptions, ...optionsIn };
256
- }
257
- const queryKey = this.liveKey(optionsIn);
258
- const { settleOnFirstData, ...queryOptions } = optionsIn;
259
- return {
260
- structuralSharing: createStructuralSharing(this.options.serializer),
261
- ...queryOptions,
262
- queryKey,
263
- queryFn: (fnContext) => {
264
- return intercept(toArray(this.options.liveInterceptors), {
265
- path: this.path,
266
- context: {
267
- [OPERATION_CONTEXT_SYMBOL]: {
268
- key: queryKey,
269
- type: 'live',
270
- },
271
- ...optionsIn.context,
272
- },
273
- input: optionsIn.input,
274
- fnContext,
275
- }, ({ fnContext, input, context }) => {
276
- if (input === skipToken || optionsIn.queryFn === skipToken) {
277
- throw new Error('queryFn should not be called when skipToken used for skipping');
278
- }
279
- if (optionsIn.queryFn) {
280
- return optionsIn.queryFn(fnContext);
281
- }
282
- return liveQuery(async (queryContext) => {
283
- const output = await this.call(input, {
284
- signal: queryContext.signal,
285
- context,
286
- });
287
- if (!isAsyncIteratorObject(output)) {
288
- throw new Error('liveQuery requires an AsyncIteratorObject output');
289
- }
290
- return output;
291
- }, { settleOnFirstData })(fnContext);
292
- });
293
- },
294
- ...(optionsIn.input === skipToken || optionsIn.queryFn === skipToken
295
- ? { enabled: false }
296
- : {}),
297
- };
298
- }
299
- /**
300
- * Generate a **full matching** key for [Infinite Query Options](https://orpc.dev/docs/integrations/tanstack-query#infinite-query-options).
301
- */
302
- infiniteKey(optionsIn) {
303
- if (typeof this.options.infiniteKey === 'function') {
304
- optionsIn = this.options.infiniteKey(optionsIn);
305
- }
306
- else if (this.options.infiniteKey) {
307
- optionsIn = { ...this.options.infiniteKey, ...optionsIn };
308
- }
309
- const queryKey = optionsIn.queryKey ??
310
- this.key({
311
- type: 'infinite',
312
- input: optionsIn.input === skipToken
313
- ? skipToken
314
- : optionsIn.input(optionsIn.initialPageParam),
315
- });
316
- return queryKey;
317
- }
318
- /**
319
- * Generate options used for useInfiniteQuery/useSuspenseInfiniteQuery/prefetchInfiniteQuery/...
320
- */
321
- infiniteOptions(optionsIn) {
322
- if (typeof this.options.infiniteOptions === 'function') {
323
- optionsIn = this.options.infiniteOptions(optionsIn);
324
- }
325
- else if (this.options.infiniteOptions) {
326
- optionsIn = { ...this.options.infiniteOptions, ...optionsIn };
327
- }
328
- const queryKey = this.infiniteKey(optionsIn);
329
- return {
330
- structuralSharing: createStructuralSharing(this.options.serializer),
331
- ...optionsIn,
332
- queryKey,
333
- queryFn: (fnContext) => {
334
- return intercept(toArray(this.options.infiniteInterceptors), {
335
- path: this.path,
336
- context: {
337
- [OPERATION_CONTEXT_SYMBOL]: {
338
- key: queryKey,
339
- type: 'infinite',
340
- },
341
- ...optionsIn.context,
342
- },
343
- input: optionsIn.input === skipToken
344
- ? skipToken
345
- : optionsIn.input(fnContext.pageParam),
346
- fnContext: fnContext,
347
- }, ({ context, input, fnContext }) => {
348
- if (input === skipToken || optionsIn.queryFn === skipToken) {
349
- throw new Error('queryFn should not be called when skipToken used for skipping');
350
- }
351
- if (optionsIn.queryFn) {
352
- return optionsIn.queryFn(fnContext);
353
- }
354
- return this.call(input, { signal: fnContext.signal, context });
355
- });
356
- },
357
- ...(optionsIn.input === skipToken || optionsIn.queryFn === skipToken
358
- ? { enabled: false }
359
- : {}),
360
- };
361
- }
362
- /**
363
- * Generate a **full matching** key for [Mutation Options](https://orpc.dev/docs/integrations/tanstack-query#mutation-options).
364
- */
365
- mutationKey(optionsIn = {}) {
366
- if (typeof this.options.mutationKey === 'function') {
367
- optionsIn = this.options.mutationKey(optionsIn);
368
- }
369
- else if (this.options.mutationKey) {
370
- optionsIn = { ...this.options.mutationKey, ...optionsIn };
371
- }
372
- const mutationKey = optionsIn.mutationKey ?? this.key({ type: 'mutation' });
373
- return mutationKey;
374
- }
375
- /**
376
- * Generate options used for useMutation/...
377
- */
378
- mutationOptions(...rest) {
379
- let optionsIn = resolveMaybeOptionalOptions(rest);
380
- if (typeof this.options.mutationOptions === 'function') {
381
- optionsIn = this.options.mutationOptions(optionsIn);
382
- }
383
- else if (this.options.mutationOptions) {
384
- optionsIn = { ...this.options.mutationOptions, ...optionsIn };
385
- }
386
- const mutationKey = this.mutationKey(optionsIn);
387
- return {
388
- ...optionsIn,
389
- mutationKey,
390
- mutationFn: (input, fnContext) => {
391
- return intercept(toArray(this.options.mutationInterceptors), {
392
- path: this.path,
393
- context: {
394
- [OPERATION_CONTEXT_SYMBOL]: {
395
- key: mutationKey,
396
- type: 'mutation',
397
- },
398
- ...optionsIn.context,
399
- },
400
- fnContext,
401
- input,
402
- }, ({ input, fnContext, context }) => {
403
- if (optionsIn.mutationFn) {
404
- return optionsIn.mutationFn(input, fnContext);
405
- }
406
- return this.call(input, { context });
407
- });
408
- },
409
- };
410
- }
62
+ * Merge two procedure utils options where `override` takes priority.
63
+ * A key explicitly set to `undefined` in `override` resets the base value.
64
+ * When both sides define a key, interceptors are concatenated (base ones run first) and
65
+ * modifiers are combined so that, for object values, per-call options beat `override`,
66
+ * which beats `base`. A function modifier receives the merged options and its result wins.
67
+ */
68
+ function mergeProcedureUtilsOptions(base, override) {
69
+ const merged = {
70
+ ...base,
71
+ ...override
72
+ };
73
+ for (const key of PROCEDURE_UTILS_INTERCEPTOR_KEYS) {
74
+ const baseValue = base[key];
75
+ const overrideValue = override[key];
76
+ if (baseValue && overrideValue) merged[key] = [...baseValue, ...overrideValue];
77
+ }
78
+ for (const key of PROCEDURE_UTILS_MODIFIER_KEYS) {
79
+ const baseValue = base[key];
80
+ const overrideValue = override[key];
81
+ if (baseValue && overrideValue) merged[key] = mergeProcedureUtilsModifier(baseValue, overrideValue);
82
+ }
83
+ return merged;
411
84
  }
85
+ var ProcedureUtils = class extends SharedUtils {
86
+ /**
87
+ * Calling corresponding procedure client
88
+ */
89
+ call;
90
+ constructor(path, client, options = {}) {
91
+ super(path, options);
92
+ this.call = client;
93
+ }
94
+ /**
95
+ * Generate a **full matching** key for [Query Options](https://orpc.dev/docs/integrations/tanstack-query#query-options).
96
+ */
97
+ queryKey(...rest) {
98
+ let optionsIn = resolveMaybeOptionalOptions(rest);
99
+ if (typeof this.options.queryKey === "function") optionsIn = this.options.queryKey(optionsIn);
100
+ else if (this.options.queryKey) optionsIn = {
101
+ ...this.options.queryKey,
102
+ ...optionsIn
103
+ };
104
+ return optionsIn.queryKey ?? this.key({
105
+ type: "query",
106
+ input: optionsIn.input
107
+ });
108
+ }
109
+ /**
110
+ * Generate options used for useQuery/useSuspenseQuery/prefetchQuery/...
111
+ */
112
+ queryOptions(...rest) {
113
+ let optionsIn = resolveMaybeOptionalOptions(rest);
114
+ if (typeof this.options.queryOptions === "function") optionsIn = this.options.queryOptions(optionsIn);
115
+ else if (this.options.queryOptions) optionsIn = {
116
+ ...this.options.queryOptions,
117
+ ...optionsIn
118
+ };
119
+ const queryKey = this.queryKey(optionsIn);
120
+ return {
121
+ structuralSharing: createStructuralSharing(this.options.serializer),
122
+ ...optionsIn,
123
+ queryKey,
124
+ queryFn: (fnContext) => {
125
+ return intercept(this.options.queryInterceptors, {
126
+ path: this.path,
127
+ context: {
128
+ [OPERATION_CONTEXT_SYMBOL]: {
129
+ key: queryKey,
130
+ type: "query"
131
+ },
132
+ ...optionsIn.context
133
+ },
134
+ input: optionsIn.input,
135
+ fnContext
136
+ }, ({ context, input, fnContext }) => {
137
+ if (input === skipToken || optionsIn.queryFn === skipToken) throw new Error("queryFn should not be called when skipToken used for skipping");
138
+ if (optionsIn.queryFn) return optionsIn.queryFn(fnContext);
139
+ return this.call(input, {
140
+ signal: fnContext.signal,
141
+ context
142
+ });
143
+ });
144
+ },
145
+ ...optionsIn.input === skipToken || optionsIn.queryFn === skipToken ? { enabled: false } : {}
146
+ };
147
+ }
148
+ /**
149
+ * Generate a **full matching** key for [Streamed Query Options](https://orpc.dev/docs/integrations/tanstack-query#streamed-query-options).
150
+ */
151
+ streamedKey(...rest) {
152
+ let optionsIn = resolveMaybeOptionalOptions(rest);
153
+ if (typeof this.options.streamedKey === "function") optionsIn = this.options.streamedKey(optionsIn);
154
+ else if (this.options.streamedKey) optionsIn = {
155
+ ...this.options.streamedKey,
156
+ ...optionsIn
157
+ };
158
+ return optionsIn.queryKey ?? this.key({
159
+ type: "streamed",
160
+ input: optionsIn.input,
161
+ fnOptions: optionsIn.queryFnOptions
162
+ });
163
+ }
164
+ /**
165
+ * Configure queries for [AsyncIteratorObject](https://orpc.dev/docs/async-iterator-object).
166
+ * This is built on [TanStack Query streamedQuery](https://tanstack.com/query/latest/docs/reference/streamedQuery)
167
+ * and works with hooks like `useQuery`, `useSuspenseQuery`, or `prefetchQuery`.
168
+ * The query never returns to pending once it has data.
169
+ */
170
+ streamedOptions(...rest) {
171
+ let optionsIn = resolveMaybeOptionalOptions(rest);
172
+ if (typeof this.options.streamedOptions === "function") optionsIn = this.options.streamedOptions(optionsIn);
173
+ else if (this.options.streamedOptions) optionsIn = {
174
+ ...this.options.streamedOptions,
175
+ ...optionsIn
176
+ };
177
+ const queryKey = this.streamedKey(optionsIn);
178
+ const { settleOnFirstData, ...queryOptions } = optionsIn;
179
+ return {
180
+ ...queryOptions,
181
+ queryKey,
182
+ queryFn: (fnContext) => {
183
+ return intercept(toArray(this.options.streamedInterceptors), {
184
+ path: this.path,
185
+ context: {
186
+ [OPERATION_CONTEXT_SYMBOL]: {
187
+ key: queryKey,
188
+ type: "streamed"
189
+ },
190
+ ...optionsIn.context
191
+ },
192
+ input: optionsIn.input,
193
+ fnContext
194
+ }, ({ context, fnContext, input }) => {
195
+ if (input === skipToken || optionsIn.queryFn === skipToken) throw new Error("queryFn should not be called when skipToken used for skipping");
196
+ if (optionsIn.queryFn) return optionsIn.queryFn(fnContext);
197
+ return serializableStreamedQuery(async (queryContext) => {
198
+ const output = await this.call(input, {
199
+ signal: queryContext.signal,
200
+ context
201
+ });
202
+ if (!isAsyncIteratorObject(output)) throw new Error("streamedQuery requires an AsyncIteratorObject output");
203
+ return output;
204
+ }, {
205
+ ...optionsIn.queryFnOptions,
206
+ settleOnFirstData
207
+ })(fnContext);
208
+ });
209
+ },
210
+ ...optionsIn.input === skipToken || optionsIn.queryFn === skipToken ? { enabled: false } : {}
211
+ };
212
+ }
213
+ /**
214
+ * Generate a **full matching** key for [Live Query Options](https://orpc.dev/docs/integrations/tanstack-query#live-query-options).
215
+ */
216
+ liveKey(...rest) {
217
+ let optionsIn = resolveMaybeOptionalOptions(rest);
218
+ if (typeof this.options.liveKey === "function") optionsIn = this.options.liveKey(optionsIn);
219
+ else if (this.options.liveKey) optionsIn = {
220
+ ...this.options.liveKey,
221
+ ...optionsIn
222
+ };
223
+ return optionsIn.queryKey ?? this.key({
224
+ type: "live",
225
+ input: optionsIn.input
226
+ });
227
+ }
228
+ /**
229
+ * Configure live queries for [AsyncIteratorObject](https://orpc.dev/docs/async-iterator-object).
230
+ * Unlike `.streamedOptions` which accumulates chunks, live queries replace the entire result with each new chunk received.
231
+ * Works with hooks like `useQuery`, `useSuspenseQuery`, or `prefetchQuery`.
232
+ */
233
+ liveOptions(...rest) {
234
+ let optionsIn = resolveMaybeOptionalOptions(rest);
235
+ if (typeof this.options.liveOptions === "function") optionsIn = this.options.liveOptions(optionsIn);
236
+ else if (this.options.liveOptions) optionsIn = {
237
+ ...this.options.liveOptions,
238
+ ...optionsIn
239
+ };
240
+ const queryKey = this.liveKey(optionsIn);
241
+ const { settleOnFirstData, ...queryOptions } = optionsIn;
242
+ return {
243
+ structuralSharing: createStructuralSharing(this.options.serializer),
244
+ ...queryOptions,
245
+ queryKey,
246
+ queryFn: (fnContext) => {
247
+ return intercept(toArray(this.options.liveInterceptors), {
248
+ path: this.path,
249
+ context: {
250
+ [OPERATION_CONTEXT_SYMBOL]: {
251
+ key: queryKey,
252
+ type: "live"
253
+ },
254
+ ...optionsIn.context
255
+ },
256
+ input: optionsIn.input,
257
+ fnContext
258
+ }, ({ fnContext, input, context }) => {
259
+ if (input === skipToken || optionsIn.queryFn === skipToken) throw new Error("queryFn should not be called when skipToken used for skipping");
260
+ if (optionsIn.queryFn) return optionsIn.queryFn(fnContext);
261
+ return liveQuery(async (queryContext) => {
262
+ const output = await this.call(input, {
263
+ signal: queryContext.signal,
264
+ context
265
+ });
266
+ if (!isAsyncIteratorObject(output)) throw new Error("liveQuery requires an AsyncIteratorObject output");
267
+ return output;
268
+ }, { settleOnFirstData })(fnContext);
269
+ });
270
+ },
271
+ ...optionsIn.input === skipToken || optionsIn.queryFn === skipToken ? { enabled: false } : {}
272
+ };
273
+ }
274
+ /**
275
+ * Generate a **full matching** key for [Infinite Query Options](https://orpc.dev/docs/integrations/tanstack-query#infinite-query-options).
276
+ */
277
+ infiniteKey(optionsIn) {
278
+ if (typeof this.options.infiniteKey === "function") optionsIn = this.options.infiniteKey(optionsIn);
279
+ else if (this.options.infiniteKey) optionsIn = {
280
+ ...this.options.infiniteKey,
281
+ ...optionsIn
282
+ };
283
+ return optionsIn.queryKey ?? this.key({
284
+ type: "infinite",
285
+ input: optionsIn.input === skipToken ? skipToken : optionsIn.input(optionsIn.initialPageParam)
286
+ });
287
+ }
288
+ /**
289
+ * Generate options used for useInfiniteQuery/useSuspenseInfiniteQuery/prefetchInfiniteQuery/...
290
+ */
291
+ infiniteOptions(optionsIn) {
292
+ if (typeof this.options.infiniteOptions === "function") optionsIn = this.options.infiniteOptions(optionsIn);
293
+ else if (this.options.infiniteOptions) optionsIn = {
294
+ ...this.options.infiniteOptions,
295
+ ...optionsIn
296
+ };
297
+ const queryKey = this.infiniteKey(optionsIn);
298
+ return {
299
+ structuralSharing: createStructuralSharing(this.options.serializer),
300
+ ...optionsIn,
301
+ queryKey,
302
+ queryFn: (fnContext) => {
303
+ return intercept(toArray(this.options.infiniteInterceptors), {
304
+ path: this.path,
305
+ context: {
306
+ [OPERATION_CONTEXT_SYMBOL]: {
307
+ key: queryKey,
308
+ type: "infinite"
309
+ },
310
+ ...optionsIn.context
311
+ },
312
+ input: optionsIn.input === skipToken ? skipToken : optionsIn.input(fnContext.pageParam),
313
+ fnContext
314
+ }, ({ context, input, fnContext }) => {
315
+ if (input === skipToken || optionsIn.queryFn === skipToken) throw new Error("queryFn should not be called when skipToken used for skipping");
316
+ if (optionsIn.queryFn) return optionsIn.queryFn(fnContext);
317
+ return this.call(input, {
318
+ signal: fnContext.signal,
319
+ context
320
+ });
321
+ });
322
+ },
323
+ ...optionsIn.input === skipToken || optionsIn.queryFn === skipToken ? { enabled: false } : {}
324
+ };
325
+ }
326
+ /**
327
+ * Generate a **full matching** key for [Mutation Options](https://orpc.dev/docs/integrations/tanstack-query#mutation-options).
328
+ */
329
+ mutationKey(optionsIn = {}) {
330
+ if (typeof this.options.mutationKey === "function") optionsIn = this.options.mutationKey(optionsIn);
331
+ else if (this.options.mutationKey) optionsIn = {
332
+ ...this.options.mutationKey,
333
+ ...optionsIn
334
+ };
335
+ return optionsIn.mutationKey ?? this.key({ type: "mutation" });
336
+ }
337
+ /**
338
+ * Generate options used for useMutation/...
339
+ */
340
+ mutationOptions(...rest) {
341
+ let optionsIn = resolveMaybeOptionalOptions(rest);
342
+ if (typeof this.options.mutationOptions === "function") optionsIn = this.options.mutationOptions(optionsIn);
343
+ else if (this.options.mutationOptions) optionsIn = {
344
+ ...this.options.mutationOptions,
345
+ ...optionsIn
346
+ };
347
+ const mutationKey = this.mutationKey(optionsIn);
348
+ return {
349
+ ...optionsIn,
350
+ mutationKey,
351
+ mutationFn: (input, fnContext) => {
352
+ return intercept(toArray(this.options.mutationInterceptors), {
353
+ path: this.path,
354
+ context: {
355
+ [OPERATION_CONTEXT_SYMBOL]: {
356
+ key: mutationKey,
357
+ type: "mutation"
358
+ },
359
+ ...optionsIn.context
360
+ },
361
+ fnContext,
362
+ input
363
+ }, ({ input, fnContext, context }) => {
364
+ if (optionsIn.mutationFn) return optionsIn.mutationFn(input, fnContext);
365
+ return this.call(input, { context });
366
+ });
367
+ }
368
+ };
369
+ }
370
+ };
371
+ export { ProcedureUtils, isProcedureUtilsOptions, mergeProcedureUtilsOptions };