@orpc/tanstack-query 2.0.0-beta.5 → 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.
@@ -0,0 +1,44 @@
1
+ import { ProcedureUtilsOptions } from "./procedure-utils.js";
2
+ import { RouterUtilsOptions } from "./router-utils.js";
3
+ import { OrderablePlugin } from "@orpc/shared";
4
+ import { AnyNestedClient, InferClientContext, InferClientError } from "@orpc/client";
5
+ /**
6
+ * Plugin that packages reusable defaults and interceptors for router utils.
7
+ *
8
+ * @see {@link https://orpc.dev/docs/integrations/tanstack-query#plugins | TanStack Query Integration - Plugins}
9
+ */
10
+ export interface RouterUtilsPlugin<T extends AnyNestedClient> extends OrderablePlugin {
11
+ /**
12
+ * Initializes the router utils plugin and returns updated router utils options.
13
+ * Called once per plugin instance during composition.
14
+ *
15
+ * This method allows plugins to wrap, extend, or transform router utils options,
16
+ * such as interceptors or configuration.
17
+ *
18
+ * @param options - The current router utils options from previous plugins or base configuration
19
+ * @returns Transformed router utils options with this plugin's modifications applied
20
+ *
21
+ * @example
22
+ * ```ts
23
+ * init(options) {
24
+ * return {
25
+ * ...options,
26
+ * mutationInterceptors: [...(options.mutationInterceptors ?? []), myInterceptor]
27
+ * }
28
+ * }
29
+ * ```
30
+ */
31
+ init?(options: RouterUtilsOptions<T>): RouterUtilsOptions<T>;
32
+ /**
33
+ * Initializes per-procedure router utils options and returns updated options.
34
+ * Called once per procedure utils instance during composition.
35
+ *
36
+ * This method allows plugins to customize merged procedure-specific defaults,
37
+ * such as query or mutation options, for the current path.
38
+ *
39
+ * @param path - The current procedure path
40
+ * @param options - The merged procedure utils options for the current procedure
41
+ * @returns Transformed procedure utils options with this plugin's modifications applied
42
+ */
43
+ initProcedureOptions?(path: string[], options: ProcedureUtilsOptions<InferClientContext<T>, any, any, InferClientError<T>>): ProcedureUtilsOptions<InferClientContext<T>, any, any, InferClientError<T>>;
44
+ }
package/dist/plugin.js ADDED
@@ -0,0 +1,19 @@
1
+ import { sortPlugins } from "@orpc/shared";
2
+ // src/plugin.ts
3
+ var CompositeRouterUtilsPlugin = class {
4
+ plugins;
5
+ name = "~composite";
6
+ constructor(plugins = []) {
7
+ this.plugins = plugins;
8
+ this.plugins = sortPlugins(plugins);
9
+ }
10
+ init(options) {
11
+ for (const plugin of this.plugins) if (plugin.init) options = plugin.init(options);
12
+ return options;
13
+ }
14
+ initProcedureOptions(path, options) {
15
+ for (const plugin of this.plugins) if (plugin.initProcedureOptions) options = plugin.initProcedureOptions(path, options);
16
+ return options;
17
+ }
18
+ };
19
+ export { CompositeRouterUtilsPlugin };
@@ -0,0 +1,164 @@
1
+ import { OperationKeyBaseOptions } from "./key.js";
2
+ import { SharedUtils } from "./shared-utils.js";
3
+ import { InferLiveQueryOutput, InferStreamedQueryOutput, InfiniteKeyOptions, InfiniteOptionsIn, InfiniteOptionsOut, LiveOptionsIn, MutationKeyOptions, MutationOptionsIn, MutationOptionsOut, OperationContext, QueryKeyOptions, QueryOptionsIn, QueryOptionsOut, StreamedKeyOptions, StreamedOptionsIn, StreamedOptionsOut } from "./types.js";
4
+ import { Interceptor, MaybeOptionalOptions, PromiseWithError } from "@orpc/shared";
5
+ import { Client, ClientContext } from "@orpc/client";
6
+ import { DataTag, InfiniteData, MutationFunctionContext, QueryFunctionContext, QueryKey, SkipToken } from "@tanstack/query-core";
7
+ export interface ProcedureUtilsQueryInterceptorOptions<TClientContext extends ClientContext, TInput> {
8
+ path: string[];
9
+ context: TClientContext & OperationContext;
10
+ input: TInput | SkipToken;
11
+ fnContext: QueryFunctionContext;
12
+ }
13
+ export type ProcedureUtilsQueryInterceptor<TClientContext extends ClientContext, TInput, TOutput, TError> = Interceptor<ProcedureUtilsQueryInterceptorOptions<TClientContext, TInput>, PromiseWithError<TOutput, TError>>;
14
+ export interface ProcedureUtilsStreamedInterceptorOptions<TClientContext extends ClientContext, TInput> extends ProcedureUtilsQueryInterceptorOptions<TClientContext, TInput> {}
15
+ export type ProcedureUtilsStreamedInterceptor<TClientContext extends ClientContext, TInput, TOutput, TError> = Interceptor<ProcedureUtilsStreamedInterceptorOptions<TClientContext, TInput>, PromiseWithError<InferStreamedQueryOutput<TOutput>, TError>>;
16
+ export interface ProcedureUtilsLiveInterceptorOptions<TClientContext extends ClientContext, TInput> extends ProcedureUtilsQueryInterceptorOptions<TClientContext, TInput> {}
17
+ export type ProcedureUtilsLiveInterceptor<TClientContext extends ClientContext, TInput, TOutput, TError> = Interceptor<ProcedureUtilsLiveInterceptorOptions<TClientContext, TInput>, PromiseWithError<InferLiveQueryOutput<TOutput>, TError>>;
18
+ export interface ProcedureUtilsInfiniteInterceptorOptions<TClientContext extends ClientContext, TInput> extends ProcedureUtilsQueryInterceptorOptions<TClientContext, TInput> {
19
+ fnContext: QueryFunctionContext<QueryKey, any>;
20
+ }
21
+ export type ProcedureUtilsInfiniteInterceptor<TClientContext extends ClientContext, TInput, TOutput, TError> = Interceptor<ProcedureUtilsInfiniteInterceptorOptions<TClientContext, TInput>, PromiseWithError<TOutput, TError>>;
22
+ export interface ProcedureUtilsMutationInterceptorOptions<TClientContext extends ClientContext, TInput> extends Omit<ProcedureUtilsQueryInterceptorOptions<TClientContext, TInput>, "input" | "fnContext"> {
23
+ input: TInput;
24
+ fnContext: MutationFunctionContext;
25
+ }
26
+ export type ProcedureUtilsMutationInterceptor<TClientContext extends ClientContext, TInput, TOutput, TError> = Interceptor<ProcedureUtilsMutationInterceptorOptions<TClientContext, TInput>, PromiseWithError<TOutput, TError>>;
27
+ /**
28
+ * Can be partial options for spread-merged options,
29
+ * or a function that receives per-call options and returns overridden this.options.
30
+ */
31
+ export type ProcedureUtilsModifier<T extends object> = Partial<T> | ((options: T) => T);
32
+ export interface ProcedureUtilsOptions<TClientContext extends ClientContext, TInput, TOutput, TError> extends OperationKeyBaseOptions {
33
+ /**
34
+ * Key options modifier for .queryKey and .queryOptions
35
+ * Can be partial options or a function that receives per-call options and returns override this.options.
36
+ */
37
+ queryKey?: ProcedureUtilsModifier<QueryKeyOptions<TInput>> | undefined;
38
+ /**
39
+ * Interceptors that intercept queryFn inside .queryOptions, guaranteed to be executed.
40
+ */
41
+ queryInterceptors?: ProcedureUtilsQueryInterceptor<TClientContext, TInput, TOutput, TError>[] | undefined;
42
+ /**
43
+ * Options modifier for .queryOptions
44
+ * Can be partial options or a function that receives per-call options and returns override this.options.
45
+ */
46
+ queryOptions?: ProcedureUtilsModifier<QueryOptionsIn<TClientContext, TInput, TOutput, TError, unknown, unknown>> | undefined;
47
+ /**
48
+ * Key options modifier for .streamedKey and .streamedOptions
49
+ * Can be partial options or a function that receives per-call options and returns override this.options.
50
+ */
51
+ streamedKey?: ProcedureUtilsModifier<StreamedKeyOptions<TInput>> | undefined;
52
+ /**
53
+ * Interceptors that intercept queryFn inside .streamedOptions, guaranteed to be executed.
54
+ */
55
+ streamedInterceptors?: ProcedureUtilsStreamedInterceptor<TClientContext, TInput, TOutput, TError>[] | undefined;
56
+ /**
57
+ * Options modifier for .streamedOptions
58
+ * Can be partial options or a function that receives per-call options and returns override this.options.
59
+ */
60
+ streamedOptions?: ProcedureUtilsModifier<StreamedOptionsIn<TClientContext, TInput, InferStreamedQueryOutput<TOutput>, TError, unknown, unknown>> | undefined;
61
+ /**
62
+ * Key options modifier for .liveKey and .liveOptions
63
+ * Can be partial options or a function that receives per-call options and returns override this.options.
64
+ */
65
+ liveKey?: ProcedureUtilsModifier<QueryKeyOptions<TInput>> | undefined;
66
+ /**
67
+ * Interceptors that intercept queryFn inside .liveOptions, guaranteed to be executed.
68
+ */
69
+ liveInterceptors?: ProcedureUtilsLiveInterceptor<TClientContext, TInput, TOutput, TError>[] | undefined;
70
+ /**
71
+ * Options modifier for .liveOptions
72
+ * Can be partial options or a function that receives per-call options and returns override this.options.
73
+ */
74
+ liveOptions?: ProcedureUtilsModifier<LiveOptionsIn<TClientContext, TInput, InferLiveQueryOutput<TOutput>, TError, unknown, unknown>> | undefined;
75
+ /**
76
+ * Key options modifier for .infiniteKey and .infiniteOptions
77
+ * Can be partial options or a function that receives per-call options and returns override this.options.
78
+ */
79
+ infiniteKey?: ProcedureUtilsModifier<InfiniteKeyOptions<TInput, unknown>> | undefined;
80
+ /**
81
+ * Interceptors that intercept queryFn inside .infiniteOptions, guaranteed to be executed.
82
+ */
83
+ infiniteInterceptors?: ProcedureUtilsInfiniteInterceptor<TClientContext, TInput, TOutput, TError>[] | undefined;
84
+ /**
85
+ * Options modifier for .infiniteOptions
86
+ * Can be partial options or a function that receives per-call options and returns override this.options.
87
+ */
88
+ infiniteOptions?: ProcedureUtilsModifier<InfiniteOptionsIn<TClientContext, TInput, TOutput, TError, unknown, unknown, unknown>> | undefined;
89
+ /**
90
+ * Key options modifier for .mutationKey and .mutationOptions
91
+ * Can be partial options or a function that receives per-call options and returns override this.options.
92
+ */
93
+ mutationKey?: ProcedureUtilsModifier<MutationKeyOptions> | undefined;
94
+ /**
95
+ * Interceptors that intercept mutationFn inside .mutationOptions, guaranteed to be executed.
96
+ */
97
+ mutationInterceptors?: ProcedureUtilsMutationInterceptor<TClientContext, TInput, TOutput, TError>[] | undefined;
98
+ /**
99
+ * Options modifier for .mutationOptions
100
+ * Can be partial options or a function that receives per-call options and returns override this.options.
101
+ */
102
+ mutationOptions?: ProcedureUtilsModifier<MutationOptionsIn<TClientContext, TInput, TOutput, TError, unknown>> | undefined;
103
+ }
104
+ /**
105
+ * Merge two procedure utils options where `override` takes priority.
106
+ * A key explicitly set to `undefined` in `override` resets the base value.
107
+ * When both sides define a key, interceptors are concatenated (base ones run first) and
108
+ * modifiers are combined so that, for object values, per-call options beat `override`,
109
+ * which beats `base`. A function modifier receives the merged options and its result wins.
110
+ */
111
+ export declare function mergeProcedureUtilsOptions<TClientContext extends ClientContext, TInput, TOutput, TError>(base: ProcedureUtilsOptions<TClientContext, TInput, TOutput, TError>, override: ProcedureUtilsOptions<TClientContext, TInput, TOutput, TError>): ProcedureUtilsOptions<TClientContext, TInput, TOutput, TError>;
112
+ export declare class ProcedureUtils<TClientContext extends ClientContext, TInput, TOutput, TError> extends SharedUtils<TInput> {
113
+ protected readonly options: ProcedureUtilsOptions<TClientContext, TInput, TOutput, TError>;
114
+ /**
115
+ * Calling corresponding procedure client
116
+ */
117
+ call: Client<TClientContext, TInput, TOutput, TError>;
118
+ constructor(path: string[], client: Client<TClientContext, TInput, TOutput, TError>, options?: ProcedureUtilsOptions<TClientContext, TInput, TOutput, TError>);
119
+ /**
120
+ * Generate a **full matching** key for [Query Options](https://orpc.dev/docs/integrations/tanstack-query#query-options).
121
+ */
122
+ queryKey(...rest: MaybeOptionalOptions<QueryKeyOptions<TInput>>): DataTag<QueryKey, TOutput, TError>;
123
+ /**
124
+ * Generate options used for useQuery/useSuspenseQuery/prefetchQuery/...
125
+ */
126
+ queryOptions<USelectData = TOutput, UInitialData = undefined>(...rest: MaybeOptionalOptions<QueryOptionsIn<TClientContext, TInput, TOutput, TError, USelectData, UInitialData>>): NoInfer<QueryOptionsOut<TOutput, TError, USelectData, UInitialData>>;
127
+ /**
128
+ * Generate a **full matching** key for [Streamed Query Options](https://orpc.dev/docs/integrations/tanstack-query#streamed-query-options).
129
+ */
130
+ streamedKey(...rest: MaybeOptionalOptions<StreamedKeyOptions<TInput>>): DataTag<QueryKey, InferStreamedQueryOutput<TOutput>, TError>;
131
+ /**
132
+ * Configure queries for [AsyncIteratorObject](https://orpc.dev/docs/async-iterator-object).
133
+ * This is built on [TanStack Query streamedQuery](https://tanstack.com/query/latest/docs/reference/streamedQuery)
134
+ * and works with hooks like `useQuery`, `useSuspenseQuery`, or `prefetchQuery`.
135
+ * The query never returns to pending once it has data.
136
+ */
137
+ streamedOptions<USelectData = InferStreamedQueryOutput<TOutput>, UInitialData = undefined>(...rest: MaybeOptionalOptions<StreamedOptionsIn<TClientContext, TInput, InferStreamedQueryOutput<TOutput>, TError, USelectData, UInitialData>>): NoInfer<StreamedOptionsOut<InferStreamedQueryOutput<TOutput>, TError, USelectData, UInitialData>>;
138
+ /**
139
+ * Generate a **full matching** key for [Live Query Options](https://orpc.dev/docs/integrations/tanstack-query#live-query-options).
140
+ */
141
+ liveKey(...rest: MaybeOptionalOptions<QueryKeyOptions<TInput>>): DataTag<QueryKey, InferLiveQueryOutput<TOutput>, TError>;
142
+ /**
143
+ * Configure live queries for [AsyncIteratorObject](https://orpc.dev/docs/async-iterator-object).
144
+ * Unlike `.streamedOptions` which accumulates chunks, live queries replace the entire result with each new chunk received.
145
+ * Works with hooks like `useQuery`, `useSuspenseQuery`, or `prefetchQuery`.
146
+ */
147
+ liveOptions<USelectData = InferLiveQueryOutput<TOutput>, UInitialData = undefined>(...rest: MaybeOptionalOptions<LiveOptionsIn<TClientContext, TInput, InferLiveQueryOutput<TOutput>, TError, USelectData, UInitialData>>): NoInfer<QueryOptionsOut<InferLiveQueryOutput<TOutput>, TError, USelectData, UInitialData>>;
148
+ /**
149
+ * Generate a **full matching** key for [Infinite Query Options](https://orpc.dev/docs/integrations/tanstack-query#infinite-query-options).
150
+ */
151
+ infiniteKey<UPageParam>(optionsIn: InfiniteKeyOptions<TInput, UPageParam>): DataTag<QueryKey, InfiniteData<TOutput, UPageParam>, TError>;
152
+ /**
153
+ * Generate options used for useInfiniteQuery/useSuspenseInfiniteQuery/prefetchInfiniteQuery/...
154
+ */
155
+ infiniteOptions<UPageParam, USelectData = InfiniteData<TOutput, UPageParam>, UInitialData = undefined>(optionsIn: InfiniteOptionsIn<TClientContext, TInput, TOutput, TError, USelectData, UPageParam, UInitialData>): NoInfer<InfiniteOptionsOut<TOutput, TError, USelectData, UPageParam, UInitialData>>;
156
+ /**
157
+ * Generate a **full matching** key for [Mutation Options](https://orpc.dev/docs/integrations/tanstack-query#mutation-options).
158
+ */
159
+ mutationKey(optionsIn?: MutationKeyOptions): DataTag<QueryKey, TOutput, TError>;
160
+ /**
161
+ * Generate options used for useMutation/...
162
+ */
163
+ mutationOptions<UMutationContext>(...rest: MaybeOptionalOptions<MutationOptionsIn<TClientContext, TInput, TOutput, TError, UMutationContext>>): NoInfer<MutationOptionsOut<TInput, TOutput, TError, UMutationContext>>;
164
+ }
@@ -0,0 +1,371 @@
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
9
+ const PROCEDURE_UTILS_INTERCEPTOR_KEYS = [
10
+ "queryInterceptors",
11
+ "streamedInterceptors",
12
+ "liveInterceptors",
13
+ "infiniteInterceptors",
14
+ "mutationInterceptors"
15
+ ];
16
+ const PROCEDURE_UTILS_MODIFIER_KEYS = [
17
+ "queryKey",
18
+ "queryOptions",
19
+ "streamedKey",
20
+ "streamedOptions",
21
+ "liveKey",
22
+ "liveOptions",
23
+ "infiniteKey",
24
+ "infiniteOptions",
25
+ "mutationKey",
26
+ "mutationOptions"
27
+ ];
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;
43
+ }
44
+ function mergeProcedureUtilsModifier(base, override) {
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));
60
+ }
61
+ /**
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;
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 };
@@ -0,0 +1,62 @@
1
+ import { OperationKeyBaseOptions } from "./key.js";
2
+ import { RouterUtilsPlugin } from "./plugin.js";
3
+ import { ProcedureUtils, ProcedureUtilsInfiniteInterceptor, ProcedureUtilsLiveInterceptor, ProcedureUtilsMutationInterceptor, ProcedureUtilsOptions, ProcedureUtilsQueryInterceptor, ProcedureUtilsStreamedInterceptor } from "./procedure-utils.js";
4
+ import { SharedUtils } from "./shared-utils.js";
5
+ import { Public } from "@orpc/shared";
6
+ import { AnyNestedClient, Client, InferClientContext, InferClientError } from "@orpc/client";
7
+ /**
8
+ * The utils shape derived from a client: procedure clients map to procedure
9
+ * utils, and routers map recursively to nested utils.
10
+ *
11
+ * @see {@link https://orpc.dev/docs/integrations/tanstack-query | TanStack Query Integration}
12
+ */
13
+ export type RouterUtils<T extends AnyNestedClient> = T extends Client<infer UClientContext, infer UInput, infer UOutput, infer UError> ? Public<ProcedureUtils<UClientContext, UInput, UOutput, UError>> : { [K in keyof T]: T[K] extends AnyNestedClient ? RouterUtils<T[K]> : never; } & Public<SharedUtils<unknown>>;
14
+ export type RouterUtilsScoped<T extends AnyNestedClient> = T extends Client<infer UClientContext, infer UInput, infer UOutput, infer UError> ? ProcedureUtilsOptions<UClientContext, UInput, UOutput, UError> : { [K in keyof T]?: (T[K] extends AnyNestedClient ? RouterUtilsScoped<T[K]> : never) | undefined; };
15
+ export interface RouterUtilsOptions<T extends AnyNestedClient> extends OperationKeyBaseOptions {
16
+ /**
17
+ * Base path for all query keys.
18
+ *
19
+ * @internal
20
+ */
21
+ path?: string[] | undefined;
22
+ /**
23
+ * Interceptors that intercept queryFn inside .queryOptions
24
+ */
25
+ queryInterceptors?: ProcedureUtilsQueryInterceptor<InferClientContext<T>, unknown, unknown, InferClientError<T>>[] | undefined;
26
+ /**
27
+ * Interceptors that intercept queryFn inside .streamedOptions
28
+ */
29
+ streamedInterceptors?: ProcedureUtilsStreamedInterceptor<InferClientContext<T>, unknown, unknown, InferClientError<T>>[] | undefined;
30
+ /**
31
+ * Interceptors that intercept queryFn inside .liveOptions
32
+ */
33
+ liveInterceptors?: ProcedureUtilsLiveInterceptor<InferClientContext<T>, unknown, unknown, InferClientError<T>>[] | undefined;
34
+ /**
35
+ * Interceptors that intercept queryFn inside .infiniteOptions
36
+ */
37
+ infiniteInterceptors?: ProcedureUtilsInfiniteInterceptor<InferClientContext<T>, unknown, unknown, InferClientError<T>>[] | undefined;
38
+ /**
39
+ * Interceptors that intercept mutationFn inside .mutationOptions
40
+ */
41
+ mutationInterceptors?: ProcedureUtilsMutationInterceptor<InferClientContext<T>, unknown, unknown, InferClientError<T>>[] | undefined;
42
+ /**
43
+ * Per-procedure options following the shape of the router.
44
+ * Allows fine-grained configuration of individual procedure utils
45
+ * without affecting the rest of the router.
46
+ */
47
+ scoped?: RouterUtilsScoped<T> | undefined;
48
+ /**
49
+ * Plugins to extend router utils behavior.
50
+ */
51
+ plugins?: RouterUtilsPlugin<T>[] | undefined;
52
+ }
53
+ /**
54
+ * Creates TanStack Query utils from a client, exposing query/mutation
55
+ * option builders for every procedure in the router.
56
+ *
57
+ * @remarks
58
+ * **Note**: Both client-side and server-side clients are supported.
59
+ *
60
+ * @see {@link https://orpc.dev/docs/integrations/tanstack-query | TanStack Query Integration}
61
+ */
62
+ export declare function createRouterUtils<T extends AnyNestedClient>(client: T, options?: NoInfer<RouterUtilsOptions<T>>): RouterUtils<T>;