@orpc/tanstack-query 2.0.0-beta.48 → 2.0.0-beta.49
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 +4 -4
- package/dist/contract-utils.d.ts +37 -0
- package/dist/contract-utils.js +42 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.js +10 -0
- package/dist/key.d.ts +50 -0
- package/dist/key.js +50 -0
- package/dist/live-query.d.ts +4 -0
- package/dist/live-query.js +36 -0
- package/dist/meta.d.ts +42 -0
- package/dist/meta.js +55 -0
- package/dist/plugin.d.ts +51 -0
- package/dist/plugin.js +25 -0
- package/dist/procedure-utils.d.ts +167 -0
- package/dist/procedure-utils.js +411 -0
- package/dist/router-utils.d.ts +67 -0
- package/dist/router-utils.js +69 -0
- package/dist/shared-utils.d.ts +11 -0
- package/dist/shared-utils.js +19 -0
- package/dist/ssr.d.ts +37 -0
- package/dist/ssr.js +45 -0
- package/dist/stream-query.d.ts +38 -0
- package/dist/stream-query.js +52 -0
- package/dist/structural-sharing.d.ts +14 -0
- package/dist/structural-sharing.js +160 -0
- package/dist/types.d.ts +87 -0
- package/dist/types.js +7 -0
- package/package.json +39 -41
- package/dist/contract-utils.d.mts +0 -35
- package/dist/contract-utils.mjs +0 -23
- package/dist/index.d.mts +0 -16
- package/dist/index.mjs +0 -10
- package/dist/key.d.mts +0 -62
- package/dist/key.mjs +0 -30
- package/dist/live-query.d.mts +0 -6
- package/dist/live-query.mjs +0 -16
- package/dist/meta.d.mts +0 -55
- package/dist/meta.mjs +0 -37
- package/dist/plugin.d.mts +0 -51
- package/dist/plugin.mjs +0 -25
- package/dist/procedure-utils.d.mts +0 -232
- package/dist/procedure-utils.mjs +0 -406
- package/dist/router-utils.d.mts +0 -57
- package/dist/router-utils.mjs +0 -51
- package/dist/shared-utils.d.mts +0 -11
- package/dist/shared-utils.mjs +0 -17
- package/dist/ssr.d.mts +0 -38
- package/dist/ssr.mjs +0 -57
- package/dist/stream-query.d.mts +0 -30
- package/dist/stream-query.mjs +0 -58
- package/dist/structural-sharing.d.mts +0 -14
- package/dist/structural-sharing.mjs +0 -140
- package/dist/types.d.mts +0 -138
- package/dist/types.mjs +0 -2
|
@@ -0,0 +1,411 @@
|
|
|
1
|
+
import { intercept, isAsyncIteratorObject, isNonPrimitive, resolveMaybeOptionalOptions, toArray, } from '@orpc/shared';
|
|
2
|
+
import { skipToken } from '@tanstack/query-core';
|
|
3
|
+
import { liveQuery } from './live-query.js';
|
|
4
|
+
import { SharedUtils } from './shared-utils.js';
|
|
5
|
+
import { serializableStreamedQuery } from './stream-query.js';
|
|
6
|
+
import { createStructuralSharing } from './structural-sharing.js';
|
|
7
|
+
import { OPERATION_CONTEXT_SYMBOL } from './types.js';
|
|
8
|
+
const PROCEDURE_UTILS_INTERCEPTOR_KEYS = [
|
|
9
|
+
'queryInterceptors',
|
|
10
|
+
'streamedInterceptors',
|
|
11
|
+
'liveInterceptors',
|
|
12
|
+
'infiniteInterceptors',
|
|
13
|
+
'mutationInterceptors',
|
|
14
|
+
];
|
|
15
|
+
const PROCEDURE_UTILS_MODIFIER_KEYS = [
|
|
16
|
+
'queryKey',
|
|
17
|
+
'queryOptions',
|
|
18
|
+
'streamedKey',
|
|
19
|
+
'streamedOptions',
|
|
20
|
+
'liveKey',
|
|
21
|
+
'liveOptions',
|
|
22
|
+
'infiniteKey',
|
|
23
|
+
'infiniteOptions',
|
|
24
|
+
'mutationKey',
|
|
25
|
+
'mutationOptions',
|
|
26
|
+
];
|
|
27
|
+
export function isProcedureUtilsOptions(value) {
|
|
28
|
+
if (!isNonPrimitive(value)) {
|
|
29
|
+
return false;
|
|
30
|
+
}
|
|
31
|
+
for (const key of Object.keys(value)) {
|
|
32
|
+
if (value[key] === undefined) {
|
|
33
|
+
continue;
|
|
34
|
+
}
|
|
35
|
+
if (key === 'prefix') {
|
|
36
|
+
if (typeof value[key] !== 'string') {
|
|
37
|
+
return false;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
else if (key === 'serializer') {
|
|
41
|
+
if (!isNonPrimitive(value[key]) || typeof value[key].serializeJson !== 'function') {
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
else if (PROCEDURE_UTILS_INTERCEPTOR_KEYS.includes(key)) {
|
|
46
|
+
if (!Array.isArray(value[key]) || value[key].some((i) => typeof i !== 'function')) {
|
|
47
|
+
return false;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
else if (PROCEDURE_UTILS_MODIFIER_KEYS.includes(key)) {
|
|
51
|
+
if (!isNonPrimitive(value[key])) {
|
|
52
|
+
return false;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return true;
|
|
57
|
+
}
|
|
58
|
+
function mergeProcedureUtilsModifier(base, override) {
|
|
59
|
+
if (typeof override !== 'function') {
|
|
60
|
+
if (typeof base !== 'function') {
|
|
61
|
+
return { ...base, ...override };
|
|
62
|
+
}
|
|
63
|
+
return (options) => base({ ...override, ...options });
|
|
64
|
+
}
|
|
65
|
+
const applyBase = typeof base === 'function' ? base : (options) => ({ ...base, ...options });
|
|
66
|
+
return (options) => override(applyBase(options));
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Merge two procedure utils options where `override` takes priority.
|
|
70
|
+
* A key explicitly set to `undefined` in `override` resets the base value.
|
|
71
|
+
* When both sides define a key, interceptors are concatenated (base ones run first) and
|
|
72
|
+
* modifiers are combined so that, for object values, per-call options beat `override`,
|
|
73
|
+
* which beats `base`. A function modifier receives the merged options and its result wins.
|
|
74
|
+
*/
|
|
75
|
+
export function mergeProcedureUtilsOptions(base, override) {
|
|
76
|
+
const merged = { ...base, ...override };
|
|
77
|
+
for (const key of PROCEDURE_UTILS_INTERCEPTOR_KEYS) {
|
|
78
|
+
const baseValue = base[key];
|
|
79
|
+
const overrideValue = override[key];
|
|
80
|
+
if (baseValue && overrideValue) {
|
|
81
|
+
merged[key] = [...baseValue, ...overrideValue];
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
for (const key of PROCEDURE_UTILS_MODIFIER_KEYS) {
|
|
85
|
+
const baseValue = base[key];
|
|
86
|
+
const overrideValue = override[key];
|
|
87
|
+
if (baseValue && overrideValue) {
|
|
88
|
+
merged[key] = mergeProcedureUtilsModifier(baseValue, overrideValue);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
return merged;
|
|
92
|
+
}
|
|
93
|
+
export class ProcedureUtils extends SharedUtils {
|
|
94
|
+
/**
|
|
95
|
+
* Calling corresponding procedure client
|
|
96
|
+
*/
|
|
97
|
+
call;
|
|
98
|
+
constructor(path, client, options = {}) {
|
|
99
|
+
super(path, options);
|
|
100
|
+
this.call = client;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Generate a **full matching** key for [Query Options](https://orpc.dev/docs/integrations/tanstack-query#query-options).
|
|
104
|
+
*/
|
|
105
|
+
queryKey(...rest) {
|
|
106
|
+
let optionsIn = resolveMaybeOptionalOptions(rest);
|
|
107
|
+
if (typeof this.options.queryKey === 'function') {
|
|
108
|
+
optionsIn = this.options.queryKey(optionsIn);
|
|
109
|
+
}
|
|
110
|
+
else if (this.options.queryKey) {
|
|
111
|
+
optionsIn = { ...this.options.queryKey, ...optionsIn };
|
|
112
|
+
}
|
|
113
|
+
const queryKey = optionsIn.queryKey ?? this.key({ type: 'query', input: optionsIn.input });
|
|
114
|
+
return queryKey;
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Generate options used for useQuery/useSuspenseQuery/prefetchQuery/...
|
|
118
|
+
*/
|
|
119
|
+
queryOptions(...rest) {
|
|
120
|
+
let optionsIn = resolveMaybeOptionalOptions(rest);
|
|
121
|
+
if (typeof this.options.queryOptions === 'function') {
|
|
122
|
+
optionsIn = this.options.queryOptions(optionsIn);
|
|
123
|
+
}
|
|
124
|
+
else if (this.options.queryOptions) {
|
|
125
|
+
optionsIn = { ...this.options.queryOptions, ...optionsIn };
|
|
126
|
+
}
|
|
127
|
+
const queryKey = this.queryKey(optionsIn);
|
|
128
|
+
return {
|
|
129
|
+
structuralSharing: createStructuralSharing(this.options.serializer),
|
|
130
|
+
...optionsIn,
|
|
131
|
+
queryKey,
|
|
132
|
+
queryFn: (fnContext) => {
|
|
133
|
+
return intercept(this.options.queryInterceptors, {
|
|
134
|
+
path: this.path,
|
|
135
|
+
context: {
|
|
136
|
+
[OPERATION_CONTEXT_SYMBOL]: {
|
|
137
|
+
key: queryKey,
|
|
138
|
+
type: 'query',
|
|
139
|
+
},
|
|
140
|
+
...optionsIn.context,
|
|
141
|
+
},
|
|
142
|
+
input: optionsIn.input,
|
|
143
|
+
fnContext,
|
|
144
|
+
}, ({ context, input, fnContext }) => {
|
|
145
|
+
if (input === skipToken || optionsIn.queryFn === skipToken) {
|
|
146
|
+
throw new Error('queryFn should not be called when skipToken used for skipping');
|
|
147
|
+
}
|
|
148
|
+
if (optionsIn.queryFn) {
|
|
149
|
+
return optionsIn.queryFn(fnContext);
|
|
150
|
+
}
|
|
151
|
+
return this.call(input, { signal: fnContext.signal, context });
|
|
152
|
+
});
|
|
153
|
+
},
|
|
154
|
+
...(optionsIn.input === skipToken || optionsIn.queryFn === skipToken
|
|
155
|
+
? { enabled: false }
|
|
156
|
+
: {}),
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Generate a **full matching** key for [Streamed Query Options](https://orpc.dev/docs/integrations/tanstack-query#streamed-query-options).
|
|
161
|
+
*/
|
|
162
|
+
streamedKey(...rest) {
|
|
163
|
+
let optionsIn = resolveMaybeOptionalOptions(rest);
|
|
164
|
+
if (typeof this.options.streamedKey === 'function') {
|
|
165
|
+
optionsIn = this.options.streamedKey(optionsIn);
|
|
166
|
+
}
|
|
167
|
+
else if (this.options.streamedKey) {
|
|
168
|
+
optionsIn = { ...this.options.streamedKey, ...optionsIn };
|
|
169
|
+
}
|
|
170
|
+
const queryKey = optionsIn.queryKey ??
|
|
171
|
+
this.key({
|
|
172
|
+
type: 'streamed',
|
|
173
|
+
input: optionsIn.input,
|
|
174
|
+
fnOptions: optionsIn.queryFnOptions,
|
|
175
|
+
});
|
|
176
|
+
return queryKey;
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Configure queries for [AsyncIteratorObject](https://orpc.dev/docs/async-iterator-object).
|
|
180
|
+
* This is built on [TanStack Query streamedQuery](https://tanstack.com/query/latest/docs/reference/streamedQuery)
|
|
181
|
+
* and works with hooks like `useQuery`, `useSuspenseQuery`, or `prefetchQuery`.
|
|
182
|
+
* The query never returns to pending once it has data.
|
|
183
|
+
*/
|
|
184
|
+
streamedOptions(...rest) {
|
|
185
|
+
let optionsIn = resolveMaybeOptionalOptions(rest);
|
|
186
|
+
if (typeof this.options.streamedOptions === 'function') {
|
|
187
|
+
optionsIn = this.options.streamedOptions(optionsIn);
|
|
188
|
+
}
|
|
189
|
+
else if (this.options.streamedOptions) {
|
|
190
|
+
optionsIn = { ...this.options.streamedOptions, ...optionsIn };
|
|
191
|
+
}
|
|
192
|
+
const queryKey = this.streamedKey(optionsIn);
|
|
193
|
+
const { settleOnFirstData, ...queryOptions } = optionsIn;
|
|
194
|
+
return {
|
|
195
|
+
...queryOptions,
|
|
196
|
+
queryKey,
|
|
197
|
+
queryFn: (fnContext) => {
|
|
198
|
+
return intercept(toArray(this.options.streamedInterceptors), {
|
|
199
|
+
path: this.path,
|
|
200
|
+
context: {
|
|
201
|
+
[OPERATION_CONTEXT_SYMBOL]: {
|
|
202
|
+
key: queryKey,
|
|
203
|
+
type: 'streamed',
|
|
204
|
+
},
|
|
205
|
+
...optionsIn.context,
|
|
206
|
+
},
|
|
207
|
+
input: optionsIn.input,
|
|
208
|
+
fnContext,
|
|
209
|
+
}, ({ context, fnContext, input }) => {
|
|
210
|
+
if (input === skipToken || optionsIn.queryFn === skipToken) {
|
|
211
|
+
throw new Error('queryFn should not be called when skipToken used for skipping');
|
|
212
|
+
}
|
|
213
|
+
if (optionsIn.queryFn) {
|
|
214
|
+
return optionsIn.queryFn(fnContext);
|
|
215
|
+
}
|
|
216
|
+
return serializableStreamedQuery(async (queryContext) => {
|
|
217
|
+
const output = await this.call(input, { signal: queryContext.signal, context });
|
|
218
|
+
if (!isAsyncIteratorObject(output)) {
|
|
219
|
+
throw new Error('streamedQuery requires an AsyncIteratorObject output');
|
|
220
|
+
}
|
|
221
|
+
return output;
|
|
222
|
+
}, { ...optionsIn.queryFnOptions, settleOnFirstData })(fnContext);
|
|
223
|
+
});
|
|
224
|
+
},
|
|
225
|
+
...(optionsIn.input === skipToken || optionsIn.queryFn === skipToken
|
|
226
|
+
? { enabled: false }
|
|
227
|
+
: {}),
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* Generate a **full matching** key for [Live Query Options](https://orpc.dev/docs/integrations/tanstack-query#live-query-options).
|
|
232
|
+
*/
|
|
233
|
+
liveKey(...rest) {
|
|
234
|
+
let optionsIn = resolveMaybeOptionalOptions(rest);
|
|
235
|
+
if (typeof this.options.liveKey === 'function') {
|
|
236
|
+
optionsIn = this.options.liveKey(optionsIn);
|
|
237
|
+
}
|
|
238
|
+
else if (this.options.liveKey) {
|
|
239
|
+
optionsIn = { ...this.options.liveKey, ...optionsIn };
|
|
240
|
+
}
|
|
241
|
+
const queryKey = optionsIn.queryKey ?? this.key({ type: 'live', input: optionsIn.input });
|
|
242
|
+
return queryKey;
|
|
243
|
+
}
|
|
244
|
+
/**
|
|
245
|
+
* Configure live queries for [AsyncIteratorObject](https://orpc.dev/docs/async-iterator-object).
|
|
246
|
+
* Unlike `.streamedOptions` which accumulates chunks, live queries replace the entire result with each new chunk received.
|
|
247
|
+
* Works with hooks like `useQuery`, `useSuspenseQuery`, or `prefetchQuery`.
|
|
248
|
+
*/
|
|
249
|
+
liveOptions(...rest) {
|
|
250
|
+
let optionsIn = resolveMaybeOptionalOptions(rest);
|
|
251
|
+
if (typeof this.options.liveOptions === 'function') {
|
|
252
|
+
optionsIn = this.options.liveOptions(optionsIn);
|
|
253
|
+
}
|
|
254
|
+
else if (this.options.liveOptions) {
|
|
255
|
+
optionsIn = { ...this.options.liveOptions, ...optionsIn };
|
|
256
|
+
}
|
|
257
|
+
const queryKey = this.liveKey(optionsIn);
|
|
258
|
+
const { settleOnFirstData, ...queryOptions } = optionsIn;
|
|
259
|
+
return {
|
|
260
|
+
structuralSharing: createStructuralSharing(this.options.serializer),
|
|
261
|
+
...queryOptions,
|
|
262
|
+
queryKey,
|
|
263
|
+
queryFn: (fnContext) => {
|
|
264
|
+
return intercept(toArray(this.options.liveInterceptors), {
|
|
265
|
+
path: this.path,
|
|
266
|
+
context: {
|
|
267
|
+
[OPERATION_CONTEXT_SYMBOL]: {
|
|
268
|
+
key: queryKey,
|
|
269
|
+
type: 'live',
|
|
270
|
+
},
|
|
271
|
+
...optionsIn.context,
|
|
272
|
+
},
|
|
273
|
+
input: optionsIn.input,
|
|
274
|
+
fnContext,
|
|
275
|
+
}, ({ fnContext, input, context }) => {
|
|
276
|
+
if (input === skipToken || optionsIn.queryFn === skipToken) {
|
|
277
|
+
throw new Error('queryFn should not be called when skipToken used for skipping');
|
|
278
|
+
}
|
|
279
|
+
if (optionsIn.queryFn) {
|
|
280
|
+
return optionsIn.queryFn(fnContext);
|
|
281
|
+
}
|
|
282
|
+
return liveQuery(async (queryContext) => {
|
|
283
|
+
const output = await this.call(input, {
|
|
284
|
+
signal: queryContext.signal,
|
|
285
|
+
context,
|
|
286
|
+
});
|
|
287
|
+
if (!isAsyncIteratorObject(output)) {
|
|
288
|
+
throw new Error('liveQuery requires an AsyncIteratorObject output');
|
|
289
|
+
}
|
|
290
|
+
return output;
|
|
291
|
+
}, { settleOnFirstData })(fnContext);
|
|
292
|
+
});
|
|
293
|
+
},
|
|
294
|
+
...(optionsIn.input === skipToken || optionsIn.queryFn === skipToken
|
|
295
|
+
? { enabled: false }
|
|
296
|
+
: {}),
|
|
297
|
+
};
|
|
298
|
+
}
|
|
299
|
+
/**
|
|
300
|
+
* Generate a **full matching** key for [Infinite Query Options](https://orpc.dev/docs/integrations/tanstack-query#infinite-query-options).
|
|
301
|
+
*/
|
|
302
|
+
infiniteKey(optionsIn) {
|
|
303
|
+
if (typeof this.options.infiniteKey === 'function') {
|
|
304
|
+
optionsIn = this.options.infiniteKey(optionsIn);
|
|
305
|
+
}
|
|
306
|
+
else if (this.options.infiniteKey) {
|
|
307
|
+
optionsIn = { ...this.options.infiniteKey, ...optionsIn };
|
|
308
|
+
}
|
|
309
|
+
const queryKey = optionsIn.queryKey ??
|
|
310
|
+
this.key({
|
|
311
|
+
type: 'infinite',
|
|
312
|
+
input: optionsIn.input === skipToken
|
|
313
|
+
? skipToken
|
|
314
|
+
: optionsIn.input(optionsIn.initialPageParam),
|
|
315
|
+
});
|
|
316
|
+
return queryKey;
|
|
317
|
+
}
|
|
318
|
+
/**
|
|
319
|
+
* Generate options used for useInfiniteQuery/useSuspenseInfiniteQuery/prefetchInfiniteQuery/...
|
|
320
|
+
*/
|
|
321
|
+
infiniteOptions(optionsIn) {
|
|
322
|
+
if (typeof this.options.infiniteOptions === 'function') {
|
|
323
|
+
optionsIn = this.options.infiniteOptions(optionsIn);
|
|
324
|
+
}
|
|
325
|
+
else if (this.options.infiniteOptions) {
|
|
326
|
+
optionsIn = { ...this.options.infiniteOptions, ...optionsIn };
|
|
327
|
+
}
|
|
328
|
+
const queryKey = this.infiniteKey(optionsIn);
|
|
329
|
+
return {
|
|
330
|
+
structuralSharing: createStructuralSharing(this.options.serializer),
|
|
331
|
+
...optionsIn,
|
|
332
|
+
queryKey,
|
|
333
|
+
queryFn: (fnContext) => {
|
|
334
|
+
return intercept(toArray(this.options.infiniteInterceptors), {
|
|
335
|
+
path: this.path,
|
|
336
|
+
context: {
|
|
337
|
+
[OPERATION_CONTEXT_SYMBOL]: {
|
|
338
|
+
key: queryKey,
|
|
339
|
+
type: 'infinite',
|
|
340
|
+
},
|
|
341
|
+
...optionsIn.context,
|
|
342
|
+
},
|
|
343
|
+
input: optionsIn.input === skipToken
|
|
344
|
+
? skipToken
|
|
345
|
+
: optionsIn.input(fnContext.pageParam),
|
|
346
|
+
fnContext: fnContext,
|
|
347
|
+
}, ({ context, input, fnContext }) => {
|
|
348
|
+
if (input === skipToken || optionsIn.queryFn === skipToken) {
|
|
349
|
+
throw new Error('queryFn should not be called when skipToken used for skipping');
|
|
350
|
+
}
|
|
351
|
+
if (optionsIn.queryFn) {
|
|
352
|
+
return optionsIn.queryFn(fnContext);
|
|
353
|
+
}
|
|
354
|
+
return this.call(input, { signal: fnContext.signal, context });
|
|
355
|
+
});
|
|
356
|
+
},
|
|
357
|
+
...(optionsIn.input === skipToken || optionsIn.queryFn === skipToken
|
|
358
|
+
? { enabled: false }
|
|
359
|
+
: {}),
|
|
360
|
+
};
|
|
361
|
+
}
|
|
362
|
+
/**
|
|
363
|
+
* Generate a **full matching** key for [Mutation Options](https://orpc.dev/docs/integrations/tanstack-query#mutation-options).
|
|
364
|
+
*/
|
|
365
|
+
mutationKey(optionsIn = {}) {
|
|
366
|
+
if (typeof this.options.mutationKey === 'function') {
|
|
367
|
+
optionsIn = this.options.mutationKey(optionsIn);
|
|
368
|
+
}
|
|
369
|
+
else if (this.options.mutationKey) {
|
|
370
|
+
optionsIn = { ...this.options.mutationKey, ...optionsIn };
|
|
371
|
+
}
|
|
372
|
+
const mutationKey = optionsIn.mutationKey ?? this.key({ type: 'mutation' });
|
|
373
|
+
return mutationKey;
|
|
374
|
+
}
|
|
375
|
+
/**
|
|
376
|
+
* Generate options used for useMutation/...
|
|
377
|
+
*/
|
|
378
|
+
mutationOptions(...rest) {
|
|
379
|
+
let optionsIn = resolveMaybeOptionalOptions(rest);
|
|
380
|
+
if (typeof this.options.mutationOptions === 'function') {
|
|
381
|
+
optionsIn = this.options.mutationOptions(optionsIn);
|
|
382
|
+
}
|
|
383
|
+
else if (this.options.mutationOptions) {
|
|
384
|
+
optionsIn = { ...this.options.mutationOptions, ...optionsIn };
|
|
385
|
+
}
|
|
386
|
+
const mutationKey = this.mutationKey(optionsIn);
|
|
387
|
+
return {
|
|
388
|
+
...optionsIn,
|
|
389
|
+
mutationKey,
|
|
390
|
+
mutationFn: (input, fnContext) => {
|
|
391
|
+
return intercept(toArray(this.options.mutationInterceptors), {
|
|
392
|
+
path: this.path,
|
|
393
|
+
context: {
|
|
394
|
+
[OPERATION_CONTEXT_SYMBOL]: {
|
|
395
|
+
key: mutationKey,
|
|
396
|
+
type: 'mutation',
|
|
397
|
+
},
|
|
398
|
+
...optionsIn.context,
|
|
399
|
+
},
|
|
400
|
+
fnContext,
|
|
401
|
+
input,
|
|
402
|
+
}, ({ input, fnContext, context }) => {
|
|
403
|
+
if (optionsIn.mutationFn) {
|
|
404
|
+
return optionsIn.mutationFn(input, fnContext);
|
|
405
|
+
}
|
|
406
|
+
return this.call(input, { context });
|
|
407
|
+
});
|
|
408
|
+
},
|
|
409
|
+
};
|
|
410
|
+
}
|
|
411
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import type { AnyNestedClient, Client, InferClientContext, InferClientError } from '@orpc/client';
|
|
2
|
+
import type { Public } from '@orpc/shared';
|
|
3
|
+
import type { OperationKeyBaseOptions } from './key.ts';
|
|
4
|
+
import type { RouterUtilsPlugin } from './plugin.ts';
|
|
5
|
+
import type { ProcedureUtilsInfiniteInterceptor, ProcedureUtilsLiveInterceptor, ProcedureUtilsMutationInterceptor, ProcedureUtilsOptions, ProcedureUtilsQueryInterceptor, ProcedureUtilsStreamedInterceptor } from './procedure-utils.ts';
|
|
6
|
+
import { ProcedureUtils } from './procedure-utils.ts';
|
|
7
|
+
import { SharedUtils } from './shared-utils.ts';
|
|
8
|
+
/**
|
|
9
|
+
* The utils shape derived from a client: procedure clients map to procedure
|
|
10
|
+
* utils, and routers map recursively to nested utils.
|
|
11
|
+
*
|
|
12
|
+
* @see {@link https://orpc.dev/docs/integrations/tanstack-query | TanStack Query Integration}
|
|
13
|
+
*/
|
|
14
|
+
export type RouterUtils<T extends AnyNestedClient> = T extends Client<infer UClientContext, infer UInput, infer UOutput, infer UError> ? Public<ProcedureUtils<UClientContext, UInput, UOutput, UError>> : {
|
|
15
|
+
[K in keyof T]: T[K] extends AnyNestedClient ? RouterUtils<T[K]> : never;
|
|
16
|
+
} & Public<SharedUtils<unknown>>;
|
|
17
|
+
export type RouterUtilsScoped<T extends AnyNestedClient> = T extends Client<infer UClientContext, infer UInput, infer UOutput, infer UError> ? ProcedureUtilsOptions<UClientContext, UInput, UOutput, UError> : {
|
|
18
|
+
[K in keyof T]?: (T[K] extends AnyNestedClient ? RouterUtilsScoped<T[K]> : never) | undefined;
|
|
19
|
+
};
|
|
20
|
+
export interface RouterUtilsOptions<T extends AnyNestedClient> extends OperationKeyBaseOptions {
|
|
21
|
+
/**
|
|
22
|
+
* Base path for all query keys.
|
|
23
|
+
*
|
|
24
|
+
* @internal
|
|
25
|
+
*/
|
|
26
|
+
path?: string[] | undefined;
|
|
27
|
+
/**
|
|
28
|
+
* Interceptors that intercept queryFn inside .queryOptions
|
|
29
|
+
*/
|
|
30
|
+
queryInterceptors?: ProcedureUtilsQueryInterceptor<InferClientContext<T>, unknown, unknown, InferClientError<T>>[] | undefined;
|
|
31
|
+
/**
|
|
32
|
+
* Interceptors that intercept queryFn inside .streamedOptions
|
|
33
|
+
*/
|
|
34
|
+
streamedInterceptors?: ProcedureUtilsStreamedInterceptor<InferClientContext<T>, unknown, unknown, InferClientError<T>>[] | undefined;
|
|
35
|
+
/**
|
|
36
|
+
* Interceptors that intercept queryFn inside .liveOptions
|
|
37
|
+
*/
|
|
38
|
+
liveInterceptors?: ProcedureUtilsLiveInterceptor<InferClientContext<T>, unknown, unknown, InferClientError<T>>[] | undefined;
|
|
39
|
+
/**
|
|
40
|
+
* Interceptors that intercept queryFn inside .infiniteOptions
|
|
41
|
+
*/
|
|
42
|
+
infiniteInterceptors?: ProcedureUtilsInfiniteInterceptor<InferClientContext<T>, unknown, unknown, InferClientError<T>>[] | undefined;
|
|
43
|
+
/**
|
|
44
|
+
* Interceptors that intercept mutationFn inside .mutationOptions
|
|
45
|
+
*/
|
|
46
|
+
mutationInterceptors?: ProcedureUtilsMutationInterceptor<InferClientContext<T>, unknown, unknown, InferClientError<T>>[] | undefined;
|
|
47
|
+
/**
|
|
48
|
+
* Per-procedure options following the shape of the router.
|
|
49
|
+
* Allows fine-grained configuration of individual procedure utils
|
|
50
|
+
* without affecting the rest of the router.
|
|
51
|
+
*/
|
|
52
|
+
scoped?: RouterUtilsScoped<T> | undefined;
|
|
53
|
+
/**
|
|
54
|
+
* Plugins to extend router utils behavior.
|
|
55
|
+
*/
|
|
56
|
+
plugins?: RouterUtilsPlugin<T>[] | undefined;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Creates TanStack Query utils from a client, exposing query/mutation
|
|
60
|
+
* option builders for every procedure in the router.
|
|
61
|
+
*
|
|
62
|
+
* @remarks
|
|
63
|
+
* **Note**: Both client-side and server-side clients are supported.
|
|
64
|
+
*
|
|
65
|
+
* @see {@link https://orpc.dev/docs/integrations/tanstack-query | TanStack Query Integration}
|
|
66
|
+
*/
|
|
67
|
+
export declare function createRouterUtils<T extends AnyNestedClient>(client: T, options?: NoInfer<RouterUtilsOptions<T>>): RouterUtils<T>;
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { RECURSIVE_CLIENT_UNWRAP_KEYS } from '@orpc/client';
|
|
2
|
+
import { bindMethods, isNonPrimitive, pick, toArray } from '@orpc/shared';
|
|
3
|
+
import { CompositeRouterUtilsPlugin } from './plugin.js';
|
|
4
|
+
import { isProcedureUtilsOptions, mergeProcedureUtilsOptions, ProcedureUtils, } from './procedure-utils.js';
|
|
5
|
+
import { SharedUtils } from './shared-utils.js';
|
|
6
|
+
/**
|
|
7
|
+
* Creates TanStack Query utils from a client, exposing query/mutation
|
|
8
|
+
* option builders for every procedure in the router.
|
|
9
|
+
*
|
|
10
|
+
* @remarks
|
|
11
|
+
* **Note**: Both client-side and server-side clients are supported.
|
|
12
|
+
*
|
|
13
|
+
* @see {@link https://orpc.dev/docs/integrations/tanstack-query | TanStack Query Integration}
|
|
14
|
+
*/
|
|
15
|
+
export function createRouterUtils(client, options = {}) {
|
|
16
|
+
const plugin = new CompositeRouterUtilsPlugin(options.plugins);
|
|
17
|
+
options = plugin.init(options);
|
|
18
|
+
return createRouterUtilsInternal(client, options, plugin);
|
|
19
|
+
}
|
|
20
|
+
function createRouterUtilsInternal(client, options, plugin) {
|
|
21
|
+
const path = toArray(options.path);
|
|
22
|
+
const utils = typeof client === 'function' &&
|
|
23
|
+
(options.scoped === undefined || isProcedureUtilsOptions(options.scoped))
|
|
24
|
+
? bindMethods(new ProcedureUtils(path, client, plugin.initProcedureOptions(path, mergeProcedureUtilsOptions(pick(options, [
|
|
25
|
+
'prefix',
|
|
26
|
+
'serializer',
|
|
27
|
+
'queryInterceptors',
|
|
28
|
+
'streamedInterceptors',
|
|
29
|
+
'liveInterceptors',
|
|
30
|
+
'infiniteInterceptors',
|
|
31
|
+
'mutationInterceptors',
|
|
32
|
+
]), options.scoped ?? {}))), { unbound: ['call'] })
|
|
33
|
+
: bindMethods(new SharedUtils(path, options));
|
|
34
|
+
const cache = new Map();
|
|
35
|
+
const recursive = new Proxy(utils, {
|
|
36
|
+
get(target, prop) {
|
|
37
|
+
const value = Reflect.get(target, prop);
|
|
38
|
+
if (RECURSIVE_CLIENT_UNWRAP_KEYS.has(prop)) {
|
|
39
|
+
return value ?? Reflect.get(Object.prototype, prop);
|
|
40
|
+
}
|
|
41
|
+
const nextClient = Reflect.get(client, prop);
|
|
42
|
+
if (typeof prop !== 'string' || !isNonPrimitive(nextClient)) {
|
|
43
|
+
return value;
|
|
44
|
+
}
|
|
45
|
+
let result = cache.get(prop);
|
|
46
|
+
if (result === undefined) {
|
|
47
|
+
const nextUtils = createRouterUtilsInternal(nextClient, {
|
|
48
|
+
...options,
|
|
49
|
+
path: [...path, prop],
|
|
50
|
+
scoped: options.scoped?.[prop],
|
|
51
|
+
}, plugin);
|
|
52
|
+
result =
|
|
53
|
+
typeof value !== 'function'
|
|
54
|
+
? nextUtils
|
|
55
|
+
: new Proxy(value, {
|
|
56
|
+
get(target, prop) {
|
|
57
|
+
if (typeof prop !== 'string' || RECURSIVE_CLIENT_UNWRAP_KEYS.has(prop)) {
|
|
58
|
+
return Reflect.get(target, prop);
|
|
59
|
+
}
|
|
60
|
+
return Reflect.get(nextUtils, prop);
|
|
61
|
+
},
|
|
62
|
+
});
|
|
63
|
+
cache.set(prop, result);
|
|
64
|
+
}
|
|
65
|
+
return result;
|
|
66
|
+
},
|
|
67
|
+
});
|
|
68
|
+
return recursive;
|
|
69
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { OperationKey, OperationKeyBaseOptions, OperationKeyOptions } from './key.ts';
|
|
2
|
+
import type { OperationType } from './types.ts';
|
|
3
|
+
export declare class SharedUtils<TInput> {
|
|
4
|
+
protected readonly path: string[];
|
|
5
|
+
protected readonly options: OperationKeyBaseOptions;
|
|
6
|
+
constructor(path: string[], options: OperationKeyBaseOptions);
|
|
7
|
+
/**
|
|
8
|
+
* Generate a **partial matching** key for actions like revalidating queries, checking mutation status, etc.
|
|
9
|
+
*/
|
|
10
|
+
key<TType extends OperationType>(options?: Omit<OperationKeyOptions<TType, TInput>, keyof OperationKeyBaseOptions>): OperationKey<TType, TInput>;
|
|
11
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { generateOperationKey } from './key.js';
|
|
2
|
+
export class SharedUtils {
|
|
3
|
+
path;
|
|
4
|
+
options;
|
|
5
|
+
constructor(path, options) {
|
|
6
|
+
this.path = path;
|
|
7
|
+
this.options = options;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Generate a **partial matching** key for actions like revalidating queries, checking mutation status, etc.
|
|
11
|
+
*/
|
|
12
|
+
key(options = {}) {
|
|
13
|
+
return generateOperationKey(this.path, {
|
|
14
|
+
...options,
|
|
15
|
+
prefix: this.options.prefix,
|
|
16
|
+
serializer: this.options.serializer,
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
}
|
package/dist/ssr.d.ts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { AnyNestedClient, InferClientContext, InferClientError, RPCJsonSerialization } from '@orpc/client';
|
|
2
|
+
import { RPCJsonSerializer } from '@orpc/client';
|
|
3
|
+
import type { Public } from '@orpc/shared';
|
|
4
|
+
import type { RouterUtilsPlugin } from './plugin.ts';
|
|
5
|
+
import type { ProcedureUtilsOptions } from './procedure-utils.ts';
|
|
6
|
+
/**
|
|
7
|
+
* Creates `dehydrate` and `hydrate` query client defaults that preserve oRPC types.
|
|
8
|
+
*
|
|
9
|
+
* @see {@link https://orpc.dev/docs/integrations/tanstack-query#custom-serializers | TanStack Query Integration - Custom Serializers}
|
|
10
|
+
*/
|
|
11
|
+
export declare function createHydrationOptions(serializer?: Public<RPCJsonSerializer>): {
|
|
12
|
+
dehydrate: {
|
|
13
|
+
serializeData: (data: unknown) => RPCJsonSerialization;
|
|
14
|
+
};
|
|
15
|
+
hydrate: {
|
|
16
|
+
deserializeData: (data: RPCJsonSerialization) => unknown;
|
|
17
|
+
};
|
|
18
|
+
};
|
|
19
|
+
export interface StreamQuerySSRUtilsPluginOptions {
|
|
20
|
+
/**
|
|
21
|
+
* Whether utils run on the server, where streamed and live queries settle at their first data.
|
|
22
|
+
* Defaults to TanStack Query's `environmentManager.isServer()`.
|
|
23
|
+
*/
|
|
24
|
+
isServer?: boolean | undefined;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Router utils plugin for SSR of streamed and live queries: sets `.streamedOptions` and `.liveOptions` defaults
|
|
28
|
+
* so they settle at their first data on the server, and reopen their stream on mount in the browser.
|
|
29
|
+
*
|
|
30
|
+
* @see {@link https://orpc.dev/docs/integrations/tanstack-query#streamed-and-live-queries | TanStack Query Integration - Streamed and Live Queries}
|
|
31
|
+
*/
|
|
32
|
+
export declare class StreamQuerySSRUtilsPlugin<T extends AnyNestedClient = AnyNestedClient> implements RouterUtilsPlugin<T> {
|
|
33
|
+
private readonly options;
|
|
34
|
+
readonly name = "~stream-query-ssr";
|
|
35
|
+
constructor(options?: StreamQuerySSRUtilsPluginOptions);
|
|
36
|
+
initProcedureOptions(_path: string[], options: ProcedureUtilsOptions<InferClientContext<T>, any, any, InferClientError<T>>): ProcedureUtilsOptions<InferClientContext<T>, any, any, InferClientError<T>>;
|
|
37
|
+
}
|
package/dist/ssr.js
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { RPCJsonSerializer } from '@orpc/client';
|
|
2
|
+
import { environmentManager } from '@tanstack/query-core';
|
|
3
|
+
import { mergeProcedureUtilsOptions } from './procedure-utils.js';
|
|
4
|
+
/**
|
|
5
|
+
* Creates `dehydrate` and `hydrate` query client defaults that preserve oRPC types.
|
|
6
|
+
*
|
|
7
|
+
* @see {@link https://orpc.dev/docs/integrations/tanstack-query#custom-serializers | TanStack Query Integration - Custom Serializers}
|
|
8
|
+
*/
|
|
9
|
+
export function createHydrationOptions(serializer = new RPCJsonSerializer()) {
|
|
10
|
+
return {
|
|
11
|
+
dehydrate: {
|
|
12
|
+
serializeData(data) {
|
|
13
|
+
const { json, meta } = serializer.serializeJson(data);
|
|
14
|
+
return { json, meta };
|
|
15
|
+
},
|
|
16
|
+
},
|
|
17
|
+
hydrate: {
|
|
18
|
+
deserializeData(data) {
|
|
19
|
+
return serializer.deserializeJson(data);
|
|
20
|
+
},
|
|
21
|
+
},
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Router utils plugin for SSR of streamed and live queries: sets `.streamedOptions` and `.liveOptions` defaults
|
|
26
|
+
* so they settle at their first data on the server, and reopen their stream on mount in the browser.
|
|
27
|
+
*
|
|
28
|
+
* @see {@link https://orpc.dev/docs/integrations/tanstack-query#streamed-and-live-queries | TanStack Query Integration - Streamed and Live Queries}
|
|
29
|
+
*/
|
|
30
|
+
export class StreamQuerySSRUtilsPlugin {
|
|
31
|
+
options;
|
|
32
|
+
name = '~stream-query-ssr';
|
|
33
|
+
constructor(options = {}) {
|
|
34
|
+
this.options = options;
|
|
35
|
+
}
|
|
36
|
+
initProcedureOptions(_path, options) {
|
|
37
|
+
// read when utils are built, so `environmentManager.setIsServer` applies even after the plugin is created
|
|
38
|
+
const defaults = (this.options.isServer ?? environmentManager.isServer())
|
|
39
|
+
? // the render must finish even with observers (vue, svelte), which must not reopen the settled stream
|
|
40
|
+
{ settleOnFirstData: true, refetchOnMount: false }
|
|
41
|
+
: // hydrated data reopens its stream on mount, despite a `staleTime`
|
|
42
|
+
{ refetchOnMount: 'always' };
|
|
43
|
+
return mergeProcedureUtilsOptions({ streamedOptions: defaults, liveOptions: defaults }, options);
|
|
44
|
+
}
|
|
45
|
+
}
|