@orpc/tanstack-query 2.0.0-beta.5 → 2.0.0-beta.51

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