@orpc/pinia-colada 2.0.0-beta.47 → 2.0.0-beta.48
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 +2 -3
- package/dist/contract-utils.d.mts +35 -0
- package/dist/contract-utils.mjs +23 -0
- package/dist/index.d.mts +19 -501
- package/dist/index.mjs +11 -622
- package/dist/key.d.mts +57 -0
- package/dist/key.mjs +29 -0
- package/dist/live-query.d.mts +7 -0
- package/dist/live-query.mjs +24 -0
- package/dist/meta.d.mts +55 -0
- package/dist/meta.mjs +37 -0
- package/dist/plugin.d.mts +46 -0
- package/dist/plugin.mjs +25 -0
- package/dist/procedure-utils.d.mts +245 -0
- package/dist/procedure-utils.mjs +394 -0
- package/dist/router-utils.d.mts +57 -0
- package/dist/router-utils.mjs +51 -0
- package/dist/shared-utils.d.mts +13 -0
- package/dist/shared-utils.mjs +17 -0
- package/dist/stream-query.d.mts +30 -0
- package/dist/stream-query.mjs +45 -0
- package/dist/types.d.mts +125 -0
- package/dist/types.mjs +2 -0
- package/package.json +5 -5
- package/dist/index.d.ts +0 -501
|
@@ -0,0 +1,394 @@
|
|
|
1
|
+
import { intercept, isAsyncIteratorObject, isTypescriptObject, resolveMaybeOptionalOptions } from "@orpc/shared";
|
|
2
|
+
import { liveQuery } from "./live-query.mjs";
|
|
3
|
+
import { SharedUtils } from "./shared-utils.mjs";
|
|
4
|
+
import { serializableStreamedQuery } from "./stream-query.mjs";
|
|
5
|
+
import { OPERATION_CONTEXT_SYMBOL } from "./types.mjs";
|
|
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 (!isTypescriptObject(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
|
+
} else if (key === "serializer") {
|
|
38
|
+
if (!isTypescriptObject(value[key]) || typeof value[key].serializeJson !== "function") {
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
} else if (PROCEDURE_UTILS_INTERCEPTOR_KEYS.includes(key)) {
|
|
42
|
+
if (!Array.isArray(value[key]) || value[key].some((i) => typeof i !== "function")) {
|
|
43
|
+
return false;
|
|
44
|
+
}
|
|
45
|
+
} else if (PROCEDURE_UTILS_MODIFIER_KEYS.includes(key)) {
|
|
46
|
+
if (!isTypescriptObject(value[key])) {
|
|
47
|
+
return false;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
return true;
|
|
52
|
+
}
|
|
53
|
+
function mergeProcedureUtilsModifier(base, override) {
|
|
54
|
+
if (typeof override !== "function") {
|
|
55
|
+
if (typeof base !== "function") {
|
|
56
|
+
return {
|
|
57
|
+
...base,
|
|
58
|
+
...override
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
return (options) => base({
|
|
62
|
+
...override,
|
|
63
|
+
...options
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
const applyBase = typeof base === "function" ? base : (options) => ({
|
|
67
|
+
...base,
|
|
68
|
+
...options
|
|
69
|
+
});
|
|
70
|
+
return (options) => override(applyBase(options));
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export function mergeProcedureUtilsOptions(base, override) {
|
|
74
|
+
const merged = {
|
|
75
|
+
...base,
|
|
76
|
+
...override
|
|
77
|
+
};
|
|
78
|
+
for (const key of PROCEDURE_UTILS_INTERCEPTOR_KEYS) {
|
|
79
|
+
const baseValue = base[key];
|
|
80
|
+
const overrideValue = override[key];
|
|
81
|
+
if (baseValue && overrideValue) {
|
|
82
|
+
merged[key] = [...baseValue, ...overrideValue];
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
for (const key of PROCEDURE_UTILS_MODIFIER_KEYS) {
|
|
86
|
+
const baseValue = base[key];
|
|
87
|
+
const overrideValue = override[key];
|
|
88
|
+
if (baseValue && overrideValue) {
|
|
89
|
+
merged[key] = mergeProcedureUtilsModifier(baseValue, overrideValue);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return merged;
|
|
93
|
+
}
|
|
94
|
+
export class ProcedureUtils extends SharedUtils {
|
|
95
|
+
|
|
96
|
+
call;
|
|
97
|
+
constructor(path, client, options = {}) {
|
|
98
|
+
super(path, options);
|
|
99
|
+
this.call = client;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
queryKey(...rest) {
|
|
103
|
+
let optionsIn = resolveMaybeOptionalOptions(rest);
|
|
104
|
+
if (typeof this.options.queryKey === "function") {
|
|
105
|
+
optionsIn = this.options.queryKey(optionsIn);
|
|
106
|
+
} else if (this.options.queryKey) {
|
|
107
|
+
optionsIn = {
|
|
108
|
+
...this.options.queryKey,
|
|
109
|
+
...optionsIn
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
const key = optionsIn.key ?? this.key({
|
|
113
|
+
type: "query",
|
|
114
|
+
input: optionsIn.input
|
|
115
|
+
});
|
|
116
|
+
return key;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
queryOptions(...rest) {
|
|
120
|
+
let optionsIn = resolveMaybeOptionalOptions(rest);
|
|
121
|
+
if (typeof this.options.queryOptions === "function") {
|
|
122
|
+
optionsIn = this.options.queryOptions(optionsIn);
|
|
123
|
+
} else if (this.options.queryOptions) {
|
|
124
|
+
optionsIn = {
|
|
125
|
+
...this.options.queryOptions,
|
|
126
|
+
...optionsIn
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
const { input, context, key: _keyIn, query: queryIn, ...restOptions } = optionsIn;
|
|
130
|
+
const key = this.queryKey(optionsIn);
|
|
131
|
+
return {
|
|
132
|
+
...restOptions,
|
|
133
|
+
key,
|
|
134
|
+
query: (fnContext) => {
|
|
135
|
+
return intercept(this.options.queryInterceptors, {
|
|
136
|
+
path: this.path,
|
|
137
|
+
context: {
|
|
138
|
+
[OPERATION_CONTEXT_SYMBOL]: {
|
|
139
|
+
key,
|
|
140
|
+
type: "query"
|
|
141
|
+
},
|
|
142
|
+
...context
|
|
143
|
+
},
|
|
144
|
+
input,
|
|
145
|
+
fnContext
|
|
146
|
+
}, ({ context, input, fnContext }) => {
|
|
147
|
+
if (queryIn) {
|
|
148
|
+
return queryIn(fnContext);
|
|
149
|
+
}
|
|
150
|
+
return this.call(input, {
|
|
151
|
+
signal: fnContext.signal,
|
|
152
|
+
context
|
|
153
|
+
});
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
streamedKey(...rest) {
|
|
160
|
+
let optionsIn = resolveMaybeOptionalOptions(rest);
|
|
161
|
+
if (typeof this.options.streamedKey === "function") {
|
|
162
|
+
optionsIn = this.options.streamedKey(optionsIn);
|
|
163
|
+
} else if (this.options.streamedKey) {
|
|
164
|
+
optionsIn = {
|
|
165
|
+
...this.options.streamedKey,
|
|
166
|
+
...optionsIn
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
const key = optionsIn.key ?? this.key({
|
|
170
|
+
type: "streamed",
|
|
171
|
+
input: optionsIn.input,
|
|
172
|
+
fnOptions: optionsIn.fnOptions
|
|
173
|
+
});
|
|
174
|
+
return key;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
streamedOptions(...rest) {
|
|
178
|
+
let optionsIn = resolveMaybeOptionalOptions(rest);
|
|
179
|
+
if (typeof this.options.streamedOptions === "function") {
|
|
180
|
+
optionsIn = this.options.streamedOptions(optionsIn);
|
|
181
|
+
} else if (this.options.streamedOptions) {
|
|
182
|
+
optionsIn = {
|
|
183
|
+
...this.options.streamedOptions,
|
|
184
|
+
...optionsIn
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
const { input, context, key: _keyIn, query: queryIn, fnOptions, ...restOptions } = optionsIn;
|
|
188
|
+
const key = this.streamedKey(optionsIn);
|
|
189
|
+
return {
|
|
190
|
+
...restOptions,
|
|
191
|
+
key,
|
|
192
|
+
query: (fnContext) => {
|
|
193
|
+
return intercept(this.options.streamedInterceptors, {
|
|
194
|
+
path: this.path,
|
|
195
|
+
context: {
|
|
196
|
+
[OPERATION_CONTEXT_SYMBOL]: {
|
|
197
|
+
key,
|
|
198
|
+
type: "streamed"
|
|
199
|
+
},
|
|
200
|
+
...context
|
|
201
|
+
},
|
|
202
|
+
input,
|
|
203
|
+
fnContext
|
|
204
|
+
}, ({ context, input, fnContext }) => {
|
|
205
|
+
if (queryIn) {
|
|
206
|
+
return queryIn(fnContext);
|
|
207
|
+
}
|
|
208
|
+
return serializableStreamedQuery(async (queryContext) => {
|
|
209
|
+
const output = await this.call(input, {
|
|
210
|
+
signal: queryContext.signal,
|
|
211
|
+
context
|
|
212
|
+
});
|
|
213
|
+
if (!isAsyncIteratorObject(output)) {
|
|
214
|
+
throw new Error("streamedQuery requires an AsyncIteratorObject output");
|
|
215
|
+
}
|
|
216
|
+
return output;
|
|
217
|
+
}, fnOptions)(fnContext);
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
liveKey(...rest) {
|
|
224
|
+
let optionsIn = resolveMaybeOptionalOptions(rest);
|
|
225
|
+
if (typeof this.options.liveKey === "function") {
|
|
226
|
+
optionsIn = this.options.liveKey(optionsIn);
|
|
227
|
+
} else if (this.options.liveKey) {
|
|
228
|
+
optionsIn = {
|
|
229
|
+
...this.options.liveKey,
|
|
230
|
+
...optionsIn
|
|
231
|
+
};
|
|
232
|
+
}
|
|
233
|
+
const key = optionsIn.key ?? this.key({
|
|
234
|
+
type: "live",
|
|
235
|
+
input: optionsIn.input
|
|
236
|
+
});
|
|
237
|
+
return key;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
liveOptions(...rest) {
|
|
241
|
+
let optionsIn = resolveMaybeOptionalOptions(rest);
|
|
242
|
+
if (typeof this.options.liveOptions === "function") {
|
|
243
|
+
optionsIn = this.options.liveOptions(optionsIn);
|
|
244
|
+
} else if (this.options.liveOptions) {
|
|
245
|
+
optionsIn = {
|
|
246
|
+
...this.options.liveOptions,
|
|
247
|
+
...optionsIn
|
|
248
|
+
};
|
|
249
|
+
}
|
|
250
|
+
const { input, context, key: _keyIn, query: queryIn, ...restOptions } = optionsIn;
|
|
251
|
+
const key = this.liveKey(optionsIn);
|
|
252
|
+
return {
|
|
253
|
+
...restOptions,
|
|
254
|
+
key,
|
|
255
|
+
query: (fnContext) => {
|
|
256
|
+
return intercept(this.options.liveInterceptors, {
|
|
257
|
+
path: this.path,
|
|
258
|
+
context: {
|
|
259
|
+
[OPERATION_CONTEXT_SYMBOL]: {
|
|
260
|
+
key,
|
|
261
|
+
type: "live"
|
|
262
|
+
},
|
|
263
|
+
...context
|
|
264
|
+
},
|
|
265
|
+
input,
|
|
266
|
+
fnContext
|
|
267
|
+
}, ({ context, input, fnContext }) => {
|
|
268
|
+
if (queryIn) {
|
|
269
|
+
return queryIn(fnContext);
|
|
270
|
+
}
|
|
271
|
+
return liveQuery(async (queryContext) => {
|
|
272
|
+
const output = await this.call(input, {
|
|
273
|
+
signal: queryContext.signal,
|
|
274
|
+
context
|
|
275
|
+
});
|
|
276
|
+
if (!isAsyncIteratorObject(output)) {
|
|
277
|
+
throw new Error("liveQuery requires an AsyncIteratorObject output");
|
|
278
|
+
}
|
|
279
|
+
return output;
|
|
280
|
+
})(fnContext);
|
|
281
|
+
});
|
|
282
|
+
}
|
|
283
|
+
};
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
infiniteKey(optionsIn) {
|
|
287
|
+
if (typeof this.options.infiniteKey === "function") {
|
|
288
|
+
optionsIn = this.options.infiniteKey(optionsIn);
|
|
289
|
+
} else if (this.options.infiniteKey) {
|
|
290
|
+
optionsIn = {
|
|
291
|
+
...this.options.infiniteKey,
|
|
292
|
+
...optionsIn
|
|
293
|
+
};
|
|
294
|
+
}
|
|
295
|
+
const key = optionsIn.key ?? this.key({
|
|
296
|
+
type: "infinite",
|
|
297
|
+
input: optionsIn.input(typeof optionsIn.initialPageParam === "function" ? optionsIn.initialPageParam() : optionsIn.initialPageParam)
|
|
298
|
+
});
|
|
299
|
+
return key;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
infiniteOptions(optionsIn) {
|
|
303
|
+
if (typeof this.options.infiniteOptions === "function") {
|
|
304
|
+
optionsIn = this.options.infiniteOptions(optionsIn);
|
|
305
|
+
} else if (this.options.infiniteOptions) {
|
|
306
|
+
optionsIn = {
|
|
307
|
+
...this.options.infiniteOptions,
|
|
308
|
+
...optionsIn
|
|
309
|
+
};
|
|
310
|
+
}
|
|
311
|
+
const { input, context, key: _keyIn, query: queryIn, ...restOptions } = optionsIn;
|
|
312
|
+
const key = this.infiniteKey(optionsIn);
|
|
313
|
+
return {
|
|
314
|
+
...restOptions,
|
|
315
|
+
key,
|
|
316
|
+
query: (fnContext) => {
|
|
317
|
+
return intercept(this.options.infiniteInterceptors, {
|
|
318
|
+
path: this.path,
|
|
319
|
+
context: {
|
|
320
|
+
[OPERATION_CONTEXT_SYMBOL]: {
|
|
321
|
+
key,
|
|
322
|
+
type: "infinite"
|
|
323
|
+
},
|
|
324
|
+
...context
|
|
325
|
+
},
|
|
326
|
+
input: input(fnContext.pageParam),
|
|
327
|
+
fnContext
|
|
328
|
+
}, ({ context, input, fnContext }) => {
|
|
329
|
+
if (queryIn) {
|
|
330
|
+
return queryIn(fnContext);
|
|
331
|
+
}
|
|
332
|
+
return this.call(input, {
|
|
333
|
+
signal: fnContext.signal,
|
|
334
|
+
context
|
|
335
|
+
});
|
|
336
|
+
});
|
|
337
|
+
}
|
|
338
|
+
};
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
mutationKey(...rest) {
|
|
342
|
+
let optionsIn = resolveMaybeOptionalOptions(rest);
|
|
343
|
+
if (typeof this.options.mutationKey === "function") {
|
|
344
|
+
optionsIn = this.options.mutationKey(optionsIn);
|
|
345
|
+
} else if (this.options.mutationKey) {
|
|
346
|
+
optionsIn = {
|
|
347
|
+
...this.options.mutationKey,
|
|
348
|
+
...optionsIn
|
|
349
|
+
};
|
|
350
|
+
}
|
|
351
|
+
const key = optionsIn.key ?? ((input) => this.key({
|
|
352
|
+
type: "mutation",
|
|
353
|
+
input
|
|
354
|
+
}));
|
|
355
|
+
return key;
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
mutationOptions(...rest) {
|
|
359
|
+
let optionsIn = resolveMaybeOptionalOptions(rest);
|
|
360
|
+
if (typeof this.options.mutationOptions === "function") {
|
|
361
|
+
optionsIn = this.options.mutationOptions(optionsIn);
|
|
362
|
+
} else if (this.options.mutationOptions) {
|
|
363
|
+
optionsIn = {
|
|
364
|
+
...this.options.mutationOptions,
|
|
365
|
+
...optionsIn
|
|
366
|
+
};
|
|
367
|
+
}
|
|
368
|
+
const { context, key: _keyIn, mutation: mutationIn, ...restOptions } = optionsIn;
|
|
369
|
+
const key = this.mutationKey(optionsIn);
|
|
370
|
+
return {
|
|
371
|
+
...restOptions,
|
|
372
|
+
key,
|
|
373
|
+
mutation: (input, fnContext) => {
|
|
374
|
+
return intercept(this.options.mutationInterceptors, {
|
|
375
|
+
path: this.path,
|
|
376
|
+
context: {
|
|
377
|
+
[OPERATION_CONTEXT_SYMBOL]: {
|
|
378
|
+
key: typeof key === "function" ? key(input) : key,
|
|
379
|
+
type: "mutation"
|
|
380
|
+
},
|
|
381
|
+
...context
|
|
382
|
+
},
|
|
383
|
+
input,
|
|
384
|
+
fnContext
|
|
385
|
+
}, ({ context, input, fnContext }) => {
|
|
386
|
+
if (mutationIn) {
|
|
387
|
+
return mutationIn(input, fnContext);
|
|
388
|
+
}
|
|
389
|
+
return this.call(input, { context });
|
|
390
|
+
});
|
|
391
|
+
}
|
|
392
|
+
};
|
|
393
|
+
}
|
|
394
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import type { AnyNestedClient, Client, InferClientContext, InferClientError } from "@orpc/client";
|
|
2
|
+
import type { Public } from "@orpc/shared";
|
|
3
|
+
import type { OperationKeyBaseOptions } from "./key.mjs";
|
|
4
|
+
import type { RouterUtilsPlugin } from "./plugin.mjs";
|
|
5
|
+
import type { ProcedureUtilsInfiniteInterceptor, ProcedureUtilsLiveInterceptor, ProcedureUtilsMutationInterceptor, ProcedureUtilsOptions, ProcedureUtilsQueryInterceptor, ProcedureUtilsStreamedInterceptor } from "./procedure-utils.mjs";
|
|
6
|
+
import { ProcedureUtils } from "./procedure-utils.mjs";
|
|
7
|
+
import { SharedUtils } from "./shared-utils.mjs";
|
|
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>> : { [K in keyof T]: T[K] extends AnyNestedClient ? RouterUtils<T[K]> : never } & Public<SharedUtils<unknown>>;
|
|
15
|
+
export type RouterUtilsScoped<T extends AnyNestedClient> = T extends Client<infer UClientContext, infer UInput, infer UOutput, infer UError> ? ProcedureUtilsOptions<UClientContext, UInput, UOutput, UError> : { [K in keyof T]?: (T[K] extends AnyNestedClient ? RouterUtilsScoped<T[K]> : never) | undefined };
|
|
16
|
+
export interface RouterUtilsOptions<T extends AnyNestedClient> extends OperationKeyBaseOptions {
|
|
17
|
+
/**
|
|
18
|
+
* Interceptors that intercept query inside .queryOptions
|
|
19
|
+
*/
|
|
20
|
+
queryInterceptors?: ProcedureUtilsQueryInterceptor<InferClientContext<T>, unknown, unknown, InferClientError<T>>[] | undefined;
|
|
21
|
+
/**
|
|
22
|
+
* Interceptors that intercept query inside .streamedOptions
|
|
23
|
+
*/
|
|
24
|
+
streamedInterceptors?: ProcedureUtilsStreamedInterceptor<InferClientContext<T>, unknown, unknown, InferClientError<T>>[] | undefined;
|
|
25
|
+
/**
|
|
26
|
+
* Interceptors that intercept query inside .liveOptions
|
|
27
|
+
*/
|
|
28
|
+
liveInterceptors?: ProcedureUtilsLiveInterceptor<InferClientContext<T>, unknown, unknown, InferClientError<T>>[] | undefined;
|
|
29
|
+
/**
|
|
30
|
+
* Interceptors that intercept query inside .infiniteOptions
|
|
31
|
+
*/
|
|
32
|
+
infiniteInterceptors?: ProcedureUtilsInfiniteInterceptor<InferClientContext<T>, unknown, unknown, InferClientError<T>>[] | undefined;
|
|
33
|
+
/**
|
|
34
|
+
* Interceptors that intercept mutation inside .mutationOptions
|
|
35
|
+
*/
|
|
36
|
+
mutationInterceptors?: ProcedureUtilsMutationInterceptor<InferClientContext<T>, unknown, unknown, InferClientError<T>>[] | undefined;
|
|
37
|
+
/**
|
|
38
|
+
* Per-procedure options following the shape of the router.
|
|
39
|
+
* Allows fine-grained configuration of individual procedure utils
|
|
40
|
+
* without affecting the rest of the router.
|
|
41
|
+
*/
|
|
42
|
+
scoped?: RouterUtilsScoped<T> | undefined;
|
|
43
|
+
/**
|
|
44
|
+
* Plugins to extend router utils behavior.
|
|
45
|
+
*/
|
|
46
|
+
plugins?: RouterUtilsPlugin<T>[] | undefined;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Creates Pinia Colada utils from a client, exposing query/mutation
|
|
50
|
+
* option builders for every procedure in the router.
|
|
51
|
+
*
|
|
52
|
+
* @remarks
|
|
53
|
+
* **Note**: Both client-side and server-side clients are supported.
|
|
54
|
+
*
|
|
55
|
+
* @see {@link https://orpc.dev/docs/integrations/pinia-colada | Pinia Colada Integration}
|
|
56
|
+
*/
|
|
57
|
+
export declare function createRouterUtils<T extends AnyNestedClient>(client: T, options?: NoInfer<RouterUtilsOptions<T>>): RouterUtils<T>;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { RECURSIVE_CLIENT_UNWRAP_KEYS } from "@orpc/client";
|
|
2
|
+
import { bindMethods, isTypescriptObject, pick, toArray } from "@orpc/shared";
|
|
3
|
+
import { CompositeRouterUtilsPlugin } from "./plugin.mjs";
|
|
4
|
+
import { isProcedureUtilsOptions, mergeProcedureUtilsOptions, ProcedureUtils } from "./procedure-utils.mjs";
|
|
5
|
+
import { SharedUtils } from "./shared-utils.mjs";
|
|
6
|
+
|
|
7
|
+
export function createRouterUtils(client, options = {}) {
|
|
8
|
+
const plugin = new CompositeRouterUtilsPlugin(options.plugins);
|
|
9
|
+
options = plugin.init(options);
|
|
10
|
+
return createRouterUtilsInternal(client, options, plugin);
|
|
11
|
+
}
|
|
12
|
+
function createRouterUtilsInternal(client, options, plugin) {
|
|
13
|
+
const path = toArray(options.path);
|
|
14
|
+
const utils = typeof client === "function" && (options.scoped === undefined || isProcedureUtilsOptions(options.scoped)) ? bindMethods(new ProcedureUtils(path, client, plugin.initProcedureOptions(path, mergeProcedureUtilsOptions(pick(options, [
|
|
15
|
+
"prefix",
|
|
16
|
+
"serializer",
|
|
17
|
+
"queryInterceptors",
|
|
18
|
+
"streamedInterceptors",
|
|
19
|
+
"liveInterceptors",
|
|
20
|
+
"infiniteInterceptors",
|
|
21
|
+
"mutationInterceptors"
|
|
22
|
+
]), options.scoped ?? {}))), { unbound: ["call"] }) : bindMethods(new SharedUtils(path, options));
|
|
23
|
+
const cache = new Map();
|
|
24
|
+
const recursive = new Proxy(utils, { get(target, prop) {
|
|
25
|
+
const value = Reflect.get(target, prop);
|
|
26
|
+
if (RECURSIVE_CLIENT_UNWRAP_KEYS.has(prop)) {
|
|
27
|
+
return value ?? Reflect.get(Object.prototype, prop);
|
|
28
|
+
}
|
|
29
|
+
const nextClient = Reflect.get(client, prop);
|
|
30
|
+
if (typeof prop !== "string" || !isTypescriptObject(nextClient)) {
|
|
31
|
+
return value;
|
|
32
|
+
}
|
|
33
|
+
let result = cache.get(prop);
|
|
34
|
+
if (result === undefined) {
|
|
35
|
+
const nextUtils = createRouterUtilsInternal(nextClient, {
|
|
36
|
+
...options,
|
|
37
|
+
path: [...path, prop],
|
|
38
|
+
scoped: options.scoped?.[prop]
|
|
39
|
+
}, plugin);
|
|
40
|
+
result = typeof value !== "function" ? nextUtils : new Proxy(value, { get(target, prop) {
|
|
41
|
+
if (typeof prop !== "string" || RECURSIVE_CLIENT_UNWRAP_KEYS.has(prop)) {
|
|
42
|
+
return Reflect.get(target, prop);
|
|
43
|
+
}
|
|
44
|
+
return Reflect.get(nextUtils, prop);
|
|
45
|
+
} });
|
|
46
|
+
cache.set(prop, result);
|
|
47
|
+
}
|
|
48
|
+
return result;
|
|
49
|
+
} });
|
|
50
|
+
return recursive;
|
|
51
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { OperationKey, OperationKeyBaseOptions, OperationKeyOptions } from "./key.mjs";
|
|
2
|
+
import type { OperationType } from "./types.mjs";
|
|
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,17 @@
|
|
|
1
|
+
import { generateOperationKey } from "./key.mjs";
|
|
2
|
+
export class SharedUtils {
|
|
3
|
+
path;
|
|
4
|
+
options;
|
|
5
|
+
constructor(path, options) {
|
|
6
|
+
this.path = path;
|
|
7
|
+
this.options = options;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
key(options = {}) {
|
|
11
|
+
return generateOperationKey(this.path, {
|
|
12
|
+
...options,
|
|
13
|
+
prefix: this.options.prefix,
|
|
14
|
+
serializer: this.options.serializer
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { Promisable } from "@orpc/shared";
|
|
2
|
+
import type { UseQueryFnContext } from "./types.mjs";
|
|
3
|
+
export interface SerializableStreamedQueryOptions {
|
|
4
|
+
/**
|
|
5
|
+
* Determines how data is handled when the query is fetched again
|
|
6
|
+
*
|
|
7
|
+
* - `'reset'`: Clears existing data and returns the query to a pending state.
|
|
8
|
+
* - `'append'`: Adds new streamed chunks to the existing data.
|
|
9
|
+
* - `'replace'`: Buffers streamed data and replaces the cache after the stream completes.
|
|
10
|
+
*
|
|
11
|
+
* @default 'reset'
|
|
12
|
+
*/
|
|
13
|
+
refetchMode?: "append" | "reset" | "replace" | 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
|
+
/**
|
|
23
|
+
* A Pinia Colada query function that accumulates streamed chunks into an array,
|
|
24
|
+
* publishing each chunk to the cache entry as it arrives.
|
|
25
|
+
*
|
|
26
|
+
* Inspired by [TanStack Query streamedQuery](https://tanstack.com/query/latest/docs/reference/streamedQuery) where:
|
|
27
|
+
* - Options are serializable
|
|
28
|
+
* - The output is predictable
|
|
29
|
+
*/
|
|
30
|
+
export declare function serializableStreamedQuery<T>(queryFn: (context: UseQueryFnContext) => Promisable<AsyncIterable<T>>, { refetchMode, maxChunks }?: SerializableStreamedQueryOptions): (context: UseQueryFnContext) => Promise<T[]>;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
|
|
2
|
+
export function serializableStreamedQuery(queryFn, { refetchMode = "reset", maxChunks = Number.POSITIVE_INFINITY } = {}) {
|
|
3
|
+
return async (context) => {
|
|
4
|
+
const { entry, signal } = context;
|
|
5
|
+
const previousData = entry.state.value.data;
|
|
6
|
+
const hasPreviousData = previousData !== undefined;
|
|
7
|
+
if (hasPreviousData && refetchMode === "reset") {
|
|
8
|
+
entry.state.value = {
|
|
9
|
+
status: "pending",
|
|
10
|
+
data: undefined,
|
|
11
|
+
error: null
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
const stream = await queryFn(context);
|
|
15
|
+
const shouldUpdateCacheDuringStream = !hasPreviousData || refetchMode !== "replace";
|
|
16
|
+
let result = hasPreviousData && refetchMode === "append" ? limitArraySize(previousData, maxChunks) : [];
|
|
17
|
+
if (shouldUpdateCacheDuringStream) {
|
|
18
|
+
entry.state.value = {
|
|
19
|
+
status: "success",
|
|
20
|
+
data: result,
|
|
21
|
+
error: null
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
for await (const chunk of stream) {
|
|
25
|
+
if (signal.aborted) {
|
|
26
|
+
throw signal.reason;
|
|
27
|
+
}
|
|
28
|
+
result = limitArraySize([...result, chunk], maxChunks);
|
|
29
|
+
if (shouldUpdateCacheDuringStream) {
|
|
30
|
+
entry.state.value = {
|
|
31
|
+
status: "success",
|
|
32
|
+
data: result,
|
|
33
|
+
error: null
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return result;
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
function limitArraySize(items, maxSize) {
|
|
41
|
+
if (items.length <= maxSize) {
|
|
42
|
+
return items;
|
|
43
|
+
}
|
|
44
|
+
return items.slice(items.length - maxSize);
|
|
45
|
+
}
|