@orpc/tanstack-query 2.0.0-beta.48 → 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.
Files changed (54) hide show
  1. package/README.md +4 -4
  2. package/dist/contract-utils.d.ts +37 -0
  3. package/dist/contract-utils.js +42 -0
  4. package/dist/index.d.ts +16 -0
  5. package/dist/index.js +10 -0
  6. package/dist/key.d.ts +50 -0
  7. package/dist/key.js +50 -0
  8. package/dist/live-query.d.ts +4 -0
  9. package/dist/live-query.js +36 -0
  10. package/dist/meta.d.ts +42 -0
  11. package/dist/meta.js +55 -0
  12. package/dist/plugin.d.ts +51 -0
  13. package/dist/plugin.js +25 -0
  14. package/dist/procedure-utils.d.ts +167 -0
  15. package/dist/procedure-utils.js +411 -0
  16. package/dist/router-utils.d.ts +67 -0
  17. package/dist/router-utils.js +69 -0
  18. package/dist/shared-utils.d.ts +11 -0
  19. package/dist/shared-utils.js +19 -0
  20. package/dist/ssr.d.ts +37 -0
  21. package/dist/ssr.js +45 -0
  22. package/dist/stream-query.d.ts +38 -0
  23. package/dist/stream-query.js +52 -0
  24. package/dist/structural-sharing.d.ts +14 -0
  25. package/dist/structural-sharing.js +160 -0
  26. package/dist/types.d.ts +87 -0
  27. package/dist/types.js +7 -0
  28. package/package.json +39 -41
  29. package/dist/contract-utils.d.mts +0 -35
  30. package/dist/contract-utils.mjs +0 -23
  31. package/dist/index.d.mts +0 -16
  32. package/dist/index.mjs +0 -10
  33. package/dist/key.d.mts +0 -62
  34. package/dist/key.mjs +0 -30
  35. package/dist/live-query.d.mts +0 -6
  36. package/dist/live-query.mjs +0 -16
  37. package/dist/meta.d.mts +0 -55
  38. package/dist/meta.mjs +0 -37
  39. package/dist/plugin.d.mts +0 -51
  40. package/dist/plugin.mjs +0 -25
  41. package/dist/procedure-utils.d.mts +0 -232
  42. package/dist/procedure-utils.mjs +0 -406
  43. package/dist/router-utils.d.mts +0 -57
  44. package/dist/router-utils.mjs +0 -51
  45. package/dist/shared-utils.d.mts +0 -11
  46. package/dist/shared-utils.mjs +0 -17
  47. package/dist/ssr.d.mts +0 -38
  48. package/dist/ssr.mjs +0 -57
  49. package/dist/stream-query.d.mts +0 -30
  50. package/dist/stream-query.mjs +0 -58
  51. package/dist/structural-sharing.d.mts +0 -14
  52. package/dist/structural-sharing.mjs +0 -140
  53. package/dist/types.d.mts +0 -138
  54. package/dist/types.mjs +0 -2
