@orpc/tanstack-query 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 +16 -541
- package/dist/index.mjs +10 -821
- package/dist/key.d.mts +62 -0
- package/dist/key.mjs +30 -0
- package/dist/live-query.d.mts +6 -0
- package/dist/live-query.mjs +16 -0
- package/dist/meta.d.mts +55 -0
- package/dist/meta.mjs +37 -0
- package/dist/plugin.d.mts +51 -0
- package/dist/plugin.mjs +25 -0
- package/dist/procedure-utils.d.mts +232 -0
- package/dist/procedure-utils.mjs +406 -0
- package/dist/router-utils.d.mts +57 -0
- package/dist/router-utils.mjs +51 -0
- package/dist/shared-utils.d.mts +11 -0
- package/dist/shared-utils.mjs +17 -0
- package/dist/ssr.d.mts +38 -0
- package/dist/ssr.mjs +57 -0
- package/dist/stream-query.d.mts +30 -0
- package/dist/stream-query.mjs +58 -0
- package/dist/structural-sharing.d.mts +14 -0
- package/dist/structural-sharing.mjs +140 -0
- package/dist/types.d.mts +138 -0
- package/dist/types.mjs +2 -0
- package/package.json +13 -13
- package/dist/index.d.ts +0 -541
package/dist/index.mjs
CHANGED
|
@@ -1,821 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
name = "~composite";
|
|
12
|
-
init(options) {
|
|
13
|
-
for (const plugin of this.plugins) {
|
|
14
|
-
if (plugin.init) {
|
|
15
|
-
options = plugin.init(options);
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
return options;
|
|
19
|
-
}
|
|
20
|
-
initProcedureOptions(path, options) {
|
|
21
|
-
for (const plugin of this.plugins) {
|
|
22
|
-
if (plugin.initProcedureOptions) {
|
|
23
|
-
options = plugin.initProcedureOptions(path, options);
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
return options;
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
function liveQuery(queryFn) {
|
|
31
|
-
return async (context) => {
|
|
32
|
-
const stream = await queryFn(context);
|
|
33
|
-
let last;
|
|
34
|
-
for await (const chunk of stream) {
|
|
35
|
-
throwIfAborted(context.signal);
|
|
36
|
-
last = { chunk };
|
|
37
|
-
context.client.setQueryData(context.queryKey, chunk);
|
|
38
|
-
}
|
|
39
|
-
if (!last) {
|
|
40
|
-
throw new TypeError(
|
|
41
|
-
`Live query for ${stringifyJSON(context.queryKey)} did not yield any data. Ensure the query function returns an AsyncIterable with at least one chunk.`
|
|
42
|
-
);
|
|
43
|
-
}
|
|
44
|
-
return last.chunk;
|
|
45
|
-
};
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
const defaultSerializer = new RPCJsonSerializer();
|
|
49
|
-
function generateOperationKey(path, options = {}) {
|
|
50
|
-
return [
|
|
51
|
-
...options.prefix !== void 0 ? [options.prefix] : [],
|
|
52
|
-
path,
|
|
53
|
-
{
|
|
54
|
-
...options.input === skipToken ? { skipToken: true } : options.input !== void 0 ? serializeInput(options.serializer ?? defaultSerializer, options.input) : {},
|
|
55
|
-
...options.type !== void 0 ? { type: options.type } : {},
|
|
56
|
-
...options.fnOptions !== void 0 ? { fnOptions: options.fnOptions } : {}
|
|
57
|
-
}
|
|
58
|
-
];
|
|
59
|
-
}
|
|
60
|
-
function serializeInput(serializer, input) {
|
|
61
|
-
const { json, meta } = serializer.serializeJson(input);
|
|
62
|
-
if (!meta?.length) {
|
|
63
|
-
return { input: json };
|
|
64
|
-
}
|
|
65
|
-
const inputMeta = {};
|
|
66
|
-
for (const [type, ...segments] of meta) {
|
|
67
|
-
(inputMeta[stringifyJSON(segments)] ??= []).push(type);
|
|
68
|
-
}
|
|
69
|
-
return { input: json, inputMeta };
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
class SharedUtils {
|
|
73
|
-
constructor(path, options) {
|
|
74
|
-
this.path = path;
|
|
75
|
-
this.options = options;
|
|
76
|
-
}
|
|
77
|
-
/**
|
|
78
|
-
* Generate a **partial matching** key for actions like revalidating queries, checking mutation status, etc.
|
|
79
|
-
*/
|
|
80
|
-
key(options = {}) {
|
|
81
|
-
return generateOperationKey(this.path, { ...options, prefix: this.options.prefix, serializer: this.options.serializer });
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
function serializableStreamedQuery(queryFn, { refetchMode = "reset", maxChunks = Number.POSITIVE_INFINITY } = {}) {
|
|
86
|
-
return async (context) => {
|
|
87
|
-
const query = context.client.getQueryCache().find({ queryKey: context.queryKey, exact: true });
|
|
88
|
-
const hasPreviousData = !!query && query.state.data !== void 0;
|
|
89
|
-
if (hasPreviousData) {
|
|
90
|
-
if (refetchMode === "reset") {
|
|
91
|
-
query.setState({
|
|
92
|
-
status: "pending",
|
|
93
|
-
data: void 0,
|
|
94
|
-
error: null,
|
|
95
|
-
fetchStatus: "fetching"
|
|
96
|
-
});
|
|
97
|
-
} else {
|
|
98
|
-
query.setState({
|
|
99
|
-
data: limitArraySize(query.state.data, maxChunks)
|
|
100
|
-
});
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
let result = [];
|
|
104
|
-
const stream = await queryFn(context);
|
|
105
|
-
const shouldUpdateCacheDuringStream = !hasPreviousData || refetchMode !== "replace";
|
|
106
|
-
context.client.setQueryData(
|
|
107
|
-
context.queryKey,
|
|
108
|
-
(prev = []) => limitArraySize(prev, maxChunks)
|
|
109
|
-
);
|
|
110
|
-
if (context.signal.aborted) {
|
|
111
|
-
await stream[Symbol.asyncIterator]().return?.();
|
|
112
|
-
throw context.signal.reason;
|
|
113
|
-
}
|
|
114
|
-
for await (const chunk of stream) {
|
|
115
|
-
if (context.signal.aborted) {
|
|
116
|
-
throw context.signal.reason;
|
|
117
|
-
}
|
|
118
|
-
result.push(chunk);
|
|
119
|
-
result = limitArraySize(result, maxChunks);
|
|
120
|
-
if (shouldUpdateCacheDuringStream) {
|
|
121
|
-
context.client.setQueryData(
|
|
122
|
-
context.queryKey,
|
|
123
|
-
(prev = []) => limitArraySize([...prev, chunk], maxChunks)
|
|
124
|
-
);
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
if (!shouldUpdateCacheDuringStream) {
|
|
128
|
-
context.client.setQueryData(context.queryKey, result);
|
|
129
|
-
}
|
|
130
|
-
const cachedData = context.client.getQueryData(context.queryKey);
|
|
131
|
-
if (cachedData) {
|
|
132
|
-
return limitArraySize(cachedData, maxChunks);
|
|
133
|
-
}
|
|
134
|
-
return result;
|
|
135
|
-
};
|
|
136
|
-
}
|
|
137
|
-
function limitArraySize(items, maxSize) {
|
|
138
|
-
if (items.length <= maxSize) {
|
|
139
|
-
return items;
|
|
140
|
-
}
|
|
141
|
-
return items.slice(items.length - maxSize);
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
function createStructuralSharing(serializer) {
|
|
145
|
-
return (oldData, newData) => replaceEqualDeep(oldData, newData, 0, serializer);
|
|
146
|
-
}
|
|
147
|
-
function replaceEqualDeep(a, b, depth, serializer) {
|
|
148
|
-
if (Object.is(a, b)) {
|
|
149
|
-
return a;
|
|
150
|
-
}
|
|
151
|
-
if (depth > 500 || typeof a !== "object" || typeof b !== "object" || a === null || b === null) {
|
|
152
|
-
return b;
|
|
153
|
-
}
|
|
154
|
-
const proto = Object.getPrototypeOf(a);
|
|
155
|
-
if (proto !== Object.getPrototypeOf(b)) {
|
|
156
|
-
return b;
|
|
157
|
-
}
|
|
158
|
-
if ((proto === Date.prototype || proto === URL.prototype || proto === Set.prototype || proto === Map.prototype) && !hasOwnEnumerableKey(a) && !hasOwnEnumerableKey(b)) {
|
|
159
|
-
return replaceEqualBuiltIn(a, b, proto, depth, serializer);
|
|
160
|
-
}
|
|
161
|
-
if (Array.isArray(a)) {
|
|
162
|
-
return isPlainArray(a) && isPlainArray(b) ? replaceEqualArray(a, b, depth, serializer) : b;
|
|
163
|
-
}
|
|
164
|
-
if (proto === Object.prototype || proto === null || !proto.constructor) {
|
|
165
|
-
return replaceEqualObject(a, b, proto, depth, serializer);
|
|
166
|
-
}
|
|
167
|
-
return replaceEqualSerialized(a, b, depth, serializer);
|
|
168
|
-
}
|
|
169
|
-
function replaceEqualArray(a, b, depth, serializer) {
|
|
170
|
-
let copy;
|
|
171
|
-
for (let i = 0; i < b.length; i++) {
|
|
172
|
-
let value = b[i];
|
|
173
|
-
if (i < a.length) {
|
|
174
|
-
const aValue = a[i];
|
|
175
|
-
if (typeof aValue === "object" && typeof value === "object") {
|
|
176
|
-
value = replaceEqualDeep(aValue, value, depth + 1, serializer);
|
|
177
|
-
}
|
|
178
|
-
if (copy === void 0 && Object.is(value, aValue)) {
|
|
179
|
-
continue;
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
copy ??= copyArrayPrefix(a, i);
|
|
183
|
-
copy.push(value);
|
|
184
|
-
}
|
|
185
|
-
return copy ?? (a.length === b.length ? a : copyArrayPrefix(a, b.length));
|
|
186
|
-
}
|
|
187
|
-
function copyArrayPrefix(a, end) {
|
|
188
|
-
const copy = [];
|
|
189
|
-
for (let i = 0; i < end; i++) {
|
|
190
|
-
copy.push(a[i]);
|
|
191
|
-
}
|
|
192
|
-
return copy;
|
|
193
|
-
}
|
|
194
|
-
function replaceEqualObject(a, b, proto, depth, serializer) {
|
|
195
|
-
const bKeys = Object.keys(b);
|
|
196
|
-
let copy;
|
|
197
|
-
for (let i = 0; i < bKeys.length; i++) {
|
|
198
|
-
const key = bKeys[i];
|
|
199
|
-
let value = b[key];
|
|
200
|
-
if (Object.hasOwn(a, key)) {
|
|
201
|
-
const aValue = a[key];
|
|
202
|
-
if (typeof aValue === "object" && typeof value === "object") {
|
|
203
|
-
value = replaceEqualDeep(aValue, value, depth + 1, serializer);
|
|
204
|
-
}
|
|
205
|
-
if (copy === void 0 && Object.is(value, aValue)) {
|
|
206
|
-
continue;
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
copy ??= copyObjectPrefix(a, bKeys, i, proto);
|
|
210
|
-
assign(copy, key, value);
|
|
211
|
-
}
|
|
212
|
-
return copy ?? (Object.keys(a).length === bKeys.length ? a : copyObjectPrefix(a, bKeys, bKeys.length, proto));
|
|
213
|
-
}
|
|
214
|
-
function copyObjectPrefix(a, keys, end, proto) {
|
|
215
|
-
const copy = proto === Object.prototype ? {} : Object.create(proto);
|
|
216
|
-
for (let i = 0; i < end; i++) {
|
|
217
|
-
assign(copy, keys[i], a[keys[i]]);
|
|
218
|
-
}
|
|
219
|
-
return copy;
|
|
220
|
-
}
|
|
221
|
-
function assign(object, key, value) {
|
|
222
|
-
if (key === "__proto__") {
|
|
223
|
-
setOwn(object, key, value);
|
|
224
|
-
} else {
|
|
225
|
-
object[key] = value;
|
|
226
|
-
}
|
|
227
|
-
}
|
|
228
|
-
function replaceEqualBuiltIn(a, b, proto, depth, serializer) {
|
|
229
|
-
if (proto === Date.prototype) {
|
|
230
|
-
return Object.is(a.getTime(), b.getTime()) ? a : b;
|
|
231
|
-
}
|
|
232
|
-
if (proto === URL.prototype) {
|
|
233
|
-
return a.href === b.href ? a : b;
|
|
234
|
-
}
|
|
235
|
-
if (a.size !== b.size) {
|
|
236
|
-
return b;
|
|
237
|
-
}
|
|
238
|
-
if (proto === Set.prototype) {
|
|
239
|
-
const aValues2 = a.values();
|
|
240
|
-
for (const bValue of b.values()) {
|
|
241
|
-
const aValue = aValues2.next().value;
|
|
242
|
-
if (!Object.is(replaceEqualDeep(aValue, bValue, depth + 1, serializer), aValue)) {
|
|
243
|
-
return b;
|
|
244
|
-
}
|
|
245
|
-
}
|
|
246
|
-
return a;
|
|
247
|
-
}
|
|
248
|
-
const aKeys = a.keys();
|
|
249
|
-
const aValues = a.values();
|
|
250
|
-
const bValues = b.values();
|
|
251
|
-
for (const bKey of b.keys()) {
|
|
252
|
-
const aKey = aKeys.next().value;
|
|
253
|
-
const aValue = aValues.next().value;
|
|
254
|
-
if (!Object.is(replaceEqualDeep(aKey, bKey, depth + 1, serializer), aKey) || !Object.is(replaceEqualDeep(aValue, bValues.next().value, depth + 1, serializer), aValue)) {
|
|
255
|
-
return b;
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
return a;
|
|
259
|
-
}
|
|
260
|
-
function replaceEqualSerialized(a, b, depth, serializer) {
|
|
261
|
-
const aSerialized = serializer?.serializeJson(a);
|
|
262
|
-
if (!aSerialized?.meta?.length) {
|
|
263
|
-
return b;
|
|
264
|
-
}
|
|
265
|
-
const bSerialized = serializer.serializeJson(b);
|
|
266
|
-
return stringifyJSON(aSerialized.meta) === stringifyJSON(bSerialized.meta) && Object.is(replaceEqualDeep(aSerialized.json, bSerialized.json, depth + 1, serializer), aSerialized.json) ? a : b;
|
|
267
|
-
}
|
|
268
|
-
function hasOwnEnumerableKey(value) {
|
|
269
|
-
for (const key in value) {
|
|
270
|
-
if (Object.hasOwn(value, key)) {
|
|
271
|
-
return true;
|
|
272
|
-
}
|
|
273
|
-
}
|
|
274
|
-
return false;
|
|
275
|
-
}
|
|
276
|
-
function isPlainArray(value) {
|
|
277
|
-
return value.length === Object.keys(value).length;
|
|
278
|
-
}
|
|
279
|
-
|
|
280
|
-
const OPERATION_CONTEXT_SYMBOL = Symbol.for("ORPC_TANSTACK_QUERY_OPERATION_CONTEXT");
|
|
281
|
-
|
|
282
|
-
const PROCEDURE_UTILS_INTERCEPTOR_KEYS = [
|
|
283
|
-
"queryInterceptors",
|
|
284
|
-
"streamedInterceptors",
|
|
285
|
-
"liveInterceptors",
|
|
286
|
-
"infiniteInterceptors",
|
|
287
|
-
"mutationInterceptors"
|
|
288
|
-
];
|
|
289
|
-
const PROCEDURE_UTILS_MODIFIER_KEYS = [
|
|
290
|
-
"queryKey",
|
|
291
|
-
"queryOptions",
|
|
292
|
-
"streamedKey",
|
|
293
|
-
"streamedOptions",
|
|
294
|
-
"liveKey",
|
|
295
|
-
"liveOptions",
|
|
296
|
-
"infiniteKey",
|
|
297
|
-
"infiniteOptions",
|
|
298
|
-
"mutationKey",
|
|
299
|
-
"mutationOptions"
|
|
300
|
-
];
|
|
301
|
-
function isProcedureUtilsOptions(value) {
|
|
302
|
-
if (!isTypescriptObject(value)) {
|
|
303
|
-
return false;
|
|
304
|
-
}
|
|
305
|
-
for (const key of Object.keys(value)) {
|
|
306
|
-
if (value[key] === void 0) {
|
|
307
|
-
continue;
|
|
308
|
-
}
|
|
309
|
-
if (key === "prefix") {
|
|
310
|
-
if (typeof value[key] !== "string") {
|
|
311
|
-
return false;
|
|
312
|
-
}
|
|
313
|
-
} else if (key === "serializer") {
|
|
314
|
-
if (!isTypescriptObject(value[key]) || typeof value[key].serializeJson !== "function") {
|
|
315
|
-
return false;
|
|
316
|
-
}
|
|
317
|
-
} else if (PROCEDURE_UTILS_INTERCEPTOR_KEYS.includes(key)) {
|
|
318
|
-
if (!Array.isArray(value[key]) || value[key].some((i) => typeof i !== "function")) {
|
|
319
|
-
return false;
|
|
320
|
-
}
|
|
321
|
-
} else if (PROCEDURE_UTILS_MODIFIER_KEYS.includes(key)) {
|
|
322
|
-
if (!isTypescriptObject(value[key])) {
|
|
323
|
-
return false;
|
|
324
|
-
}
|
|
325
|
-
}
|
|
326
|
-
}
|
|
327
|
-
return true;
|
|
328
|
-
}
|
|
329
|
-
function mergeProcedureUtilsModifier(base, override) {
|
|
330
|
-
if (typeof override !== "function") {
|
|
331
|
-
if (typeof base !== "function") {
|
|
332
|
-
return { ...base, ...override };
|
|
333
|
-
}
|
|
334
|
-
return (options) => base({ ...override, ...options });
|
|
335
|
-
}
|
|
336
|
-
const applyBase = typeof base === "function" ? base : (options) => ({ ...base, ...options });
|
|
337
|
-
return (options) => override(applyBase(options));
|
|
338
|
-
}
|
|
339
|
-
function mergeProcedureUtilsOptions(base, override) {
|
|
340
|
-
const merged = { ...base, ...override };
|
|
341
|
-
for (const key of PROCEDURE_UTILS_INTERCEPTOR_KEYS) {
|
|
342
|
-
const baseValue = base[key];
|
|
343
|
-
const overrideValue = override[key];
|
|
344
|
-
if (baseValue && overrideValue) {
|
|
345
|
-
merged[key] = [...baseValue, ...overrideValue];
|
|
346
|
-
}
|
|
347
|
-
}
|
|
348
|
-
for (const key of PROCEDURE_UTILS_MODIFIER_KEYS) {
|
|
349
|
-
const baseValue = base[key];
|
|
350
|
-
const overrideValue = override[key];
|
|
351
|
-
if (baseValue && overrideValue) {
|
|
352
|
-
merged[key] = mergeProcedureUtilsModifier(baseValue, overrideValue);
|
|
353
|
-
}
|
|
354
|
-
}
|
|
355
|
-
return merged;
|
|
356
|
-
}
|
|
357
|
-
class ProcedureUtils extends SharedUtils {
|
|
358
|
-
/**
|
|
359
|
-
* Calling corresponding procedure client
|
|
360
|
-
*/
|
|
361
|
-
call;
|
|
362
|
-
constructor(path, client, options = {}) {
|
|
363
|
-
super(path, options);
|
|
364
|
-
this.call = client;
|
|
365
|
-
}
|
|
366
|
-
/**
|
|
367
|
-
* Generate a **full matching** key for [Query Options](https://orpc.dev/docs/integrations/tanstack-query#query-options).
|
|
368
|
-
*/
|
|
369
|
-
queryKey(...rest) {
|
|
370
|
-
let optionsIn = resolveMaybeOptionalOptions(rest);
|
|
371
|
-
if (typeof this.options.queryKey === "function") {
|
|
372
|
-
optionsIn = this.options.queryKey(optionsIn);
|
|
373
|
-
} else if (this.options.queryKey) {
|
|
374
|
-
optionsIn = { ...this.options.queryKey, ...optionsIn };
|
|
375
|
-
}
|
|
376
|
-
const queryKey = optionsIn.queryKey ?? this.key({ type: "query", input: optionsIn.input });
|
|
377
|
-
return queryKey;
|
|
378
|
-
}
|
|
379
|
-
/**
|
|
380
|
-
* Generate options used for useQuery/useSuspenseQuery/prefetchQuery/...
|
|
381
|
-
*/
|
|
382
|
-
queryOptions(...rest) {
|
|
383
|
-
let optionsIn = resolveMaybeOptionalOptions(rest);
|
|
384
|
-
if (typeof this.options.queryOptions === "function") {
|
|
385
|
-
optionsIn = this.options.queryOptions(optionsIn);
|
|
386
|
-
} else if (this.options.queryOptions) {
|
|
387
|
-
optionsIn = { ...this.options.queryOptions, ...optionsIn };
|
|
388
|
-
}
|
|
389
|
-
const queryKey = this.queryKey(optionsIn);
|
|
390
|
-
return {
|
|
391
|
-
structuralSharing: createStructuralSharing(this.options.serializer),
|
|
392
|
-
...optionsIn,
|
|
393
|
-
queryKey,
|
|
394
|
-
queryFn: (fnContext) => {
|
|
395
|
-
return intercept(
|
|
396
|
-
this.options.queryInterceptors,
|
|
397
|
-
{
|
|
398
|
-
path: this.path,
|
|
399
|
-
context: {
|
|
400
|
-
[OPERATION_CONTEXT_SYMBOL]: {
|
|
401
|
-
key: queryKey,
|
|
402
|
-
type: "query"
|
|
403
|
-
},
|
|
404
|
-
...optionsIn.context
|
|
405
|
-
},
|
|
406
|
-
input: optionsIn.input,
|
|
407
|
-
fnContext
|
|
408
|
-
},
|
|
409
|
-
({ context, input, fnContext: fnContext2 }) => {
|
|
410
|
-
if (input === skipToken || optionsIn.queryFn === skipToken) {
|
|
411
|
-
throw new Error("queryFn should not be called when skipToken used for skipping");
|
|
412
|
-
}
|
|
413
|
-
if (optionsIn.queryFn) {
|
|
414
|
-
return optionsIn.queryFn(fnContext2);
|
|
415
|
-
}
|
|
416
|
-
return this.call(input, { signal: fnContext2.signal, context });
|
|
417
|
-
}
|
|
418
|
-
);
|
|
419
|
-
},
|
|
420
|
-
...optionsIn.input === skipToken || optionsIn.queryFn === skipToken ? { enabled: false } : {}
|
|
421
|
-
};
|
|
422
|
-
}
|
|
423
|
-
/**
|
|
424
|
-
* Generate a **full matching** key for [Streamed Query Options](https://orpc.dev/docs/integrations/tanstack-query#streamed-query-options).
|
|
425
|
-
*/
|
|
426
|
-
streamedKey(...rest) {
|
|
427
|
-
let optionsIn = resolveMaybeOptionalOptions(rest);
|
|
428
|
-
if (typeof this.options.streamedKey === "function") {
|
|
429
|
-
optionsIn = this.options.streamedKey(optionsIn);
|
|
430
|
-
} else if (this.options.streamedKey) {
|
|
431
|
-
optionsIn = { ...this.options.streamedKey, ...optionsIn };
|
|
432
|
-
}
|
|
433
|
-
const queryKey = optionsIn.queryKey ?? this.key({ type: "streamed", input: optionsIn.input, fnOptions: optionsIn.queryFnOptions });
|
|
434
|
-
return queryKey;
|
|
435
|
-
}
|
|
436
|
-
/**
|
|
437
|
-
* Configure queries for [AsyncIteratorObject](https://orpc.dev/docs/async-iterator-object).
|
|
438
|
-
* This is built on [TanStack Query streamedQuery](https://tanstack.com/query/latest/docs/reference/streamedQuery)
|
|
439
|
-
* and works with hooks like `useQuery`, `useSuspenseQuery`, or `prefetchQuery`.
|
|
440
|
-
*/
|
|
441
|
-
streamedOptions(...rest) {
|
|
442
|
-
let optionsIn = resolveMaybeOptionalOptions(rest);
|
|
443
|
-
if (typeof this.options.streamedOptions === "function") {
|
|
444
|
-
optionsIn = this.options.streamedOptions(optionsIn);
|
|
445
|
-
} else if (this.options.streamedOptions) {
|
|
446
|
-
optionsIn = { ...this.options.streamedOptions, ...optionsIn };
|
|
447
|
-
}
|
|
448
|
-
const queryKey = this.streamedKey(optionsIn);
|
|
449
|
-
return {
|
|
450
|
-
...optionsIn,
|
|
451
|
-
queryKey,
|
|
452
|
-
queryFn: (fnContext) => {
|
|
453
|
-
return intercept(
|
|
454
|
-
toArray(this.options.streamedInterceptors),
|
|
455
|
-
{
|
|
456
|
-
path: this.path,
|
|
457
|
-
context: {
|
|
458
|
-
[OPERATION_CONTEXT_SYMBOL]: {
|
|
459
|
-
key: queryKey,
|
|
460
|
-
type: "streamed"
|
|
461
|
-
},
|
|
462
|
-
...optionsIn.context
|
|
463
|
-
},
|
|
464
|
-
input: optionsIn.input,
|
|
465
|
-
fnContext
|
|
466
|
-
},
|
|
467
|
-
({ context, fnContext: fnContext2, input }) => {
|
|
468
|
-
if (input === skipToken || optionsIn.queryFn === skipToken) {
|
|
469
|
-
throw new Error("queryFn should not be called when skipToken used for skipping");
|
|
470
|
-
}
|
|
471
|
-
if (optionsIn.queryFn) {
|
|
472
|
-
return optionsIn.queryFn(fnContext2);
|
|
473
|
-
}
|
|
474
|
-
return serializableStreamedQuery(
|
|
475
|
-
async (queryContext) => {
|
|
476
|
-
const output = await this.call(input, { signal: queryContext.signal, context });
|
|
477
|
-
if (!isAsyncIteratorObject(output)) {
|
|
478
|
-
throw new Error("streamedQuery requires an AsyncIteratorObject output");
|
|
479
|
-
}
|
|
480
|
-
return output;
|
|
481
|
-
},
|
|
482
|
-
optionsIn.queryFnOptions
|
|
483
|
-
)(fnContext2);
|
|
484
|
-
}
|
|
485
|
-
);
|
|
486
|
-
},
|
|
487
|
-
...optionsIn.input === skipToken || optionsIn.queryFn === skipToken ? { enabled: false } : {}
|
|
488
|
-
};
|
|
489
|
-
}
|
|
490
|
-
/**
|
|
491
|
-
* Generate a **full matching** key for [Live Query Options](https://orpc.dev/docs/integrations/tanstack-query#live-query-options).
|
|
492
|
-
*/
|
|
493
|
-
liveKey(...rest) {
|
|
494
|
-
let optionsIn = resolveMaybeOptionalOptions(rest);
|
|
495
|
-
if (typeof this.options.liveKey === "function") {
|
|
496
|
-
optionsIn = this.options.liveKey(optionsIn);
|
|
497
|
-
} else if (this.options.liveKey) {
|
|
498
|
-
optionsIn = { ...this.options.liveKey, ...optionsIn };
|
|
499
|
-
}
|
|
500
|
-
const queryKey = optionsIn.queryKey ?? this.key({ type: "live", input: optionsIn.input });
|
|
501
|
-
return queryKey;
|
|
502
|
-
}
|
|
503
|
-
/**
|
|
504
|
-
* Configure live queries for [AsyncIteratorObject](https://orpc.dev/docs/async-iterator-object).
|
|
505
|
-
* Unlike `.streamedOptions` which accumulates chunks, live queries replace the entire result with each new chunk received.
|
|
506
|
-
* Works with hooks like `useQuery`, `useSuspenseQuery`, or `prefetchQuery`.
|
|
507
|
-
*/
|
|
508
|
-
liveOptions(...rest) {
|
|
509
|
-
let optionsIn = resolveMaybeOptionalOptions(rest);
|
|
510
|
-
if (typeof this.options.liveOptions === "function") {
|
|
511
|
-
optionsIn = this.options.liveOptions(optionsIn);
|
|
512
|
-
} else if (this.options.liveOptions) {
|
|
513
|
-
optionsIn = { ...this.options.liveOptions, ...optionsIn };
|
|
514
|
-
}
|
|
515
|
-
const queryKey = this.liveKey(optionsIn);
|
|
516
|
-
return {
|
|
517
|
-
structuralSharing: createStructuralSharing(this.options.serializer),
|
|
518
|
-
...optionsIn,
|
|
519
|
-
queryKey,
|
|
520
|
-
queryFn: (fnContext) => {
|
|
521
|
-
return intercept(
|
|
522
|
-
toArray(this.options.liveInterceptors),
|
|
523
|
-
{
|
|
524
|
-
path: this.path,
|
|
525
|
-
context: {
|
|
526
|
-
[OPERATION_CONTEXT_SYMBOL]: {
|
|
527
|
-
key: queryKey,
|
|
528
|
-
type: "live"
|
|
529
|
-
},
|
|
530
|
-
...optionsIn.context
|
|
531
|
-
},
|
|
532
|
-
input: optionsIn.input,
|
|
533
|
-
fnContext
|
|
534
|
-
},
|
|
535
|
-
({ fnContext: fnContext2, input, context }) => {
|
|
536
|
-
if (input === skipToken || optionsIn.queryFn === skipToken) {
|
|
537
|
-
throw new Error("queryFn should not be called when skipToken used for skipping");
|
|
538
|
-
}
|
|
539
|
-
if (optionsIn.queryFn) {
|
|
540
|
-
return optionsIn.queryFn(fnContext2);
|
|
541
|
-
}
|
|
542
|
-
return liveQuery(async (queryContext) => {
|
|
543
|
-
const output = await this.call(input, {
|
|
544
|
-
signal: queryContext.signal,
|
|
545
|
-
context
|
|
546
|
-
});
|
|
547
|
-
if (!isAsyncIteratorObject(output)) {
|
|
548
|
-
throw new Error("liveQuery requires an AsyncIteratorObject output");
|
|
549
|
-
}
|
|
550
|
-
return output;
|
|
551
|
-
})(fnContext2);
|
|
552
|
-
}
|
|
553
|
-
);
|
|
554
|
-
},
|
|
555
|
-
...optionsIn.input === skipToken || optionsIn.queryFn === skipToken ? { enabled: false } : {}
|
|
556
|
-
};
|
|
557
|
-
}
|
|
558
|
-
/**
|
|
559
|
-
* Generate a **full matching** key for [Infinite Query Options](https://orpc.dev/docs/integrations/tanstack-query#infinite-query-options).
|
|
560
|
-
*/
|
|
561
|
-
infiniteKey(optionsIn) {
|
|
562
|
-
if (typeof this.options.infiniteKey === "function") {
|
|
563
|
-
optionsIn = this.options.infiniteKey(optionsIn);
|
|
564
|
-
} else if (this.options.infiniteKey) {
|
|
565
|
-
optionsIn = { ...this.options.infiniteKey, ...optionsIn };
|
|
566
|
-
}
|
|
567
|
-
const queryKey = optionsIn.queryKey ?? this.key({
|
|
568
|
-
type: "infinite",
|
|
569
|
-
input: optionsIn.input === skipToken ? skipToken : optionsIn.input(optionsIn.initialPageParam)
|
|
570
|
-
});
|
|
571
|
-
return queryKey;
|
|
572
|
-
}
|
|
573
|
-
/**
|
|
574
|
-
* Generate options used for useInfiniteQuery/useSuspenseInfiniteQuery/prefetchInfiniteQuery/...
|
|
575
|
-
*/
|
|
576
|
-
infiniteOptions(optionsIn) {
|
|
577
|
-
if (typeof this.options.infiniteOptions === "function") {
|
|
578
|
-
optionsIn = this.options.infiniteOptions(optionsIn);
|
|
579
|
-
} else if (this.options.infiniteOptions) {
|
|
580
|
-
optionsIn = { ...this.options.infiniteOptions, ...optionsIn };
|
|
581
|
-
}
|
|
582
|
-
const queryKey = this.infiniteKey(optionsIn);
|
|
583
|
-
return {
|
|
584
|
-
structuralSharing: createStructuralSharing(this.options.serializer),
|
|
585
|
-
...optionsIn,
|
|
586
|
-
queryKey,
|
|
587
|
-
queryFn: (fnContext) => {
|
|
588
|
-
return intercept(
|
|
589
|
-
toArray(this.options.infiniteInterceptors),
|
|
590
|
-
{
|
|
591
|
-
path: this.path,
|
|
592
|
-
context: {
|
|
593
|
-
[OPERATION_CONTEXT_SYMBOL]: {
|
|
594
|
-
key: queryKey,
|
|
595
|
-
type: "infinite"
|
|
596
|
-
},
|
|
597
|
-
...optionsIn.context
|
|
598
|
-
},
|
|
599
|
-
input: optionsIn.input === skipToken ? skipToken : optionsIn.input(fnContext.pageParam),
|
|
600
|
-
fnContext
|
|
601
|
-
},
|
|
602
|
-
({ context, input, fnContext: fnContext2 }) => {
|
|
603
|
-
if (input === skipToken || optionsIn.queryFn === skipToken) {
|
|
604
|
-
throw new Error("queryFn should not be called when skipToken used for skipping");
|
|
605
|
-
}
|
|
606
|
-
if (optionsIn.queryFn) {
|
|
607
|
-
return optionsIn.queryFn(fnContext2);
|
|
608
|
-
}
|
|
609
|
-
return this.call(input, { signal: fnContext2.signal, context });
|
|
610
|
-
}
|
|
611
|
-
);
|
|
612
|
-
},
|
|
613
|
-
...optionsIn.input === skipToken || optionsIn.queryFn === skipToken ? { enabled: false } : {}
|
|
614
|
-
};
|
|
615
|
-
}
|
|
616
|
-
/**
|
|
617
|
-
* Generate a **full matching** key for [Mutation Options](https://orpc.dev/docs/integrations/tanstack-query#mutation-options).
|
|
618
|
-
*/
|
|
619
|
-
mutationKey(optionsIn = {}) {
|
|
620
|
-
if (typeof this.options.mutationKey === "function") {
|
|
621
|
-
optionsIn = this.options.mutationKey(optionsIn);
|
|
622
|
-
} else if (this.options.mutationKey) {
|
|
623
|
-
optionsIn = { ...this.options.mutationKey, ...optionsIn };
|
|
624
|
-
}
|
|
625
|
-
const mutationKey = optionsIn.mutationKey ?? this.key({ type: "mutation" });
|
|
626
|
-
return mutationKey;
|
|
627
|
-
}
|
|
628
|
-
/**
|
|
629
|
-
* Generate options used for useMutation/...
|
|
630
|
-
*/
|
|
631
|
-
mutationOptions(...rest) {
|
|
632
|
-
let optionsIn = resolveMaybeOptionalOptions(rest);
|
|
633
|
-
if (typeof this.options.mutationOptions === "function") {
|
|
634
|
-
optionsIn = this.options.mutationOptions(optionsIn);
|
|
635
|
-
} else if (this.options.mutationOptions) {
|
|
636
|
-
optionsIn = { ...this.options.mutationOptions, ...optionsIn };
|
|
637
|
-
}
|
|
638
|
-
const mutationKey = this.mutationKey(optionsIn);
|
|
639
|
-
return {
|
|
640
|
-
...optionsIn,
|
|
641
|
-
mutationKey,
|
|
642
|
-
mutationFn: (input, fnContext) => {
|
|
643
|
-
return intercept(
|
|
644
|
-
toArray(this.options.mutationInterceptors),
|
|
645
|
-
{
|
|
646
|
-
path: this.path,
|
|
647
|
-
context: {
|
|
648
|
-
[OPERATION_CONTEXT_SYMBOL]: {
|
|
649
|
-
key: mutationKey,
|
|
650
|
-
type: "mutation"
|
|
651
|
-
},
|
|
652
|
-
...optionsIn.context
|
|
653
|
-
},
|
|
654
|
-
fnContext,
|
|
655
|
-
input
|
|
656
|
-
},
|
|
657
|
-
({ input: input2, fnContext: fnContext2, context }) => {
|
|
658
|
-
if (optionsIn.mutationFn) {
|
|
659
|
-
return optionsIn.mutationFn(input2, fnContext2);
|
|
660
|
-
}
|
|
661
|
-
return this.call(input2, { context });
|
|
662
|
-
}
|
|
663
|
-
);
|
|
664
|
-
}
|
|
665
|
-
};
|
|
666
|
-
}
|
|
667
|
-
}
|
|
668
|
-
|
|
669
|
-
function createRouterUtils(client, options = {}) {
|
|
670
|
-
const plugin = new CompositeRouterUtilsPlugin(options.plugins);
|
|
671
|
-
options = plugin.init(options);
|
|
672
|
-
return createRouterUtilsInternal(client, options, plugin);
|
|
673
|
-
}
|
|
674
|
-
function createRouterUtilsInternal(client, options, plugin) {
|
|
675
|
-
const path = toArray(options.path);
|
|
676
|
-
const utils = typeof client === "function" && (options.scoped === void 0 || isProcedureUtilsOptions(options.scoped)) ? bindMethods(new ProcedureUtils(
|
|
677
|
-
path,
|
|
678
|
-
client,
|
|
679
|
-
plugin.initProcedureOptions(path, mergeProcedureUtilsOptions(
|
|
680
|
-
pick(options, [
|
|
681
|
-
"prefix",
|
|
682
|
-
"serializer",
|
|
683
|
-
"queryInterceptors",
|
|
684
|
-
"streamedInterceptors",
|
|
685
|
-
"liveInterceptors",
|
|
686
|
-
"infiniteInterceptors",
|
|
687
|
-
"mutationInterceptors"
|
|
688
|
-
]),
|
|
689
|
-
options.scoped ?? {}
|
|
690
|
-
))
|
|
691
|
-
), { unbound: ["call"] }) : bindMethods(new SharedUtils(path, options));
|
|
692
|
-
const cache = /* @__PURE__ */ new Map();
|
|
693
|
-
const recursive = new Proxy(utils, {
|
|
694
|
-
get(target, prop) {
|
|
695
|
-
const value = Reflect.get(target, prop);
|
|
696
|
-
if (RECURSIVE_CLIENT_UNWRAP_KEYS.has(prop)) {
|
|
697
|
-
return value ?? Reflect.get(Object.prototype, prop);
|
|
698
|
-
}
|
|
699
|
-
const nextClient = Reflect.get(client, prop);
|
|
700
|
-
if (typeof prop !== "string" || !isTypescriptObject(nextClient)) {
|
|
701
|
-
return value;
|
|
702
|
-
}
|
|
703
|
-
let result = cache.get(prop);
|
|
704
|
-
if (result === void 0) {
|
|
705
|
-
const nextUtils = createRouterUtilsInternal(nextClient, {
|
|
706
|
-
...options,
|
|
707
|
-
path: [...path, prop],
|
|
708
|
-
scoped: options.scoped?.[prop]
|
|
709
|
-
}, plugin);
|
|
710
|
-
result = typeof value !== "function" ? nextUtils : new Proxy(value, {
|
|
711
|
-
get(target2, prop2) {
|
|
712
|
-
if (typeof prop2 !== "string" || RECURSIVE_CLIENT_UNWRAP_KEYS.has(prop2)) {
|
|
713
|
-
return Reflect.get(target2, prop2);
|
|
714
|
-
}
|
|
715
|
-
return Reflect.get(nextUtils, prop2);
|
|
716
|
-
}
|
|
717
|
-
});
|
|
718
|
-
cache.set(prop, result);
|
|
719
|
-
}
|
|
720
|
-
return result;
|
|
721
|
-
}
|
|
722
|
-
});
|
|
723
|
-
return recursive;
|
|
724
|
-
}
|
|
725
|
-
|
|
726
|
-
function createContractUtilsFactory(clientFactory, options) {
|
|
727
|
-
const factory = (contract) => {
|
|
728
|
-
const client = clientFactory(contract);
|
|
729
|
-
const path = resolveBasePathMeta(contract);
|
|
730
|
-
if (path === void 0) {
|
|
731
|
-
throw new TypeError(
|
|
732
|
-
"ContractUtilsFactory: procedure contract must define `meta.path` that matches its path in the root router contract."
|
|
733
|
-
);
|
|
734
|
-
}
|
|
735
|
-
return createRouterUtils(client, { ...options, path, scoped: get(options.scoped, path) });
|
|
736
|
-
};
|
|
737
|
-
return factory;
|
|
738
|
-
}
|
|
739
|
-
function createContractJsonifiedUtilsFactory(clientFactory, options) {
|
|
740
|
-
return createContractUtilsFactory(clientFactory, options);
|
|
741
|
-
}
|
|
742
|
-
|
|
743
|
-
function tanstackQuery(options) {
|
|
744
|
-
return {
|
|
745
|
-
name: "~tanstack-query",
|
|
746
|
-
init(meta) {
|
|
747
|
-
const current = meta["~tanstack-query"];
|
|
748
|
-
return {
|
|
749
|
-
...meta,
|
|
750
|
-
"~tanstack-query": current ? mergeProcedureUtilsOptions(current, options) : options
|
|
751
|
-
};
|
|
752
|
-
}
|
|
753
|
-
};
|
|
754
|
-
}
|
|
755
|
-
function getTanstackQueryMeta(procedureOrLazy) {
|
|
756
|
-
return procedureOrLazy["~orpc"].meta["~tanstack-query"];
|
|
757
|
-
}
|
|
758
|
-
class ContractOptionsUtilsPlugin {
|
|
759
|
-
constructor(contract) {
|
|
760
|
-
this.contract = contract;
|
|
761
|
-
}
|
|
762
|
-
name = "~contract-options";
|
|
763
|
-
initProcedureOptions(path, options) {
|
|
764
|
-
const procedure = getRouterContract(this.contract, path);
|
|
765
|
-
if (!(procedure instanceof ProcedureContract)) {
|
|
766
|
-
return options;
|
|
767
|
-
}
|
|
768
|
-
const base = getTanstackQueryMeta(procedure);
|
|
769
|
-
if (!base) {
|
|
770
|
-
return options;
|
|
771
|
-
}
|
|
772
|
-
return mergeProcedureUtilsOptions(base, options);
|
|
773
|
-
}
|
|
774
|
-
}
|
|
775
|
-
|
|
776
|
-
function createHydrationOptions(serializer = new RPCJsonSerializer()) {
|
|
777
|
-
return {
|
|
778
|
-
dehydrate: {
|
|
779
|
-
serializeData(data) {
|
|
780
|
-
const { json, meta } = serializer.serializeJson(data);
|
|
781
|
-
return { json, meta };
|
|
782
|
-
}
|
|
783
|
-
},
|
|
784
|
-
hydrate: {
|
|
785
|
-
deserializeData(data) {
|
|
786
|
-
return serializer.deserializeJson(data);
|
|
787
|
-
}
|
|
788
|
-
}
|
|
789
|
-
};
|
|
790
|
-
}
|
|
791
|
-
const cancelOnFirstData = async ({ next, fnContext }) => {
|
|
792
|
-
const cache = fnContext.client.getQueryCache();
|
|
793
|
-
const query = cache.find({ queryKey: fnContext.queryKey, exact: true });
|
|
794
|
-
const unsubscribe = cache.subscribe((event) => {
|
|
795
|
-
if (event.query === query && event.type === "updated" && event.action.type === "success") {
|
|
796
|
-
unsubscribe();
|
|
797
|
-
void event.query.cancel({ revert: true });
|
|
798
|
-
}
|
|
799
|
-
});
|
|
800
|
-
try {
|
|
801
|
-
return await next();
|
|
802
|
-
} finally {
|
|
803
|
-
unsubscribe();
|
|
804
|
-
}
|
|
805
|
-
};
|
|
806
|
-
class StreamQuerySSRUtilsPlugin {
|
|
807
|
-
name = "~stream-query-ssr";
|
|
808
|
-
isServer;
|
|
809
|
-
constructor(options = {}) {
|
|
810
|
-
this.isServer = options.isServer ?? !("window" in globalThis);
|
|
811
|
-
}
|
|
812
|
-
initProcedureOptions(_path, options) {
|
|
813
|
-
return mergeProcedureUtilsOptions({
|
|
814
|
-
...this.isServer ? { streamedInterceptors: [cancelOnFirstData], liveInterceptors: [cancelOnFirstData] } : {},
|
|
815
|
-
streamedOptions: { initialData: [], refetchOnMount: "always" },
|
|
816
|
-
liveOptions: { refetchOnMount: "always" }
|
|
817
|
-
}, options);
|
|
818
|
-
}
|
|
819
|
-
}
|
|
820
|
-
|
|
821
|
-
export { ContractOptionsUtilsPlugin, OPERATION_CONTEXT_SYMBOL, ProcedureUtils, SharedUtils, StreamQuerySSRUtilsPlugin, OPERATION_CONTEXT_SYMBOL as TANSTACK_QUERY_OPERATION_CONTEXT_SYMBOL, createContractJsonifiedUtilsFactory, createContractUtilsFactory, createHydrationOptions, createRouterUtils, createStructuralSharing, createRouterUtils as createTanstackQueryUtils, getTanstackQueryMeta, mergeProcedureUtilsOptions, tanstackQuery };
|
|
1
|
+
export * from "./contract-utils.mjs";
|
|
2
|
+
export * from "./meta.mjs";
|
|
3
|
+
export { mergeProcedureUtilsOptions, ProcedureUtils } from "./procedure-utils.mjs";
|
|
4
|
+
export * from "./router-utils.mjs";
|
|
5
|
+
export { createRouterUtils as createTanstackQueryUtils } from "./router-utils.mjs";
|
|
6
|
+
export { SharedUtils } from "./shared-utils.mjs";
|
|
7
|
+
export * from "./ssr.mjs";
|
|
8
|
+
export * from "./structural-sharing.mjs";
|
|
9
|
+
export * from "./types.mjs";
|
|
10
|
+
export { OPERATION_CONTEXT_SYMBOL as TANSTACK_QUERY_OPERATION_CONTEXT_SYMBOL } from "./types.mjs";
|