@orpc/pinia-colada 2.0.0-beta.48 → 2.0.0-beta.50

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