@@ -1,58 +0,0 @@
1
-
2
- export function serializableStreamedQuery(queryFn, { refetchMode = "reset", maxChunks = Number.POSITIVE_INFINITY } = {}) {
3
-
4
- return async (context) => {
5
- const query = context.client.getQueryCache().find({
6
- queryKey: context.queryKey,
7
- exact: true
8
- });
9
- const hasPreviousData = !!query && query.state.data !== undefined;
10
- if (hasPreviousData) {
11
- if (refetchMode === "reset") {
12
- query.setState({
13
- status: "pending",
14
- data: undefined,
15
- error: null,
16
- fetchStatus: "fetching"
17
- });
18
- } else {
19
-
20
- query.setState({ data: limitArraySize(query.state.data, maxChunks) });
21
- }
22
- }
23
- let result = [];
24
- const stream = await queryFn(context);
25
- const shouldUpdateCacheDuringStream = !hasPreviousData || refetchMode !== "replace";
26
-
27
- context.client.setQueryData(context.queryKey, (prev = []) => limitArraySize(prev, maxChunks));
28
-
29
- if (context.signal.aborted) {
30
- await stream[Symbol.asyncIterator]().return?.();
31
- throw context.signal.reason;
32
- }
33
- for await (const chunk of stream) {
34
- if (context.signal.aborted) {
35
- throw context.signal.reason;
36
- }
37
- result.push(chunk);
38
- result = limitArraySize(result, maxChunks);
39
- if (shouldUpdateCacheDuringStream) {
40
- context.client.setQueryData(context.queryKey, (prev = []) => limitArraySize([...prev, chunk], maxChunks));
41
- }
42
- }
43
- if (!shouldUpdateCacheDuringStream) {
44
- context.client.setQueryData(context.queryKey, result);
45
- }
46
- const cachedData = context.client.getQueryData(context.queryKey);
47
- if (cachedData) {
48
- return limitArraySize(cachedData, maxChunks);
49
- }
50
- return result;
51
- };
52
- }
53
- function limitArraySize(items, maxSize) {
54
- if (items.length <= maxSize) {
55
- return items;
56
- }
57
- return items.slice(items.length - maxSize);
58
- }
@@ -1,14 +0,0 @@
1
- import type { RPCJsonSerializer } from "@orpc/client";
2
- import type { Public } from "@orpc/shared";
3
- export type StructuralSharing = <T>(oldData: unknown, newData: T) => T;
4
- /**
5
- * Creates a `structuralSharing` function that works like TanStack Query's default, but also keeps
6
- * the previous reference for equal `Date`, `URL`, `Map` and `Set` values, which oRPC's serializer
7
- * recreates on every response, and for custom types that serialize the same with a type-aware
8
- * serializer like `RPCJsonSerializer`.
9
- *
10
- * Used by default in `.queryOptions`, `.liveOptions` and `.infiniteOptions`, pass `structuralSharing: false` to opt out.
11
- *
12
- * @see {@link https://orpc.dev/docs/integrations/tanstack-query#structural-sharing | TanStack Query Integration - Structural Sharing}
13
- */
14
- export declare function createStructuralSharing(serializer?: Public<RPCJsonSerializer>): StructuralSharing;
@@ -1,140 +0,0 @@
1
- import { setOwn, stringifyJSON } from "@orpc/shared";
2
-
3
- export function createStructuralSharing(serializer) {
4
- return (oldData, newData) => replaceEqualDeep(oldData, newData, 0, serializer);
5
- }
6
- function replaceEqualDeep(a, b, depth, serializer) {
7
- if (Object.is(a, b)) {
8
- return a;
9
- }
10
- if (depth > 500 || typeof a !== "object" || typeof b !== "object" || a === null || b === null) {
11
- return b;
12
- }
13
- const proto = Object.getPrototypeOf(a);
14
- if (proto !== Object.getPrototypeOf(b)) {
15
- return b;
16
- }
17
- if ((proto === Date.prototype || proto === URL.prototype || proto === Set.prototype || proto === Map.prototype) && !hasOwnEnumerableKey(a) && !hasOwnEnumerableKey(b)) {
18
- return replaceEqualBuiltIn(a, b, proto, depth, serializer);
19
- }
20
- if (Array.isArray(a)) {
21
- return isPlainArray(a) && isPlainArray(b) ? replaceEqualArray(a, b, depth, serializer) : b;
22
- }
23
-
24
- if (proto === Object.prototype || proto === null || !proto.constructor) {
25
- return replaceEqualObject(a, b, proto, depth, serializer);
26
- }
27
- return replaceEqualSerialized(a, b, depth, serializer);
28
- }
29
- function replaceEqualArray(a, b, depth, serializer) {
30
- let copy;
31
- for (let i = 0; i < b.length; i++) {
32
- let value = b[i];
33
- if (i < a.length) {
34
- const aValue = a[i];
35
- if (typeof aValue === "object" && typeof value === "object") {
36
- value = replaceEqualDeep(aValue, value, depth + 1, serializer);
37
- }
38
- if (copy === undefined && Object.is(value, aValue)) {
39
- continue;
40
- }
41
- }
42
- copy ??= copyArrayPrefix(a, i);
43
- copy.push(value);
44
- }
45
- return copy ?? (a.length === b.length ? a : copyArrayPrefix(a, b.length));
46
- }
47
- function copyArrayPrefix(a, end) {
48
- const copy = [];
49
- for (let i = 0; i < end; i++) {
50
- copy.push(a[i]);
51
- }
52
- return copy;
53
- }
54
- function replaceEqualObject(a, b, proto, depth, serializer) {
55
- const bKeys = Object.keys(b);
56
- let copy;
57
- for (let i = 0; i < bKeys.length; i++) {
58
- const key = bKeys[i];
59
- let value = b[key];
60
- if (Object.hasOwn(a, key)) {
61
- const aValue = a[key];
62
- if (typeof aValue === "object" && typeof value === "object") {
63
- value = replaceEqualDeep(aValue, value, depth + 1, serializer);
64
- }
65
- if (copy === undefined && Object.is(value, aValue)) {
66
- continue;
67
- }
68
- }
69
- copy ??= copyObjectPrefix(a, bKeys, i, proto);
70
- assign(copy, key, value);
71
- }
72
- return copy ?? (Object.keys(a).length === bKeys.length ? a : copyObjectPrefix(a, bKeys, bKeys.length, proto));
73
- }
74
- function copyObjectPrefix(a, keys, end, proto) {
75
- const copy = proto === Object.prototype ? {} : Object.create(proto);
76
- for (let i = 0; i < end; i++) {
77
- assign(copy, keys[i], a[keys[i]]);
78
- }
79
- return copy;
80
- }
81
- function assign(object, key, value) {
82
-
83
- if (key === "__proto__") {
84
- setOwn(object, key, value);
85
- } else {
86
- object[key] = value;
87
- }
88
- }
89
- function replaceEqualBuiltIn(a, b, proto, depth, serializer) {
90
- if (proto === Date.prototype) {
91
- return Object.is(a.getTime(), b.getTime()) ? a : b;
92
- }
93
- if (proto === URL.prototype) {
94
- return a.href === b.href ? a : b;
95
- }
96
- if (a.size !== b.size) {
97
- return b;
98
- }
99
- if (proto === Set.prototype) {
100
- const aValues = a.values();
101
- for (const bValue of b.values()) {
102
- const aValue = aValues.next().value;
103
- if (!Object.is(replaceEqualDeep(aValue, bValue, depth + 1, serializer), aValue)) {
104
- return b;
105
- }
106
- }
107
- return a;
108
- }
109
- const aKeys = a.keys();
110
- const aValues = a.values();
111
- const bValues = b.values();
112
- for (const bKey of b.keys()) {
113
- const aKey = aKeys.next().value;
114
- const aValue = aValues.next().value;
115
- if (!Object.is(replaceEqualDeep(aKey, bKey, depth + 1, serializer), aKey) || !Object.is(replaceEqualDeep(aValue, bValues.next().value, depth + 1, serializer), aValue)) {
116
- return b;
117
- }
118
- }
119
- return a;
120
- }
121
- function replaceEqualSerialized(a, b, depth, serializer) {
122
- const aSerialized = serializer?.serializeJson(a);
123
- if (!aSerialized?.meta?.length) {
124
- return b;
125
- }
126
- const bSerialized = serializer.serializeJson(b);
127
- return stringifyJSON(aSerialized.meta) === stringifyJSON(bSerialized.meta) && Object.is(replaceEqualDeep(aSerialized.json, bSerialized.json, depth + 1, serializer), aSerialized.json) ? a : b;
128
- }
129
- function hasOwnEnumerableKey(value) {
130
-
131
- for (const key in value) {
132
- if (Object.hasOwn(value, key)) {
133
- return true;
134
- }
135
- }
136
- return false;
137
- }
138
- function isPlainArray(value) {
139
- return value.length === Object.keys(value).length;
140
- }
package/dist/types.d.mts DELETED
@@ -1,138 +0,0 @@
1
- import type { ClientContext } from "@orpc/client";
2
- import type { DataTag, InfiniteData, InfiniteQueryObserverOptions, MutationObserverOptions, QueryFunction, QueryKey, QueryObserverOptions, SkipToken } from "@tanstack/query-core";
3
- import type { SerializableStreamedQueryOptions } from "./stream-query.mjs";
4
- export type InferStreamedQueryOutput<TOutput> = TOutput extends AsyncIterable<infer U> ? U[] : never;
5
- export type InferLiveQueryOutput<TOutput> = TOutput extends AsyncIterable<infer U> ? U : never;
6
- export type OperationType = "query" | "streamed" | "live" | "infinite" | "mutation";
7
- /**
8
- * The symbol under which TanStack Query utils attach operation details
9
- * (key and operation type) to the client context.
10
- *
11
- * @see {@link https://orpc.dev/docs/integrations/tanstack-query#client-context | TanStack Query Integration - Client Context}
12
- */
13
- export declare const OPERATION_CONTEXT_SYMBOL: unique symbol;
14
- /**
15
- * A client context automatically populated by TanStack Query utils,
16
- * exposing the operation key and type of the calling operation.
17
- *
18
- * @see {@link https://orpc.dev/docs/integrations/tanstack-query#client-context | TanStack Query Integration - Client Context}
19
- */
20
- export interface OperationContext {
21
- [OPERATION_CONTEXT_SYMBOL]?: {
22
- key: QueryKey;
23
- type: OperationType;
24
- };
25
- }
26
- export type QueryKeyOptions<TInput> = (undefined extends TInput ? {
27
- input?: TInput | SkipToken;
28
- } : {
29
- input: TInput | SkipToken;
30
- }) | {
31
- queryKey: QueryKey;
32
- };
33
- export type QueryOptionsIn<
34
- TClientContext extends ClientContext,
35
- TInput,
36
- TOutput,
37
- TError,
38
- TSelectData,
39
- TInitialData
40
- > = (undefined extends TInput ? {
41
- input?: TInput | SkipToken;
42
- } : {
43
- input: TInput | SkipToken;
44
- }) & (object extends TClientContext ? {
45
- context?: TClientContext | undefined;
46
- } : {
47
- context: TClientContext;
48
- }) & Omit<QueryObserverOptions<TOutput, TError, TSelectData>, "queryKey" | "_defaulted" | "_optimisticResults"> & {
49
- queryKey?: QueryKey | undefined;
50
- initialData?: TInitialData;
51
- };
52
- export type QueryOptionsOut<
53
- TOutput,
54
- TError,
55
- TSelectData,
56
- TInitialData
57
- > = Pick<QueryObserverOptions<TOutput, TError, TSelectData, TOutput, DataTag<QueryKey, TOutput, TError>>, "queryKey" | "throwOnError" | "select" | "retryDelay"> & (undefined extends TInitialData ? object : {
58
- initialData: TInitialData;
59
- }) & {
60
- queryFn: QueryFunction<TOutput, DataTag<QueryKey, TOutput, TError>>;
61
- };
62
- export type StreamedKeyOptions<TInput> = (undefined extends TInput ? {
63
- input?: TInput | SkipToken;
64
- } : {
65
- input: TInput | SkipToken;
66
- }) & {
67
- queryFnOptions?: SerializableStreamedQueryOptions | undefined;
68
- } | {
69
- queryKey: QueryKey;
70
- };
71
- export type StreamedOptionsIn<
72
- TClientContext extends ClientContext,
73
- TInput,
74
- TOutput,
75
- TError,
76
- TSelectData,
77
- TInitialData
78
- > = QueryOptionsIn<TClientContext, TInput, TOutput, TError, TSelectData, TInitialData> & {
79
- queryFnOptions?: SerializableStreamedQueryOptions | undefined;
80
- };
81
- export type StreamedOptionsOut<
82
- TOutput,
83
- TError,
84
- TSelectData,
85
- TInitialData
86
- > = QueryOptionsOut<TOutput, TError, TSelectData, TInitialData>;
87
- export type InfiniteKeyOptions<
88
- TInput,
89
- TPageParam
90
- > = Pick<InfiniteOptionsIn<any, TInput, any, any, any, TPageParam, any>, "input" | "initialPageParam"> | {
91
- queryKey: QueryKey;
92
- };
93
- export type InfiniteOptionsIn<
94
- TClientContext extends ClientContext,
95
- TInput,
96
- TOutput,
97
- TError,
98
- TSelectData,
99
- TPageParam,
100
- TInitialData
101
- > = {
102
- input: ((pageParam: TPageParam) => TInput) | SkipToken;
103
- initialData?: TInitialData;
104
- queryKey?: QueryKey | undefined;
105
- } & (object extends TClientContext ? {
106
- context?: TClientContext | undefined;
107
- } : {
108
- context: TClientContext;
109
- }) & Omit<InfiniteQueryObserverOptions<TOutput, TError, TSelectData, QueryKey, TPageParam>, "queryKey" | "_defaulted" | "_optimisticResults">;
110
- export type InfiniteOptionsOut<
111
- TOutput,
112
- TError,
113
- TSelectData,
114
- TPageParam,
115
- TInitialData
116
- > = Pick<InfiniteQueryObserverOptions<TOutput, TError, TSelectData, DataTag<QueryKey, InfiniteData<TOutput, TPageParam>, TError>, TPageParam>, "queryKey" | "select" | "throwOnError" | "retryDelay" | "initialPageParam" | "getNextPageParam" | "getPreviousPageParam"> & (undefined extends TInitialData ? object : {
117
- initialData: TInitialData;
118
- }) & {
119
- queryFn: QueryFunction<TOutput, DataTag<QueryKey, InfiniteData<TOutput, TPageParam>, TError>, TPageParam>;
120
- };
121
- export type MutationKeyOptions = Pick<MutationOptionsIn<any, any, any, any, any>, "mutationKey">;
122
- export type MutationOptionsIn<
123
- TClientContext extends ClientContext,
124
- TInput,
125
- TOutput,
126
- TError,
127
- TMutationContext
128
- > = (object extends TClientContext ? {
129
- context?: TClientContext | undefined;
130
- } : {
131
- context: TClientContext;
132
- }) & MutationOptionsOut<TInput, TOutput, TError, TMutationContext>;
133
- export type MutationOptionsOut<
134
- TInput,
135
- TOutput,
136
- TError,
137
- TMutationContext
138
- > = MutationObserverOptions<TOutput, TError, TInput, TMutationContext>;
package/dist/types.mjs DELETED
@@ -1,2 +0,0 @@
1
-
2
- export const OPERATION_CONTEXT_SYMBOL = /* @__PURE__ */ Symbol.for("ORPC_TANSTACK_QUERY_OPERATION_CONTEXT");