@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
package/dist/index.mjs
DELETED
|
@@ -1,622 +0,0 @@
|
|
|
1
|
-
import { resolveBasePathMeta, getRouterContract, ProcedureContract } from '@orpc/contract';
|
|
2
|
-
import { sortPlugins, stringifyJSON, resolveMaybeOptionalOptions, intercept, isTypescriptObject, isAsyncIteratorObject, toArray, bindMethods, pick, get } from '@orpc/shared';
|
|
3
|
-
import { RPCJsonSerializer, RECURSIVE_CLIENT_UNWRAP_KEYS } from '@orpc/client';
|
|
4
|
-
|
|
5
|
-
class CompositeRouterUtilsPlugin {
|
|
6
|
-
constructor(plugins = []) {
|
|
7
|
-
this.plugins = plugins;
|
|
8
|
-
this.plugins = sortPlugins(plugins);
|
|
9
|
-
}
|
|
10
|
-
name = "~composite";
|
|
11
|
-
init(options) {
|
|
12
|
-
for (const plugin of this.plugins) {
|
|
13
|
-
if (plugin.init) {
|
|
14
|
-
options = plugin.init(options);
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
return options;
|
|
18
|
-
}
|
|
19
|
-
initProcedureOptions(path, options) {
|
|
20
|
-
for (const plugin of this.plugins) {
|
|
21
|
-
if (plugin.initProcedureOptions) {
|
|
22
|
-
options = plugin.initProcedureOptions(path, options);
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
return options;
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
function liveQuery(queryFn) {
|
|
30
|
-
return async (context) => {
|
|
31
|
-
const { entry, signal } = context;
|
|
32
|
-
const stream = await queryFn(context);
|
|
33
|
-
let last;
|
|
34
|
-
for await (const chunk of stream) {
|
|
35
|
-
if (signal.aborted) {
|
|
36
|
-
throw signal.reason;
|
|
37
|
-
}
|
|
38
|
-
last = { chunk };
|
|
39
|
-
entry.state.value = { status: "success", data: chunk, error: null };
|
|
40
|
-
}
|
|
41
|
-
if (!last) {
|
|
42
|
-
throw new TypeError(
|
|
43
|
-
`Live query for ${stringifyJSON(entry.key)} did not yield any data. Ensure the query function returns an AsyncIterable with at least one chunk.`
|
|
44
|
-
);
|
|
45
|
-
}
|
|
46
|
-
return last.chunk;
|
|
47
|
-
};
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
const defaultSerializer = new RPCJsonSerializer();
|
|
51
|
-
function generateOperationKey(path, options = {}) {
|
|
52
|
-
return [
|
|
53
|
-
...options.prefix !== void 0 ? [options.prefix] : [],
|
|
54
|
-
path,
|
|
55
|
-
{
|
|
56
|
-
...options.input !== void 0 ? serializeInput(options.serializer ?? defaultSerializer, options.input) : {},
|
|
57
|
-
...options.type !== void 0 ? { type: options.type } : {},
|
|
58
|
-
...options.fnOptions !== void 0 ? { fnOptions: options.fnOptions } : {}
|
|
59
|
-
}
|
|
60
|
-
];
|
|
61
|
-
}
|
|
62
|
-
function serializeInput(serializer, input) {
|
|
63
|
-
const { json, meta } = serializer.serializeJson(input);
|
|
64
|
-
if (!meta?.length) {
|
|
65
|
-
return { input: json };
|
|
66
|
-
}
|
|
67
|
-
const inputMeta = {};
|
|
68
|
-
for (const [type, ...segments] of meta) {
|
|
69
|
-
(inputMeta[stringifyJSON(segments)] ??= []).push(type);
|
|
70
|
-
}
|
|
71
|
-
return { input: json, inputMeta };
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
class SharedUtils {
|
|
75
|
-
constructor(path, options) {
|
|
76
|
-
this.path = path;
|
|
77
|
-
this.options = options;
|
|
78
|
-
}
|
|
79
|
-
/**
|
|
80
|
-
* Generate a **partial matching** key for actions like revalidating queries, checking mutation status, etc.
|
|
81
|
-
*
|
|
82
|
-
* @see {@link https://orpc.dev/docs/integrations/pinia-colada#querymutation-key | Pinia Colada Integration - Query/Mutation Key}
|
|
83
|
-
*/
|
|
84
|
-
key(options = {}) {
|
|
85
|
-
return generateOperationKey(this.path, { ...options, prefix: this.options.prefix, serializer: this.options.serializer });
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
function serializableStreamedQuery(queryFn, { refetchMode = "reset", maxChunks = Number.POSITIVE_INFINITY } = {}) {
|
|
90
|
-
return async (context) => {
|
|
91
|
-
const { entry, signal } = context;
|
|
92
|
-
const previousData = entry.state.value.data;
|
|
93
|
-
const hasPreviousData = previousData !== void 0;
|
|
94
|
-
if (hasPreviousData && refetchMode === "reset") {
|
|
95
|
-
entry.state.value = { status: "pending", data: void 0, error: null };
|
|
96
|
-
}
|
|
97
|
-
const stream = await queryFn(context);
|
|
98
|
-
const shouldUpdateCacheDuringStream = !hasPreviousData || refetchMode !== "replace";
|
|
99
|
-
let result = hasPreviousData && refetchMode === "append" ? limitArraySize(previousData, maxChunks) : [];
|
|
100
|
-
if (shouldUpdateCacheDuringStream) {
|
|
101
|
-
entry.state.value = { status: "success", data: result, error: null };
|
|
102
|
-
}
|
|
103
|
-
for await (const chunk of stream) {
|
|
104
|
-
if (signal.aborted) {
|
|
105
|
-
throw signal.reason;
|
|
106
|
-
}
|
|
107
|
-
result = limitArraySize([...result, chunk], maxChunks);
|
|
108
|
-
if (shouldUpdateCacheDuringStream) {
|
|
109
|
-
entry.state.value = { status: "success", data: result, error: null };
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
return result;
|
|
113
|
-
};
|
|
114
|
-
}
|
|
115
|
-
function limitArraySize(items, maxSize) {
|
|
116
|
-
if (items.length <= maxSize) {
|
|
117
|
-
return items;
|
|
118
|
-
}
|
|
119
|
-
return items.slice(items.length - maxSize);
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
const OPERATION_CONTEXT_SYMBOL = Symbol.for("ORPC_PINIA_COLADA_OPERATION_CONTEXT");
|
|
123
|
-
|
|
124
|
-
const PROCEDURE_UTILS_INTERCEPTOR_KEYS = [
|
|
125
|
-
"queryInterceptors",
|
|
126
|
-
"streamedInterceptors",
|
|
127
|
-
"liveInterceptors",
|
|
128
|
-
"infiniteInterceptors",
|
|
129
|
-
"mutationInterceptors"
|
|
130
|
-
];
|
|
131
|
-
const PROCEDURE_UTILS_MODIFIER_KEYS = [
|
|
132
|
-
"queryKey",
|
|
133
|
-
"queryOptions",
|
|
134
|
-
"streamedKey",
|
|
135
|
-
"streamedOptions",
|
|
136
|
-
"liveKey",
|
|
137
|
-
"liveOptions",
|
|
138
|
-
"infiniteKey",
|
|
139
|
-
"infiniteOptions",
|
|
140
|
-
"mutationKey",
|
|
141
|
-
"mutationOptions"
|
|
142
|
-
];
|
|
143
|
-
function isProcedureUtilsOptions(value) {
|
|
144
|
-
if (!isTypescriptObject(value)) {
|
|
145
|
-
return false;
|
|
146
|
-
}
|
|
147
|
-
for (const key of Object.keys(value)) {
|
|
148
|
-
if (value[key] === void 0) {
|
|
149
|
-
continue;
|
|
150
|
-
}
|
|
151
|
-
if (key === "prefix") {
|
|
152
|
-
if (typeof value[key] !== "string") {
|
|
153
|
-
return false;
|
|
154
|
-
}
|
|
155
|
-
} else if (key === "serializer") {
|
|
156
|
-
if (!isTypescriptObject(value[key]) || typeof value[key].serializeJson !== "function") {
|
|
157
|
-
return false;
|
|
158
|
-
}
|
|
159
|
-
} else if (PROCEDURE_UTILS_INTERCEPTOR_KEYS.includes(key)) {
|
|
160
|
-
if (!Array.isArray(value[key]) || value[key].some((i) => typeof i !== "function")) {
|
|
161
|
-
return false;
|
|
162
|
-
}
|
|
163
|
-
} else if (PROCEDURE_UTILS_MODIFIER_KEYS.includes(key)) {
|
|
164
|
-
if (!isTypescriptObject(value[key])) {
|
|
165
|
-
return false;
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
return true;
|
|
170
|
-
}
|
|
171
|
-
function mergeProcedureUtilsModifier(base, override) {
|
|
172
|
-
if (typeof override !== "function") {
|
|
173
|
-
if (typeof base !== "function") {
|
|
174
|
-
return { ...base, ...override };
|
|
175
|
-
}
|
|
176
|
-
return (options) => base({ ...override, ...options });
|
|
177
|
-
}
|
|
178
|
-
const applyBase = typeof base === "function" ? base : (options) => ({ ...base, ...options });
|
|
179
|
-
return (options) => override(applyBase(options));
|
|
180
|
-
}
|
|
181
|
-
function mergeProcedureUtilsOptions(base, override) {
|
|
182
|
-
const merged = { ...base, ...override };
|
|
183
|
-
for (const key of PROCEDURE_UTILS_INTERCEPTOR_KEYS) {
|
|
184
|
-
const baseValue = base[key];
|
|
185
|
-
const overrideValue = override[key];
|
|
186
|
-
if (baseValue && overrideValue) {
|
|
187
|
-
merged[key] = [...baseValue, ...overrideValue];
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
for (const key of PROCEDURE_UTILS_MODIFIER_KEYS) {
|
|
191
|
-
const baseValue = base[key];
|
|
192
|
-
const overrideValue = override[key];
|
|
193
|
-
if (baseValue && overrideValue) {
|
|
194
|
-
merged[key] = mergeProcedureUtilsModifier(baseValue, overrideValue);
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
return merged;
|
|
198
|
-
}
|
|
199
|
-
class ProcedureUtils extends SharedUtils {
|
|
200
|
-
/**
|
|
201
|
-
* Calling corresponding procedure client
|
|
202
|
-
*
|
|
203
|
-
* @see {@link https://orpc.dev/docs/integrations/pinia-colada#calling-procedure-clients | Pinia Colada Integration - Calling Procedure Clients}
|
|
204
|
-
*/
|
|
205
|
-
call;
|
|
206
|
-
constructor(path, client, options = {}) {
|
|
207
|
-
super(path, options);
|
|
208
|
-
this.call = client;
|
|
209
|
-
}
|
|
210
|
-
/**
|
|
211
|
-
* Generate a **full matching** key for useQuery/...
|
|
212
|
-
*
|
|
213
|
-
* @see {@link https://orpc.dev/docs/integrations/pinia-colada#querymutation-key | Pinia Colada Integration - Query/Mutation Key}
|
|
214
|
-
*/
|
|
215
|
-
queryKey(...rest) {
|
|
216
|
-
let optionsIn = resolveMaybeOptionalOptions(rest);
|
|
217
|
-
if (typeof this.options.queryKey === "function") {
|
|
218
|
-
optionsIn = this.options.queryKey(optionsIn);
|
|
219
|
-
} else if (this.options.queryKey) {
|
|
220
|
-
optionsIn = { ...this.options.queryKey, ...optionsIn };
|
|
221
|
-
}
|
|
222
|
-
const key = optionsIn.key ?? this.key({ type: "query", input: optionsIn.input });
|
|
223
|
-
return key;
|
|
224
|
-
}
|
|
225
|
-
/**
|
|
226
|
-
* Generate options used for useQuery/...
|
|
227
|
-
*
|
|
228
|
-
* @see {@link https://orpc.dev/docs/integrations/pinia-colada#query-options-utility | Pinia Colada Integration - Query Options Utility}
|
|
229
|
-
*/
|
|
230
|
-
queryOptions(...rest) {
|
|
231
|
-
let optionsIn = resolveMaybeOptionalOptions(rest);
|
|
232
|
-
if (typeof this.options.queryOptions === "function") {
|
|
233
|
-
optionsIn = this.options.queryOptions(optionsIn);
|
|
234
|
-
} else if (this.options.queryOptions) {
|
|
235
|
-
optionsIn = { ...this.options.queryOptions, ...optionsIn };
|
|
236
|
-
}
|
|
237
|
-
const { input, context, key: _keyIn, query: queryIn, ...restOptions } = optionsIn;
|
|
238
|
-
const key = this.queryKey(optionsIn);
|
|
239
|
-
return {
|
|
240
|
-
...restOptions,
|
|
241
|
-
key,
|
|
242
|
-
query: (fnContext) => {
|
|
243
|
-
return intercept(
|
|
244
|
-
this.options.queryInterceptors,
|
|
245
|
-
{
|
|
246
|
-
path: this.path,
|
|
247
|
-
context: {
|
|
248
|
-
[OPERATION_CONTEXT_SYMBOL]: {
|
|
249
|
-
key,
|
|
250
|
-
type: "query"
|
|
251
|
-
},
|
|
252
|
-
...context
|
|
253
|
-
},
|
|
254
|
-
input,
|
|
255
|
-
fnContext
|
|
256
|
-
},
|
|
257
|
-
({ context: context2, input: input2, fnContext: fnContext2 }) => {
|
|
258
|
-
if (queryIn) {
|
|
259
|
-
return queryIn(fnContext2);
|
|
260
|
-
}
|
|
261
|
-
return this.call(input2, { signal: fnContext2.signal, context: context2 });
|
|
262
|
-
}
|
|
263
|
-
);
|
|
264
|
-
}
|
|
265
|
-
};
|
|
266
|
-
}
|
|
267
|
-
/**
|
|
268
|
-
* Generate a **full matching** key for [Streamed Query Options](https://orpc.dev/docs/integrations/pinia-colada#streamed-query-options-utility).
|
|
269
|
-
*
|
|
270
|
-
* @see {@link https://orpc.dev/docs/integrations/pinia-colada#querymutation-key | Pinia Colada Integration - Query/Mutation Key}
|
|
271
|
-
*/
|
|
272
|
-
streamedKey(...rest) {
|
|
273
|
-
let optionsIn = resolveMaybeOptionalOptions(rest);
|
|
274
|
-
if (typeof this.options.streamedKey === "function") {
|
|
275
|
-
optionsIn = this.options.streamedKey(optionsIn);
|
|
276
|
-
} else if (this.options.streamedKey) {
|
|
277
|
-
optionsIn = { ...this.options.streamedKey, ...optionsIn };
|
|
278
|
-
}
|
|
279
|
-
const key = optionsIn.key ?? this.key({ type: "streamed", input: optionsIn.input, fnOptions: optionsIn.fnOptions });
|
|
280
|
-
return key;
|
|
281
|
-
}
|
|
282
|
-
/**
|
|
283
|
-
* Configure queries for [AsyncIteratorObject](https://orpc.dev/docs/async-iterator-object).
|
|
284
|
-
* The resulting data is an array of chunks, and each new chunk is appended as it arrives.
|
|
285
|
-
* Works with `useQuery` and any other API that accepts query options.
|
|
286
|
-
*/
|
|
287
|
-
streamedOptions(...rest) {
|
|
288
|
-
let optionsIn = resolveMaybeOptionalOptions(rest);
|
|
289
|
-
if (typeof this.options.streamedOptions === "function") {
|
|
290
|
-
optionsIn = this.options.streamedOptions(optionsIn);
|
|
291
|
-
} else if (this.options.streamedOptions) {
|
|
292
|
-
optionsIn = { ...this.options.streamedOptions, ...optionsIn };
|
|
293
|
-
}
|
|
294
|
-
const { input, context, key: _keyIn, query: queryIn, fnOptions, ...restOptions } = optionsIn;
|
|
295
|
-
const key = this.streamedKey(optionsIn);
|
|
296
|
-
return {
|
|
297
|
-
...restOptions,
|
|
298
|
-
key,
|
|
299
|
-
query: (fnContext) => {
|
|
300
|
-
return intercept(
|
|
301
|
-
this.options.streamedInterceptors,
|
|
302
|
-
{
|
|
303
|
-
path: this.path,
|
|
304
|
-
context: {
|
|
305
|
-
[OPERATION_CONTEXT_SYMBOL]: {
|
|
306
|
-
key,
|
|
307
|
-
type: "streamed"
|
|
308
|
-
},
|
|
309
|
-
...context
|
|
310
|
-
},
|
|
311
|
-
input,
|
|
312
|
-
fnContext
|
|
313
|
-
},
|
|
314
|
-
({ context: context2, input: input2, fnContext: fnContext2 }) => {
|
|
315
|
-
if (queryIn) {
|
|
316
|
-
return queryIn(fnContext2);
|
|
317
|
-
}
|
|
318
|
-
return serializableStreamedQuery(
|
|
319
|
-
async (queryContext) => {
|
|
320
|
-
const output = await this.call(input2, { signal: queryContext.signal, context: context2 });
|
|
321
|
-
if (!isAsyncIteratorObject(output)) {
|
|
322
|
-
throw new Error("streamedQuery requires an AsyncIteratorObject output");
|
|
323
|
-
}
|
|
324
|
-
return output;
|
|
325
|
-
},
|
|
326
|
-
fnOptions
|
|
327
|
-
)(fnContext2);
|
|
328
|
-
}
|
|
329
|
-
);
|
|
330
|
-
}
|
|
331
|
-
};
|
|
332
|
-
}
|
|
333
|
-
/**
|
|
334
|
-
* Generate a **full matching** key for [Live Query Options](https://orpc.dev/docs/integrations/pinia-colada#live-query-options-utility).
|
|
335
|
-
*
|
|
336
|
-
* @see {@link https://orpc.dev/docs/integrations/pinia-colada#querymutation-key | Pinia Colada Integration - Query/Mutation Key}
|
|
337
|
-
*/
|
|
338
|
-
liveKey(...rest) {
|
|
339
|
-
let optionsIn = resolveMaybeOptionalOptions(rest);
|
|
340
|
-
if (typeof this.options.liveKey === "function") {
|
|
341
|
-
optionsIn = this.options.liveKey(optionsIn);
|
|
342
|
-
} else if (this.options.liveKey) {
|
|
343
|
-
optionsIn = { ...this.options.liveKey, ...optionsIn };
|
|
344
|
-
}
|
|
345
|
-
const key = optionsIn.key ?? this.key({ type: "live", input: optionsIn.input });
|
|
346
|
-
return key;
|
|
347
|
-
}
|
|
348
|
-
/**
|
|
349
|
-
* Configure live queries for [AsyncIteratorObject](https://orpc.dev/docs/async-iterator-object).
|
|
350
|
-
* Unlike `.streamedOptions` which accumulates chunks, live queries replace the entire result with each new chunk received.
|
|
351
|
-
* Works with `useQuery` and any other API that accepts query options.
|
|
352
|
-
*/
|
|
353
|
-
liveOptions(...rest) {
|
|
354
|
-
let optionsIn = resolveMaybeOptionalOptions(rest);
|
|
355
|
-
if (typeof this.options.liveOptions === "function") {
|
|
356
|
-
optionsIn = this.options.liveOptions(optionsIn);
|
|
357
|
-
} else if (this.options.liveOptions) {
|
|
358
|
-
optionsIn = { ...this.options.liveOptions, ...optionsIn };
|
|
359
|
-
}
|
|
360
|
-
const { input, context, key: _keyIn, query: queryIn, ...restOptions } = optionsIn;
|
|
361
|
-
const key = this.liveKey(optionsIn);
|
|
362
|
-
return {
|
|
363
|
-
...restOptions,
|
|
364
|
-
key,
|
|
365
|
-
query: (fnContext) => {
|
|
366
|
-
return intercept(
|
|
367
|
-
this.options.liveInterceptors,
|
|
368
|
-
{
|
|
369
|
-
path: this.path,
|
|
370
|
-
context: {
|
|
371
|
-
[OPERATION_CONTEXT_SYMBOL]: {
|
|
372
|
-
key,
|
|
373
|
-
type: "live"
|
|
374
|
-
},
|
|
375
|
-
...context
|
|
376
|
-
},
|
|
377
|
-
input,
|
|
378
|
-
fnContext
|
|
379
|
-
},
|
|
380
|
-
({ context: context2, input: input2, fnContext: fnContext2 }) => {
|
|
381
|
-
if (queryIn) {
|
|
382
|
-
return queryIn(fnContext2);
|
|
383
|
-
}
|
|
384
|
-
return liveQuery(async (queryContext) => {
|
|
385
|
-
const output = await this.call(input2, { signal: queryContext.signal, context: context2 });
|
|
386
|
-
if (!isAsyncIteratorObject(output)) {
|
|
387
|
-
throw new Error("liveQuery requires an AsyncIteratorObject output");
|
|
388
|
-
}
|
|
389
|
-
return output;
|
|
390
|
-
})(fnContext2);
|
|
391
|
-
}
|
|
392
|
-
);
|
|
393
|
-
}
|
|
394
|
-
};
|
|
395
|
-
}
|
|
396
|
-
/**
|
|
397
|
-
* Generate a **full matching** key for useInfiniteQuery/...
|
|
398
|
-
*
|
|
399
|
-
* @see {@link https://orpc.dev/docs/integrations/pinia-colada#querymutation-key | Pinia Colada Integration - Query/Mutation Key}
|
|
400
|
-
*/
|
|
401
|
-
infiniteKey(optionsIn) {
|
|
402
|
-
if (typeof this.options.infiniteKey === "function") {
|
|
403
|
-
optionsIn = this.options.infiniteKey(optionsIn);
|
|
404
|
-
} else if (this.options.infiniteKey) {
|
|
405
|
-
optionsIn = { ...this.options.infiniteKey, ...optionsIn };
|
|
406
|
-
}
|
|
407
|
-
const key = optionsIn.key ?? this.key({
|
|
408
|
-
type: "infinite",
|
|
409
|
-
input: optionsIn.input(
|
|
410
|
-
typeof optionsIn.initialPageParam === "function" ? optionsIn.initialPageParam() : optionsIn.initialPageParam
|
|
411
|
-
)
|
|
412
|
-
});
|
|
413
|
-
return key;
|
|
414
|
-
}
|
|
415
|
-
/**
|
|
416
|
-
* Generate options used for useInfiniteQuery/...
|
|
417
|
-
*
|
|
418
|
-
* @see {@link https://orpc.dev/docs/integrations/pinia-colada#infinite-query-options-utility | Pinia Colada Integration - Infinite Query Options Utility}
|
|
419
|
-
*/
|
|
420
|
-
infiniteOptions(optionsIn) {
|
|
421
|
-
if (typeof this.options.infiniteOptions === "function") {
|
|
422
|
-
optionsIn = this.options.infiniteOptions(optionsIn);
|
|
423
|
-
} else if (this.options.infiniteOptions) {
|
|
424
|
-
optionsIn = { ...this.options.infiniteOptions, ...optionsIn };
|
|
425
|
-
}
|
|
426
|
-
const { input, context, key: _keyIn, query: queryIn, ...restOptions } = optionsIn;
|
|
427
|
-
const key = this.infiniteKey(optionsIn);
|
|
428
|
-
return {
|
|
429
|
-
...restOptions,
|
|
430
|
-
key,
|
|
431
|
-
query: (fnContext) => {
|
|
432
|
-
return intercept(
|
|
433
|
-
this.options.infiniteInterceptors,
|
|
434
|
-
{
|
|
435
|
-
path: this.path,
|
|
436
|
-
context: {
|
|
437
|
-
[OPERATION_CONTEXT_SYMBOL]: {
|
|
438
|
-
key,
|
|
439
|
-
type: "infinite"
|
|
440
|
-
},
|
|
441
|
-
...context
|
|
442
|
-
},
|
|
443
|
-
input: input(fnContext.pageParam),
|
|
444
|
-
fnContext
|
|
445
|
-
},
|
|
446
|
-
({ context: context2, input: input2, fnContext: fnContext2 }) => {
|
|
447
|
-
if (queryIn) {
|
|
448
|
-
return queryIn(fnContext2);
|
|
449
|
-
}
|
|
450
|
-
return this.call(input2, { signal: fnContext2.signal, context: context2 });
|
|
451
|
-
}
|
|
452
|
-
);
|
|
453
|
-
}
|
|
454
|
-
};
|
|
455
|
-
}
|
|
456
|
-
/**
|
|
457
|
-
* Generate a **full matching** key for useMutation/...
|
|
458
|
-
*
|
|
459
|
-
* @see {@link https://orpc.dev/docs/integrations/pinia-colada#querymutation-key | Pinia Colada Integration - Query/Mutation Key}
|
|
460
|
-
*/
|
|
461
|
-
mutationKey(...rest) {
|
|
462
|
-
let optionsIn = resolveMaybeOptionalOptions(rest);
|
|
463
|
-
if (typeof this.options.mutationKey === "function") {
|
|
464
|
-
optionsIn = this.options.mutationKey(optionsIn);
|
|
465
|
-
} else if (this.options.mutationKey) {
|
|
466
|
-
optionsIn = { ...this.options.mutationKey, ...optionsIn };
|
|
467
|
-
}
|
|
468
|
-
const key = optionsIn.key ?? ((input) => this.key({ type: "mutation", input }));
|
|
469
|
-
return key;
|
|
470
|
-
}
|
|
471
|
-
/**
|
|
472
|
-
* Generate options used for useMutation/...
|
|
473
|
-
*
|
|
474
|
-
* @see {@link https://orpc.dev/docs/integrations/pinia-colada#mutation-options | Pinia Colada Integration - Mutation Options}
|
|
475
|
-
*/
|
|
476
|
-
mutationOptions(...rest) {
|
|
477
|
-
let optionsIn = resolveMaybeOptionalOptions(rest);
|
|
478
|
-
if (typeof this.options.mutationOptions === "function") {
|
|
479
|
-
optionsIn = this.options.mutationOptions(optionsIn);
|
|
480
|
-
} else if (this.options.mutationOptions) {
|
|
481
|
-
optionsIn = { ...this.options.mutationOptions, ...optionsIn };
|
|
482
|
-
}
|
|
483
|
-
const { context, key: _keyIn, mutation: mutationIn, ...restOptions } = optionsIn;
|
|
484
|
-
const key = this.mutationKey(optionsIn);
|
|
485
|
-
return {
|
|
486
|
-
...restOptions,
|
|
487
|
-
key,
|
|
488
|
-
mutation: (input, fnContext) => {
|
|
489
|
-
return intercept(
|
|
490
|
-
this.options.mutationInterceptors,
|
|
491
|
-
{
|
|
492
|
-
path: this.path,
|
|
493
|
-
context: {
|
|
494
|
-
[OPERATION_CONTEXT_SYMBOL]: {
|
|
495
|
-
key: typeof key === "function" ? key(input) : key,
|
|
496
|
-
type: "mutation"
|
|
497
|
-
},
|
|
498
|
-
...context
|
|
499
|
-
},
|
|
500
|
-
input,
|
|
501
|
-
fnContext
|
|
502
|
-
},
|
|
503
|
-
({ context: context2, input: input2, fnContext: fnContext2 }) => {
|
|
504
|
-
if (mutationIn) {
|
|
505
|
-
return mutationIn(input2, fnContext2);
|
|
506
|
-
}
|
|
507
|
-
return this.call(input2, { context: context2 });
|
|
508
|
-
}
|
|
509
|
-
);
|
|
510
|
-
}
|
|
511
|
-
};
|
|
512
|
-
}
|
|
513
|
-
}
|
|
514
|
-
|
|
515
|
-
function createRouterUtils(client, options = {}) {
|
|
516
|
-
const plugin = new CompositeRouterUtilsPlugin(options.plugins);
|
|
517
|
-
options = plugin.init(options);
|
|
518
|
-
return createRouterUtilsInternal(client, options, plugin);
|
|
519
|
-
}
|
|
520
|
-
function createRouterUtilsInternal(client, options, plugin) {
|
|
521
|
-
const path = toArray(options.path);
|
|
522
|
-
const utils = typeof client === "function" && (options.scoped === void 0 || isProcedureUtilsOptions(options.scoped)) ? bindMethods(new ProcedureUtils(
|
|
523
|
-
path,
|
|
524
|
-
client,
|
|
525
|
-
plugin.initProcedureOptions(path, mergeProcedureUtilsOptions(
|
|
526
|
-
pick(options, [
|
|
527
|
-
"prefix",
|
|
528
|
-
"serializer",
|
|
529
|
-
"queryInterceptors",
|
|
530
|
-
"streamedInterceptors",
|
|
531
|
-
"liveInterceptors",
|
|
532
|
-
"infiniteInterceptors",
|
|
533
|
-
"mutationInterceptors"
|
|
534
|
-
]),
|
|
535
|
-
options.scoped ?? {}
|
|
536
|
-
))
|
|
537
|
-
), { unbound: ["call"] }) : bindMethods(new SharedUtils(path, options));
|
|
538
|
-
const cache = /* @__PURE__ */ new Map();
|
|
539
|
-
const recursive = new Proxy(utils, {
|
|
540
|
-
get(target, prop) {
|
|
541
|
-
const value = Reflect.get(target, prop);
|
|
542
|
-
if (RECURSIVE_CLIENT_UNWRAP_KEYS.has(prop)) {
|
|
543
|
-
return value ?? Reflect.get(Object.prototype, prop);
|
|
544
|
-
}
|
|
545
|
-
const nextClient = Reflect.get(client, prop);
|
|
546
|
-
if (typeof prop !== "string" || !isTypescriptObject(nextClient)) {
|
|
547
|
-
return value;
|
|
548
|
-
}
|
|
549
|
-
let result = cache.get(prop);
|
|
550
|
-
if (result === void 0) {
|
|
551
|
-
const nextUtils = createRouterUtilsInternal(nextClient, {
|
|
552
|
-
...options,
|
|
553
|
-
path: [...path, prop],
|
|
554
|
-
scoped: options.scoped?.[prop]
|
|
555
|
-
}, plugin);
|
|
556
|
-
result = typeof value !== "function" ? nextUtils : new Proxy(value, {
|
|
557
|
-
get(target2, prop2) {
|
|
558
|
-
if (typeof prop2 !== "string" || RECURSIVE_CLIENT_UNWRAP_KEYS.has(prop2)) {
|
|
559
|
-
return Reflect.get(target2, prop2);
|
|
560
|
-
}
|
|
561
|
-
return Reflect.get(nextUtils, prop2);
|
|
562
|
-
}
|
|
563
|
-
});
|
|
564
|
-
cache.set(prop, result);
|
|
565
|
-
}
|
|
566
|
-
return result;
|
|
567
|
-
}
|
|
568
|
-
});
|
|
569
|
-
return recursive;
|
|
570
|
-
}
|
|
571
|
-
|
|
572
|
-
function createContractUtilsFactory(clientFactory, options) {
|
|
573
|
-
const factory = (contract) => {
|
|
574
|
-
const client = clientFactory(contract);
|
|
575
|
-
const path = resolveBasePathMeta(contract);
|
|
576
|
-
if (path === void 0) {
|
|
577
|
-
throw new TypeError(
|
|
578
|
-
"ContractUtilsFactory: procedure contract must define `meta.path` that matches its path in the root router contract."
|
|
579
|
-
);
|
|
580
|
-
}
|
|
581
|
-
return createRouterUtils(client, { ...options, path, scoped: get(options.scoped, path) });
|
|
582
|
-
};
|
|
583
|
-
return factory;
|
|
584
|
-
}
|
|
585
|
-
function createContractJsonifiedUtilsFactory(clientFactory, options) {
|
|
586
|
-
return createContractUtilsFactory(clientFactory, options);
|
|
587
|
-
}
|
|
588
|
-
|
|
589
|
-
function piniaColada(options) {
|
|
590
|
-
return {
|
|
591
|
-
name: "~pinia-colada",
|
|
592
|
-
init(meta) {
|
|
593
|
-
const current = meta["~pinia-colada"];
|
|
594
|
-
return {
|
|
595
|
-
...meta,
|
|
596
|
-
"~pinia-colada": current ? mergeProcedureUtilsOptions(current, options) : options
|
|
597
|
-
};
|
|
598
|
-
}
|
|
599
|
-
};
|
|
600
|
-
}
|
|
601
|
-
function getPiniaColadaMeta(procedureOrLazy) {
|
|
602
|
-
return procedureOrLazy["~orpc"].meta["~pinia-colada"];
|
|
603
|
-
}
|
|
604
|
-
class ContractOptionsUtilsPlugin {
|
|
605
|
-
constructor(contract) {
|
|
606
|
-
this.contract = contract;
|
|
607
|
-
}
|
|
608
|
-
name = "~contract-options";
|
|
609
|
-
initProcedureOptions(path, options) {
|
|
610
|
-
const procedure = getRouterContract(this.contract, path);
|
|
611
|
-
if (!(procedure instanceof ProcedureContract)) {
|
|
612
|
-
return options;
|
|
613
|
-
}
|
|
614
|
-
const base = getPiniaColadaMeta(procedure);
|
|
615
|
-
if (!base) {
|
|
616
|
-
return options;
|
|
617
|
-
}
|
|
618
|
-
return mergeProcedureUtilsOptions(base, options);
|
|
619
|
-
}
|
|
620
|
-
}
|
|
621
|
-
|
|
622
|
-
export { ContractOptionsUtilsPlugin, OPERATION_CONTEXT_SYMBOL, OPERATION_CONTEXT_SYMBOL as PINIA_COLADA_OPERATION_CONTEXT_SYMBOL, ProcedureUtils, SharedUtils, createContractJsonifiedUtilsFactory, createContractUtilsFactory, createRouterUtils as createORPCVueColadaUtils, createRouterUtils as createPiniaColadaUtils, createRouterUtils, getPiniaColadaMeta, mergeProcedureUtilsOptions, piniaColada };
|