@orpc/tanstack-query 1.8.8 → 1.9.0

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.d.mts CHANGED
@@ -1,15 +1,40 @@
1
1
  import { ClientContext, Client, NestedClient } from '@orpc/client';
2
- import { SetOptional, PartialDeep, MaybeOptionalOptions } from '@orpc/shared';
3
- import { SkipToken, QueryKey, QueryObserverOptions, DataTag, QueryFunction, experimental_streamedQuery, InfiniteQueryObserverOptions, InfiniteData, MutationObserverOptions } from '@tanstack/query-core';
2
+ import { Promisable, SetOptional, PartialDeep, MaybeOptionalOptions } from '@orpc/shared';
3
+ import { QueryKey, QueryFunctionContext, QueryFunction, SkipToken, QueryObserverOptions, DataTag, InfiniteQueryObserverOptions, InfiniteData, MutationObserverOptions } from '@tanstack/query-core';
4
+
5
+ interface experimental_SerializableStreamedQueryOptions {
6
+ /**
7
+ * Determines how data is handled when the query is refetched.
8
+ *
9
+ * - `'reset'`: Clears existing data and sets the query back to a pending state.
10
+ * - `'append'`: Appends new data chunks to the existing data.
11
+ * - `'replace'`: Collects all streamed data and replaces the cache once the stream finishes.
12
+ *
13
+ * @default 'reset'
14
+ */
15
+ refetchMode?: 'append' | 'reset' | 'replace';
16
+ /**
17
+ * Limits the number of data chunks stored in the query result.
18
+ * Older chunks are removed when the limit is reached.
19
+ *
20
+ * @default Number.POSITIVE_INFINITY (unlimited)
21
+ */
22
+ maxChunks?: number;
23
+ }
24
+ /**
25
+ * A variant of `streamedQuery` where:
26
+ * - Options are serializable
27
+ * - The output is predictable
28
+ */
29
+ declare function experimental_serializableStreamedQuery<TQueryFnData = unknown, TQueryKey extends QueryKey = QueryKey>(queryFn: (context: QueryFunctionContext<TQueryKey>) => Promisable<AsyncIterable<TQueryFnData>>, { refetchMode, maxChunks }?: experimental_SerializableStreamedQueryOptions): QueryFunction<Array<TQueryFnData>, TQueryKey>;
4
30
 
5
31
  type experimental_StreamedQueryOutput<TOutput> = TOutput extends AsyncIterable<infer U> ? U[] : never;
6
32
  type experimental_LiveQueryOutput<TOutput> = TOutput extends AsyncIterable<infer U> ? U : never;
7
- type experimental_StreamedQueryOptions = Omit<Parameters<typeof experimental_streamedQuery>[0], 'queryFn'>;
8
33
  type OperationType = 'query' | 'streamed' | 'live' | 'infinite' | 'mutation';
9
34
  type OperationKeyOptions<TType extends OperationType, TInput> = {
10
35
  type?: TType;
11
36
  input?: TType extends 'mutation' ? never : PartialDeep<TInput>;
12
- fnOptions?: TType extends 'streamed' ? experimental_StreamedQueryOptions : never;
37
+ fnOptions?: TType extends 'streamed' ? experimental_SerializableStreamedQueryOptions : never;
13
38
  };
14
39
  type OperationKey<TType extends OperationType, TInput> = [path: readonly string[], options: OperationKeyOptions<TType, TInput>];
15
40
  declare const OPERATION_CONTEXT_SYMBOL: unique symbol;
@@ -39,10 +64,10 @@ interface QueryOptionsBase<TOutput, TError> {
39
64
  enabled?: boolean;
40
65
  }
41
66
  type experimental_StreamedKeyOptions<TInput> = QueryKeyOptions<TInput> & {
42
- queryFnOptions?: experimental_StreamedQueryOptions;
67
+ queryFnOptions?: experimental_SerializableStreamedQueryOptions;
43
68
  };
