@orpc/tanstack-query 2.0.0-beta.44 → 2.0.0-beta.46

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/dist/index.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  import { resolveBasePathMeta, getRouterContract, ProcedureContract } from '@orpc/contract';
2
- import { sortPlugins, throwIfAborted, stringifyJSON, resolveMaybeOptionalOptions, intercept, toArray, isAsyncIteratorObject, isTypescriptObject, bindMethods, pick, get } from '@orpc/shared';
3
- import { RECURSIVE_CLIENT_UNWRAP_KEYS } from '@orpc/client';
2
+ import { sortPlugins, throwIfAborted, stringifyJSON, setOwn, resolveMaybeOptionalOptions, intercept, toArray, isAsyncIteratorObject, isTypescriptObject, bindMethods, pick, get } from '@orpc/shared';
3
+ import { RPCJsonSerializer, RECURSIVE_CLIENT_UNWRAP_KEYS } from '@orpc/client';
4
4
  import { skipToken } from '@tanstack/query-core';
5
5
 
6
6
  class CompositeRouterUtilsPlugin {
@@ -27,18 +27,6 @@ class CompositeRouterUtilsPlugin {
27
27
  }
28
28
  }
29
29
 
30
- function generateOperationKey(path, options = {}) {
31
- return [
32
- ...options.prefix !== void 0 ? [options.prefix] : [],
33
- path,
34
- {
35
- ...options.input === skipToken ? { skipToken: true } : options.input !== void 0 ? { input: options.input } : {},
36
- ...options.type !== void 0 ? { type: options.type } : {},
37
- ...options.fnOptions !== void 0 ? { fnOptions: options.fnOptions } : {}
38
- }
39
- ];
40
- }
41
-
42
30
  function liveQuery(queryFn) {
43
31
  return async (context) => {
44
32
  const stream = await queryFn(context);
@@ -57,6 +45,30 @@ function liveQuery(queryFn) {
57
45
  };
58
46
  }
59
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
+
60
72
  class SharedUtils {
61
73
  constructor(path, options) {
62
74
  this.path = path;
@@ -66,7 +78,7 @@ class SharedUtils {
66
78
  * Generate a **partial matching** key for actions like revalidating queries, checking mutation status, etc.
67
79
  */
68
80
  key(options = {}) {
69
- return generateOperationKey(this.path, { ...options, prefix: this.options.prefix });
81
+ return generateOperationKey(this.path, { ...options, prefix: this.options.prefix, serializer: this.options.serializer });
70
82
  }
71
83
  }
72
84
 
@@ -83,10 +95,9 @@ function serializableStreamedQuery(queryFn, { refetchMode = "reset", maxChunks =
83
95
  fetchStatus: "fetching"
84
96
  });
85
97
  } else {
86
- context.client.setQueryData(
87
- context.queryKey,
88
- (prev = []) => limitArraySize(prev, maxChunks)
89
- );
98
+ query.setState({
99
+ data: limitArraySize(query.state.data, maxChunks)
100
+ });
90
101
  }
91
102
  }
92
103
  let result = [];
@@ -96,6 +107,10 @@ function serializableStreamedQuery(queryFn, { refetchMode = "reset", maxChunks =
96
107
  context.queryKey,
97
108
  (prev = []) => limitArraySize(prev, maxChunks)
98
109
  );
110
+ if (context.signal.aborted) {
111
+ await stream[Symbol.asyncIterator]().return?.();
112
+ throw context.signal.reason;
113
+ }
99
114
  for await (const chunk of stream) {
100
115
  if (context.signal.aborted) {
101
116
  throw context.signal.reason;
@@ -126,6 +141,142 @@ function limitArraySize(items, maxSize) {
126
141
  return items.slice(items.length - maxSize);
127
142
  }
128
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
+
129
280
  const OPERATION_CONTEXT_SYMBOL = Symbol.for("ORPC_TANSTACK_QUERY_OPERATION_CONTEXT");
130
281
 
131
282
  const PROCEDURE_UTILS_INTERCEPTOR_KEYS = [
@@ -159,6 +310,10 @@ function isProcedureUtilsOptions(value) {
159
310
  if (typeof value[key] !== "string") {
160
311
  return false;
161
312
  }
313
+ } else if (key === "serializer") {
314
+ if (!isTypescriptObject(value[key]) || typeof value[key].serializeJson !== "function") {
315
+ return false;
316
+ }
162
317
  } else if (PROCEDURE_UTILS_INTERCEPTOR_KEYS.includes(key)) {
163
318
  if (!Array.isArray(value[key]) || value[key].some((i) => typeof i !== "function")) {
164
319
  return false;
@@ -218,7 +373,7 @@ class ProcedureUtils extends SharedUtils {
218
373
  } else if (this.options.queryKey) {
219
374
  optionsIn = { ...this.options.queryKey, ...optionsIn };
220
375
  }
221
- const queryKey = optionsIn.queryKey ?? generateOperationKey(this.path, { prefix: this.options.prefix, type: "query", input: optionsIn.input });
376
+ const queryKey = optionsIn.queryKey ?? this.key({ type: "query", input: optionsIn.input });
222
377
  return queryKey;
223
378
  }
224
379
  /**
@@ -233,6 +388,7 @@ class ProcedureUtils extends SharedUtils {
233
388
  }
234
389
  const queryKey = this.queryKey(optionsIn);
235
390
  return {
391
+ structuralSharing: createStructuralSharing(this.options.serializer),
236
392
  ...optionsIn,
237
393
  queryKey,
238
394
  queryFn: (fnContext) => {
@@ -274,7 +430,7 @@ class ProcedureUtils extends SharedUtils {
274
430
  } else if (this.options.streamedKey) {
275
431
  optionsIn = { ...this.options.streamedKey, ...optionsIn };
276
432
  }
277
- const queryKey = optionsIn.queryKey ?? generateOperationKey(this.path, { prefix: this.options.prefix, type: "streamed", input: optionsIn.input, fnOptions: optionsIn.queryFnOptions });
433
+ const queryKey = optionsIn.queryKey ?? this.key({ type: "streamed", input: optionsIn.input, fnOptions: optionsIn.queryFnOptions });
278
434
  return queryKey;
279
435
  }
280
436
  /**
@@ -341,7 +497,7 @@ class ProcedureUtils extends SharedUtils {
341
497
  } else if (this.options.liveKey) {
342
498
  optionsIn = { ...this.options.liveKey, ...optionsIn };
343
499
  }
344
- const queryKey = optionsIn.queryKey ?? generateOperationKey(this.path, { prefix: this.options.prefix, type: "live", input: optionsIn.input });
500
+ const queryKey = optionsIn.queryKey ?? this.key({ type: "live", input: optionsIn.input });
345
501
  return queryKey;
346
502
  }
347
503
  /**
@@ -358,6 +514,7 @@ class ProcedureUtils extends SharedUtils {
358
514
  }
359
515
  const queryKey = this.liveKey(optionsIn);
360
516
  return {
517
+ structuralSharing: createStructuralSharing(this.options.serializer),
361
518
  ...optionsIn,
362
519
  queryKey,
363
520
  queryFn: (fnContext) => {
@@ -407,8 +564,7 @@ class ProcedureUtils extends SharedUtils {
407
564
  } else if (this.options.infiniteKey) {
408
565
  optionsIn = { ...this.options.infiniteKey, ...optionsIn };
409
566
  }
410
- const queryKey = optionsIn.queryKey ?? generateOperationKey(this.path, {
411
- prefix: this.options.prefix,
567
+ const queryKey = optionsIn.queryKey ?? this.key({
412
568
  type: "infinite",
413
569
  input: optionsIn.input === skipToken ? skipToken : optionsIn.input(optionsIn.initialPageParam)
414
570
  });
@@ -425,6 +581,7 @@ class ProcedureUtils extends SharedUtils {
425
581
  }
426
582
  const queryKey = this.infiniteKey(optionsIn);
427
583
  return {
584
+ structuralSharing: createStructuralSharing(this.options.serializer),
428
585
  ...optionsIn,
429
586
  queryKey,
430
587
  queryFn: (fnContext) => {
@@ -465,7 +622,7 @@ class ProcedureUtils extends SharedUtils {
465
622
  } else if (this.options.mutationKey) {
466
623
  optionsIn = { ...this.options.mutationKey, ...optionsIn };
467
624
  }
468
- const mutationKey = optionsIn.mutationKey ?? generateOperationKey(this.path, { prefix: this.options.prefix, type: "mutation" });
625
+ const mutationKey = optionsIn.mutationKey ?? this.key({ type: "mutation" });
469
626
  return mutationKey;
470
627
  }
471
628
  /**
@@ -522,6 +679,7 @@ function createRouterUtilsInternal(client, options, plugin) {
522
679
  plugin.initProcedureOptions(path, mergeProcedureUtilsOptions(
523
680
  pick(options, [
524
681
  "prefix",
682
+ "serializer",
525
683
  "queryInterceptors",
526
684
  "streamedInterceptors",
527
685
  "liveInterceptors",
@@ -535,8 +693,11 @@ function createRouterUtilsInternal(client, options, plugin) {
535
693
  const recursive = new Proxy(utils, {
536
694
  get(target, prop) {
537
695
  const value = Reflect.get(target, prop);
696
+ if (RECURSIVE_CLIENT_UNWRAP_KEYS.has(prop)) {
697
+ return value ?? Reflect.get(Object.prototype, prop);
698
+ }
538
699
  const nextClient = Reflect.get(client, prop);
539
- if (typeof prop !== "string" || RECURSIVE_CLIENT_UNWRAP_KEYS.has(prop) || !isTypescriptObject(nextClient)) {
700
+ if (typeof prop !== "string" || !isTypescriptObject(nextClient)) {
540
701
  return value;
541
702
  }
542
703
  let result = cache.get(prop);
@@ -612,4 +773,49 @@ class ContractOptionsUtilsPlugin {
612
773
  }
613
774
  }
614
775
 
615
- export { CompositeRouterUtilsPlugin, ContractOptionsUtilsPlugin, OPERATION_CONTEXT_SYMBOL, ProcedureUtils, SharedUtils, OPERATION_CONTEXT_SYMBOL as TANSTACK_QUERY_OPERATION_CONTEXT_SYMBOL, createContractJsonifiedUtilsFactory, createContractUtilsFactory, createRouterUtils, createRouterUtils as createTanstackQueryUtils, generateOperationKey, getTanstackQueryMeta, isProcedureUtilsOptions, mergeProcedureUtilsOptions, serializableStreamedQuery, tanstackQuery };
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 };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@orpc/tanstack-query",
3
3
  "type": "module",
4
- "version": "2.0.0-beta.44",
4
+ "version": "2.0.0-beta.46",
5
5
  "description": "TanStack Query integration for oRPC: typesafe queries, mutations, streaming, and SSR for React, Vue, Solid, Svelte, and Angular",
6
6
  "license": "MIT",
7
7
  "funding": [
@@ -44,22 +44,22 @@
44
44
  "@tanstack/query-core": ">=5.89.0"
45
45
  },
46
46
  "dependencies": {
47
- "@orpc/client": "2.0.0-beta.44",
48
- "@orpc/contract": "2.0.0-beta.44",
49
- "@orpc/openapi": "2.0.0-beta.44",
50
- "@orpc/shared": "2.0.0-beta.44"
47
+ "@orpc/client": "2.0.0-beta.46",
48
+ "@orpc/contract": "2.0.0-beta.46",
49
+ "@orpc/openapi": "2.0.0-beta.46",
50
+ "@orpc/shared": "2.0.0-beta.46"
51
51
  },
52
52
  "devDependencies": {
53
- "@angular/core": "^22.2.2",
54
- "@tanstack/angular-query-experimental": "^5.104.1",
55
- "@tanstack/query-core": "^5.104.1",
56
- "@tanstack/react-query": "^5.104.1",
57
- "@tanstack/solid-query": "^5.104.1",
58
- "@tanstack/svelte-query": "^6.3.1",
59
- "@tanstack/vue-query": "^5.104.1",
53
+ "@angular/core": "^22.2.3",
54
+ "@tanstack/angular-query-experimental": "^5.104.3",
55
+ "@tanstack/query-core": "^5.104.3",
56
+ "@tanstack/react-query": "^5.104.3",
57
+ "@tanstack/solid-query": "^5.104.3",
58
+ "@tanstack/svelte-query": "^6.3.3",
59
+ "@tanstack/vue-query": "^5.104.3",
60
60
  "svelte": "^5.57.2",
61
- "vue": "^3.5.43",
62
- "zod": "^4.6.5"
61
+ "vue": "^3.5.44",
62
+ "zod": "^4.6.7"
63
63
  },
64
64
  "scripts": {
65
65
  "type:check": "tsc -b"