@orpc/pinia-colada 2.0.0-beta.47 → 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 +6 -7
- package/dist/contract-utils.d.ts +37 -0
- package/dist/contract-utils.js +42 -0
- package/dist/index.d.ts +20 -501
- package/dist/index.js +14 -0
- package/dist/key.d.ts +45 -0
- package/dist/key.js +47 -0
- package/dist/live-query.d.ts +8 -0
- package/dist/live-query.js +38 -0
- package/dist/meta.d.ts +42 -0
- package/dist/meta.js +55 -0
- package/dist/plugin.d.ts +46 -0
- package/dist/plugin.js +25 -0
- package/dist/procedure-utils.d.ts +190 -0
- package/dist/procedure-utils.js +399 -0
- package/dist/router-utils.d.ts +67 -0
- package/dist/router-utils.js +69 -0
- package/dist/shared-utils.d.ts +13 -0
- package/dist/shared-utils.js +21 -0
- package/dist/ssr.d.ts +22 -0
- package/dist/ssr.js +24 -0
- package/dist/stream-query.d.ts +42 -0
- package/dist/stream-query.js +59 -0
- package/dist/types.d.ts +80 -0
- package/dist/types.js +7 -0
- package/package.json +31 -33
- package/dist/index.d.mts +0 -501
- package/dist/index.mjs +0 -622
|
@@ -0,0 +1,399 @@
|
|
|
1
|
+
import { intercept, isAsyncIteratorObject, isNonPrimitive, resolveMaybeOptionalOptions, } from '@orpc/shared';
|
|
2
|
+
import { liveQuery } from './live-query.js';
|
|
3
|
+
import { SharedUtils } from './shared-utils.js';
|
|
4
|
+
import { serializableStreamedQuery } from './stream-query.js';
|
|
5
|
+
import { OPERATION_CONTEXT_SYMBOL } from './types.js';
|
|
6
|
+
const PROCEDURE_UTILS_INTERCEPTOR_KEYS = [
|
|
7
|
+
'queryInterceptors',
|
|
8
|
+
'streamedInterceptors',
|
|
9
|
+
'liveInterceptors',
|
|
10
|
+
'infiniteInterceptors',
|
|
11
|
+
'mutationInterceptors',
|
|
12
|
+
];
|
|
13
|
+
const PROCEDURE_UTILS_MODIFIER_KEYS = [
|
|
14
|
+
'queryKey',
|
|
15
|
+
'queryOptions',
|
|
16
|
+
'streamedKey',
|
|
17
|
+
'streamedOptions',
|
|
18
|
+
'liveKey',
|
|
19
|
+
'liveOptions',
|
|
20
|
+
'infiniteKey',
|
|
21
|
+
'infiniteOptions',
|
|
22
|
+
'mutationKey',
|
|
23
|
+
'mutationOptions',
|
|
24
|
+
];
|
|
25
|
+
export function isProcedureUtilsOptions(value) {
|
|
26
|
+
if (!isNonPrimitive(value)) {
|
|
27
|
+
return false;
|
|
28
|
+
}
|
|
29
|
+
for (const key of Object.keys(value)) {
|
|
30
|
+
if (value[key] === undefined) {
|
|
31
|
+
continue;
|
|
32
|
+
}
|
|
33
|
+
if (key === 'prefix') {
|
|
34
|
+
if (typeof value[key] !== 'string') {
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
else if (key === 'serializer') {
|
|
39
|
+
if (!isNonPrimitive(value[key]) || typeof value[key].serializeJson !== 'function') {
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
else if (PROCEDURE_UTILS_INTERCEPTOR_KEYS.includes(key)) {
|
|
44
|
+
if (!Array.isArray(value[key]) || value[key].some((i) => typeof i !== 'function')) {
|
|
45
|
+
return false;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
else if (PROCEDURE_UTILS_MODIFIER_KEYS.includes(key)) {
|
|
49
|
+
if (!isNonPrimitive(value[key])) {
|
|
50
|
+
return false;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return true;
|
|
55
|
+
}
|
|
56
|
+
function mergeProcedureUtilsModifier(base, override) {
|
|
57
|
+
if (typeof override !== 'function') {
|
|
58
|
+
if (typeof base !== 'function') {
|
|
59
|
+
return { ...base, ...override };
|
|
60
|
+
}
|
|
61
|
+
return (options) => base({ ...override, ...options });
|
|
62
|
+
}
|
|
63
|
+
const applyBase = typeof base === 'function' ? base : (options) => ({ ...base, ...options });
|
|
64
|
+
return (options) => override(applyBase(options));
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Merge two procedure utils options where `override` takes priority.
|
|
68
|
+
* A key explicitly set to `undefined` in `override` resets the base value.
|
|
69
|
+
* When both sides define a key, interceptors are concatenated (base ones run first) and
|
|
70
|
+
* modifiers are combined so that, for object values, per-call options beat `override`,
|
|
71
|
+
* which beats `base`. A function modifier receives the merged options and its result wins.
|
|
72
|
+
*/
|
|
73
|
+
export function mergeProcedureUtilsOptions(base, override) {
|
|
74
|
+
const merged = { ...base, ...override };
|
|
75
|
+
for (const key of PROCEDURE_UTILS_INTERCEPTOR_KEYS) {
|
|
76
|
+
const baseValue = base[key];
|
|
77
|
+
const overrideValue = override[key];
|
|
78
|
+
if (baseValue && overrideValue) {
|
|
79
|
+
merged[key] = [...baseValue, ...overrideValue];
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
for (const key of PROCEDURE_UTILS_MODIFIER_KEYS) {
|
|
83
|
+
const baseValue = base[key];
|
|
84
|
+
const overrideValue = override[key];
|
|
85
|
+
if (baseValue && overrideValue) {
|
|
86
|
+
merged[key] = mergeProcedureUtilsModifier(baseValue, overrideValue);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
return merged;
|
|
90
|
+
}
|
|
91
|
+
export class ProcedureUtils extends SharedUtils {
|
|
92
|
+
/**
|
|
93
|
+
* Calling corresponding procedure client
|
|
94
|
+
*
|
|
95
|
+
* @see {@link https://orpc.dev/docs/integrations/pinia-colada#calling-procedure-clients | Pinia Colada Integration - Calling Procedure Clients}
|
|
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 useQuery/...
|
|
104
|
+
*
|
|
105
|
+
* @see {@link https://orpc.dev/docs/integrations/pinia-colada#querymutation-key | Pinia Colada Integration - Query/Mutation Key}
|
|
106
|
+
*/
|
|
107
|
+
queryKey(...rest) {
|
|
108
|
+
let optionsIn = resolveMaybeOptionalOptions(rest);
|
|
109
|
+
if (typeof this.options.queryKey === 'function') {
|
|
110
|
+
optionsIn = this.options.queryKey(optionsIn);
|
|
111
|
+
}
|
|
112
|
+
else if (this.options.queryKey) {
|
|
113
|
+
optionsIn = { ...this.options.queryKey, ...optionsIn };
|
|
114
|
+
}
|
|
115
|
+
const key = optionsIn.key ?? this.key({ type: 'query', input: optionsIn.input });
|
|
116
|
+
return key;
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Generate options used for useQuery/...
|
|
120
|
+
*
|
|
121
|
+
* @see {@link https://orpc.dev/docs/integrations/pinia-colada#query-options-utility | Pinia Colada Integration - Query Options Utility}
|
|
122
|
+
*/
|
|
123
|
+
queryOptions(...rest) {
|
|
124
|
+
let optionsIn = resolveMaybeOptionalOptions(rest);
|
|
125
|
+
if (typeof this.options.queryOptions === 'function') {
|
|
126
|
+
optionsIn = this.options.queryOptions(optionsIn);
|
|
127
|
+
}
|
|
128
|
+
else if (this.options.queryOptions) {
|
|
129
|
+
optionsIn = { ...this.options.queryOptions, ...optionsIn };
|
|
130
|
+
}
|
|
131
|
+
const { input, context, key: _keyIn, query: queryIn, ...restOptions } = optionsIn;
|
|
132
|
+
const key = this.queryKey(optionsIn);
|
|
133
|
+
return {
|
|
134
|
+
...restOptions,
|
|
135
|
+
key,
|
|
136
|
+
query: (fnContext) => {
|
|
137
|
+
return intercept(this.options.queryInterceptors, {
|
|
138
|
+
path: this.path,
|
|
139
|
+
context: {
|
|
140
|
+
[OPERATION_CONTEXT_SYMBOL]: {
|
|
141
|
+
key,
|
|
142
|
+
type: 'query',
|
|
143
|
+
},
|
|
144
|
+
...context,
|
|
145
|
+
},
|
|
146
|
+
input: input,
|
|
147
|
+
fnContext,
|
|
148
|
+
}, ({ context, input, fnContext }) => {
|
|
149
|
+
if (queryIn) {
|
|
150
|
+
return queryIn(fnContext);
|
|
151
|
+
}
|
|
152
|
+
return this.call(input, { signal: fnContext.signal, context });
|
|
153
|
+
});
|
|
154
|
+
},
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Generate a **full matching** key for [Streamed Query Options](https://orpc.dev/docs/integrations/pinia-colada#streamed-query-options-utility).
|
|
159
|
+
*
|
|
160
|
+
* @see {@link https://orpc.dev/docs/integrations/pinia-colada#querymutation-key | Pinia Colada Integration - Query/Mutation Key}
|
|
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 key = optionsIn.key ??
|
|
171
|
+
this.key({
|
|
172
|
+
type: 'streamed',
|
|
173
|
+
input: optionsIn.input,
|
|
174
|
+
fnOptions: optionsIn.fnOptions,
|
|
175
|
+
});
|
|
176
|
+
return key;
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Configure queries for [AsyncIteratorObject](https://orpc.dev/docs/async-iterator-object).
|
|
180
|
+
* The resulting data is an array of chunks, and each new chunk is appended as it arrives.
|
|
181
|
+
* It never returns to pending once it has data.
|
|
182
|
+
* Works with `useQuery` and any other API that accepts query options.
|
|
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 { input, context, key: _keyIn, query: queryIn, fnOptions, settleOnFirstData, ...restOptions } = optionsIn;
|
|
193
|
+
const key = this.streamedKey(optionsIn);
|
|
194
|
+
return {
|
|
195
|
+
...restOptions,
|
|
196
|
+
key,
|
|
197
|
+
query: (fnContext) => {
|
|
198
|
+
return intercept(this.options.streamedInterceptors, {
|
|
199
|
+
path: this.path,
|
|
200
|
+
context: {
|
|
201
|
+
[OPERATION_CONTEXT_SYMBOL]: {
|
|
202
|
+
key,
|
|
203
|
+
type: 'streamed',
|
|
204
|
+
},
|
|
205
|
+
...context,
|
|
206
|
+
},
|
|
207
|
+
input: input,
|
|
208
|
+
fnContext,
|
|
209
|
+
}, ({ context, input, fnContext }) => {
|
|
210
|
+
if (queryIn) {
|
|
211
|
+
return queryIn(fnContext);
|
|
212
|
+
}
|
|
213
|
+
return serializableStreamedQuery(async (queryContext) => {
|
|
214
|
+
const output = await this.call(input, { signal: queryContext.signal, context });
|
|
215
|
+
if (!isAsyncIteratorObject(output)) {
|
|
216
|
+
throw new Error('streamedQuery requires an AsyncIteratorObject output');
|
|
217
|
+
}
|
|
218
|
+
return output;
|
|
219
|
+
}, { ...fnOptions, settleOnFirstData })(fnContext);
|
|
220
|
+
});
|
|
221
|
+
},
|
|
222
|
+
};
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* Generate a **full matching** key for [Live Query Options](https://orpc.dev/docs/integrations/pinia-colada#live-query-options-utility).
|
|
226
|
+
*
|
|
227
|
+
* @see {@link https://orpc.dev/docs/integrations/pinia-colada#querymutation-key | Pinia Colada Integration - Query/Mutation Key}
|
|
228
|
+
*/
|
|
229
|
+
liveKey(...rest) {
|
|
230
|
+
let optionsIn = resolveMaybeOptionalOptions(rest);
|
|
231
|
+
if (typeof this.options.liveKey === 'function') {
|
|
232
|
+
optionsIn = this.options.liveKey(optionsIn);
|
|
233
|
+
}
|
|
234
|
+
else if (this.options.liveKey) {
|
|
235
|
+
optionsIn = { ...this.options.liveKey, ...optionsIn };
|
|
236
|
+
}
|
|
237
|
+
const key = optionsIn.key ?? this.key({ type: 'live', input: optionsIn.input });
|
|
238
|
+
return key;
|
|
239
|
+
}
|
|
240
|
+
/**
|
|
241
|
+
* Configure live queries for [AsyncIteratorObject](https://orpc.dev/docs/async-iterator-object).
|
|
242
|
+
* Unlike `.streamedOptions` which accumulates chunks, live queries replace the entire result with each new chunk received.
|
|
243
|
+
* Works with `useQuery` and any other API that accepts query options.
|
|
244
|
+
*/
|
|
245
|
+
liveOptions(...rest) {
|
|
246
|
+
let optionsIn = resolveMaybeOptionalOptions(rest);
|
|
247
|
+
if (typeof this.options.liveOptions === 'function') {
|
|
248
|
+
optionsIn = this.options.liveOptions(optionsIn);
|
|
249
|
+
}
|
|
250
|
+
else if (this.options.liveOptions) {
|
|
251
|
+
optionsIn = { ...this.options.liveOptions, ...optionsIn };
|
|
252
|
+
}
|
|
253
|
+
const { input, context, key: _keyIn, query: queryIn, settleOnFirstData, ...restOptions } = optionsIn;
|
|
254
|
+
const key = this.liveKey(optionsIn);
|
|
255
|
+
return {
|
|
256
|
+
...restOptions,
|
|
257
|
+
key,
|
|
258
|
+
query: (fnContext) => {
|
|
259
|
+
return intercept(this.options.liveInterceptors, {
|
|
260
|
+
path: this.path,
|
|
261
|
+
context: {
|
|
262
|
+
[OPERATION_CONTEXT_SYMBOL]: {
|
|
263
|
+
key,
|
|
264
|
+
type: 'live',
|
|
265
|
+
},
|
|
266
|
+
...context,
|
|
267
|
+
},
|
|
268
|
+
input: input,
|
|
269
|
+
fnContext,
|
|
270
|
+
}, ({ context, input, fnContext }) => {
|
|
271
|
+
if (queryIn) {
|
|
272
|
+
return queryIn(fnContext);
|
|
273
|
+
}
|
|
274
|
+
return liveQuery(async (queryContext) => {
|
|
275
|
+
const output = await this.call(input, { signal: queryContext.signal, context });
|
|
276
|
+
if (!isAsyncIteratorObject(output)) {
|
|
277
|
+
throw new Error('liveQuery requires an AsyncIteratorObject output');
|
|
278
|
+
}
|
|
279
|
+
return output;
|
|
280
|
+
}, { settleOnFirstData })(fnContext);
|
|
281
|
+
});
|
|
282
|
+
},
|
|
283
|
+
};
|
|
284
|
+
}
|
|
285
|
+
/**
|
|
286
|
+
* Generate a **full matching** key for useInfiniteQuery/...
|
|
287
|
+
*
|
|
288
|
+
* @see {@link https://orpc.dev/docs/integrations/pinia-colada#querymutation-key | Pinia Colada Integration - Query/Mutation Key}
|
|
289
|
+
*/
|
|
290
|
+
infiniteKey(optionsIn) {
|
|
291
|
+
if (typeof this.options.infiniteKey === 'function') {
|
|
292
|
+
optionsIn = this.options.infiniteKey(optionsIn);
|
|
293
|
+
}
|
|
294
|
+
else if (this.options.infiniteKey) {
|
|
295
|
+
optionsIn = { ...this.options.infiniteKey, ...optionsIn };
|
|
296
|
+
}
|
|
297
|
+
const key = optionsIn.key ??
|
|
298
|
+
this.key({
|
|
299
|
+
type: 'infinite',
|
|
300
|
+
input: optionsIn.input(typeof optionsIn.initialPageParam === 'function'
|
|
301
|
+
? optionsIn.initialPageParam()
|
|
302
|
+
: optionsIn.initialPageParam),
|
|
303
|
+
});
|
|
304
|
+
return key;
|
|
305
|
+
}
|
|
306
|
+
/**
|
|
307
|
+
* Generate options used for useInfiniteQuery/...
|
|
308
|
+
*
|
|
309
|
+
* @see {@link https://orpc.dev/docs/integrations/pinia-colada#infinite-query-options-utility | Pinia Colada Integration - Infinite Query Options Utility}
|
|
310
|
+
*/
|
|
311
|
+
infiniteOptions(optionsIn) {
|
|
312
|
+
if (typeof this.options.infiniteOptions === 'function') {
|
|
313
|
+
optionsIn = this.options.infiniteOptions(optionsIn);
|
|
314
|
+
}
|
|
315
|
+
else if (this.options.infiniteOptions) {
|
|
316
|
+
optionsIn = { ...this.options.infiniteOptions, ...optionsIn };
|
|
317
|
+
}
|
|
318
|
+
const { input, context, key: _keyIn, query: queryIn, ...restOptions } = optionsIn;
|
|
319
|
+
const key = this.infiniteKey(optionsIn);
|
|
320
|
+
return {
|
|
321
|
+
...restOptions,
|
|
322
|
+
key,
|
|
323
|
+
query: (fnContext) => {
|
|
324
|
+
return intercept(this.options.infiniteInterceptors, {
|
|
325
|
+
path: this.path,
|
|
326
|
+
context: {
|
|
327
|
+
[OPERATION_CONTEXT_SYMBOL]: {
|
|
328
|
+
key,
|
|
329
|
+
type: 'infinite',
|
|
330
|
+
},
|
|
331
|
+
...context,
|
|
332
|
+
},
|
|
333
|
+
input: input(fnContext.pageParam),
|
|
334
|
+
fnContext,
|
|
335
|
+
}, ({ context, input, fnContext }) => {
|
|
336
|
+
if (queryIn) {
|
|
337
|
+
return queryIn(fnContext);
|
|
338
|
+
}
|
|
339
|
+
return this.call(input, { signal: fnContext.signal, context });
|
|
340
|
+
});
|
|
341
|
+
},
|
|
342
|
+
};
|
|
343
|
+
}
|
|
344
|
+
/**
|
|
345
|
+
* Generate a **full matching** key for useMutation/...
|
|
346
|
+
*
|
|
347
|
+
* @see {@link https://orpc.dev/docs/integrations/pinia-colada#querymutation-key | Pinia Colada Integration - Query/Mutation Key}
|
|
348
|
+
*/
|
|
349
|
+
mutationKey(...rest) {
|
|
350
|
+
let optionsIn = resolveMaybeOptionalOptions(rest);
|
|
351
|
+
if (typeof this.options.mutationKey === 'function') {
|
|
352
|
+
optionsIn = this.options.mutationKey(optionsIn);
|
|
353
|
+
}
|
|
354
|
+
else if (this.options.mutationKey) {
|
|
355
|
+
optionsIn = { ...this.options.mutationKey, ...optionsIn };
|
|
356
|
+
}
|
|
357
|
+
const key = optionsIn.key ?? ((input) => this.key({ type: 'mutation', input: input }));
|
|
358
|
+
return key;
|
|
359
|
+
}
|
|
360
|
+
/**
|
|
361
|
+
* Generate options used for useMutation/...
|
|
362
|
+
*
|
|
363
|
+
* @see {@link https://orpc.dev/docs/integrations/pinia-colada#mutation-options | Pinia Colada Integration - Mutation Options}
|
|
364
|
+
*/
|
|
365
|
+
mutationOptions(...rest) {
|
|
366
|
+
let optionsIn = resolveMaybeOptionalOptions(rest);
|
|
367
|
+
if (typeof this.options.mutationOptions === 'function') {
|
|
368
|
+
optionsIn = this.options.mutationOptions(optionsIn);
|
|
369
|
+
}
|
|
370
|
+
else if (this.options.mutationOptions) {
|
|
371
|
+
optionsIn = { ...this.options.mutationOptions, ...optionsIn };
|
|
372
|
+
}
|
|
373
|
+
const { context, key: _keyIn, mutation: mutationIn, ...restOptions } = optionsIn;
|
|
374
|
+
const key = this.mutationKey(optionsIn);
|
|
375
|
+
return {
|
|
376
|
+
...restOptions,
|
|
377
|
+
key,
|
|
378
|
+
mutation: (input, fnContext) => {
|
|
379
|
+
return intercept(this.options.mutationInterceptors, {
|
|
380
|
+
path: this.path,
|
|
381
|
+
context: {
|
|
382
|
+
[OPERATION_CONTEXT_SYMBOL]: {
|
|
383
|
+
key: typeof key === 'function' ? key(input) : key,
|
|
384
|
+
type: 'mutation',
|
|
385
|
+
},
|
|
386
|
+
...context,
|
|
387
|
+
},
|
|
388
|
+
input,
|
|
389
|
+
fnContext,
|
|
390
|
+
}, ({ context, input, fnContext }) => {
|
|
391
|
+
if (mutationIn) {
|
|
392
|
+
return mutationIn(input, fnContext);
|
|
393
|
+
}
|
|
394
|
+
return this.call(input, { context });
|
|
395
|
+
});
|
|
396
|
+
},
|
|
397
|
+
};
|
|
398
|
+
}
|
|
399
|
+
}
|
|
@@ -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/pinia-colada | Pinia Colada 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 query inside .queryOptions
|
|
29
|
+
*/
|
|
30
|
+
queryInterceptors?: ProcedureUtilsQueryInterceptor<InferClientContext<T>, unknown, unknown, InferClientError<T>>[] | undefined;
|
|
31
|
+
/**
|
|
32
|
+
* Interceptors that intercept query inside .streamedOptions
|
|
33
|
+
*/
|
|
34
|
+
streamedInterceptors?: ProcedureUtilsStreamedInterceptor<InferClientContext<T>, unknown, unknown, InferClientError<T>>[] | undefined;
|
|
35
|
+
/**
|
|
36
|
+
* Interceptors that intercept query inside .liveOptions
|
|
37
|
+
*/
|
|
38
|
+
liveInterceptors?: ProcedureUtilsLiveInterceptor<InferClientContext<T>, unknown, unknown, InferClientError<T>>[] | undefined;
|
|
39
|
+
/**
|
|
40
|
+
* Interceptors that intercept query inside .infiniteOptions
|
|
41
|
+
*/
|
|
42
|
+
infiniteInterceptors?: ProcedureUtilsInfiniteInterceptor<InferClientContext<T>, unknown, unknown, InferClientError<T>>[] | undefined;
|
|
43
|
+
/**
|
|
44
|
+
* Interceptors that intercept mutation 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 Pinia Colada 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/pinia-colada | Pinia Colada 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 Pinia Colada 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/pinia-colada | Pinia Colada 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,13 @@
|
|
|
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
|
+
* @see {@link https://orpc.dev/docs/integrations/pinia-colada#querymutation-key | Pinia Colada Integration - Query/Mutation Key}
|
|
11
|
+
*/
|
|
12
|
+
key<TType extends OperationType>(options?: Omit<OperationKeyOptions<TType, TInput>, keyof OperationKeyBaseOptions>): OperationKey<TType, TInput>;
|
|
13
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
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
|
+
* @see {@link https://orpc.dev/docs/integrations/pinia-colada#querymutation-key | Pinia Colada Integration - Query/Mutation Key}
|
|
13
|
+
*/
|
|
14
|
+
key(options = {}) {
|
|
15
|
+
return generateOperationKey(this.path, {
|
|
16
|
+
...options,
|
|
17
|
+
prefix: this.options.prefix,
|
|
18
|
+
serializer: this.options.serializer,
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
}
|
package/dist/ssr.d.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { AnyNestedClient, InferClientContext, InferClientError } from '@orpc/client';
|
|
2
|
+
import type { RouterUtilsPlugin } from './plugin.ts';
|
|
3
|
+
import type { ProcedureUtilsOptions } from './procedure-utils.ts';
|
|
4
|
+
export interface StreamQuerySSRUtilsPluginOptions {
|
|
5
|
+
/**
|
|
6
|
+
* Whether utils run on the server, where streamed and live queries always settle at their first data.
|
|
7
|
+
* Defaults to `true` when there is no global `window`, or in Deno.
|
|
8
|
+
*/
|
|
9
|
+
isServer?: boolean | undefined;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Router utils plugin for SSR of streamed and live queries: sets `settleOnFirstData: true` on the server,
|
|
13
|
+
* and `staleTime: 0` in the browser, so hydrated data is stale and its stream reopens when a component mounts.
|
|
14
|
+
*
|
|
15
|
+
* @see {@link https://orpc.dev/docs/integrations/pinia-colada#streamed-and-live-queries-in-ssr | Pinia Colada Integration - Streamed and Live Queries in SSR}
|
|
16
|
+
*/
|
|
17
|
+
export declare class StreamQuerySSRUtilsPlugin<T extends AnyNestedClient = AnyNestedClient> implements RouterUtilsPlugin<T> {
|
|
18
|
+
readonly name = "~stream-query-ssr";
|
|
19
|
+
private readonly defaults;
|
|
20
|
+
constructor(options?: StreamQuerySSRUtilsPluginOptions);
|
|
21
|
+
initProcedureOptions(_path: string[], options: ProcedureUtilsOptions<InferClientContext<T>, any, any, InferClientError<T>>): ProcedureUtilsOptions<InferClientContext<T>, any, any, InferClientError<T>>;
|
|
22
|
+
}
|
package/dist/ssr.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { mergeProcedureUtilsOptions } from './procedure-utils.js';
|
|
2
|
+
/**
|
|
3
|
+
* Router utils plugin for SSR of streamed and live queries: sets `settleOnFirstData: true` on the server,
|
|
4
|
+
* and `staleTime: 0` in the browser, so hydrated data is stale and its stream reopens when a component mounts.
|
|
5
|
+
*
|
|
6
|
+
* @see {@link https://orpc.dev/docs/integrations/pinia-colada#streamed-and-live-queries-in-ssr | Pinia Colada Integration - Streamed and Live Queries in SSR}
|
|
7
|
+
*/
|
|
8
|
+
export class StreamQuerySSRUtilsPlugin {
|
|
9
|
+
name = '~stream-query-ssr';
|
|
10
|
+
defaults;
|
|
11
|
+
constructor(options = {}) {
|
|
12
|
+
// the server always settles, even when observed, and keeps the staleTime so components sharing a query do not reopen it
|
|
13
|
+
this.defaults =
|
|
14
|
+
(options.isServer ?? (!('window' in globalThis) || 'Deno' in globalThis))
|
|
15
|
+
? { settleOnFirstData: true }
|
|
16
|
+
: { staleTime: 0 };
|
|
17
|
+
}
|
|
18
|
+
initProcedureOptions(_path, options) {
|
|
19
|
+
return mergeProcedureUtilsOptions({
|
|
20
|
+
streamedOptions: this.defaults,
|
|
21
|
+
liveOptions: this.defaults,
|
|
22
|
+
}, options);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { Promisable } from '@orpc/shared';
|
|
2
|
+
import type { UseQueryFnContext } from './types.ts';
|
|
3
|
+
export interface SerializableStreamedQueryOptions {
|
|
4
|
+
/**
|
|
5
|
+
* What a fetch does with existing data once its stream connects.
|
|
6
|
+
* Until then the data stays, so an entry with data never returns to `pending`.
|
|
7
|
+
*
|
|
8
|
+
* - `'reset'`: the data becomes `[]`, then each chunk is appended.
|
|
9
|
+
* - `'append'`: the data is kept (trimmed to `maxChunks`), and each chunk is appended.
|
|
10
|
+
*
|
|
11
|
+
* @default 'reset'
|
|
12
|
+
*/
|
|
13
|
+
refetchMode?: 'append' | 'reset' | undefined;
|
|
14
|
+
/**
|
|
15
|
+
* Limits the number of data chunks stored in the query result.
|
|
16
|
+
* Older chunks are removed when the limit is reached.
|
|
17
|
+
*
|
|
18
|
+
* @default Number.POSITIVE_INFINITY (unlimited)
|
|
19
|
+
*/
|
|
20
|
+
maxChunks?: number | undefined;
|
|
21
|
+
}
|
|
22
|
+
export interface SettleOnFirstDataOptions {
|
|
23
|
+
/**
|
|
24
|
+
* Settles the fetch at its first data (streamed: when the stream connects; live: on the first event)
|
|
25
|
+
* instead of following the stream to its end. The stream is closed and the data kept.
|
|
26
|
+
* The stream reopens on mount only once the entry is stale (e.g. with `staleTime: 0`, as `StreamQuerySSRUtilsPlugin` sets in the browser).
|
|
27
|
+
* Use it only where nothing watches the stream, such as server rendering:
|
|
28
|
+
* a refetch while a component uses the query would also settle, closing its stream.
|
|
29
|
+
*
|
|
30
|
+
* @default false
|
|
31
|
+
*/
|
|
32
|
+
settleOnFirstData?: boolean | undefined;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* A Pinia Colada query function that accumulates streamed chunks into an array,
|
|
36
|
+
* publishing each chunk to the cache entry as it arrives.
|
|
37
|
+
*
|
|
38
|
+
* Inspired by [TanStack Query streamedQuery](https://tanstack.com/query/latest/docs/reference/streamedQuery) where:
|
|
39
|
+
* - Options are serializable
|
|
40
|
+
* - The output is predictable
|
|
41
|
+
*/
|
|
42
|
+
export declare function serializableStreamedQuery<T>(queryFn: (context: UseQueryFnContext) => Promisable<AsyncIterable<T>>, { refetchMode, maxChunks, settleOnFirstData, }?: SerializableStreamedQueryOptions & SettleOnFirstDataOptions): (context: UseQueryFnContext) => Promise<T[]>;
|