@orpc/tanstack-query 0.0.0-next.78cfb43 → 0.0.0-next.7eaeef7

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.
package/README.md CHANGED
@@ -30,7 +30,7 @@
30
30
  - **🔗 End-to-End Type Safety**: Ensure type-safe inputs, outputs, and errors from client to server.
31
31
  - **📘 First-Class OpenAPI**: Built-in support that fully adheres to the OpenAPI standard.
32
32
  - **📝 Contract-First Development**: Optionally define your API contract before implementation.
33
- - **⚙️ Framework Integrations**: Seamlessly integrate with TanStack Query (React, Vue, Solid, Svelte), Pinia Colada, and more.
33
+ - **⚙️ Framework Integrations**: Seamlessly integrate with TanStack Query (React, Vue, Solid, Svelte, Angular), Pinia Colada, and more.
34
34
  - **🚀 Server Actions**: Fully compatible with React Server Actions on Next.js, TanStack Start, and other platforms.
35
35
  - **🔠 Standard Schema Support**: Works out of the box with Zod, Valibot, ArkType, and other schema validators.
36
36
  - **🗃️ Native Types**: Supports native types like Date, File, Blob, BigInt, URL, and more.
@@ -49,14 +49,12 @@ You can find the full documentation [here](https://orpc.unnoq.com).
49
49
  - [@orpc/contract](https://www.npmjs.com/package/@orpc/contract): Build your API contract.
50
50
  - [@orpc/server](https://www.npmjs.com/package/@orpc/server): Build your API or implement API contract.
51
51
  - [@orpc/client](https://www.npmjs.com/package/@orpc/client): Consume your API on the client with type-safety.
52
- - [@orpc/nest](https://www.npmjs.com/package/@orpc/nest): Deeply integrate oRPC with NestJS.
52
+ - [@orpc/openapi](https://www.npmjs.com/package/@orpc/openapi): Generate OpenAPI specs and handle OpenAPI requests.
53
+ - [@orpc/nest](https://www.npmjs.com/package/@orpc/nest): Deeply integrate oRPC with [NestJS](https://nestjs.com/).
53
54
  - [@orpc/react](https://www.npmjs.com/package/@orpc/react): Utilities for integrating oRPC with React and React Server Actions.
54
- - [@orpc/react-query](https://www.npmjs.com/package/@orpc/react-query): Integration with [React Query](https://tanstack.com/query/latest/docs/framework/react/overview).
55
- - [@orpc/vue-query](https://www.npmjs.com/package/@orpc/vue-query): Integration with [Vue Query](https://tanstack.com/query/latest/docs/framework/vue/overview).
56
- - [@orpc/solid-query](https://www.npmjs.com/package/@orpc/solid-query): Integration with [Solid Query](https://tanstack.com/query/latest/docs/framework/solid/overview).
57
- - [@orpc/svelte-query](https://www.npmjs.com/package/@orpc/svelte-query): Integration with [Svelte Query](https://tanstack.com/query/latest/docs/framework/svelte/overview).
55
+ - [@orpc/tanstack-query](https://www.npmjs.com/package/@orpc/tanstack-query): [TanStack Query](https://tanstack.com/query/latest) integration.
58
56
  - [@orpc/vue-colada](https://www.npmjs.com/package/@orpc/vue-colada): Integration with [Pinia Colada](https://pinia-colada.esm.dev/).
59
- - [@orpc/openapi](https://www.npmjs.com/package/@orpc/openapi): Generate OpenAPI specs and handle OpenAPI requests.
57
+ - [@orpc/hey-api](https://www.npmjs.com/package/@orpc/hey-api): [Hey API](https://heyapi.dev/) integration.
60
58
  - [@orpc/zod](https://www.npmjs.com/package/@orpc/zod): More schemas that [Zod](https://zod.dev/) doesn't support yet.
61
59
  - [@orpc/valibot](https://www.npmjs.com/package/@orpc/valibot): OpenAPI spec generation from [Valibot](https://valibot.dev/).
62
60
  - [@orpc/arktype](https://www.npmjs.com/package/@orpc/arktype): OpenAPI spec generation from [ArkType](https://arktype.io/).
package/dist/index.d.mts CHANGED
@@ -1,13 +1,178 @@
1
- import { PartialDeep } from '@orpc/shared';
2
- import { QueryKey } from '@tanstack/query-core';
1
+ import { ClientContext, Client, NestedClient } from '@orpc/client';
2
+ import { SetOptional, PartialDeep, MaybeOptionalOptions } from '@orpc/shared';
3
+ import { SkipToken, QueryKey, QueryObserverOptions, DataTag, QueryFunction, experimental_streamedQuery, InfiniteQueryObserverOptions, InfiniteData, MutationObserverOptions } from '@tanstack/query-core';
3
4
 
4
- type OperationType = 'query' | 'streamed' | 'infinite' | 'mutation';
5
-
6
- interface BuildKeyOptions<TType extends OperationType, TInput> {
5
+ type experimental_StreamedQueryOutput<TOutput> = TOutput extends AsyncIterable<infer U> ? U[] : never;
6
+ type experimental_LiveQueryOutput<TOutput> = TOutput extends AsyncIterable<infer U> ? U : never;
7
+ type experimental_StreamedQueryOptions = Omit<Parameters<typeof experimental_streamedQuery>[0], 'queryFn'>;
8
+ type OperationType = 'query' | 'streamed' | 'live' | 'infinite' | 'mutation';
9
+ type OperationKeyOptions<TType extends OperationType, TInput> = {
7
10
  type?: TType;
8
11
  input?: TType extends 'mutation' ? never : PartialDeep<TInput>;
12
+ fnOptions?: TType extends 'streamed' ? experimental_StreamedQueryOptions : never;
13
+ };
14
+ type OperationKey<TType extends OperationType, TInput> = [path: readonly string[], options: OperationKeyOptions<TType, TInput>];
15
+ declare const OPERATION_CONTEXT_SYMBOL: unique symbol;
16
+ interface OperationContext {
17
+ [OPERATION_CONTEXT_SYMBOL]?: {
18
+ key: QueryKey;
19
+ type: OperationType;
20
+ };
21
+ }
22
+ type QueryKeyOptions<TInput> = (undefined extends TInput ? {
23
+ input?: TInput | SkipToken;
24
+ } : {
25
+ input: TInput | SkipToken;
26
+ }) & {
27
+ queryKey?: QueryKey;
28
+ };
29
+ type QueryOptionsIn<TClientContext extends ClientContext, TInput, TOutput, TError, TSelectData> = QueryKeyOptions<TInput> & (Record<never, never> extends TClientContext ? {
30
+ context?: TClientContext;
31
+ } : {
32
+ context: TClientContext;
33
+ }) & Omit<QueryObserverOptions<TOutput, TError, TSelectData>, 'queryKey'>;
34
+ interface QueryOptionsBase<TOutput, TError> {
35
+ queryKey: DataTag<QueryKey, TOutput, TError>;
36
+ queryFn: QueryFunction<TOutput>;
37
+ throwOnError?: (error: TError) => boolean;
38
+ retryDelay?: (count: number, error: TError) => number;
39
+ enabled: boolean | undefined;
40
+ }
41
+ type experimental_StreamedKeyOptions<TInput> = QueryKeyOptions<TInput> & {
42
+ queryFnOptions?: experimental_StreamedQueryOptions;
43
+ };
44
+ type experimental_StreamedOptionsIn<TClientContext extends ClientContext, TInput, TOutput, TError, TSelectData> = QueryOptionsIn<TClientContext, TInput, TOutput, TError, TSelectData> & {
45
+ queryFnOptions?: experimental_StreamedQueryOptions;
46
+ };
47
+ interface experimental_StreamedOptionsBase<TOutput, TError> extends QueryOptionsBase<TOutput, TError> {
48
+ }
49
+ type InfiniteOptionsIn<TClientContext extends ClientContext, TInput, TOutput, TError, TSelectData, TPageParam> = {
50
+ input: ((pageParam: TPageParam) => TInput) | SkipToken;
51
+ } & (Record<never, never> extends TClientContext ? {
52
+ context?: TClientContext;
53
+ } : {
54
+ context: TClientContext;
55
+ }) & SetOptional<InfiniteQueryObserverOptions<TOutput, TError, TSelectData, QueryKey, TPageParam>, 'queryKey'>;
56
+ interface InfiniteOptionsBase<TOutput, TError, TPageParam> {
57
+ queryKey: DataTag<QueryKey, InfiniteData<TOutput, TPageParam>, TError>;
58
+ queryFn: QueryFunction<TOutput, QueryKey, TPageParam>;
59
+ select?(): InfiniteData<TOutput, TPageParam>;
60
+ throwOnError?: (error: TError) => boolean;
61
+ retryDelay?: (count: number, error: TError) => number;
62
+ enabled: boolean | undefined;
63
+ }
64
+ type MutationOptionsIn<TClientContext extends ClientContext, TInput, TOutput, TError, TMutationContext> = (Record<never, never> extends TClientContext ? {
65
+ context?: TClientContext;
66
+ } : {
67
+ context: TClientContext;
68
+ }) & MutationOptions<TInput, TOutput, TError, TMutationContext>;
69
+ type MutationOptions<TInput, TOutput, TError, TMutationContext> = MutationObserverOptions<TOutput, TError, TInput, TMutationContext>;
70
+
71
+ /**
72
+ * Utils at any level (procedure or router)
73
+ */
74
+ interface GeneralUtils<TInput> {
75
+ /**
76
+ * Generate a **partial matching** key for actions like revalidating queries, checking mutation status, etc.
77
+ *
78
+ * @see {@link https://orpc.unnoq.com/docs/integrations/tanstack-query#query-mutation-key Tanstack Query/Mutation Key Docs}
79
+ */
80
+ key<TType extends OperationType>(options?: OperationKeyOptions<TType, TInput>): OperationKey<TType, TInput>;
81
+ }
82
+ declare function createGeneralUtils<TInput>(path: readonly string[]): GeneralUtils<TInput>;
83
+
84
+ declare function generateOperationKey<TType extends OperationType, TInput>(path: readonly string[], state?: OperationKeyOptions<TType, TInput>): OperationKey<TType, TInput>;
85
+
86
+ interface ProcedureUtils<TClientContext extends ClientContext, TInput, TOutput, TError> {
87
+ /**
88
+ * Calling corresponding procedure client
89
+ *
90
+ * @see {@link https://orpc.unnoq.com/docs/integrations/tanstack-query#calling-clients Tanstack Calling Procedure Client Docs}
91
+ */
92
+ call: Client<TClientContext, TInput, TOutput, TError>;
93
+ /**
94
+ * Generate a **full matching** key for [Query Options](https://orpc.unnoq.com/docs/integrations/tanstack-query#query-options).
95
+ *
96
+ * @see {@link https://orpc.unnoq.com/docs/integrations/tanstack-query#query-mutation-key Tanstack Query/Mutation Key Docs}
97
+ */
98
+ queryKey(...rest: MaybeOptionalOptions<QueryKeyOptions<TInput>>): DataTag<QueryKey, TOutput, TError>;
99
+ /**
100
+ * Generate options used for useQuery/useSuspenseQuery/prefetchQuery/...
101
+ *
102
+ * @see {@link https://orpc.unnoq.com/docs/integrations/tanstack-query#query-options Tanstack Query Options Utility Docs}
103
+ */
104
+ queryOptions<U, USelectData = TOutput>(...rest: MaybeOptionalOptions<U & QueryOptionsIn<TClientContext, TInput, TOutput, TError, USelectData>>): NoInfer<U & Omit<QueryOptionsBase<TOutput, TError>, keyof U>>;
105
+ /**
106
+ * Generate a **full matching** key for [Streamed Query Options](https://orpc.unnoq.com/docs/integrations/tanstack-query#streamed-query-options).
107
+ *
108
+ * @see {@link https://orpc.unnoq.com/docs/integrations/tanstack-query#query-mutation-key Tanstack Query/Mutation Key Docs}
109
+ */
110
+ experimental_streamedKey(...rest: MaybeOptionalOptions<experimental_StreamedKeyOptions<TInput>>): DataTag<QueryKey, experimental_StreamedQueryOutput<TOutput>, TError>;
111
+ /**
112
+ * Configure queries for [Event Iterator](https://orpc.unnoq.com/docs/event-iterator).
113
+ * This is built on [TanStack Query streamedQuery](https://tanstack.com/query/latest/docs/reference/streamedQuery)
114
+ * and works with hooks like `useQuery`, `useSuspenseQuery`, or `prefetchQuery`.
115
+ *
116
+ * @see {@link https://orpc.unnoq.com/docs/integrations/tanstack-query#streamed-query-options Tanstack Streamed Query Options Utility Docs}
117
+ */
118
+ experimental_streamedOptions<U, USelectData = experimental_StreamedQueryOutput<TOutput>>(...rest: MaybeOptionalOptions<U & experimental_StreamedOptionsIn<TClientContext, TInput, experimental_StreamedQueryOutput<TOutput>, TError, USelectData>>): NoInfer<U & Omit<experimental_StreamedOptionsBase<experimental_StreamedQueryOutput<TOutput>, TError>, keyof U>>;
119
+ /**
120
+ * Generate a **full matching** key for [Live Query Options](https://orpc.unnoq.com/docs/integrations/tanstack-query#live-query-options).
121
+ *
122
+ * @see {@link https://orpc.unnoq.com/docs/integrations/tanstack-query#query-mutation-key Tanstack Query/Mutation Key Docs}
123
+ */
124
+ experimental_liveKey(...rest: MaybeOptionalOptions<QueryKeyOptions<TInput>>): DataTag<QueryKey, experimental_LiveQueryOutput<TOutput>, TError>;
125
+ /**
126
+ * Configure live queries for [Event Iterator](https://orpc.unnoq.com/docs/event-iterator).
127
+ * Unlike `.streamedOptions` which accumulates chunks, live queries replace the entire result with each new chunk received.
128
+ * Works with hooks like `useQuery`, `useSuspenseQuery`, or `prefetchQuery`.
129
+ *
130
+ * @see {@link https://orpc.unnoq.com/docs/integrations/tanstack-query#live-query-options Tanstack Live Query Options Utility Docs}
131
+ */
132
+ experimental_liveOptions<U, USelectData = experimental_LiveQueryOutput<TOutput>>(...rest: MaybeOptionalOptions<U & QueryOptionsIn<TClientContext, TInput, experimental_LiveQueryOutput<TOutput>, TError, USelectData>>): NoInfer<U & Omit<QueryOptionsBase<experimental_LiveQueryOutput<TOutput>, TError>, keyof U>>;
133
+ /**
134
+ * Generate a **full matching** key for [Infinite Query Options](https://orpc.unnoq.com/docs/integrations/tanstack-query#infinite-query-options).
135
+ *
136
+ * @see {@link https://orpc.unnoq.com/docs/integrations/tanstack-query#query-mutation-key Tanstack Query/Mutation Key Docs}
137
+ */
138
+ infiniteKey<UPageParam>(options: Pick<InfiniteOptionsIn<TClientContext, TInput, TOutput, TError, InfiniteData<TOutput, UPageParam>, UPageParam>, 'input' | 'initialPageParam' | 'queryKey'>): DataTag<QueryKey, InfiniteData<TOutput, UPageParam>, TError>;
139
+ /**
140
+ * Generate options used for useInfiniteQuery/useSuspenseInfiniteQuery/prefetchInfiniteQuery/...
141
+ *
142
+ * @see {@link https://orpc.unnoq.com/docs/integrations/tanstack-query#infinite-query-options Tanstack Infinite Query Options Utility Docs}
143
+ */
144
+ infiniteOptions<U, UPageParam, USelectData = InfiniteData<TOutput, UPageParam>>(options: U & InfiniteOptionsIn<TClientContext, TInput, TOutput, TError, USelectData, UPageParam>): NoInfer<U & Omit<InfiniteOptionsBase<TOutput, TError, UPageParam>, keyof U>>;
145
+ /**
146
+ * Generate a **full matching** key for [Mutation Options](https://orpc.unnoq.com/docs/integrations/tanstack-query#mutation-options).
147
+ *
148
+ * @see {@link https://orpc.unnoq.com/docs/integrations/tanstack-query#query-mutation-key Tanstack Query/Mutation Key Docs}
149
+ */
150
+ mutationKey(options?: Pick<MutationOptionsIn<TClientContext, TInput, TOutput, TError, any>, 'mutationKey'>): DataTag<QueryKey, TOutput, TError>;
151
+ /**
152
+ * Generate options used for useMutation/...
153
+ *
154
+ * @see {@link https://orpc.unnoq.com/docs/integrations/tanstack-query#mutation-options Tanstack Mutation Options Docs}
155
+ */
156
+ mutationOptions<UMutationContext>(...rest: MaybeOptionalOptions<MutationOptionsIn<TClientContext, TInput, TOutput, TError, UMutationContext>>): NoInfer<MutationOptions<TInput, TOutput, TError, UMutationContext>>;
157
+ }
158
+ interface CreateProcedureUtilsOptions {
159
+ path: readonly string[];
160
+ }
161
+ declare function createProcedureUtils<TClientContext extends ClientContext, TInput, TOutput, TError>(client: Client<TClientContext, TInput, TOutput, TError>, options: CreateProcedureUtilsOptions): ProcedureUtils<TClientContext, TInput, TOutput, TError>;
162
+
163
+ type RouterUtils<T extends NestedClient<any>> = T extends Client<infer UClientContext, infer UInput, infer UOutput, infer UError> ? ProcedureUtils<UClientContext, UInput, UOutput, UError> & GeneralUtils<UInput> : {
164
+ [K in keyof T]: T[K] extends NestedClient<any> ? RouterUtils<T[K]> : never;
165
+ } & GeneralUtils<unknown>;
166
+ interface CreateRouterUtilsOptions {
167
+ path?: readonly string[];
9
168
  }
10
- declare function buildKey<TType extends OperationType, TInput>(path: string[], options?: BuildKeyOptions<TType, TInput>): QueryKey;
169
+ /**
170
+ * Create a router utils from a client.
171
+ *
172
+ * @info Both client-side and server-side clients are supported.
173
+ * @see {@link https://orpc.unnoq.com/docs/integrations/tanstack-query Tanstack Query Integration}
174
+ */
175
+ declare function createRouterUtils<T extends NestedClient<any>>(client: T, options?: CreateRouterUtilsOptions): RouterUtils<T>;
11
176
 
12
- export { buildKey };
13
- export type { BuildKeyOptions, OperationType };
177
+ export { OPERATION_CONTEXT_SYMBOL, OPERATION_CONTEXT_SYMBOL as TANSTACK_QUERY_OPERATION_CONTEXT_SYMBOL, createGeneralUtils, createProcedureUtils, createRouterUtils, createRouterUtils as createTanstackQueryUtils, generateOperationKey };
178
+ export type { CreateProcedureUtilsOptions, CreateRouterUtilsOptions, GeneralUtils, InfiniteOptionsBase, InfiniteOptionsIn, MutationOptions, MutationOptionsIn, OperationContext, OperationKey, OperationKeyOptions, OperationType, ProcedureUtils, QueryKeyOptions, QueryOptionsBase, QueryOptionsIn, RouterUtils, OperationContext as TanstackQueryOperationContext, experimental_LiveQueryOutput, experimental_StreamedKeyOptions, experimental_StreamedOptionsBase, experimental_StreamedOptionsIn, experimental_StreamedQueryOutput };
package/dist/index.d.ts CHANGED
@@ -1,13 +1,178 @@
1
- import { PartialDeep } from '@orpc/shared';
2
- import { QueryKey } from '@tanstack/query-core';
1
+ import { ClientContext, Client, NestedClient } from '@orpc/client';
2
+ import { SetOptional, PartialDeep, MaybeOptionalOptions } from '@orpc/shared';
3
+ import { SkipToken, QueryKey, QueryObserverOptions, DataTag, QueryFunction, experimental_streamedQuery, InfiniteQueryObserverOptions, InfiniteData, MutationObserverOptions } from '@tanstack/query-core';
3
4
 
4
- type OperationType = 'query' | 'streamed' | 'infinite' | 'mutation';
5
-
6
- interface BuildKeyOptions<TType extends OperationType, TInput> {
5
+ type experimental_StreamedQueryOutput<TOutput> = TOutput extends AsyncIterable<infer U> ? U[] : never;
6
+ type experimental_LiveQueryOutput<TOutput> = TOutput extends AsyncIterable<infer U> ? U : never;
7
+ type experimental_StreamedQueryOptions = Omit<Parameters<typeof experimental_streamedQuery>[0], 'queryFn'>;
8
+ type OperationType = 'query' | 'streamed' | 'live' | 'infinite' | 'mutation';
9
+ type OperationKeyOptions<TType extends OperationType, TInput> = {
7
10
  type?: TType;
8
11
  input?: TType extends 'mutation' ? never : PartialDeep<TInput>;
12
+ fnOptions?: TType extends 'streamed' ? experimental_StreamedQueryOptions : never;
13
+ };
14
+ type OperationKey<TType extends OperationType, TInput> = [path: readonly string[], options: OperationKeyOptions<TType, TInput>];
15
+ declare const OPERATION_CONTEXT_SYMBOL: unique symbol;
16
+ interface OperationContext {
17
+ [OPERATION_CONTEXT_SYMBOL]?: {
18
+ key: QueryKey;
19
+ type: OperationType;
20
+ };
21
+ }
22
+ type QueryKeyOptions<TInput> = (undefined extends TInput ? {
23
+ input?: TInput | SkipToken;
24
+ } : {
25
+ input: TInput | SkipToken;
26
+ }) & {
27
+ queryKey?: QueryKey;
28
+ };
29
+ type QueryOptionsIn<TClientContext extends ClientContext, TInput, TOutput, TError, TSelectData> = QueryKeyOptions<TInput> & (Record<never, never> extends TClientContext ? {
30
+ context?: TClientContext;
31
+ } : {
32
+ context: TClientContext;
33
+ }) & Omit<QueryObserverOptions<TOutput, TError, TSelectData>, 'queryKey'>;
34
+ interface QueryOptionsBase<TOutput, TError> {
35
+ queryKey: DataTag<QueryKey, TOutput, TError>;
36
+ queryFn: QueryFunction<TOutput>;
37
+ throwOnError?: (error: TError) => boolean;
38
+ retryDelay?: (count: number, error: TError) => number;
39
+ enabled: boolean | undefined;
40
+ }
41
+ type experimental_StreamedKeyOptions<TInput> = QueryKeyOptions<TInput> & {
42
+ queryFnOptions?: experimental_StreamedQueryOptions;
43
+ };
44
+ type experimental_StreamedOptionsIn<TClientContext extends ClientContext, TInput, TOutput, TError, TSelectData> = QueryOptionsIn<TClientContext, TInput, TOutput, TError, TSelectData> & {
45
+ queryFnOptions?: experimental_StreamedQueryOptions;
46
+ };
47
+ interface experimental_StreamedOptionsBase<TOutput, TError> extends QueryOptionsBase<TOutput, TError> {
48
+ }
49
+ type InfiniteOptionsIn<TClientContext extends ClientContext, TInput, TOutput, TError, TSelectData, TPageParam> = {
50
+ input: ((pageParam: TPageParam) => TInput) | SkipToken;
51
+ } & (Record<never, never> extends TClientContext ? {
52
+ context?: TClientContext;
53
+ } : {
54
+ context: TClientContext;
55
+ }) & SetOptional<InfiniteQueryObserverOptions<TOutput, TError, TSelectData, QueryKey, TPageParam>, 'queryKey'>;
56
+ interface InfiniteOptionsBase<TOutput, TError, TPageParam> {
57
+ queryKey: DataTag<QueryKey, InfiniteData<TOutput, TPageParam>, TError>;
58
+ queryFn: QueryFunction<TOutput, QueryKey, TPageParam>;
59
+ select?(): InfiniteData<TOutput, TPageParam>;
60
+ throwOnError?: (error: TError) => boolean;
61
+ retryDelay?: (count: number, error: TError) => number;
62
+ enabled: boolean | undefined;
63
+ }
64
+ type MutationOptionsIn<TClientContext extends ClientContext, TInput, TOutput, TError, TMutationContext> = (Record<never, never> extends TClientContext ? {
65
+ context?: TClientContext;
66
+ } : {
67
+ context: TClientContext;
68
+ }) & MutationOptions<TInput, TOutput, TError, TMutationContext>;
69
+ type MutationOptions<TInput, TOutput, TError, TMutationContext> = MutationObserverOptions<TOutput, TError, TInput, TMutationContext>;
70
+
71
+ /**
72
+ * Utils at any level (procedure or router)
73
+ */
74
+ interface GeneralUtils<TInput> {
75
+ /**
76
+ * Generate a **partial matching** key for actions like revalidating queries, checking mutation status, etc.
77
+ *
78
+ * @see {@link https://orpc.unnoq.com/docs/integrations/tanstack-query#query-mutation-key Tanstack Query/Mutation Key Docs}
79
+ */
80
+ key<TType extends OperationType>(options?: OperationKeyOptions<TType, TInput>): OperationKey<TType, TInput>;
81
+ }
82
+ declare function createGeneralUtils<TInput>(path: readonly string[]): GeneralUtils<TInput>;
83
+
84
+ declare function generateOperationKey<TType extends OperationType, TInput>(path: readonly string[], state?: OperationKeyOptions<TType, TInput>): OperationKey<TType, TInput>;
85
+
86
+ interface ProcedureUtils<TClientContext extends ClientContext, TInput, TOutput, TError> {
87
+ /**
88
+ * Calling corresponding procedure client
89
+ *
90
+ * @see {@link https://orpc.unnoq.com/docs/integrations/tanstack-query#calling-clients Tanstack Calling Procedure Client Docs}
91
+ */
92
+ call: Client<TClientContext, TInput, TOutput, TError>;
93
+ /**
94
+ * Generate a **full matching** key for [Query Options](https://orpc.unnoq.com/docs/integrations/tanstack-query#query-options).
95
+ *
96
+ * @see {@link https://orpc.unnoq.com/docs/integrations/tanstack-query#query-mutation-key Tanstack Query/Mutation Key Docs}
97
+ */
98
+ queryKey(...rest: MaybeOptionalOptions<QueryKeyOptions<TInput>>): DataTag<QueryKey, TOutput, TError>;
99
+ /**
100
+ * Generate options used for useQuery/useSuspenseQuery/prefetchQuery/...
101
+ *
102
+ * @see {@link https://orpc.unnoq.com/docs/integrations/tanstack-query#query-options Tanstack Query Options Utility Docs}
103
+ */
104
+ queryOptions<U, USelectData = TOutput>(...rest: MaybeOptionalOptions<U & QueryOptionsIn<TClientContext, TInput, TOutput, TError, USelectData>>): NoInfer<U & Omit<QueryOptionsBase<TOutput, TError>, keyof U>>;
105
+ /**
106
+ * Generate a **full matching** key for [Streamed Query Options](https://orpc.unnoq.com/docs/integrations/tanstack-query#streamed-query-options).
107
+ *
108
+ * @see {@link https://orpc.unnoq.com/docs/integrations/tanstack-query#query-mutation-key Tanstack Query/Mutation Key Docs}
109
+ */
110
+ experimental_streamedKey(...rest: MaybeOptionalOptions<experimental_StreamedKeyOptions<TInput>>): DataTag<QueryKey, experimental_StreamedQueryOutput<TOutput>, TError>;
111
+ /**
112
+ * Configure queries for [Event Iterator](https://orpc.unnoq.com/docs/event-iterator).
113
+ * This is built on [TanStack Query streamedQuery](https://tanstack.com/query/latest/docs/reference/streamedQuery)
114
+ * and works with hooks like `useQuery`, `useSuspenseQuery`, or `prefetchQuery`.
115
+ *
116
+ * @see {@link https://orpc.unnoq.com/docs/integrations/tanstack-query#streamed-query-options Tanstack Streamed Query Options Utility Docs}
117
+ */
118
+ experimental_streamedOptions<U, USelectData = experimental_StreamedQueryOutput<TOutput>>(...rest: MaybeOptionalOptions<U & experimental_StreamedOptionsIn<TClientContext, TInput, experimental_StreamedQueryOutput<TOutput>, TError, USelectData>>): NoInfer<U & Omit<experimental_StreamedOptionsBase<experimental_StreamedQueryOutput<TOutput>, TError>, keyof U>>;
119
+ /**
120
+ * Generate a **full matching** key for [Live Query Options](https://orpc.unnoq.com/docs/integrations/tanstack-query#live-query-options).
121
+ *
122
+ * @see {@link https://orpc.unnoq.com/docs/integrations/tanstack-query#query-mutation-key Tanstack Query/Mutation Key Docs}
123
+ */
124
+ experimental_liveKey(...rest: MaybeOptionalOptions<QueryKeyOptions<TInput>>): DataTag<QueryKey, experimental_LiveQueryOutput<TOutput>, TError>;
125
+ /**
126
+ * Configure live queries for [Event Iterator](https://orpc.unnoq.com/docs/event-iterator).
127
+ * Unlike `.streamedOptions` which accumulates chunks, live queries replace the entire result with each new chunk received.
128
+ * Works with hooks like `useQuery`, `useSuspenseQuery`, or `prefetchQuery`.
129
+ *
130
+ * @see {@link https://orpc.unnoq.com/docs/integrations/tanstack-query#live-query-options Tanstack Live Query Options Utility Docs}
131
+ */
132
+ experimental_liveOptions<U, USelectData = experimental_LiveQueryOutput<TOutput>>(...rest: MaybeOptionalOptions<U & QueryOptionsIn<TClientContext, TInput, experimental_LiveQueryOutput<TOutput>, TError, USelectData>>): NoInfer<U & Omit<QueryOptionsBase<experimental_LiveQueryOutput<TOutput>, TError>, keyof U>>;
133
+ /**
134
+ * Generate a **full matching** key for [Infinite Query Options](https://orpc.unnoq.com/docs/integrations/tanstack-query#infinite-query-options).
135
+ *
136
+ * @see {@link https://orpc.unnoq.com/docs/integrations/tanstack-query#query-mutation-key Tanstack Query/Mutation Key Docs}
137
+ */
138
+ infiniteKey<UPageParam>(options: Pick<InfiniteOptionsIn<TClientContext, TInput, TOutput, TError, InfiniteData<TOutput, UPageParam>, UPageParam>, 'input' | 'initialPageParam' | 'queryKey'>): DataTag<QueryKey, InfiniteData<TOutput, UPageParam>, TError>;
139
+ /**
140
+ * Generate options used for useInfiniteQuery/useSuspenseInfiniteQuery/prefetchInfiniteQuery/...
141
+ *
142
+ * @see {@link https://orpc.unnoq.com/docs/integrations/tanstack-query#infinite-query-options Tanstack Infinite Query Options Utility Docs}
143
+ */
144
+ infiniteOptions<U, UPageParam, USelectData = InfiniteData<TOutput, UPageParam>>(options: U & InfiniteOptionsIn<TClientContext, TInput, TOutput, TError, USelectData, UPageParam>): NoInfer<U & Omit<InfiniteOptionsBase<TOutput, TError, UPageParam>, keyof U>>;
145
+ /**
146
+ * Generate a **full matching** key for [Mutation Options](https://orpc.unnoq.com/docs/integrations/tanstack-query#mutation-options).
147
+ *
148
+ * @see {@link https://orpc.unnoq.com/docs/integrations/tanstack-query#query-mutation-key Tanstack Query/Mutation Key Docs}
149
+ */
150
+ mutationKey(options?: Pick<MutationOptionsIn<TClientContext, TInput, TOutput, TError, any>, 'mutationKey'>): DataTag<QueryKey, TOutput, TError>;
151
+ /**
152
+ * Generate options used for useMutation/...
153
+ *
154
+ * @see {@link https://orpc.unnoq.com/docs/integrations/tanstack-query#mutation-options Tanstack Mutation Options Docs}
155
+ */
156
+ mutationOptions<UMutationContext>(...rest: MaybeOptionalOptions<MutationOptionsIn<TClientContext, TInput, TOutput, TError, UMutationContext>>): NoInfer<MutationOptions<TInput, TOutput, TError, UMutationContext>>;
157
+ }
158
+ interface CreateProcedureUtilsOptions {
159
+ path: readonly string[];
160
+ }
161
+ declare function createProcedureUtils<TClientContext extends ClientContext, TInput, TOutput, TError>(client: Client<TClientContext, TInput, TOutput, TError>, options: CreateProcedureUtilsOptions): ProcedureUtils<TClientContext, TInput, TOutput, TError>;
162
+
163
+ type RouterUtils<T extends NestedClient<any>> = T extends Client<infer UClientContext, infer UInput, infer UOutput, infer UError> ? ProcedureUtils<UClientContext, UInput, UOutput, UError> & GeneralUtils<UInput> : {
164
+ [K in keyof T]: T[K] extends NestedClient<any> ? RouterUtils<T[K]> : never;
165
+ } & GeneralUtils<unknown>;
166
+ interface CreateRouterUtilsOptions {
167
+ path?: readonly string[];
9
168
  }
10
- declare function buildKey<TType extends OperationType, TInput>(path: string[], options?: BuildKeyOptions<TType, TInput>): QueryKey;
169
+ /**
170
+ * Create a router utils from a client.
171
+ *
172
+ * @info Both client-side and server-side clients are supported.
173
+ * @see {@link https://orpc.unnoq.com/docs/integrations/tanstack-query Tanstack Query Integration}
174
+ */
175
+ declare function createRouterUtils<T extends NestedClient<any>>(client: T, options?: CreateRouterUtilsOptions): RouterUtils<T>;
11
176
 
12
- export { buildKey };
13
- export type { BuildKeyOptions, OperationType };
177
+ export { OPERATION_CONTEXT_SYMBOL, OPERATION_CONTEXT_SYMBOL as TANSTACK_QUERY_OPERATION_CONTEXT_SYMBOL, createGeneralUtils, createProcedureUtils, createRouterUtils, createRouterUtils as createTanstackQueryUtils, generateOperationKey };
178
+ export type { CreateProcedureUtilsOptions, CreateRouterUtilsOptions, GeneralUtils, InfiniteOptionsBase, InfiniteOptionsIn, MutationOptions, MutationOptionsIn, OperationContext, OperationKey, OperationKeyOptions, OperationType, ProcedureUtils, QueryKeyOptions, QueryOptionsBase, QueryOptionsIn, RouterUtils, OperationContext as TanstackQueryOperationContext, experimental_LiveQueryOutput, experimental_StreamedKeyOptions, experimental_StreamedOptionsBase, experimental_StreamedOptionsIn, experimental_StreamedQueryOutput };
package/dist/index.mjs CHANGED
@@ -1,10 +1,218 @@
1
- function buildKey(path, options = {}) {
2
- const withInput = options.input !== void 0 ? { input: options?.input } : {};
3
- const withType = options.type !== void 0 ? { type: options?.type } : {};
1
+ import { stringifyJSON, isAsyncIteratorObject, toArray } from '@orpc/shared';
2
+ import { skipToken, experimental_streamedQuery } from '@tanstack/query-core';
3
+
4
+ function generateOperationKey(path, state = {}) {
4
5
  return [path, {
5
- ...withInput,
6
- ...withType
6
+ ...state.input !== void 0 ? { input: state.input } : {},
7
+ ...state.type !== void 0 ? { type: state.type } : {},
8
+ ...state.fnOptions !== void 0 ? { fnOptions: state.fnOptions } : {}
7
9
  }];
8
10
  }
9
11
 
10
- export { buildKey };
12
+ function createGeneralUtils(path) {
13
+ return {
14
+ key(options) {
15
+ return generateOperationKey(path, options);
16
+ }
17
+ };
18
+ }
19
+
20
+ function experimental_liveQuery(queryFn) {
21
+ return async (context) => {
22
+ const stream = await queryFn(context);
23
+ let last;
24
+ for await (const chunk of stream) {
25
+ if (context.signal.aborted) {
26
+ break;
27
+ }
28
+ last = { chunk };
29
+ context.client.setQueryData(context.queryKey, chunk);
30
+ }
31
+ if (!last) {
32
+ throw new Error(
33
+ `Live query for ${stringifyJSON(context.queryKey)} did not yield any data. Ensure the query function returns an AsyncIterable with at least one chunk.`
34
+ );
35
+ }
36
+ return last.chunk;
37
+ };
38
+ }
39
+
40
+ const OPERATION_CONTEXT_SYMBOL = Symbol("ORPC_OPERATION_CONTEXT");
41
+
42
+ function createProcedureUtils(client, options) {
43
+ const utils = {
44
+ call: client,
45
+ queryKey(...[optionsIn = {}]) {
46
+ const queryKey = optionsIn.queryKey ?? generateOperationKey(options.path, { type: "query", input: optionsIn.input });
47
+ return queryKey;
48
+ },
49
+ queryOptions(...[optionsIn = {}]) {
50
+ const queryKey = utils.queryKey(optionsIn);
51
+ return {
52
+ queryFn: ({ signal }) => {
53
+ if (optionsIn.input === skipToken) {
54
+ throw new Error("queryFn should not be called with skipToken used as input");
55
+ }
56
+ return client(optionsIn.input, {
57
+ signal,
58
+ context: {
59
+ [OPERATION_CONTEXT_SYMBOL]: {
60
+ key: queryKey,
61
+ type: "query"
62
+ },
63
+ ...optionsIn.context
64
+ }
65
+ });
66
+ },
67
+ enabled: optionsIn.input === skipToken ? false : void 0,
68
+ ...optionsIn,
69
+ queryKey
70
+ };
71
+ },
72
+ experimental_streamedKey(...[optionsIn = {}]) {
73
+ const queryKey = optionsIn.queryKey ?? generateOperationKey(options.path, { type: "streamed", input: optionsIn.input, fnOptions: optionsIn.queryFnOptions });
74
+ return queryKey;
75
+ },
76
+ experimental_streamedOptions(...[optionsIn = {}]) {
77
+ const queryKey = utils.experimental_streamedKey(optionsIn);
78
+ return {
79
+ enabled: optionsIn.input === skipToken ? false : void 0,
80
+ queryFn: experimental_streamedQuery({
81
+ queryFn: async ({ signal }) => {
82
+ if (optionsIn.input === skipToken) {
83
+ throw new Error("queryFn should not be called with skipToken used as input");
84
+ }
85
+ const output = await client(optionsIn.input, {
86
+ signal,
87
+ context: {
88
+ [OPERATION_CONTEXT_SYMBOL]: {
89
+ key: queryKey,
90
+ type: "streamed"
91
+ },
92
+ ...optionsIn.context
93
+ }
94
+ });
95
+ if (!isAsyncIteratorObject(output)) {
96
+ throw new Error("streamedQuery requires an event iterator output");
97
+ }
98
+ return output;
99
+ },
100
+ ...optionsIn.queryFnOptions
101
+ }),
102
+ ...optionsIn,
103
+ queryKey
104
+ };
105
+ },
106
+ experimental_liveKey(...[optionsIn = {}]) {
107
+ const queryKey = optionsIn.queryKey ?? generateOperationKey(options.path, { type: "live", input: optionsIn.input });
108
+ return queryKey;
109
+ },
110
+ experimental_liveOptions(...[optionsIn = {}]) {
111
+ const queryKey = utils.experimental_liveKey(optionsIn);
112
+ return {
113
+ enabled: optionsIn.input === skipToken ? false : void 0,
114
+ queryFn: experimental_liveQuery(async ({ signal }) => {
115
+ if (optionsIn.input === skipToken) {
116
+ throw new Error("queryFn should not be called with skipToken used as input");
117
+ }
118
+ const output = await client(optionsIn.input, {
119
+ signal,
120
+ context: {
121
+ [OPERATION_CONTEXT_SYMBOL]: {
122
+ key: queryKey,
123
+ type: "live"
124
+ },
125
+ ...optionsIn.context
126
+ }
127
+ });
128
+ if (!isAsyncIteratorObject(output)) {
129
+ throw new Error("liveQuery requires an event iterator output");
130
+ }
131
+ return output;
132
+ }),
133
+ ...optionsIn,
134
+ queryKey
135
+ };
136
+ },
137
+ infiniteKey(optionsIn) {
138
+ const queryKey = optionsIn.queryKey ?? generateOperationKey(options.path, {
139
+ type: "infinite",
140
+ input: optionsIn.input === skipToken ? skipToken : optionsIn.input(optionsIn.initialPageParam)
141
+ });
142
+ return queryKey;
143
+ },
144
+ infiniteOptions(optionsIn) {
145
+ const queryKey = utils.infiniteKey(optionsIn);
146
+ return {
147
+ queryFn: ({ pageParam, signal }) => {
148
+ if (optionsIn.input === skipToken) {
149
+ throw new Error("queryFn should not be called with skipToken used as input");
150
+ }
151
+ return client(optionsIn.input(pageParam), {
152
+ signal,
153
+ context: {
154
+ [OPERATION_CONTEXT_SYMBOL]: {
155
+ key: queryKey,
156
+ type: "infinite"
157
+ },
158
+ ...optionsIn.context
159
+ }
160
+ });
161
+ },
162
+ enabled: optionsIn.input === skipToken ? false : void 0,
163
+ ...optionsIn,
164
+ queryKey
165
+ };
166
+ },
167
+ mutationKey(...[optionsIn = {}]) {
168
+ const mutationKey = optionsIn.mutationKey ?? generateOperationKey(options.path, { type: "mutation" });
169
+ return mutationKey;
170
+ },
171
+ mutationOptions(...[optionsIn = {}]) {
172
+ const mutationKey = utils.mutationKey(optionsIn);
173
+ return {
174
+ mutationFn: (input) => client(input, {
175
+ context: {
176
+ [OPERATION_CONTEXT_SYMBOL]: {
177
+ key: mutationKey,
178
+ type: "mutation"
179
+ },
180
+ ...optionsIn.context
181
+ }
182
+ }),
183
+ ...optionsIn,
184
+ mutationKey
185
+ };
186
+ }
187
+ };
188
+ return utils;
189
+ }
190
+
191
+ function createRouterUtils(client, options = {}) {
192
+ const path = toArray(options.path);
193
+ const generalUtils = createGeneralUtils(path);
194
+ const procedureUtils = createProcedureUtils(client, { path });
195
+ const recursive = new Proxy({
196
+ ...generalUtils,
197
+ ...procedureUtils
198
+ }, {
199
+ get(target, prop) {
200
+ const value = Reflect.get(target, prop);
201
+ if (typeof prop !== "string") {
202
+ return value;
203
+ }
204
+ const nextUtils = createRouterUtils(client[prop], { ...options, path: [...path, prop] });
205
+ if (typeof value !== "function") {
206
+ return nextUtils;
207
+ }
208
+ return new Proxy(value, {
209
+ get(_, prop2) {
210
+ return Reflect.get(nextUtils, prop2);
211
+ }
212
+ });
213
+ }
214
+ });
215
+ return recursive;
216
+ }
217
+
218
+ export { OPERATION_CONTEXT_SYMBOL, OPERATION_CONTEXT_SYMBOL as TANSTACK_QUERY_OPERATION_CONTEXT_SYMBOL, createGeneralUtils, createProcedureUtils, createRouterUtils, createRouterUtils as createTanstackQueryUtils, generateOperationKey };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@orpc/tanstack-query",
3
3
  "type": "module",
4
- "version": "0.0.0-next.78cfb43",
4
+ "version": "0.0.0-next.7eaeef7",
5
5
  "license": "MIT",
6
6
  "homepage": "https://orpc.unnoq.com",
7
7
  "repository": {
@@ -24,15 +24,23 @@
24
24
  "dist"
25
25
  ],
26
26
  "peerDependencies": {
27
- "@tanstack/query-core": ">=5.55.0",
28
- "@orpc/client": "0.0.0-next.78cfb43"
27
+ "@tanstack/query-core": ">=5.80.2",
28
+ "@orpc/client": "0.0.0-next.7eaeef7"
29
29
  },
30
30
  "dependencies": {
31
- "@orpc/shared": "0.0.0-next.78cfb43"
31
+ "@orpc/shared": "0.0.0-next.7eaeef7"
32
32
  },
33
33
  "devDependencies": {
34
- "@tanstack/query-core": "^5.72.3",
35
- "zod": "^3.25.11"
34
+ "@angular/core": "^20.1.4",
35
+ "@tanstack/angular-query-experimental": "^5.83.0",
36
+ "@tanstack/query-core": "^5.83.0",
37
+ "@tanstack/react-query": "^5.83.0",
38
+ "@tanstack/solid-query": "^5.83.0",
39
+ "@tanstack/svelte-query": "^5.83.0",
40
+ "@tanstack/vue-query": "^5.83.0",
41
+ "svelte": "^5.37.1",
42
+ "vue": "^3.5.18",
43
+ "zod": "^4.0.14"
36
44
  },
37
45
  "scripts": {
38
46
  "build": "unbuild",