@orpc/tanstack-query 2.0.0-beta.45 → 2.0.0-beta.47
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 +53 -19
- package/dist/index.d.mts +56 -35
- package/dist/index.d.ts +56 -35
- package/dist/index.mjs +181 -32
- package/package.json +5 -5
package/README.md
CHANGED
|
@@ -21,9 +21,40 @@
|
|
|
21
21
|
</a>
|
|
22
22
|
</div>
|
|
23
23
|
|
|
24
|
+
**oRPC** is a TypeScript framework for building end-to-end typesafe APIs. Write your API once as plain TypeScript functions, and oRPC gives you a fully typed client, a REST API with an OpenAPI document, and MCP tools for AI agents, with no code generation. It runs on Node.js, Bun, Deno, Cloudflare Workers, and more.
|
|
25
|
+
|
|
26
|
+
Define procedures on the server and serve them with an [RPC handler](https://orpc.dev/docs/rpc/handler):
|
|
27
|
+
|
|
28
|
+
```ts
|
|
29
|
+
import { os } from '@orpc/server'
|
|
30
|
+
import * as z from 'zod'
|
|
31
|
+
|
|
32
|
+
export const router = {
|
|
33
|
+
planet: {
|
|
34
|
+
find: os
|
|
35
|
+
.input(z.object({ id: z.number() }))
|
|
36
|
+
.handler(({ input }) => ({ id: input.id, name: 'Earth' })),
|
|
37
|
+
},
|
|
38
|
+
}
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
Then call them from a client that knows every input and output, without generating anything:
|
|
42
|
+
|
|
43
|
+
```ts
|
|
44
|
+
import type { RouterClient } from '@orpc/server'
|
|
45
|
+
import type { router } from './router'
|
|
46
|
+
import { createORPCClient } from '@orpc/client'
|
|
47
|
+
import { RPCLink } from '@orpc/client/fetch'
|
|
48
|
+
|
|
49
|
+
const link = new RPCLink({ origin: 'http://localhost:3000', url: '/rpc' })
|
|
50
|
+
const client: RouterClient<typeof router> = createORPCClient(link)
|
|
51
|
+
|
|
52
|
+
const planet = await client.planet.find({ id: 1 }) // { id: number, name: string }
|
|
53
|
+
```
|
|
54
|
+
|
|
24
55
|
## Documentation
|
|
25
56
|
|
|
26
|
-
|
|
57
|
+
Read the full documentation at [orpc.dev](https://orpc.dev), starting with the [Getting Started guide](https://orpc.dev/docs/getting-started).
|
|
27
58
|
|
|
28
59
|
## Packages
|
|
29
60
|
|
|
@@ -56,21 +87,21 @@ You can read the documentation [here](https://orpc.dev).
|
|
|
56
87
|
|
|
57
88
|
**Framework & ecosystem integrations**
|
|
58
89
|
|
|
59
|
-
| Package
|
|
60
|
-
|
|
|
61
|
-
| [@orpc/next](https://www.npmjs.com/package/@orpc/next)
|
|
62
|
-
| [@orpc/ai-sdk](https://www.npmjs.com/package/@orpc/ai-sdk)
|
|
63
|
-
| [@orpc/mcp](https://www.npmjs.com/package/@orpc/mcp)
|
|
64
|
-
| [@orpc/tanstack-query](https://www.npmjs.com/package/@orpc/tanstack-query)
|
|
65
|
-
| [@orpc/pinia-colada](https://www.npmjs.com/package/@orpc/pinia-colada)
|
|
66
|
-
| [@orpc/swr](https://www.npmjs.com/package/@orpc/swr)
|
|
67
|
-
| [@orpc/msw](https://www.npmjs.com/package/@orpc/msw)
|
|
68
|
-
| [@orpc/
|
|
69
|
-
| [@orpc/nest](https://www.npmjs.com/package/@orpc/nest)
|
|
70
|
-
| [@orpc/node](https://www.npmjs.com/package/@orpc/node)
|
|
71
|
-
| [@orpc/bun](https://www.npmjs.com/package/@orpc/bun)
|
|
72
|
-
| [@orpc/cloudflare](https://www.npmjs.com/package/@orpc/cloudflare)
|
|
73
|
-
| [@orpc/trpc](https://www.npmjs.com/package/@orpc/trpc)
|
|
90
|
+
| Package | Purpose | Docs |
|
|
91
|
+
| -------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
92
|
+
| [@orpc/next](https://www.npmjs.com/package/@orpc/next) | Integrate with [Next.js Server Functions](https://nextjs.org/docs/app/getting-started/mutating-data). | [Next.js](https://orpc.dev/docs/integrations/next) |
|
|
93
|
+
| [@orpc/ai-sdk](https://www.npmjs.com/package/@orpc/ai-sdk) | Turn contracts and procedures into [AI SDK](https://ai-sdk.dev/) tools. | [AI SDK](https://orpc.dev/docs/integrations/ai-sdk) |
|
|
94
|
+
| [@orpc/mcp](https://www.npmjs.com/package/@orpc/mcp) | Expose procedures as [MCP](https://modelcontextprotocol.io/) tools. | [MCP](https://orpc.dev/docs/integrations/mcp) |
|
|
95
|
+
| [@orpc/tanstack-query](https://www.npmjs.com/package/@orpc/tanstack-query) | Integrate with [TanStack Query](https://tanstack.com/query/latest). | [TanStack Query](https://orpc.dev/docs/integrations/tanstack-query) |
|
|
96
|
+
| [@orpc/pinia-colada](https://www.npmjs.com/package/@orpc/pinia-colada) | Integrate with [Pinia Colada](https://pinia-colada.esm.dev/). | [Pinia Colada](https://orpc.dev/docs/integrations/pinia-colada) |
|
|
97
|
+
| [@orpc/swr](https://www.npmjs.com/package/@orpc/swr) | Integrate with [SWR](https://swr.vercel.app/). | [SWR](https://orpc.dev/docs/integrations/swr) |
|
|
98
|
+
| [@orpc/msw](https://www.npmjs.com/package/@orpc/msw) | Mock procedures with [Mock Service Worker](https://mswjs.io/). | [MSW](https://orpc.dev/docs/integrations/msw) |
|
|
99
|
+
| [@orpc/effect](https://www.npmjs.com/package/@orpc/effect) | Integrate with [Effect](https://effect.website/). | [Effect](https://orpc.dev/docs/integrations/effect) |
|
|
100
|
+
| [@orpc/nest](https://www.npmjs.com/package/@orpc/nest) | Implement your contract with [NestJS](https://nestjs.com/). | [NestJS](https://orpc.dev/docs/integrations/nest) |
|
|
101
|
+
| [@orpc/node](https://www.npmjs.com/package/@orpc/node) | [Node.js](https://nodejs.org/) plugins for static file serving and large uploads. | [Static File](https://orpc.dev/docs/plugins/static-file), [Tmp File Upload](https://orpc.dev/docs/plugins/tmp-file-upload), [Batch Response Compression](https://orpc.dev/docs/plugins/batch-response-compression) |
|
|
102
|
+
| [@orpc/bun](https://www.npmjs.com/package/@orpc/bun) | Adapters for [Bun's Redis](https://bun.sh/). | [Publisher](https://orpc.dev/docs/helpers/publisher), [Rate Limit](https://orpc.dev/docs/helpers/ratelimit), [Lock](https://orpc.dev/docs/helpers/lock) |
|
|
103
|
+
| [@orpc/cloudflare](https://www.npmjs.com/package/@orpc/cloudflare) | Adapters for [Cloudflare Workers](https://developers.cloudflare.com/workers/). | [Publisher](https://orpc.dev/docs/helpers/publisher), [Rate Limit](https://orpc.dev/docs/helpers/ratelimit), [Lock](https://orpc.dev/docs/helpers/lock), [Traces](https://orpc.dev/docs/integrations/cloudflare-traces) |
|
|
104
|
+
| [@orpc/trpc](https://www.npmjs.com/package/@orpc/trpc) | Reuse existing [tRPC](https://trpc.io/) routers within oRPC. | [tRPC](https://orpc.dev/docs/integrations/trpc) |
|
|
74
105
|
|
|
75
106
|
**Observability**
|
|
76
107
|
|
|
@@ -125,10 +156,11 @@ Like what we build over at [middleapi](https://github.com/middleapi)? You can he
|
|
|
125
156
|
<td align="center"><a href="https://github.com/christ12938?ref=middleapi&utm_source=middleapi&utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="christ12938"><img src="https://avatars.githubusercontent.com/u/25758598?v=4" width="167" alt="christ12938"/><br />christ12938</a></td>
|
|
126
157
|
<td align="center"><a href="https://github.com/Ryanjso?ref=middleapi&utm_source=middleapi&utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="Ryan Soderberg"><img src="https://avatars.githubusercontent.com/u/39172778?u=5ed913c31d57e7221b75784abcad48c7ebddde27&v=4" width="167" alt="Ryan Soderberg"/><br />Ryan Soderberg</a></td>
|
|
127
158
|
<td align="center"><a href="https://github.com/itigoore01?ref=middleapi&utm_source=middleapi&utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="shota"><img src="https://avatars.githubusercontent.com/u/11831107?u=c976a6dc7e055eb026304c46c99100ed22b0c8e0&v=4" width="167" alt="shota"/><br />shota</a></td>
|
|
159
|
+
<td align="center"><a href="https://github.com/Nic13Gamer?ref=middleapi&utm_source=middleapi&utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="Nicholas"><img src="https://avatars.githubusercontent.com/u/54724556?u=56a7ab430ce7a80d648ab6eba051d454a818ed0b&v=4" width="167" alt="Nicholas"/><br />Nicholas</a></td>
|
|
128
160
|
<td align="center"><a href="https://github.com/ellis-driscoll?ref=middleapi&utm_source=middleapi&utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="Ellis Driscoll"><img src="https://avatars.githubusercontent.com/u/70685966?u=c5f95bc33b5991d9744abe00052542e4a2ed3cb9&v=4" width="167" alt="Ellis Driscoll"/><br />Ellis Driscoll</a></td>
|
|
129
|
-
<td align="center"><a href="https://github.com/hoangbn?ref=middleapi&utm_source=middleapi&utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="Hoang Nguyen"><img src="https://avatars.githubusercontent.com/u/38968280?u=c90084c6de65c56facabab7ba13a72a49ddbc3e4&v=4" width="167" alt="Hoang Nguyen"/><br />Hoang Nguyen</a></td>
|
|
130
161
|
</tr>
|
|
131
162
|
<tr>
|
|
163
|
+
<td align="center"><a href="https://github.com/hoangbn?ref=middleapi&utm_source=middleapi&utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="Hoang Nguyen"><img src="https://avatars.githubusercontent.com/u/38968280?u=c90084c6de65c56facabab7ba13a72a49ddbc3e4&v=4" width="167" alt="Hoang Nguyen"/><br />Hoang Nguyen</a></td>
|
|
132
164
|
<td align="center"><a href="https://opencollective.com/guest-ac41de3b?ref=middleapi&utm_source=middleapi&utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="Orestis Ioannou"><img src="https://images.opencollective.com/guest-ac41de3b/avatar/460.png" width="167" alt="Orestis Ioannou"/><br />Orestis Ioannou</a></td>
|
|
133
165
|
<td align="center"><a href="https://automatio.ai/?ref=middleapi&utm_source=middleapi&utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="Stefan Smiljkovic"><img src="https://avatars.githubusercontent.com/u/1984909?u=b7bf5bc40ed49df3c22f69d2da7b8d78709c49ed&v=4" width="167" alt="Stefan Smiljkovic"/><br />Stefan Smiljkovic</a></td>
|
|
134
166
|
</tr>
|
|
@@ -167,7 +199,7 @@ Like what we build over at [middleapi](https://github.com/middleapi)? You can he
|
|
|
167
199
|
</tr>
|
|
168
200
|
</table>
|
|
169
201
|
|
|
170
|
-
With thanks to [
|
|
202
|
+
With thanks to [39 past sponsors](https://orpc.dev/sponsors#past-sponsors) who helped get oRPC here.
|
|
171
203
|
|
|
172
204
|
## Open Source Programs
|
|
173
205
|
|
|
@@ -175,7 +207,9 @@ With thanks to [40 past sponsors](https://orpc.dev/sponsors#past-sponsors) who h
|
|
|
175
207
|
|
|
176
208
|
<table>
|
|
177
209
|
<tr>
|
|
178
|
-
<td align="center"><a href="https://claude.com/contact-sales/claude-for-oss" target="_blank" rel="noopener" title="Claude for Open Source"><img src="https://cdn.simpleicons.org/claude" width="64" alt="Claude"/><br />Claude
|
|
210
|
+
<td align="center"><a href="https://claude.com/contact-sales/claude-for-oss" target="_blank" rel="noopener" title="Claude for Open Source"><img src="https://cdn.simpleicons.org/claude" width="64" alt="Claude"/><br />Claude</a></td>
|
|
211
|
+
<td align="center"><a href="https://pullfrog.com/for-oss" target="_blank" rel="noopener" title="Pullfrog for OSS"><img src="https://avatars.githubusercontent.com/in/1768019?s=128&v=4" width="64" alt="Pullfrog"/><br />Pullfrog</a></td>
|
|
212
|
+
<td align="center"><a href="https://macroscope.com/open-source" target="_blank" rel="noopener" title="Macroscope for Open Source"><img src="https://avatars.githubusercontent.com/in/900172?s=128&v=4" width="64" alt="Macroscope"/><br />Macroscope</a></td>
|
|
179
213
|
</tr>
|
|
180
214
|
</table>
|
|
181
215
|
|
package/dist/index.d.mts
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import { ClientContext, RPCJsonSerializer, Client, AnyNestedClient, InferClientContext, InferClientError, RPCJsonSerialization } from '@orpc/client';
|
|
2
|
+
export { AnyNestedClient } from '@orpc/client';
|
|
2
3
|
import { RouterContract, RouterContractClient, ContractClientFactory, AnySchema, ErrorMap, MetaPlugin, Meta, InferSchemaInput, InferSchemaOutput, ORPCErrorFromErrorMap } from '@orpc/contract';
|
|
3
|
-
import { JsonifiedClient } from '@orpc/openapi';
|
|
4
|
-
import {
|
|
5
|
-
|
|
4
|
+
import { JsonifiedClient, ContractJsonifiedClientFactory } from '@orpc/openapi';
|
|
5
|
+
import { Public, PartialDeep, Interceptor, PromiseWithError, MaybeOptionalOptions, OrderablePlugin, ThrowableError } from '@orpc/shared';
|
|
6
|
+
export { Public } from '@orpc/shared';
|
|
7
|
+
import { SkipToken, QueryKey, QueryObserverOptions, InfiniteQueryObserverOptions, MutationObserverOptions, DataTag, QueryFunction, InfiniteData, QueryFunctionContext, MutationFunctionContext } from '@tanstack/query-core';
|
|
6
8
|
|
|
7
9
|
interface SerializableStreamedQueryOptions {
|
|
8
10
|
/**
|
|
@@ -23,12 +25,6 @@ interface SerializableStreamedQueryOptions {
|
|
|
23
25
|
*/
|
|
24
26
|
maxChunks?: number | undefined;
|
|
25
27
|
}
|
|
26
|
-
/**
|
|
27
|
-
* A variant of `streamedQuery` where:
|
|
28
|
-
* - Options are serializable
|
|
29
|
-
* - The output is predictable
|
|
30
|
-
*/
|
|
31
|
-
declare function serializableStreamedQuery<TQueryFnData = unknown, TQueryKey extends QueryKey = QueryKey>(queryFn: (context: QueryFunctionContext<TQueryKey>) => Promisable<AsyncIterable<TQueryFnData>>, { refetchMode, maxChunks }?: SerializableStreamedQueryOptions): QueryFunction<TQueryFnData[], TQueryKey>;
|
|
32
28
|
|
|
33
29
|
type InferStreamedQueryOutput<TOutput> = TOutput extends AsyncIterable<infer U> ? U[] : never;
|
|
34
30
|
type InferLiveQueryOutput<TOutput> = TOutput extends AsyncIterable<infer U> ? U : never;
|
|
@@ -121,35 +117,44 @@ interface OperationKeyPrefixOptions {
|
|
|
121
117
|
*/
|
|
122
118
|
prefix?: string | undefined;
|
|
123
119
|
}
|
|
124
|
-
|
|
120
|
+
interface OperationKeyBaseOptions extends OperationKeyPrefixOptions {
|
|
121
|
+
/**
|
|
122
|
+
* Serializes inputs into JSON-compatible values when building keys,
|
|
123
|
+
* and compares custom types in structural sharing.
|
|
124
|
+
* Pass the serializer your link uses so custom types are supported.
|
|
125
|
+
*
|
|
126
|
+
* @default new RPCJsonSerializer()
|
|
127
|
+
*/
|
|
128
|
+
serializer?: Public<RPCJsonSerializer> | undefined;
|
|
129
|
+
}
|
|
130
|
+
type OperationKeyOptions<TType extends OperationType, TInput> = OperationKeyBaseOptions & {
|
|
125
131
|
type?: TType | undefined;
|
|
126
132
|
input?: (TType extends 'mutation' ? never : PartialDeep<TInput>) | undefined;
|
|
127
133
|
fnOptions?: (TType extends 'streamed' ? SerializableStreamedQueryOptions : never) | undefined;
|
|
128
134
|
};
|
|
129
|
-
type OperationKeyContent<TType extends OperationType, TInput> = Omit<OperationKeyOptions<TType, TInput>,
|
|
135
|
+
type OperationKeyContent<TType extends OperationType, TInput> = Omit<OperationKeyOptions<TType, TInput>, keyof OperationKeyBaseOptions> & {
|
|
130
136
|
/**
|
|
131
137
|
* Set in place of `input` when the operation is disabled with `skipToken`,
|
|
132
138
|
* because query key hashers drop symbols.
|
|
133
139
|
*/
|
|
134
140
|
skipToken?: TType extends 'mutation' ? never : true;
|
|
141
|
+
/**
|
|
142
|
+
* Serializer types of non-JSON values in `input`, keyed by their JSON-encoded path,
|
|
143
|
+
* so inputs with the same JSON form (like a `Date` and its ISO string) produce different keys.
|
|
144
|
+
* Keyed by path (not a list) so partial keys still match.
|
|
145
|
+
*/
|
|
146
|
+
inputMeta?: Record<string, string[]> | undefined;
|
|
135
147
|
};
|
|
136
148
|
type OperationKey<TType extends OperationType, TInput> = [path: string[], options: OperationKeyContent<TType, TInput>] | [prefix: string, path: string[], options: OperationKeyContent<TType, TInput>];
|
|
137
|
-
declare function generateOperationKey<TType extends OperationType, TInput>(path: string[], options?: OperationKeyOptions<TType, TInput>): OperationKey<TType, TInput>;
|
|
138
|
-
/**
|
|
139
|
-
* Creates a `queryKeyHashFn` that supports oRPC types such as `bigint` and `Date`.
|
|
140
|
-
*
|
|
141
|
-
* @see {@link https://orpc.dev/docs/integrations/tanstack-query#custom-serializers | TanStack Query Integration - Custom Serializers}
|
|
142
|
-
*/
|
|
143
|
-
declare function createQueryKeyHashFn(serializer?: Public<RPCJsonSerializer>): QueryKeyHashFunction<QueryKey>;
|
|
144
149
|
|
|
145
150
|
declare class SharedUtils<TInput> {
|
|
146
151
|
protected readonly path: string[];
|
|
147
|
-
protected readonly options:
|
|
148
|
-
constructor(path: string[], options:
|
|
152
|
+
protected readonly options: OperationKeyBaseOptions;
|
|
153
|
+
constructor(path: string[], options: OperationKeyBaseOptions);
|
|
149
154
|
/**
|
|
150
155
|
* Generate a **partial matching** key for actions like revalidating queries, checking mutation status, etc.
|
|
151
156
|
*/
|
|
152
|
-
key<TType extends OperationType>(options?: Omit<OperationKeyOptions<TType, TInput>,
|
|
157
|
+
key<TType extends OperationType>(options?: Omit<OperationKeyOptions<TType, TInput>, keyof OperationKeyBaseOptions>): OperationKey<TType, TInput>;
|
|
153
158
|
}
|
|
154
159
|
|
|
155
160
|
interface ProcedureUtilsQueryInterceptorOptions<TClientContext extends ClientContext, TInput> {
|
|
@@ -179,7 +184,7 @@ type ProcedureUtilsMutationInterceptor<TClientContext extends ClientContext, TIn
|
|
|
179
184
|
* or a function that receives per-call options and returns overridden this.options.
|
|
180
185
|
*/
|
|
181
186
|
type ProcedureUtilsModifier<T extends object> = Partial<T> | ((options: T) => T);
|
|
182
|
-
interface ProcedureUtilsOptions<TClientContext extends ClientContext, TInput, TOutput, TError> extends
|
|
187
|
+
interface ProcedureUtilsOptions<TClientContext extends ClientContext, TInput, TOutput, TError> extends OperationKeyBaseOptions {
|
|
183
188
|
/**
|
|
184
189
|
* Key options modifier for .queryKey and .queryOptions
|
|
185
190
|
* Can be partial options or a function that receives per-call options and returns override this.options.
|
|
@@ -251,7 +256,6 @@ interface ProcedureUtilsOptions<TClientContext extends ClientContext, TInput, TO
|
|
|
251
256
|
*/
|
|
252
257
|
mutationOptions?: ProcedureUtilsModifier<MutationOptionsIn<TClientContext, TInput, TOutput, TError, unknown>> | undefined;
|
|
253
258
|
}
|
|
254
|
-
declare function isProcedureUtilsOptions(value: unknown): value is ProcedureUtilsOptions<any, any, any, any>;
|
|
255
259
|
/**
|
|
256
260
|
* Merge two procedure utils options where `override` takes priority.
|
|
257
261
|
* A key explicitly set to `undefined` in `override` resets the base value.
|
|
@@ -353,13 +357,6 @@ interface RouterUtilsPlugin<T extends AnyNestedClient> extends OrderablePlugin {
|
|
|
353
357
|
*/
|
|
354
358
|
initProcedureOptions?(path: string[], options: ProcedureUtilsOptions<InferClientContext<T>, any, any, InferClientError<T>>): ProcedureUtilsOptions<InferClientContext<T>, any, any, InferClientError<T>>;
|
|
355
359
|
}
|
|
356
|
-
declare class CompositeRouterUtilsPlugin<T extends AnyNestedClient> implements RouterUtilsPlugin<T> {
|
|
357
|
-
protected readonly plugins: RouterUtilsPlugin<T>[];
|
|
358
|
-
readonly name = "~composite";
|
|
359
|
-
constructor(plugins?: RouterUtilsPlugin<T>[]);
|
|
360
|
-
init(options: RouterUtilsOptions<T>): RouterUtilsOptions<T>;
|
|
361
|
-
initProcedureOptions(path: string[], options: ProcedureUtilsOptions<InferClientContext<T>, any, any, InferClientError<T>>): ProcedureUtilsOptions<InferClientContext<T>, any, any, InferClientError<T>>;
|
|
362
|
-
}
|
|
363
360
|
|
|
364
361
|
/**
|
|
365
362
|
* The utils shape derived from a client: procedure clients map to procedure
|
|
@@ -373,7 +370,7 @@ type RouterUtils<T extends AnyNestedClient> = T extends Client<infer UClientCont
|
|
|
373
370
|
type RouterUtilsScoped<T extends AnyNestedClient> = T extends Client<infer UClientContext, infer UInput, infer UOutput, infer UError> ? ProcedureUtilsOptions<UClientContext, UInput, UOutput, UError> : {
|
|
374
371
|
[K in keyof T]?: (T[K] extends AnyNestedClient ? RouterUtilsScoped<T[K]> : never) | undefined;
|
|
375
372
|
};
|
|
376
|
-
interface RouterUtilsOptions<T extends AnyNestedClient> extends
|
|
373
|
+
interface RouterUtilsOptions<T extends AnyNestedClient> extends OperationKeyBaseOptions {
|
|
377
374
|
/**
|
|
378
375
|
* Base path for all query keys.
|
|
379
376
|
*
|
|
@@ -443,9 +440,20 @@ interface ContractJsonifiedUtilsFactory<TClientContext extends ClientContext> {
|
|
|
443
440
|
}
|
|
444
441
|
interface ContractJsonifiedUtilsFactoryOptions<TClientContext extends ClientContext> extends Omit<RouterUtilsOptions<JsonifiedClient<RouterContractClient<RouterContract, TClientContext>>>, 'path'> {
|
|
445
442
|
}
|
|
446
|
-
|
|
443
|
+
/**
|
|
444
|
+
* Like `createContractUtilsFactory`, but for clients whose types are wrapped in `JsonifiedClient`,
|
|
445
|
+
* such as clients using OpenAPI Link.
|
|
446
|
+
*
|
|
447
|
+
* @remarks
|
|
448
|
+
* Takes a client factory created by `createContractJsonifiedClientFactory` from `@orpc/openapi`.
|
|
449
|
+
*
|
|
450
|
+
* **Note**: The contract must define a `path` meta matching its position in the root router contract.
|
|
451
|
+
*
|
|
452
|
+
* @see {@link https://orpc.dev/docs/contract/client-factory#tanstack-query-integration | Contract Client Factory - TanStack Query Integration}
|
|
453
|
+
*/
|
|
454
|
+
declare function createContractJsonifiedUtilsFactory<TClientContext extends ClientContext>(clientFactory: ContractJsonifiedClientFactory<TClientContext>, options: ContractJsonifiedUtilsFactoryOptions<TClientContext>): ContractJsonifiedUtilsFactory<TClientContext>;
|
|
447
455
|
|
|
448
|
-
type TanstackQueryMetaOptions<TClientContext extends ClientContext, TInput, TOutput, TError> = Omit<ProcedureUtilsOptions<TClientContext, TInput, TOutput, TError>,
|
|
456
|
+
type TanstackQueryMetaOptions<TClientContext extends ClientContext, TInput, TOutput, TError> = Omit<ProcedureUtilsOptions<TClientContext, TInput, TOutput, TError>, keyof OperationKeyBaseOptions>;
|
|
449
457
|
interface TanstackQueryMetaPlugin<TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap> extends MetaPlugin<TInputSchema, TOutputSchema, TErrorMap> {
|
|
450
458
|
name: '~tanstack-query';
|
|
451
459
|
}
|
|
@@ -516,5 +524,18 @@ declare class StreamQuerySSRUtilsPlugin<T extends AnyNestedClient = AnyNestedCli
|
|
|
516
524
|
initProcedureOptions(_path: string[], options: ProcedureUtilsOptions<InferClientContext<T>, any, any, InferClientError<T>>): ProcedureUtilsOptions<InferClientContext<T>, any, any, InferClientError<T>>;
|
|
517
525
|
}
|
|
518
526
|
|
|
519
|
-
|
|
520
|
-
|
|
527
|
+
type StructuralSharing = <T>(oldData: unknown, newData: T) => T;
|
|
528
|
+
/**
|
|
529
|
+
* Creates a `structuralSharing` function that works like TanStack Query's default, but also keeps
|
|
530
|
+
* the previous reference for equal `Date`, `URL`, `Map` and `Set` values, which oRPC's serializer
|
|
531
|
+
* recreates on every response, and for custom types that serialize the same with a type-aware
|
|
532
|
+
* serializer like `RPCJsonSerializer`.
|
|
533
|
+
*
|
|
534
|
+
* Used by default in `.queryOptions`, `.liveOptions` and `.infiniteOptions`, pass `structuralSharing: false` to opt out.
|
|
535
|
+
*
|
|
536
|
+
* @see {@link https://orpc.dev/docs/integrations/tanstack-query#structural-sharing | TanStack Query Integration - Structural Sharing}
|
|
537
|
+
*/
|
|
538
|
+
declare function createStructuralSharing(serializer?: Public<RPCJsonSerializer>): StructuralSharing;
|
|
539
|
+
|
|
540
|
+
export { ContractOptionsUtilsPlugin, OPERATION_CONTEXT_SYMBOL, ProcedureUtils, SharedUtils, StreamQuerySSRUtilsPlugin, OPERATION_CONTEXT_SYMBOL as TANSTACK_QUERY_OPERATION_CONTEXT_SYMBOL, createContractJsonifiedUtilsFactory, createContractUtilsFactory, createHydrationOptions, createRouterUtils, createStructuralSharing, createRouterUtils as createTanstackQueryUtils, getTanstackQueryMeta, mergeProcedureUtilsOptions, tanstackQuery };
|
|
541
|
+
export type { ContractJsonifiedUtilsFactory, ContractJsonifiedUtilsFactoryOptions, ContractUtilsFactory, ContractUtilsFactoryOptions, InferLiveQueryOutput, InferStreamedQueryOutput, InfiniteKeyOptions, InfiniteOptionsIn, InfiniteOptionsOut, MutationKeyOptions, MutationOptionsIn, MutationOptionsOut, OperationContext, OperationKey, OperationKeyBaseOptions, OperationKeyOptions, OperationKeyPrefixOptions, OperationType, ProcedureUtilsInfiniteInterceptor, ProcedureUtilsInfiniteInterceptorOptions, ProcedureUtilsLiveInterceptor, ProcedureUtilsLiveInterceptorOptions, ProcedureUtilsModifier, ProcedureUtilsMutationInterceptor, ProcedureUtilsMutationInterceptorOptions, ProcedureUtilsOptions, ProcedureUtilsQueryInterceptor, ProcedureUtilsQueryInterceptorOptions, ProcedureUtilsStreamedInterceptor, ProcedureUtilsStreamedInterceptorOptions, QueryKeyOptions, QueryOptionsIn, QueryOptionsOut, RouterUtils, RouterUtilsOptions, RouterUtilsPlugin, RouterUtilsScoped, SerializableStreamedQueryOptions, StreamQuerySSRUtilsPluginOptions, StreamedKeyOptions, StreamedOptionsIn, StreamedOptionsOut, StructuralSharing, TanstackQueryMetaOptions, TanstackQueryMetaPlugin, OperationContext as TanstackQueryOperationContext };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import { ClientContext, RPCJsonSerializer, Client, AnyNestedClient, InferClientContext, InferClientError, RPCJsonSerialization } from '@orpc/client';
|
|
2
|
+
export { AnyNestedClient } from '@orpc/client';
|
|
2
3
|
import { RouterContract, RouterContractClient, ContractClientFactory, AnySchema, ErrorMap, MetaPlugin, Meta, InferSchemaInput, InferSchemaOutput, ORPCErrorFromErrorMap } from '@orpc/contract';
|
|
3
|
-
import { JsonifiedClient } from '@orpc/openapi';
|
|
4
|
-
import {
|
|
5
|
-
|
|
4
|
+
import { JsonifiedClient, ContractJsonifiedClientFactory } from '@orpc/openapi';
|
|
5
|
+
import { Public, PartialDeep, Interceptor, PromiseWithError, MaybeOptionalOptions, OrderablePlugin, ThrowableError } from '@orpc/shared';
|
|
6
|
+
export { Public } from '@orpc/shared';
|
|
7
|
+
import { SkipToken, QueryKey, QueryObserverOptions, InfiniteQueryObserverOptions, MutationObserverOptions, DataTag, QueryFunction, InfiniteData, QueryFunctionContext, MutationFunctionContext } from '@tanstack/query-core';
|
|
6
8
|
|
|
7
9
|
interface SerializableStreamedQueryOptions {
|
|
8
10
|
/**
|
|
@@ -23,12 +25,6 @@ interface SerializableStreamedQueryOptions {
|
|
|
23
25
|
*/
|
|
24
26
|
maxChunks?: number | undefined;
|
|
25
27
|
}
|
|
26
|
-
/**
|
|
27
|
-
* A variant of `streamedQuery` where:
|
|
28
|
-
* - Options are serializable
|
|
29
|
-
* - The output is predictable
|
|
30
|
-
*/
|
|
31
|
-
declare function serializableStreamedQuery<TQueryFnData = unknown, TQueryKey extends QueryKey = QueryKey>(queryFn: (context: QueryFunctionContext<TQueryKey>) => Promisable<AsyncIterable<TQueryFnData>>, { refetchMode, maxChunks }?: SerializableStreamedQueryOptions): QueryFunction<TQueryFnData[], TQueryKey>;
|
|
32
28
|
|
|
33
29
|
type InferStreamedQueryOutput<TOutput> = TOutput extends AsyncIterable<infer U> ? U[] : never;
|
|
34
30
|
type InferLiveQueryOutput<TOutput> = TOutput extends AsyncIterable<infer U> ? U : never;
|
|
@@ -121,35 +117,44 @@ interface OperationKeyPrefixOptions {
|
|
|
121
117
|
*/
|
|
122
118
|
prefix?: string | undefined;
|
|
123
119
|
}
|
|
124
|
-
|
|
120
|
+
interface OperationKeyBaseOptions extends OperationKeyPrefixOptions {
|
|
121
|
+
/**
|
|
122
|
+
* Serializes inputs into JSON-compatible values when building keys,
|
|
123
|
+
* and compares custom types in structural sharing.
|
|
124
|
+
* Pass the serializer your link uses so custom types are supported.
|
|
125
|
+
*
|
|
126
|
+
* @default new RPCJsonSerializer()
|
|
127
|
+
*/
|
|
128
|
+
serializer?: Public<RPCJsonSerializer> | undefined;
|
|
129
|
+
}
|
|
130
|
+
type OperationKeyOptions<TType extends OperationType, TInput> = OperationKeyBaseOptions & {
|
|
125
131
|
type?: TType | undefined;
|
|
126
132
|
input?: (TType extends 'mutation' ? never : PartialDeep<TInput>) | undefined;
|
|
127
133
|
fnOptions?: (TType extends 'streamed' ? SerializableStreamedQueryOptions : never) | undefined;
|
|
128
134
|
};
|
|
129
|
-
type OperationKeyContent<TType extends OperationType, TInput> = Omit<OperationKeyOptions<TType, TInput>,
|
|
135
|
+
type OperationKeyContent<TType extends OperationType, TInput> = Omit<OperationKeyOptions<TType, TInput>, keyof OperationKeyBaseOptions> & {
|
|
130
136
|
/**
|
|
131
137
|
* Set in place of `input` when the operation is disabled with `skipToken`,
|
|
132
138
|
* because query key hashers drop symbols.
|
|
133
139
|
*/
|
|
134
140
|
skipToken?: TType extends 'mutation' ? never : true;
|
|
141
|
+
/**
|
|
142
|
+
* Serializer types of non-JSON values in `input`, keyed by their JSON-encoded path,
|
|
143
|
+
* so inputs with the same JSON form (like a `Date` and its ISO string) produce different keys.
|
|
144
|
+
* Keyed by path (not a list) so partial keys still match.
|
|
145
|
+
*/
|
|
146
|
+
inputMeta?: Record<string, string[]> | undefined;
|
|
135
147
|
};
|
|
136
148
|
type OperationKey<TType extends OperationType, TInput> = [path: string[], options: OperationKeyContent<TType, TInput>] | [prefix: string, path: string[], options: OperationKeyContent<TType, TInput>];
|
|
137
|
-
declare function generateOperationKey<TType extends OperationType, TInput>(path: string[], options?: OperationKeyOptions<TType, TInput>): OperationKey<TType, TInput>;
|
|
138
|
-
/**
|
|
139
|
-
* Creates a `queryKeyHashFn` that supports oRPC types such as `bigint` and `Date`.
|
|
140
|
-
*
|
|
141
|
-
* @see {@link https://orpc.dev/docs/integrations/tanstack-query#custom-serializers | TanStack Query Integration - Custom Serializers}
|
|
142
|
-
*/
|
|
143
|
-
declare function createQueryKeyHashFn(serializer?: Public<RPCJsonSerializer>): QueryKeyHashFunction<QueryKey>;
|
|
144
149
|
|
|
145
150
|
declare class SharedUtils<TInput> {
|
|
146
151
|
protected readonly path: string[];
|
|
147
|
-
protected readonly options:
|
|
148
|
-
constructor(path: string[], options:
|
|
152
|
+
protected readonly options: OperationKeyBaseOptions;
|
|
153
|
+
constructor(path: string[], options: OperationKeyBaseOptions);
|
|
149
154
|
/**
|
|
150
155
|
* Generate a **partial matching** key for actions like revalidating queries, checking mutation status, etc.
|
|
151
156
|
*/
|
|
152
|
-
key<TType extends OperationType>(options?: Omit<OperationKeyOptions<TType, TInput>,
|
|
157
|
+
key<TType extends OperationType>(options?: Omit<OperationKeyOptions<TType, TInput>, keyof OperationKeyBaseOptions>): OperationKey<TType, TInput>;
|
|
153
158
|
}
|
|
154
159
|
|
|
155
160
|
interface ProcedureUtilsQueryInterceptorOptions<TClientContext extends ClientContext, TInput> {
|
|
@@ -179,7 +184,7 @@ type ProcedureUtilsMutationInterceptor<TClientContext extends ClientContext, TIn
|
|
|
179
184
|
* or a function that receives per-call options and returns overridden this.options.
|
|
180
185
|
*/
|
|
181
186
|
type ProcedureUtilsModifier<T extends object> = Partial<T> | ((options: T) => T);
|
|
182
|
-
interface ProcedureUtilsOptions<TClientContext extends ClientContext, TInput, TOutput, TError> extends
|
|
187
|
+
interface ProcedureUtilsOptions<TClientContext extends ClientContext, TInput, TOutput, TError> extends OperationKeyBaseOptions {
|
|
183
188
|
/**
|
|
184
189
|
* Key options modifier for .queryKey and .queryOptions
|
|
185
190
|
* Can be partial options or a function that receives per-call options and returns override this.options.
|
|
@@ -251,7 +256,6 @@ interface ProcedureUtilsOptions<TClientContext extends ClientContext, TInput, TO
|
|
|
251
256
|
*/
|
|
252
257
|
mutationOptions?: ProcedureUtilsModifier<MutationOptionsIn<TClientContext, TInput, TOutput, TError, unknown>> | undefined;
|
|
253
258
|
}
|
|
254
|
-
declare function isProcedureUtilsOptions(value: unknown): value is ProcedureUtilsOptions<any, any, any, any>;
|
|
255
259
|
/**
|
|
256
260
|
* Merge two procedure utils options where `override` takes priority.
|
|
257
261
|
* A key explicitly set to `undefined` in `override` resets the base value.
|
|
@@ -353,13 +357,6 @@ interface RouterUtilsPlugin<T extends AnyNestedClient> extends OrderablePlugin {
|
|
|
353
357
|
*/
|
|
354
358
|
initProcedureOptions?(path: string[], options: ProcedureUtilsOptions<InferClientContext<T>, any, any, InferClientError<T>>): ProcedureUtilsOptions<InferClientContext<T>, any, any, InferClientError<T>>;
|
|
355
359
|
}
|
|
356
|
-
declare class CompositeRouterUtilsPlugin<T extends AnyNestedClient> implements RouterUtilsPlugin<T> {
|
|
357
|
-
protected readonly plugins: RouterUtilsPlugin<T>[];
|
|
358
|
-
readonly name = "~composite";
|
|
359
|
-
constructor(plugins?: RouterUtilsPlugin<T>[]);
|
|
360
|
-
init(options: RouterUtilsOptions<T>): RouterUtilsOptions<T>;
|
|
361
|
-
initProcedureOptions(path: string[], options: ProcedureUtilsOptions<InferClientContext<T>, any, any, InferClientError<T>>): ProcedureUtilsOptions<InferClientContext<T>, any, any, InferClientError<T>>;
|
|
362
|
-
}
|
|
363
360
|
|
|
364
361
|
/**
|
|
365
362
|
* The utils shape derived from a client: procedure clients map to procedure
|
|
@@ -373,7 +370,7 @@ type RouterUtils<T extends AnyNestedClient> = T extends Client<infer UClientCont
|
|
|
373
370
|
type RouterUtilsScoped<T extends AnyNestedClient> = T extends Client<infer UClientContext, infer UInput, infer UOutput, infer UError> ? ProcedureUtilsOptions<UClientContext, UInput, UOutput, UError> : {
|
|
374
371
|
[K in keyof T]?: (T[K] extends AnyNestedClient ? RouterUtilsScoped<T[K]> : never) | undefined;
|
|
375
372
|
};
|
|
376
|
-
interface RouterUtilsOptions<T extends AnyNestedClient> extends
|
|
373
|
+
interface RouterUtilsOptions<T extends AnyNestedClient> extends OperationKeyBaseOptions {
|
|
377
374
|
/**
|
|
378
375
|
* Base path for all query keys.
|
|
379
376
|
*
|
|
@@ -443,9 +440,20 @@ interface ContractJsonifiedUtilsFactory<TClientContext extends ClientContext> {
|
|
|
443
440
|
}
|
|
444
441
|
interface ContractJsonifiedUtilsFactoryOptions<TClientContext extends ClientContext> extends Omit<RouterUtilsOptions<JsonifiedClient<RouterContractClient<RouterContract, TClientContext>>>, 'path'> {
|
|
445
442
|
}
|
|
446
|
-
|
|
443
|
+
/**
|
|
444
|
+
* Like `createContractUtilsFactory`, but for clients whose types are wrapped in `JsonifiedClient`,
|
|
445
|
+
* such as clients using OpenAPI Link.
|
|
446
|
+
*
|
|
447
|
+
* @remarks
|
|
448
|
+
* Takes a client factory created by `createContractJsonifiedClientFactory` from `@orpc/openapi`.
|
|
449
|
+
*
|
|
450
|
+
* **Note**: The contract must define a `path` meta matching its position in the root router contract.
|
|
451
|
+
*
|
|
452
|
+
* @see {@link https://orpc.dev/docs/contract/client-factory#tanstack-query-integration | Contract Client Factory - TanStack Query Integration}
|
|
453
|
+
*/
|
|
454
|
+
declare function createContractJsonifiedUtilsFactory<TClientContext extends ClientContext>(clientFactory: ContractJsonifiedClientFactory<TClientContext>, options: ContractJsonifiedUtilsFactoryOptions<TClientContext>): ContractJsonifiedUtilsFactory<TClientContext>;
|
|
447
455
|
|
|
448
|
-
type TanstackQueryMetaOptions<TClientContext extends ClientContext, TInput, TOutput, TError> = Omit<ProcedureUtilsOptions<TClientContext, TInput, TOutput, TError>,
|
|
456
|
+
type TanstackQueryMetaOptions<TClientContext extends ClientContext, TInput, TOutput, TError> = Omit<ProcedureUtilsOptions<TClientContext, TInput, TOutput, TError>, keyof OperationKeyBaseOptions>;
|
|
449
457
|
interface TanstackQueryMetaPlugin<TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap> extends MetaPlugin<TInputSchema, TOutputSchema, TErrorMap> {
|
|
450
458
|
name: '~tanstack-query';
|
|
451
459
|
}
|
|
@@ -516,5 +524,18 @@ declare class StreamQuerySSRUtilsPlugin<T extends AnyNestedClient = AnyNestedCli
|
|
|
516
524
|
initProcedureOptions(_path: string[], options: ProcedureUtilsOptions<InferClientContext<T>, any, any, InferClientError<T>>): ProcedureUtilsOptions<InferClientContext<T>, any, any, InferClientError<T>>;
|
|
517
525
|
}
|
|
518
526
|
|
|
519
|
-
|
|
520
|
-
|
|
527
|
+
type StructuralSharing = <T>(oldData: unknown, newData: T) => T;
|
|
528
|
+
/**
|
|
529
|
+
* Creates a `structuralSharing` function that works like TanStack Query's default, but also keeps
|
|
530
|
+
* the previous reference for equal `Date`, `URL`, `Map` and `Set` values, which oRPC's serializer
|
|
531
|
+
* recreates on every response, and for custom types that serialize the same with a type-aware
|
|
532
|
+
* serializer like `RPCJsonSerializer`.
|
|
533
|
+
*
|
|
534
|
+
* Used by default in `.queryOptions`, `.liveOptions` and `.infiniteOptions`, pass `structuralSharing: false` to opt out.
|
|
535
|
+
*
|
|
536
|
+
* @see {@link https://orpc.dev/docs/integrations/tanstack-query#structural-sharing | TanStack Query Integration - Structural Sharing}
|
|
537
|
+
*/
|
|
538
|
+
declare function createStructuralSharing(serializer?: Public<RPCJsonSerializer>): StructuralSharing;
|
|
539
|
+
|
|
540
|
+
export { ContractOptionsUtilsPlugin, OPERATION_CONTEXT_SYMBOL, ProcedureUtils, SharedUtils, StreamQuerySSRUtilsPlugin, OPERATION_CONTEXT_SYMBOL as TANSTACK_QUERY_OPERATION_CONTEXT_SYMBOL, createContractJsonifiedUtilsFactory, createContractUtilsFactory, createHydrationOptions, createRouterUtils, createStructuralSharing, createRouterUtils as createTanstackQueryUtils, getTanstackQueryMeta, mergeProcedureUtilsOptions, tanstackQuery };
|
|
541
|
+
export type { ContractJsonifiedUtilsFactory, ContractJsonifiedUtilsFactoryOptions, ContractUtilsFactory, ContractUtilsFactoryOptions, InferLiveQueryOutput, InferStreamedQueryOutput, InfiniteKeyOptions, InfiniteOptionsIn, InfiniteOptionsOut, MutationKeyOptions, MutationOptionsIn, MutationOptionsOut, OperationContext, OperationKey, OperationKeyBaseOptions, OperationKeyOptions, OperationKeyPrefixOptions, OperationType, ProcedureUtilsInfiniteInterceptor, ProcedureUtilsInfiniteInterceptorOptions, ProcedureUtilsLiveInterceptor, ProcedureUtilsLiveInterceptorOptions, ProcedureUtilsModifier, ProcedureUtilsMutationInterceptor, ProcedureUtilsMutationInterceptorOptions, ProcedureUtilsOptions, ProcedureUtilsQueryInterceptor, ProcedureUtilsQueryInterceptorOptions, ProcedureUtilsStreamedInterceptor, ProcedureUtilsStreamedInterceptorOptions, QueryKeyOptions, QueryOptionsIn, QueryOptionsOut, RouterUtils, RouterUtilsOptions, RouterUtilsPlugin, RouterUtilsScoped, SerializableStreamedQueryOptions, StreamQuerySSRUtilsPluginOptions, StreamedKeyOptions, StreamedOptionsIn, StreamedOptionsOut, StructuralSharing, TanstackQueryMetaOptions, TanstackQueryMetaPlugin, OperationContext as TanstackQueryOperationContext };
|
package/dist/index.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { resolveBasePathMeta, getRouterContract, ProcedureContract } from '@orpc/contract';
|
|
2
|
-
import { sortPlugins, stringifyJSON,
|
|
2
|
+
import { sortPlugins, throwIfAborted, stringifyJSON, setOwn, resolveMaybeOptionalOptions, intercept, toArray, isAsyncIteratorObject, isTypescriptObject, bindMethods, pick, get } from '@orpc/shared';
|
|
3
3
|
import { RPCJsonSerializer, RECURSIVE_CLIENT_UNWRAP_KEYS } from '@orpc/client';
|
|
4
|
-
import {
|
|
4
|
+
import { skipToken } from '@tanstack/query-core';
|
|
5
5
|
|
|
6
6
|
class CompositeRouterUtilsPlugin {
|
|
7
7
|
constructor(plugins = []) {
|
|
@@ -27,27 +27,6 @@ class CompositeRouterUtilsPlugin {
|
|
|
27
27
|
}
|
|
28
28
|
}
|
|
29
29
|
|
|
30
|
-
function generateOperationKey(path, options = {}) {
|
|
31
|
-
return [
|
|
32
|
-
...options.prefix !== void 0 ? [options.prefix] : [],
|
|
33
|
-
path,
|
|
34
|
-
{
|
|
35
|
-
...options.input === skipToken ? { skipToken: true } : options.input !== void 0 ? { input: options.input } : {},
|
|
36
|
-
...options.type !== void 0 ? { type: options.type } : {},
|
|
37
|
-
...options.fnOptions !== void 0 ? { fnOptions: options.fnOptions } : {}
|
|
38
|
-
}
|
|
39
|
-
];
|
|
40
|
-
}
|
|
41
|
-
function createQueryKeyHashFn(serializer = new RPCJsonSerializer()) {
|
|
42
|
-
return (queryKey) => {
|
|
43
|
-
const { json, meta } = serializer.serializeJson(queryKey);
|
|
44
|
-
return hashKey([
|
|
45
|
-
json,
|
|
46
|
-
meta?.map((entry) => stringifyJSON(entry)).sort()
|
|
47
|
-
]);
|
|
48
|
-
};
|
|
49
|
-
}
|
|
50
|
-
|
|
51
30
|
function liveQuery(queryFn) {
|
|
52
31
|
return async (context) => {
|
|
53
32
|
const stream = await queryFn(context);
|
|
@@ -66,6 +45,30 @@ function liveQuery(queryFn) {
|
|
|
66
45
|
};
|
|
67
46
|
}
|
|
68
47
|
|
|
48
|
+
const defaultSerializer = new RPCJsonSerializer();
|
|
49
|
+
function generateOperationKey(path, options = {}) {
|
|
50
|
+
return [
|
|
51
|
+
...options.prefix !== void 0 ? [options.prefix] : [],
|
|
52
|
+
path,
|
|
53
|
+
{
|
|
54
|
+
...options.input === skipToken ? { skipToken: true } : options.input !== void 0 ? serializeInput(options.serializer ?? defaultSerializer, options.input) : {},
|
|
55
|
+
...options.type !== void 0 ? { type: options.type } : {},
|
|
56
|
+
...options.fnOptions !== void 0 ? { fnOptions: options.fnOptions } : {}
|
|
57
|
+
}
|
|
58
|
+
];
|
|
59
|
+
}
|
|
60
|
+
function serializeInput(serializer, input) {
|
|
61
|
+
const { json, meta } = serializer.serializeJson(input);
|
|
62
|
+
if (!meta?.length) {
|
|
63
|
+
return { input: json };
|
|
64
|
+
}
|
|
65
|
+
const inputMeta = {};
|
|
66
|
+
for (const [type, ...segments] of meta) {
|
|
67
|
+
(inputMeta[stringifyJSON(segments)] ??= []).push(type);
|
|
68
|
+
}
|
|
69
|
+
return { input: json, inputMeta };
|
|
70
|
+
}
|
|
71
|
+
|
|
69
72
|
class SharedUtils {
|
|
70
73
|
constructor(path, options) {
|
|
71
74
|
this.path = path;
|
|
@@ -75,7 +78,7 @@ class SharedUtils {
|
|
|
75
78
|
* Generate a **partial matching** key for actions like revalidating queries, checking mutation status, etc.
|
|
76
79
|
*/
|
|
77
80
|
key(options = {}) {
|
|
78
|
-
return generateOperationKey(this.path, { ...options, prefix: this.options.prefix });
|
|
81
|
+
return generateOperationKey(this.path, { ...options, prefix: this.options.prefix, serializer: this.options.serializer });
|
|
79
82
|
}
|
|
80
83
|
}
|
|
81
84
|
|
|
@@ -138,6 +141,142 @@ function limitArraySize(items, maxSize) {
|
|
|
138
141
|
return items.slice(items.length - maxSize);
|
|
139
142
|
}
|
|
140
143
|
|
|
144
|
+
function createStructuralSharing(serializer) {
|
|
145
|
+
return (oldData, newData) => replaceEqualDeep(oldData, newData, 0, serializer);
|
|
146
|
+
}
|
|
147
|
+
function replaceEqualDeep(a, b, depth, serializer) {
|
|
148
|
+
if (Object.is(a, b)) {
|
|
149
|
+
return a;
|
|
150
|
+
}
|
|
151
|
+
if (depth > 500 || typeof a !== "object" || typeof b !== "object" || a === null || b === null) {
|
|
152
|
+
return b;
|
|
153
|
+
}
|
|
154
|
+
const proto = Object.getPrototypeOf(a);
|
|
155
|
+
if (proto !== Object.getPrototypeOf(b)) {
|
|
156
|
+
return b;
|
|
157
|
+
}
|
|
158
|
+
if ((proto === Date.prototype || proto === URL.prototype || proto === Set.prototype || proto === Map.prototype) && !hasOwnEnumerableKey(a) && !hasOwnEnumerableKey(b)) {
|
|
159
|
+
return replaceEqualBuiltIn(a, b, proto, depth, serializer);
|
|
160
|
+
}
|
|
161
|
+
if (Array.isArray(a)) {
|
|
162
|
+
return isPlainArray(a) && isPlainArray(b) ? replaceEqualArray(a, b, depth, serializer) : b;
|
|
163
|
+
}
|
|
164
|
+
if (proto === Object.prototype || proto === null || !proto.constructor) {
|
|
165
|
+
return replaceEqualObject(a, b, proto, depth, serializer);
|
|
166
|
+
}
|
|
167
|
+
return replaceEqualSerialized(a, b, depth, serializer);
|
|
168
|
+
}
|
|
169
|
+
function replaceEqualArray(a, b, depth, serializer) {
|
|
170
|
+
let copy;
|
|
171
|
+
for (let i = 0; i < b.length; i++) {
|
|
172
|
+
let value = b[i];
|
|
173
|
+
if (i < a.length) {
|
|
174
|
+
const aValue = a[i];
|
|
175
|
+
if (typeof aValue === "object" && typeof value === "object") {
|
|
176
|
+
value = replaceEqualDeep(aValue, value, depth + 1, serializer);
|
|
177
|
+
}
|
|
178
|
+
if (copy === void 0 && Object.is(value, aValue)) {
|
|
179
|
+
continue;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
copy ??= copyArrayPrefix(a, i);
|
|
183
|
+
copy.push(value);
|
|
184
|
+
}
|
|
185
|
+
return copy ?? (a.length === b.length ? a : copyArrayPrefix(a, b.length));
|
|
186
|
+
}
|
|
187
|
+
function copyArrayPrefix(a, end) {
|
|
188
|
+
const copy = [];
|
|
189
|
+
for (let i = 0; i < end; i++) {
|
|
190
|
+
copy.push(a[i]);
|
|
191
|
+
}
|
|
192
|
+
return copy;
|
|
193
|
+
}
|
|
194
|
+
function replaceEqualObject(a, b, proto, depth, serializer) {
|
|
195
|
+
const bKeys = Object.keys(b);
|
|
196
|
+
let copy;
|
|
197
|
+
for (let i = 0; i < bKeys.length; i++) {
|
|
198
|
+
const key = bKeys[i];
|
|
199
|
+
let value = b[key];
|
|
200
|
+
if (Object.hasOwn(a, key)) {
|
|
201
|
+
const aValue = a[key];
|
|
202
|
+
if (typeof aValue === "object" && typeof value === "object") {
|
|
203
|
+
value = replaceEqualDeep(aValue, value, depth + 1, serializer);
|
|
204
|
+
}
|
|
205
|
+
if (copy === void 0 && Object.is(value, aValue)) {
|
|
206
|
+
continue;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
copy ??= copyObjectPrefix(a, bKeys, i, proto);
|
|
210
|
+
assign(copy, key, value);
|
|
211
|
+
}
|
|
212
|
+
return copy ?? (Object.keys(a).length === bKeys.length ? a : copyObjectPrefix(a, bKeys, bKeys.length, proto));
|
|
213
|
+
}
|
|
214
|
+
function copyObjectPrefix(a, keys, end, proto) {
|
|
215
|
+
const copy = proto === Object.prototype ? {} : Object.create(proto);
|
|
216
|
+
for (let i = 0; i < end; i++) {
|
|
217
|
+
assign(copy, keys[i], a[keys[i]]);
|
|
218
|
+
}
|
|
219
|
+
return copy;
|
|
220
|
+
}
|
|
221
|
+
function assign(object, key, value) {
|
|
222
|
+
if (key === "__proto__") {
|
|
223
|
+
setOwn(object, key, value);
|
|
224
|
+
} else {
|
|
225
|
+
object[key] = value;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
function replaceEqualBuiltIn(a, b, proto, depth, serializer) {
|
|
229
|
+
if (proto === Date.prototype) {
|
|
230
|
+
return Object.is(a.getTime(), b.getTime()) ? a : b;
|
|
231
|
+
}
|
|
232
|
+
if (proto === URL.prototype) {
|
|
233
|
+
return a.href === b.href ? a : b;
|
|
234
|
+
}
|
|
235
|
+
if (a.size !== b.size) {
|
|
236
|
+
return b;
|
|
237
|
+
}
|
|
238
|
+
if (proto === Set.prototype) {
|
|
239
|
+
const aValues2 = a.values();
|
|
240
|
+
for (const bValue of b.values()) {
|
|
241
|
+
const aValue = aValues2.next().value;
|
|
242
|
+
if (!Object.is(replaceEqualDeep(aValue, bValue, depth + 1, serializer), aValue)) {
|
|
243
|
+
return b;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
return a;
|
|
247
|
+
}
|
|
248
|
+
const aKeys = a.keys();
|
|
249
|
+
const aValues = a.values();
|
|
250
|
+
const bValues = b.values();
|
|
251
|
+
for (const bKey of b.keys()) {
|
|
252
|
+
const aKey = aKeys.next().value;
|
|
253
|
+
const aValue = aValues.next().value;
|
|
254
|
+
if (!Object.is(replaceEqualDeep(aKey, bKey, depth + 1, serializer), aKey) || !Object.is(replaceEqualDeep(aValue, bValues.next().value, depth + 1, serializer), aValue)) {
|
|
255
|
+
return b;
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
return a;
|
|
259
|
+
}
|
|
260
|
+
function replaceEqualSerialized(a, b, depth, serializer) {
|
|
261
|
+
const aSerialized = serializer?.serializeJson(a);
|
|
262
|
+
if (!aSerialized?.meta?.length) {
|
|
263
|
+
return b;
|
|
264
|
+
}
|
|
265
|
+
const bSerialized = serializer.serializeJson(b);
|
|
266
|
+
return stringifyJSON(aSerialized.meta) === stringifyJSON(bSerialized.meta) && Object.is(replaceEqualDeep(aSerialized.json, bSerialized.json, depth + 1, serializer), aSerialized.json) ? a : b;
|
|
267
|
+
}
|
|
268
|
+
function hasOwnEnumerableKey(value) {
|
|
269
|
+
for (const key in value) {
|
|
270
|
+
if (Object.hasOwn(value, key)) {
|
|
271
|
+
return true;
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
return false;
|
|
275
|
+
}
|
|
276
|
+
function isPlainArray(value) {
|
|
277
|
+
return value.length === Object.keys(value).length;
|
|
278
|
+
}
|
|
279
|
+
|
|
141
280
|
const OPERATION_CONTEXT_SYMBOL = Symbol.for("ORPC_TANSTACK_QUERY_OPERATION_CONTEXT");
|
|
142
281
|
|
|
143
282
|
const PROCEDURE_UTILS_INTERCEPTOR_KEYS = [
|
|
@@ -171,6 +310,10 @@ function isProcedureUtilsOptions(value) {
|
|
|
171
310
|
if (typeof value[key] !== "string") {
|
|
172
311
|
return false;
|
|
173
312
|
}
|
|
313
|
+
} else if (key === "serializer") {
|
|
314
|
+
if (!isTypescriptObject(value[key]) || typeof value[key].serializeJson !== "function") {
|
|
315
|
+
return false;
|
|
316
|
+
}
|
|
174
317
|
} else if (PROCEDURE_UTILS_INTERCEPTOR_KEYS.includes(key)) {
|
|
175
318
|
if (!Array.isArray(value[key]) || value[key].some((i) => typeof i !== "function")) {
|
|
176
319
|
return false;
|
|
@@ -230,7 +373,7 @@ class ProcedureUtils extends SharedUtils {
|
|
|
230
373
|
} else if (this.options.queryKey) {
|
|
231
374
|
optionsIn = { ...this.options.queryKey, ...optionsIn };
|
|
232
375
|
}
|
|
233
|
-
const queryKey = optionsIn.queryKey ??
|
|
376
|
+
const queryKey = optionsIn.queryKey ?? this.key({ type: "query", input: optionsIn.input });
|
|
234
377
|
return queryKey;
|
|
235
378
|
}
|
|
236
379
|
/**
|
|
@@ -245,6 +388,7 @@ class ProcedureUtils extends SharedUtils {
|
|
|
245
388
|
}
|
|
246
389
|
const queryKey = this.queryKey(optionsIn);
|
|
247
390
|
return {
|
|
391
|
+
structuralSharing: createStructuralSharing(this.options.serializer),
|
|
248
392
|
...optionsIn,
|
|
249
393
|
queryKey,
|
|
250
394
|
queryFn: (fnContext) => {
|
|
@@ -286,7 +430,7 @@ class ProcedureUtils extends SharedUtils {
|
|
|
286
430
|
} else if (this.options.streamedKey) {
|
|
287
431
|
optionsIn = { ...this.options.streamedKey, ...optionsIn };
|
|
288
432
|
}
|
|
289
|
-
const queryKey = optionsIn.queryKey ??
|
|
433
|
+
const queryKey = optionsIn.queryKey ?? this.key({ type: "streamed", input: optionsIn.input, fnOptions: optionsIn.queryFnOptions });
|
|
290
434
|
return queryKey;
|
|
291
435
|
}
|
|
292
436
|
/**
|
|
@@ -353,7 +497,7 @@ class ProcedureUtils extends SharedUtils {
|
|
|
353
497
|
} else if (this.options.liveKey) {
|
|
354
498
|
optionsIn = { ...this.options.liveKey, ...optionsIn };
|
|
355
499
|
}
|
|
356
|
-
const queryKey = optionsIn.queryKey ??
|
|
500
|
+
const queryKey = optionsIn.queryKey ?? this.key({ type: "live", input: optionsIn.input });
|
|
357
501
|
return queryKey;
|
|
358
502
|
}
|
|
359
503
|
/**
|
|
@@ -370,6 +514,7 @@ class ProcedureUtils extends SharedUtils {
|
|
|
370
514
|
}
|
|
371
515
|
const queryKey = this.liveKey(optionsIn);
|
|
372
516
|
return {
|
|
517
|
+
structuralSharing: createStructuralSharing(this.options.serializer),
|
|
373
518
|
...optionsIn,
|
|
374
519
|
queryKey,
|
|
375
520
|
queryFn: (fnContext) => {
|
|
@@ -419,8 +564,7 @@ class ProcedureUtils extends SharedUtils {
|
|
|
419
564
|
} else if (this.options.infiniteKey) {
|
|
420
565
|
optionsIn = { ...this.options.infiniteKey, ...optionsIn };
|
|
421
566
|
}
|
|
422
|
-
const queryKey = optionsIn.queryKey ??
|
|
423
|
-
prefix: this.options.prefix,
|
|
567
|
+
const queryKey = optionsIn.queryKey ?? this.key({
|
|
424
568
|
type: "infinite",
|
|
425
569
|
input: optionsIn.input === skipToken ? skipToken : optionsIn.input(optionsIn.initialPageParam)
|
|
426
570
|
});
|
|
@@ -437,6 +581,7 @@ class ProcedureUtils extends SharedUtils {
|
|
|
437
581
|
}
|
|
438
582
|
const queryKey = this.infiniteKey(optionsIn);
|
|
439
583
|
return {
|
|
584
|
+
structuralSharing: createStructuralSharing(this.options.serializer),
|
|
440
585
|
...optionsIn,
|
|
441
586
|
queryKey,
|
|
442
587
|
queryFn: (fnContext) => {
|
|
@@ -477,7 +622,7 @@ class ProcedureUtils extends SharedUtils {
|
|
|
477
622
|
} else if (this.options.mutationKey) {
|
|
478
623
|
optionsIn = { ...this.options.mutationKey, ...optionsIn };
|
|
479
624
|
}
|
|
480
|
-
const mutationKey = optionsIn.mutationKey ??
|
|
625
|
+
const mutationKey = optionsIn.mutationKey ?? this.key({ type: "mutation" });
|
|
481
626
|
return mutationKey;
|
|
482
627
|
}
|
|
483
628
|
/**
|
|
@@ -534,6 +679,7 @@ function createRouterUtilsInternal(client, options, plugin) {
|
|
|
534
679
|
plugin.initProcedureOptions(path, mergeProcedureUtilsOptions(
|
|
535
680
|
pick(options, [
|
|
536
681
|
"prefix",
|
|
682
|
+
"serializer",
|
|
537
683
|
"queryInterceptors",
|
|
538
684
|
"streamedInterceptors",
|
|
539
685
|
"liveInterceptors",
|
|
@@ -547,8 +693,11 @@ function createRouterUtilsInternal(client, options, plugin) {
|
|
|
547
693
|
const recursive = new Proxy(utils, {
|
|
548
694
|
get(target, prop) {
|
|
549
695
|
const value = Reflect.get(target, prop);
|
|
696
|
+
if (RECURSIVE_CLIENT_UNWRAP_KEYS.has(prop)) {
|
|
697
|
+
return value ?? Reflect.get(Object.prototype, prop);
|
|
698
|
+
}
|
|
550
699
|
const nextClient = Reflect.get(client, prop);
|
|
551
|
-
if (typeof prop !== "string" ||
|
|
700
|
+
if (typeof prop !== "string" || !isTypescriptObject(nextClient)) {
|
|
552
701
|
return value;
|
|
553
702
|
}
|
|
554
703
|
let result = cache.get(prop);
|
|
@@ -669,4 +818,4 @@ class StreamQuerySSRUtilsPlugin {
|
|
|
669
818
|
}
|
|
670
819
|
}
|
|
671
820
|
|
|
672
|
-
export {
|
|
821
|
+
export { ContractOptionsUtilsPlugin, OPERATION_CONTEXT_SYMBOL, ProcedureUtils, SharedUtils, StreamQuerySSRUtilsPlugin, OPERATION_CONTEXT_SYMBOL as TANSTACK_QUERY_OPERATION_CONTEXT_SYMBOL, createContractJsonifiedUtilsFactory, createContractUtilsFactory, createHydrationOptions, createRouterUtils, createStructuralSharing, createRouterUtils as createTanstackQueryUtils, getTanstackQueryMeta, mergeProcedureUtilsOptions, tanstackQuery };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@orpc/tanstack-query",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "2.0.0-beta.
|
|
4
|
+
"version": "2.0.0-beta.47",
|
|
5
5
|
"description": "TanStack Query integration for oRPC: typesafe queries, mutations, streaming, and SSR for React, Vue, Solid, Svelte, and Angular",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"funding": [
|
|
@@ -44,10 +44,10 @@
|
|
|
44
44
|
"@tanstack/query-core": ">=5.89.0"
|
|
45
45
|
},
|
|
46
46
|
"dependencies": {
|
|
47
|
-
"@orpc/client": "2.0.0-beta.
|
|
48
|
-
"@orpc/contract": "2.0.0-beta.
|
|
49
|
-
"@orpc/openapi": "2.0.0-beta.
|
|
50
|
-
"@orpc/shared": "2.0.0-beta.
|
|
47
|
+
"@orpc/client": "2.0.0-beta.47",
|
|
48
|
+
"@orpc/contract": "2.0.0-beta.47",
|
|
49
|
+
"@orpc/openapi": "2.0.0-beta.47",
|
|
50
|
+
"@orpc/shared": "2.0.0-beta.47"
|
|
51
51
|
},
|
|
52
52
|
"devDependencies": {
|
|
53
53
|
"@angular/core": "^22.2.3",
|