44
69
  type experimental_StreamedOptionsIn<TClientContext extends ClientContext, TInput, TOutput, TError, TSelectData> = QueryOptionsIn<TClientContext, TInput, TOutput, TError, TSelectData> & {
45
- queryFnOptions?: experimental_StreamedQueryOptions;
70
+ queryFnOptions?: experimental_SerializableStreamedQueryOptions;
46
71
  };
47
72
  interface experimental_StreamedOptionsBase<TOutput, TError> extends QueryOptionsBase<TOutput, TError> {
48
73
  }
@@ -174,5 +199,5 @@ interface CreateRouterUtilsOptions {
174
199
  */
175
200
  declare function createRouterUtils<T extends NestedClient<any>>(client: T, options?: CreateRouterUtilsOptions): RouterUtils<T>;
176
201
 
177
- export { OPERATION_CONTEXT_SYMBOL, OPERATION_CONTEXT_SYMBOL as TANSTACK_QUERY_OPERATION_CONTEXT_SYMBOL, createGeneralUtils, createProcedureUtils, createRouterUtils, createRouterUtils as createTanstackQueryUtils, generateOperationKey };
178
- export type { CreateProcedureUtilsOptions, CreateRouterUtilsOptions, GeneralUtils, InfiniteOptionsBase, InfiniteOptionsIn, MutationOptions, MutationOptionsIn, OperationContext, OperationKey, OperationKeyOptions, OperationType, ProcedureUtils, QueryKeyOptions, QueryOptionsBase, QueryOptionsIn, RouterUtils, OperationContext as TanstackQueryOperationContext, experimental_LiveQueryOutput, experimental_StreamedKeyOptions, experimental_StreamedOptionsBase, experimental_StreamedOptionsIn, experimental_StreamedQueryOutput };
202
+ export { OPERATION_CONTEXT_SYMBOL, OPERATION_CONTEXT_SYMBOL as TANSTACK_QUERY_OPERATION_CONTEXT_SYMBOL, createGeneralUtils, createProcedureUtils, createRouterUtils, createRouterUtils as createTanstackQueryUtils, experimental_serializableStreamedQuery, generateOperationKey };
203
+ export type { CreateProcedureUtilsOptions, CreateRouterUtilsOptions, GeneralUtils, InfiniteOptionsBase, InfiniteOptionsIn, MutationOptions, MutationOptionsIn, OperationContext, OperationKey, OperationKeyOptions, OperationType, ProcedureUtils, QueryKeyOptions, QueryOptionsBase, QueryOptionsIn, RouterUtils, OperationContext as TanstackQueryOperationContext, experimental_LiveQueryOutput, experimental_SerializableStreamedQueryOptions, experimental_StreamedKeyOptions, experimental_StreamedOptionsBase, experimental_StreamedOptionsIn, experimental_StreamedQueryOutput };
package/dist/index.d.ts CHANGED
@@ -1,15 +1,40 @@
1
1
  import { ClientContext, Client, NestedClient } from '@orpc/client';
2
- import { SetOptional, PartialDeep, MaybeOptionalOptions } from '@orpc/shared';
3
- import { SkipToken, QueryKey, QueryObserverOptions, DataTag, QueryFunction, experimental_streamedQuery, InfiniteQueryObserverOptions, InfiniteData, MutationObserverOptions } from '@tanstack/query-core';
2
+ import { Promisable, SetOptional, PartialDeep, MaybeOptionalOptions } from '@orpc/shared';
3
+ import { QueryKey, QueryFunctionContext, QueryFunction, SkipToken, QueryObserverOptions, DataTag, InfiniteQueryObserverOptions, InfiniteData, MutationObserverOptions } from '@tanstack/query-core';
4
+
5
+ interface experimental_SerializableStreamedQueryOptions {
6
+ /**
7
+ * Determines how data is handled when the query is refetched.
8
+ *
9
+ * - `'reset'`: Clears existing data and sets the query back to a pending state.
10
+ * - `'append'`: Appends new data chunks to the existing data.
11
+ * - `'replace'`: Collects all streamed data and replaces the cache once the stream finishes.
12
+ *
13
+ * @default 'reset'
14
+ */
15
+ refetchMode?: 'append' | 'reset' | 'replace';
16
+ /**
17
+ * Limits the number of data chunks stored in the query result.
18
+ * Older chunks are removed when the limit is reached.
19
+ *
20
+ * @default Number.POSITIVE_INFINITY (unlimited)
21
+ */
22
+ maxChunks?: number;
23
+ }
24
+ /**
25
+ * A variant of `streamedQuery` where:
26
+ * - Options are serializable
27
+ * - The output is predictable
28
+ */
29
+ declare function experimental_serializableStreamedQuery<TQueryFnData = unknown, TQueryKey extends QueryKey = QueryKey>(queryFn: (context: QueryFunctionContext<TQueryKey>) => Promisable<AsyncIterable<TQueryFnData>>, { refetchMode, maxChunks }?: experimental_SerializableStreamedQueryOptions): QueryFunction<Array<TQueryFnData>, TQueryKey>;
4
30
 
5
31
  type experimental_StreamedQueryOutput<TOutput> = TOutput extends AsyncIterable<infer U> ? U[] : never;
6
32
  type experimental_LiveQueryOutput<TOutput> = TOutput extends AsyncIterable<infer U> ? U : never;
7
- type experimental_StreamedQueryOptions = Omit<Parameters<typeof experimental_streamedQuery>[0], 'queryFn'>;
8
33
  type OperationType = 'query' | 'streamed' | 'live' | 'infinite' | 'mutation';
9
34
  type OperationKeyOptions<TType extends OperationType, TInput> = {
10
35
  type?: TType;
11
36
  input?: TType extends 'mutation' ? never : PartialDeep<TInput>;
12
- fnOptions?: TType extends 'streamed' ? experimental_StreamedQueryOptions : never;
37
+ fnOptions?: TType extends 'streamed' ? experimental_SerializableStreamedQueryOptions : never;
13
38
  };
14
39
  type OperationKey<TType extends OperationType, TInput> = [path: readonly string[], options: OperationKeyOptions<TType, TInput>];
15
40
  declare const OPERATION_CONTEXT_SYMBOL: unique symbol;
@@ -39,10 +64,10 @@ interface QueryOptionsBase<TOutput, TError> {
39
64
  enabled?: boolean;
40
65
  }
41
66
  type experimental_StreamedKeyOptions<TInput> = QueryKeyOptions<TInput> & {
42
- queryFnOptions?: experimental_StreamedQueryOptions;
67
+ queryFnOptions?: experimental_SerializableStreamedQueryOptions;
43
68
  };
44
69
  type experimental_StreamedOptionsIn<TClientContext extends ClientContext, TInput, TOutput, TError, TSelectData> = QueryOptionsIn<TClientContext, TInput, TOutput, TError, TSelectData> & {
45
- queryFnOptions?: experimental_StreamedQueryOptions;
70
+ queryFnOptions?: experimental_SerializableStreamedQueryOptions;
46
71
  };
47
72
  interface experimental_StreamedOptionsBase<TOutput, TError> extends QueryOptionsBase<TOutput, TError> {
48
73
  }
@@ -174,5 +199,5 @@ interface CreateRouterUtilsOptions {
174
199
  */
175
200
  declare function createRouterUtils<T extends NestedClient<any>>(client: T, options?: CreateRouterUtilsOptions): RouterUtils<T>;
176
201
 
177
- export { OPERATION_CONTEXT_SYMBOL, OPERATION_CONTEXT_SYMBOL as TANSTACK_QUERY_OPERATION_CONTEXT_SYMBOL, createGeneralUtils, createProcedureUtils, createRouterUtils, createRouterUtils as createTanstackQueryUtils, generateOperationKey };
178
- export type { CreateProcedureUtilsOptions, CreateRouterUtilsOptions, GeneralUtils, InfiniteOptionsBase, InfiniteOptionsIn, MutationOptions, MutationOptionsIn, OperationContext, OperationKey, OperationKeyOptions, OperationType, ProcedureUtils, QueryKeyOptions, QueryOptionsBase, QueryOptionsIn, RouterUtils, OperationContext as TanstackQueryOperationContext, experimental_LiveQueryOutput, experimental_StreamedKeyOptions, experimental_StreamedOptionsBase, experimental_StreamedOptionsIn, experimental_StreamedQueryOutput };
202
+ export { OPERATION_CONTEXT_SYMBOL, OPERATION_CONTEXT_SYMBOL as TANSTACK_QUERY_OPERATION_CONTEXT_SYMBOL, createGeneralUtils, createProcedureUtils, createRouterUtils, createRouterUtils as createTanstackQueryUtils, experimental_serializableStreamedQuery, generateOperationKey };
203
+ export type { CreateProcedureUtilsOptions, CreateRouterUtilsOptions, GeneralUtils, InfiniteOptionsBase, InfiniteOptionsIn, MutationOptions, MutationOptionsIn, OperationContext, OperationKey, OperationKeyOptions, OperationType, ProcedureUtils, QueryKeyOptions, QueryOptionsBase, QueryOptionsIn, RouterUtils, OperationContext as TanstackQueryOperationContext, experimental_LiveQueryOutput, experimental_SerializableStreamedQueryOptions, experimental_StreamedKeyOptions, experimental_StreamedOptionsBase, experimental_StreamedOptionsIn, experimental_StreamedQueryOutput };
package/dist/index.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  import { stringifyJSON, isAsyncIteratorObject, toArray } from '@orpc/shared';
2
- import { skipToken, experimental_streamedQuery } from '@tanstack/query-core';
2
+ import { skipToken } from '@tanstack/query-core';
3
3
 
4
4
  function generateOperationKey(path, state = {}) {
5
5
  return [path, {
@@ -23,7 +23,7 @@ function experimental_liveQuery(queryFn) {
23
23
  let last;
24
24
  for await (const chunk of stream) {
25
25
  if (context.signal.aborted) {
26
- break;
26
+ throw context.signal.reason;
27
27
  }
28
28
  last = { chunk };
29
29
  context.client.setQueryData(context.queryKey, chunk);
@@ -37,6 +37,49 @@ function experimental_liveQuery(queryFn) {
37
37
  };
38
38
  }
39
39
 
40
+ function experimental_serializableStreamedQuery(queryFn, { refetchMode = "reset", maxChunks = Number.POSITIVE_INFINITY } = {}) {
41
+ return async (context) => {
42
+ const query = context.client.getQueryCache().find({ queryKey: context.queryKey, exact: true });
43
+ const isRefetch = !!query && query.state.data !== void 0;
44
+ if (isRefetch && refetchMode === "reset") {
45
+ query.setState({
46
+ status: "pending",
47
+ data: void 0,
48
+ error: null,
49
+ fetchStatus: "fetching"
50
+ });
51
+ }
52
+ let result = [];
53
+ const stream = await queryFn(context);
54
+ for await (const chunk of stream) {
55
+ if (context.signal.aborted) {
56
+ throw context.signal.reason;
57
+ }
58
+ if (!isRefetch || refetchMode !== "replace") {
59
+ context.client.setQueryData(
60
+ context.queryKey,
61
+ (prev = []) => {
62
+ return addToEnd(prev, chunk, maxChunks);
63
+ }
64
+ );
65
+ }
66
+ result = addToEnd(result, chunk, maxChunks);
67
+ }
68
+ if (isRefetch && refetchMode === "replace") {
69
+ context.client.setQueryData(context.queryKey, result);
70
+ }
71
+ const currentResult = context.client.getQueryData(context.queryKey);
72
+ if (!Array.isArray(currentResult)) {
73
+ return result;
74
+ }
75
+ return currentResult;
76
+ };
77
+ }
78
+ function addToEnd(items, item, max) {
79
+ const newItems = [...items, item];
80
+ return newItems.length > max ? newItems.slice(newItems.length - max) : newItems;
81
+ }
82
+
40
83
  const OPERATION_CONTEXT_SYMBOL = Symbol("ORPC_OPERATION_CONTEXT");
41
84
 
42
85
  function createProcedureUtils(client, options) {
@@ -76,8 +119,8 @@ function createProcedureUtils(client, options) {
76
119
  experimental_streamedOptions(...[optionsIn = {}]) {
77
120
  const queryKey = utils.experimental_streamedKey(optionsIn);
78
121
  return {
79
- queryFn: experimental_streamedQuery({
80
- queryFn: async ({ signal }) => {
122
+ queryFn: experimental_serializableStreamedQuery(
123
+ async ({ signal }) => {
81
124
  if (optionsIn.input === skipToken) {
82
125
  throw new Error("queryFn should not be called with skipToken used as input");
83
126
  }
@@ -96,8 +139,8 @@ function createProcedureUtils(client, options) {
96
139
  }
97
140
  return output;
98
141
  },
99
- ...optionsIn.queryFnOptions
100
- }),
142
+ optionsIn.queryFnOptions
143
+ ),
101
144
  ...optionsIn.input === skipToken ? { enabled: false } : {},
102
145
  ...optionsIn,
103
146
  queryKey
@@ -215,4 +258,4 @@ function createRouterUtils(client, options = {}) {
215
258
  return recursive;
216
259
  }
217
260
 
218
- export { OPERATION_CONTEXT_SYMBOL, OPERATION_CONTEXT_SYMBOL as TANSTACK_QUERY_OPERATION_CONTEXT_SYMBOL, createGeneralUtils, createProcedureUtils, createRouterUtils, createRouterUtils as createTanstackQueryUtils, generateOperationKey };
261
+ export { OPERATION_CONTEXT_SYMBOL, OPERATION_CONTEXT_SYMBOL as TANSTACK_QUERY_OPERATION_CONTEXT_SYMBOL, createGeneralUtils, createProcedureUtils, createRouterUtils, createRouterUtils as createTanstackQueryUtils, experimental_serializableStreamedQuery, generateOperationKey };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@orpc/tanstack-query",
3
3
  "type": "module",
4
- "version": "1.8.8",
4
+ "version": "1.9.0",
5
5
  "license": "MIT",
6
6
  "homepage": "https://orpc.unnoq.com",
7
7
  "repository": {
@@ -25,22 +25,22 @@
25
25
  ],
26
26
  "peerDependencies": {
27
27
  "@tanstack/query-core": ">=5.80.2",
28
- "@orpc/client": "1.8.8"
28
+ "@orpc/client": "1.9.0"
29
29
  },
30
30
  "dependencies": {
31
- "@orpc/shared": "1.8.8"
31
+ "@orpc/shared": "1.9.0"
32
32
  },
33
33
  "devDependencies": {
34
- "@angular/core": "^20.2.3",
35
- "@tanstack/angular-query-experimental": "^5.85.9",
36
- "@tanstack/query-core": "^5.85.9",
37
- "@tanstack/react-query": "^5.85.9",
38
- "@tanstack/solid-query": "^5.85.9",
39
- "@tanstack/svelte-query": "^5.85.9",
40
- "@tanstack/vue-query": "^5.85.9",
41
- "svelte": "^5.38.6",
34
+ "@angular/core": "^20.3.1",
35
+ "@tanstack/angular-query-experimental": "^5.90.1",
36
+ "@tanstack/query-core": "^5.90.1",
37
+ "@tanstack/react-query": "^5.90.1",
38
+ "@tanstack/solid-query": "^5.90.1",
39
+ "@tanstack/svelte-query": "^5.90.1",
40
+ "@tanstack/vue-query": "^5.90.1",
41
+ "svelte": "^5.39.4",
42
42
  "vue": "^3.5.21",
43
- "zod": "^4.1.5"
43
+ "zod": "^4.1.11"
44
44
  },
45
45
  "scripts": {
46
46
  "build": "unbuild",