@orpc/tanstack-query 2.0.0-beta.49 → 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.
@@ -1,12 +1,11 @@
1
- import type { ClientContext } from '@orpc/client';
2
- import type { ContractClientFactory, RouterContract, RouterContractClient } from '@orpc/contract';
3
- import type { ContractJsonifiedClientFactory, JsonifiedClient } from '@orpc/openapi';
4
- import type { RouterUtils, RouterUtilsOptions } from './router-utils.ts';
1
+ import { RouterUtils, RouterUtilsOptions } from "./router-utils.js";
2
+ import { ContractClientFactory, RouterContract, RouterContractClient } from "@orpc/contract";
3
+ import { ClientContext } from "@orpc/client";
4
+ import { ContractJsonifiedClientFactory, JsonifiedClient } from "@orpc/openapi";
5
5
  export interface ContractUtilsFactory<TClientContext extends ClientContext> {
6
- <T extends RouterContract>(contract: T): RouterUtils<RouterContractClient<T, TClientContext>>;
7
- }
8
- export interface ContractUtilsFactoryOptions<TClientContext extends ClientContext> extends Omit<RouterUtilsOptions<RouterContractClient<RouterContract, TClientContext>>, 'path'> {
6
+ <T extends RouterContract>(contract: T): RouterUtils<RouterContractClient<T, TClientContext>>;
9
7
  }
8
+ export interface ContractUtilsFactoryOptions<TClientContext extends ClientContext> extends Omit<RouterUtilsOptions<RouterContractClient<RouterContract, TClientContext>>, "path"> {}
10
9
  /**
11
10
  * Creates a factory that builds TanStack Query utils directly from a contract,
12
11
  * using the given contract client factory. Useful for scaling large projects
@@ -19,10 +18,9 @@ export interface ContractUtilsFactoryOptions<TClientContext extends ClientContex
19
18
  */
20
19
  export declare function createContractUtilsFactory<TClientContext extends ClientContext>(clientFactory: ContractClientFactory<TClientContext>, options: ContractUtilsFactoryOptions<TClientContext>): ContractUtilsFactory<TClientContext>;
21
20
  export interface ContractJsonifiedUtilsFactory<TClientContext extends ClientContext> {
22
- <T extends RouterContract>(contract: T): RouterUtils<JsonifiedClient<RouterContractClient<T, TClientContext>>>;
23
- }
24
- export interface ContractJsonifiedUtilsFactoryOptions<TClientContext extends ClientContext> extends Omit<RouterUtilsOptions<JsonifiedClient<RouterContractClient<RouterContract, TClientContext>>>, 'path'> {
21
+ <T extends RouterContract>(contract: T): RouterUtils<JsonifiedClient<RouterContractClient<T, TClientContext>>>;
25
22
  }
23
+ export interface ContractJsonifiedUtilsFactoryOptions<TClientContext extends ClientContext> extends Omit<RouterUtilsOptions<JsonifiedClient<RouterContractClient<RouterContract, TClientContext>>>, "path"> {}
26
24
  /**
27
25
  * Like `createContractUtilsFactory`, but for clients whose types are wrapped in `JsonifiedClient`,
28
26
  * such as clients using OpenAPI Link.
@@ -34,4 +32,4 @@ export interface ContractJsonifiedUtilsFactoryOptions<TClientContext extends Cli
34
32
  *
35
33
  * @see {@link https://orpc.dev/docs/contract/client-factory#tanstack-query-integration | Contract Client Factory - TanStack Query Integration}
36
34
  */
37
- export declare function createContractJsonifiedUtilsFactory<TClientContext extends ClientContext>(clientFactory: ContractJsonifiedClientFactory<TClientContext>, options: ContractJsonifiedUtilsFactoryOptions<TClientContext>): ContractJsonifiedUtilsFactory<TClientContext>;
35
+ export declare function createContractJsonifiedUtilsFactory<TClientContext extends ClientContext>(clientFactory: ContractJsonifiedClientFactory<TClientContext>, options: ContractJsonifiedUtilsFactoryOptions<TClientContext>): ContractJsonifiedUtilsFactory<TClientContext>;
@@ -1,42 +1,42 @@
1
- import { resolveBasePathMeta } from '@orpc/contract';
2
- import { get } from '@orpc/shared';
3
- import { createRouterUtils } from './router-utils.js';
1
+ import { createRouterUtils } from "./router-utils.js";
2
+ import { resolveBasePathMeta } from "@orpc/contract";
3
+ import { get } from "@orpc/shared";
4
+ // src/contract-utils.ts
4
5
  /**
5
- * Creates a factory that builds TanStack Query utils directly from a contract,
6
- * using the given contract client factory. Useful for scaling large projects
7
- * where each part only needs a slice of the router.
8
- *
9
- * @remarks
10
- * **Note**: The contract must define a `path` meta matching its position in the root router contract.
11
- *
12
- * @see {@link https://orpc.dev/docs/contract/client-factory#tanstack-query-integration | Contract Client Factory - TanStack Query Integration}
13
- */
14
- export function createContractUtilsFactory(clientFactory, options) {
15
- const factory = (contract) => {
16
- const client = clientFactory(contract);
17
- const path = resolveBasePathMeta(contract);
18
- if (path === undefined) {
19
- throw new TypeError('ContractUtilsFactory: procedure contract must define `meta.path` that matches its path in the root router contract.');
20
- }
21
- return createRouterUtils(client, {
22
- ...options,
23
- path,
24
- scoped: get(options.scoped, path),
25
- });
26
- };
27
- return factory;
6
+ * Creates a factory that builds TanStack Query utils directly from a contract,
7
+ * using the given contract client factory. Useful for scaling large projects
8
+ * where each part only needs a slice of the router.
9
+ *
10
+ * @remarks
11
+ * **Note**: The contract must define a `path` meta matching its position in the root router contract.
12
+ *
13
+ * @see {@link https://orpc.dev/docs/contract/client-factory#tanstack-query-integration | Contract Client Factory - TanStack Query Integration}
14
+ */
15
+ function createContractUtilsFactory(clientFactory, options) {
16
+ const factory = (contract) => {
17
+ const client = clientFactory(contract);
18
+ const path = resolveBasePathMeta(contract);
19
+ if (path === void 0) throw new TypeError("ContractUtilsFactory: procedure contract must define `meta.path` that matches its path in the root router contract.");
20
+ return createRouterUtils(client, {
21
+ ...options,
22
+ path,
23
+ scoped: get(options.scoped, path)
24
+ });
25
+ };
26
+ return factory;
28
27
  }
29
28
  /**
30
- * Like `createContractUtilsFactory`, but for clients whose types are wrapped in `JsonifiedClient`,
31
- * such as clients using OpenAPI Link.
32
- *
33
- * @remarks
34
- * Takes a client factory created by `createContractJsonifiedClientFactory` from `@orpc/openapi`.
35
- *
36
- * **Note**: The contract must define a `path` meta matching its position in the root router contract.
37
- *
38
- * @see {@link https://orpc.dev/docs/contract/client-factory#tanstack-query-integration | Contract Client Factory - TanStack Query Integration}
39
- */
40
- export function createContractJsonifiedUtilsFactory(clientFactory, options) {
41
- return createContractUtilsFactory(clientFactory, options);
29
+ * Like `createContractUtilsFactory`, but for clients whose types are wrapped in `JsonifiedClient`,
30
+ * such as clients using OpenAPI Link.
31
+ *
32
+ * @remarks
33
+ * Takes a client factory created by `createContractJsonifiedClientFactory` from `@orpc/openapi`.
34
+ *
35
+ * **Note**: The contract must define a `path` meta matching its position in the root router contract.
36
+ *
37
+ * @see {@link https://orpc.dev/docs/contract/client-factory#tanstack-query-integration | Contract Client Factory - TanStack Query Integration}
38
+ */
39
+ function createContractJsonifiedUtilsFactory(clientFactory, options) {
40
+ return createContractUtilsFactory(clientFactory, options);
42
41
  }
42
+ export { createContractJsonifiedUtilsFactory, createContractUtilsFactory };
package/dist/index.d.ts CHANGED
@@ -1,16 +1,14 @@
1
- export * from './contract-utils.ts';
2
- export type { OperationKey, OperationKeyBaseOptions, OperationKeyOptions, OperationKeyPrefixOptions, } from './key.ts';
3
- export * from './meta.ts';
4
- export type { RouterUtilsPlugin } from './plugin.ts';
5
- export { mergeProcedureUtilsOptions, ProcedureUtils } from './procedure-utils.ts';
6
- export type { ProcedureUtilsInfiniteInterceptor, ProcedureUtilsInfiniteInterceptorOptions, ProcedureUtilsLiveInterceptor, ProcedureUtilsLiveInterceptorOptions, ProcedureUtilsModifier, ProcedureUtilsMutationInterceptor, ProcedureUtilsMutationInterceptorOptions, ProcedureUtilsOptions, ProcedureUtilsQueryInterceptor, ProcedureUtilsQueryInterceptorOptions, ProcedureUtilsStreamedInterceptor, ProcedureUtilsStreamedInterceptorOptions, } from './procedure-utils.ts';
7
- export * from './router-utils.ts';
8
- export { createRouterUtils as createTanstackQueryUtils } from './router-utils.ts';
9
- export { SharedUtils } from './shared-utils.ts';
10
- export * from './ssr.ts';
11
- export type { SerializableStreamedQueryOptions, SettleOnFirstDataOptions } from './stream-query.ts';
12
- export * from './structural-sharing.ts';
13
- export * from './types.ts';
14
- export { OPERATION_CONTEXT_SYMBOL as TANSTACK_QUERY_OPERATION_CONTEXT_SYMBOL, type OperationContext as TanstackQueryOperationContext, } from './types.ts';
15
- export type { AnyNestedClient } from '@orpc/client';
16
- export type { Public } from '@orpc/shared';
1
+ import { ContractJsonifiedUtilsFactory, ContractJsonifiedUtilsFactoryOptions, ContractUtilsFactory, ContractUtilsFactoryOptions, createContractJsonifiedUtilsFactory, createContractUtilsFactory } from "./contract-utils.js";
2
+ import { OperationKey, OperationKeyBaseOptions, OperationKeyOptions, OperationKeyPrefixOptions } from "./key.js";
3
+ import { ContractOptionsUtilsPlugin, TanstackQueryMetaOptions, TanstackQueryMetaPlugin, getTanstackQueryMeta, tanstackQuery } from "./meta.js";
4
+ import { RouterUtilsPlugin } from "./plugin.js";
5
+ import { ProcedureUtils, ProcedureUtilsInfiniteInterceptor, ProcedureUtilsInfiniteInterceptorOptions, ProcedureUtilsLiveInterceptor, ProcedureUtilsLiveInterceptorOptions, ProcedureUtilsModifier, ProcedureUtilsMutationInterceptor, ProcedureUtilsMutationInterceptorOptions, ProcedureUtilsOptions, ProcedureUtilsQueryInterceptor, ProcedureUtilsQueryInterceptorOptions, ProcedureUtilsStreamedInterceptor, ProcedureUtilsStreamedInterceptorOptions, mergeProcedureUtilsOptions } from "./procedure-utils.js";
6
+ import { RouterUtils, RouterUtilsOptions, RouterUtilsScoped, createRouterUtils } from "./router-utils.js";
7
+ import { SharedUtils } from "./shared-utils.js";
8
+ import { StreamQuerySSRUtilsPlugin, StreamQuerySSRUtilsPluginOptions, createHydrationOptions } from "./ssr.js";
9
+ import { SerializableStreamedQueryOptions, SettleOnFirstDataOptions } from "./stream-query.js";
10
+ import { StructuralSharing, createStructuralSharing } from "./structural-sharing.js";
11
+ import { InferLiveQueryOutput, InferStreamedQueryOutput, InfiniteKeyOptions, InfiniteOptionsIn, InfiniteOptionsOut, LiveOptionsIn, MutationKeyOptions, MutationOptionsIn, MutationOptionsOut, OPERATION_CONTEXT_SYMBOL, OperationContext, OperationType, QueryKeyOptions, QueryOptionsIn, QueryOptionsOut, StreamedKeyOptions, StreamedOptionsIn, StreamedOptionsOut } from "./types.js";
12
+ import { Public } from "@orpc/shared";
13
+ import { AnyNestedClient } from "@orpc/client";
14
+ export { type AnyNestedClient, ContractJsonifiedUtilsFactory, ContractJsonifiedUtilsFactoryOptions, ContractOptionsUtilsPlugin, ContractUtilsFactory, ContractUtilsFactoryOptions, InferLiveQueryOutput, InferStreamedQueryOutput, InfiniteKeyOptions, InfiniteOptionsIn, InfiniteOptionsOut, LiveOptionsIn, MutationKeyOptions, MutationOptionsIn, MutationOptionsOut, OPERATION_CONTEXT_SYMBOL, OperationContext, type OperationKey, type OperationKeyBaseOptions, type OperationKeyOptions, type OperationKeyPrefixOptions, OperationType, ProcedureUtils, type ProcedureUtilsInfiniteInterceptor, type ProcedureUtilsInfiniteInterceptorOptions, type ProcedureUtilsLiveInterceptor, type ProcedureUtilsLiveInterceptorOptions, type ProcedureUtilsModifier, type ProcedureUtilsMutationInterceptor, type ProcedureUtilsMutationInterceptorOptions, type ProcedureUtilsOptions, type ProcedureUtilsQueryInterceptor, type ProcedureUtilsQueryInterceptorOptions, type ProcedureUtilsStreamedInterceptor, type ProcedureUtilsStreamedInterceptorOptions, type Public, QueryKeyOptions, QueryOptionsIn, QueryOptionsOut, RouterUtils, RouterUtilsOptions, type RouterUtilsPlugin, RouterUtilsScoped, type SerializableStreamedQueryOptions, type SettleOnFirstDataOptions, SharedUtils, StreamQuerySSRUtilsPlugin, StreamQuerySSRUtilsPluginOptions, StreamedKeyOptions, StreamedOptionsIn, StreamedOptionsOut, StructuralSharing, OPERATION_CONTEXT_SYMBOL as TANSTACK_QUERY_OPERATION_CONTEXT_SYMBOL, TanstackQueryMetaOptions, TanstackQueryMetaPlugin, type OperationContext as TanstackQueryOperationContext, createContractJsonifiedUtilsFactory, createContractUtilsFactory, createHydrationOptions, createRouterUtils, createStructuralSharing, createRouterUtils as createTanstackQueryUtils, getTanstackQueryMeta, mergeProcedureUtilsOptions, tanstackQuery };
package/dist/index.js CHANGED
@@ -1,10 +1,8 @@
1
- export * from './contract-utils.js';
2
- export * from './meta.js';
3
- export { mergeProcedureUtilsOptions, ProcedureUtils } from './procedure-utils.js';
4
- export * from './router-utils.js';
5
- export { createRouterUtils as createTanstackQueryUtils } from './router-utils.js';
6
- export { SharedUtils } from './shared-utils.js';
7
- export * from './ssr.js';
8
- export * from './structural-sharing.js';
9
- export * from './types.js';
10
- export { OPERATION_CONTEXT_SYMBOL as TANSTACK_QUERY_OPERATION_CONTEXT_SYMBOL, } from './types.js';
1
+ export { createContractJsonifiedUtilsFactory, createContractUtilsFactory } from "./contract-utils.js";
2
+ export { ContractOptionsUtilsPlugin, getTanstackQueryMeta, tanstackQuery } from "./meta.js";
3
+ export { ProcedureUtils, mergeProcedureUtilsOptions } from "./procedure-utils.js";
4
+ export { createRouterUtils, createRouterUtils as createTanstackQueryUtils } from "./router-utils.js";
5
+ export { SharedUtils } from "./shared-utils.js";
6
+ export { StreamQuerySSRUtilsPlugin, createHydrationOptions } from "./ssr.js";
7
+ export { createStructuralSharing } from "./structural-sharing.js";
8
+ export { OPERATION_CONTEXT_SYMBOL, OPERATION_CONTEXT_SYMBOL as TANSTACK_QUERY_OPERATION_CONTEXT_SYMBOL } from "./types.js";
package/dist/key.d.ts CHANGED
@@ -1,50 +1,41 @@
1
- import { RPCJsonSerializer } from '@orpc/client';
2
- import type { PartialDeep, Public } from '@orpc/shared';
3
- import type { SerializableStreamedQueryOptions } from './stream-query.ts';
4
- import type { OperationType } from './types.ts';
1
+ import { SerializableStreamedQueryOptions } from "./stream-query.js";
2
+ import { OperationType } from "./types.js";
3
+ import { PartialDeep, Public } from "@orpc/shared";
4
+ import { RPCJsonSerializer } from "@orpc/client";
5
5
  export interface OperationKeyPrefixOptions {
6
- /**
7
- * Prepended as the first element of the key when present.
8
- * Use this to avoid key conflicts when multiple router utils share the same client.
9
- */
10
- prefix?: string | undefined;
6
+ /**
7
+ * Prepended as the first element of the key when present.
8
+ * Use this to avoid key conflicts when multiple router utils share the same client.
9
+ */
10
+ prefix?: string | undefined;
11
11
  }
12
12
  export interface OperationKeyBaseOptions extends OperationKeyPrefixOptions {
13
- /**
14
- * Serializes inputs into JSON-compatible values when building keys,
15
- * and compares custom types in structural sharing.
16
- * Pass the serializer your link uses so custom types are supported.
17
- *
18
- * @default new RPCJsonSerializer()
19
- */
20
- serializer?: Public<RPCJsonSerializer> | undefined;
13
+ /**
14
+ * Serializes inputs into JSON-compatible values when building keys,
15
+ * and compares custom types in structural sharing.
16
+ * Pass the serializer your link uses so custom types are supported.
17
+ *
18
+ * @default new RPCJsonSerializer()
19
+ */
20
+ serializer?: Public<RPCJsonSerializer> | undefined;
21
21
  }
22
22
  export type OperationKeyOptions<TType extends OperationType, TInput> = OperationKeyBaseOptions & {
23
- type?: TType | undefined;
24
- input?: (TType extends 'mutation' ? never : PartialDeep<TInput>) | undefined;
25
- fnOptions?: (TType extends 'streamed' ? SerializableStreamedQueryOptions : never) | undefined;
23
+ type?: TType | undefined;
24
+ input?: (TType extends "mutation" ? never : PartialDeep<TInput>) | undefined;
25
+ fnOptions?: (TType extends "streamed" ? SerializableStreamedQueryOptions : never) | undefined;
26
26
  };
27
27
  type OperationKeyContent<TType extends OperationType, TInput> = Omit<OperationKeyOptions<TType, TInput>, keyof OperationKeyBaseOptions> & {
28
- /**
29
- * Set in place of `input` when the operation is disabled with `skipToken`,
30
- * because query key hashers drop symbols.
31
- */
32
- skipToken?: TType extends 'mutation' ? never : true;
33
- /**
34
- * Serializer types of non-JSON values in `input`, keyed by their JSON-encoded path,
35
- * so inputs with the same JSON form (like a `Date` and its ISO string) produce different keys.
36
- * Keyed by path (not a list) so partial keys still match.
37
- */
38
- inputMeta?: Record<string, string[]> | undefined;
28
+ /**
29
+ * Set in place of `input` when the operation is disabled with `skipToken`,
30
+ * because query key hashers drop symbols.
31
+ */
32
+ skipToken?: TType extends "mutation" ? never : true;
33
+ /**
34
+ * Serializer types of non-JSON values in `input`, keyed by their JSON-encoded path,
35
+ * so inputs with the same JSON form (like a `Date` and its ISO string) produce different keys.
36
+ * Keyed by path (not a list) so partial keys still match.
37
+ */
38
+ inputMeta?: Record<string, string[]> | undefined;
39
39
  };
40
40
  export type OperationKey<TType extends OperationType, TInput> = [path: string[], options: OperationKeyContent<TType, TInput>] | [prefix: string, path: string[], options: OperationKeyContent<TType, TInput>];
41
- /**
42
- * Generate a TanStack Query key for a procedure.
43
- *
44
- * The input is serialized to JSON-compatible values, so keys hash and partially match
45
- * by value, including types like `Date`, `Set`, `Map` and custom serializer types.
46
- *
47
- * @see {@link https://orpc.dev/docs/integrations/tanstack-query#key-serializer | TanStack Query Integration - Key Serializer}
48
- */
49
- export declare function generateOperationKey<TType extends OperationType, TInput>(path: string[], options?: OperationKeyOptions<TType, TInput>): OperationKey<TType, TInput>;
50
- export {};
41
+ export {};
package/dist/key.js CHANGED
@@ -1,50 +1,46 @@
1
- import { RPCJsonSerializer } from '@orpc/client';
2
- import { stringifyJSON } from '@orpc/shared';
3
- import { skipToken } from '@tanstack/query-core';
1
+ import { stringifyJSON } from "@orpc/shared";
2
+ import { RPCJsonSerializer } from "@orpc/client";
3
+ import { skipToken } from "@tanstack/query-core";
4
+ // src/key.ts
4
5
  const defaultSerializer = /* @__PURE__ */ new RPCJsonSerializer();
5
6
  /**
6
- * Generate a TanStack Query key for a procedure.
7
- *
8
- * The input is serialized to JSON-compatible values, so keys hash and partially match
9
- * by value, including types like `Date`, `Set`, `Map` and custom serializer types.
10
- *
11
- * @see {@link https://orpc.dev/docs/integrations/tanstack-query#key-serializer | TanStack Query Integration - Key Serializer}
12
- */
13
- export function generateOperationKey(path, options = {}) {
14
- return [
15
- ...(options.prefix !== undefined ? [options.prefix] : []),
16
- path,
17
- {
18
- ...(options.input === skipToken
19
- ? { skipToken: true }
20
- : options.input !== undefined
21
- ? serializeInput(options.serializer ?? defaultSerializer, options.input)
22
- : {}),
23
- ...(options.type !== undefined ? { type: options.type } : {}),
24
- ...streamedKeyOptions(options.fnOptions),
25
- },
26
- ];
7
+ * Generate a TanStack Query key for a procedure.
8
+ *
9
+ * The input is serialized to JSON-compatible values, so keys hash and partially match
10
+ * by value, including types like `Date`, `Set`, `Map` and custom serializer types.
11
+ *
12
+ * @see {@link https://orpc.dev/docs/integrations/tanstack-query#key-serializer | TanStack Query Integration - Key Serializer}
13
+ */
14
+ function generateOperationKey(path, options = {}) {
15
+ return [
16
+ ...options.prefix !== void 0 ? [options.prefix] : [],
17
+ path,
18
+ {
19
+ ...options.input === skipToken ? { skipToken: true } : options.input !== void 0 ? serializeInput(options.serializer ?? defaultSerializer, options.input) : {},
20
+ ...options.type !== void 0 ? { type: options.type } : {},
21
+ ...streamedKeyOptions(options.fnOptions)
22
+ }
23
+ ];
27
24
  }
28
25
  function serializeInput(serializer, input) {
29
- const { json, meta } = serializer.serializeJson(input);
30
- if (!meta?.length) {
31
- return { input: json };
32
- }
33
- const inputMeta = {};
34
- for (const [type, ...segments] of meta) {
35
- ;
36
- (inputMeta[stringifyJSON(segments)] ??= []).push(type);
37
- }
38
- return { input: json, inputMeta };
26
+ const { json, meta } = serializer.serializeJson(input);
27
+ if (!meta?.length) return { input: json };
28
+ const inputMeta = {};
29
+ for (const [type, ...segments] of meta) (inputMeta[stringifyJSON(segments)] ??= []).push(type);
30
+ return {
31
+ input: json,
32
+ inputMeta
33
+ };
39
34
  }
40
35
  /**
41
- * Leaves out default streamed options, so passing a default gives the same key as omitting it.
42
- */
36
+ * Leaves out default streamed options, so passing a default gives the same key as omitting it.
37
+ */
43
38
  function streamedKeyOptions(options) {
44
- const { refetchMode, maxChunks } = options ?? {};
45
- const fnOptions = {
46
- ...(refetchMode !== undefined && refetchMode !== 'reset' ? { refetchMode } : {}),
47
- ...(maxChunks !== undefined && maxChunks !== Number.POSITIVE_INFINITY ? { maxChunks } : {}),
48
- };
49
- return Object.keys(fnOptions).length > 0 ? { fnOptions } : {};
39
+ const { refetchMode, maxChunks } = options ?? {};
40
+ const fnOptions = {
41
+ ...refetchMode !== void 0 && refetchMode !== "reset" ? { refetchMode } : {},
42
+ ...maxChunks !== void 0 && maxChunks !== Number.POSITIVE_INFINITY ? { maxChunks } : {}
43
+ };
44
+ return Object.keys(fnOptions).length > 0 ? { fnOptions } : {};
50
45
  }
46
+ export { generateOperationKey };
@@ -1,36 +1,30 @@
1
- import { stringifyJSON, throwIfAborted } from '@orpc/shared';
2
- export function liveQuery(queryFn, { settleOnFirstData = false } = {}) {
3
- return async (context) => {
4
- const { client, queryKey, signal } = context;
5
- // a stream cut short (cancelled, or its last observer left) reopens when observed again;
6
- // marked at abort, so the writes of a fetch that replaces this one clear it
7
- const markStale = () => client.getQueryCache().find({ queryKey, exact: true })?.invalidate();
8
- if (signal.aborted) {
9
- markStale();
10
- }
11
- signal.addEventListener('abort', markStale, { once: true });
12
- try {
13
- const stream = await queryFn(context);
14
- if (signal.aborted) {
15
- await stream[Symbol.asyncIterator]().return?.();
16
- throw signal.reason;
17
- }
18
- let last;
19
- for await (const chunk of stream) {
20
- throwIfAborted(signal);
21
- last = { chunk };
22
- client.setQueryData(queryKey, chunk);
23
- if (settleOnFirstData) {
24
- break;
25
- }
26
- }
27
- if (!last) {
28
- throw new TypeError(`Live query for ${stringifyJSON(queryKey)} did not yield any data. Ensure the query function returns an AsyncIterable with at least one chunk.`);
29
- }
30
- return last.chunk;
31
- }
32
- finally {
33
- signal.removeEventListener('abort', markStale);
34
- }
35
- };
1
+ import { listenAbort, stringifyJSON, throwIfAborted } from "@orpc/shared";
2
+ // src/live-query.ts
3
+ function liveQuery(queryFn, { settleOnFirstData = false } = {}) {
4
+ return async (context) => {
5
+ const { client, queryKey, signal } = context;
6
+ const unlisten = listenAbort(signal, () => client.getQueryCache().find({
7
+ queryKey,
8
+ exact: true
9
+ })?.invalidate());
10
+ try {
11
+ const stream = await queryFn(context);
12
+ if (signal.aborted) {
13
+ await stream[Symbol.asyncIterator]().return?.();
14
+ throw signal.reason;
15
+ }
16
+ let last;
17
+ for await (const chunk of stream) {
18
+ throwIfAborted(signal);
19
+ last = { chunk };
20
+ client.setQueryData(queryKey, chunk);
21
+ if (settleOnFirstData) break;
22
+ }
23
+ if (!last) throw new TypeError(`Live query for ${stringifyJSON(queryKey)} did not yield any data. Ensure the query function returns an AsyncIterable with at least one chunk.`);
24
+ return last.chunk;
25
+ } finally {
26
+ unlisten();
27
+ }
28
+ };
36
29
  }
30
+ export { liveQuery };
package/dist/meta.d.ts CHANGED
@@ -1,12 +1,12 @@
1
- import type { AnyNestedClient, ClientContext, InferClientContext, InferClientError } from '@orpc/client';
2
- import type { AnySchema, ErrorMap, InferSchemaInput, InferSchemaOutput, Meta, MetaPlugin, ORPCErrorFromErrorMap, RouterContract } from '@orpc/contract';
3
- import type { ThrowableError } from '@orpc/shared';
4
- import type { OperationKeyBaseOptions } from './key.ts';
5
- import type { RouterUtilsPlugin } from './plugin.ts';
6
- import type { ProcedureUtilsOptions } from './procedure-utils.ts';
1
+ import { OperationKeyBaseOptions } from "./key.js";
2
+ import { RouterUtilsPlugin } from "./plugin.js";
3
+ import { ProcedureUtilsOptions } from "./procedure-utils.js";
4
+ import { AnySchema, ErrorMap, InferSchemaInput, InferSchemaOutput, Meta, MetaPlugin, ORPCErrorFromErrorMap, RouterContract } from "@orpc/contract";
5
+ import { ThrowableError } from "@orpc/shared";
6
+ import { AnyNestedClient, ClientContext, InferClientContext, InferClientError } from "@orpc/client";
7
7
  export type TanstackQueryMetaOptions<TClientContext extends ClientContext, TInput, TOutput, TError> = Omit<ProcedureUtilsOptions<TClientContext, TInput, TOutput, TError>, keyof OperationKeyBaseOptions>;
8
8
  export interface TanstackQueryMetaPlugin<TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap> extends MetaPlugin<TInputSchema, TOutputSchema, TErrorMap> {
9
- name: '~tanstack-query';
9
+ name: "~tanstack-query";
10
10
  }
11
11
  /**
12
12
  * Define base TanStack Query options and interceptors on a procedure contract.
@@ -20,9 +20,9 @@ export interface TanstackQueryMetaPlugin<TInputSchema extends AnySchema, TOutput
20
20
  */
21
21
  export declare function tanstackQuery<TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap>(options: TanstackQueryMetaOptions<ClientContext, InferSchemaInput<TInputSchema>, InferSchemaOutput<TOutputSchema>, ORPCErrorFromErrorMap<TErrorMap> | ThrowableError>): TanstackQueryMetaPlugin<TInputSchema, TOutputSchema, TErrorMap>;
22
22
  export declare function getTanstackQueryMeta(procedureOrLazy: {
23
- '~orpc': {
24
- meta: Meta;
25
- };
23
+ "~orpc": {
24
+ meta: Meta;
25
+ };
26
26
  }): TanstackQueryMetaOptions<ClientContext, unknown, unknown, ThrowableError> | undefined;
27
27
  /**
28
28
  * Router utils plugin that applies base options defined via {@link tanstackQuery}
@@ -35,8 +35,8 @@ export declare function getTanstackQueryMeta(procedureOrLazy: {
35
35
  * @see {@link https://orpc.dev/docs/integrations/tanstack-query#contract-options-plugin | TanStack Query Integration - Contract Options Plugin}
36
36
  */
37
37
  export declare class ContractOptionsUtilsPlugin<T extends AnyNestedClient = AnyNestedClient> implements RouterUtilsPlugin<T> {
38
- private readonly contract;
39
- readonly name = "~contract-options";
40
- constructor(contract: RouterContract);
41
- initProcedureOptions(path: string[], options: ProcedureUtilsOptions<InferClientContext<T>, any, any, InferClientError<T>>): ProcedureUtilsOptions<InferClientContext<T>, any, any, InferClientError<T>>;
42
- }
38
+ private readonly contract;
39
+ readonly name = "~contract-options";
40
+ constructor(contract: RouterContract);
41
+ initProcedureOptions(path: string[], options: ProcedureUtilsOptions<InferClientContext<T>, any, any, InferClientError<T>>): ProcedureUtilsOptions<InferClientContext<T>, any, any, InferClientError<T>>;
42
+ }
package/dist/meta.js CHANGED
@@ -1,55 +1,53 @@
1
- import { getRouterContract, ProcedureContract } from '@orpc/contract';
2
- import { mergeProcedureUtilsOptions } from './procedure-utils.js';
1
+ import { mergeProcedureUtilsOptions } from "./procedure-utils.js";
2
+ import { ProcedureContract, getRouterContract } from "@orpc/contract";
3
+ // src/meta.ts
3
4
  /**
4
- * Define base TanStack Query options and interceptors on a procedure contract.
5
- * Applied multiple times, later options are spread-merged with higher priority
6
- * while interceptors are concatenated. A key explicitly set to `undefined`
7
- * resets the value from earlier applications instead of merging.
8
- *
9
- * Apply them to router utils with {@link ContractOptionsUtilsPlugin}.
10
- *
11
- * @see {@link https://orpc.dev/docs/integrations/tanstack-query#contract-options-plugin | TanStack Query Integration - Contract Options Plugin}
12
- */
13
- export function tanstackQuery(options) {
14
- return {
15
- name: '~tanstack-query',
16
- init(meta) {
17
- const current = meta['~tanstack-query'];
18
- return {
19
- ...meta,
20
- '~tanstack-query': current ? mergeProcedureUtilsOptions(current, options) : options,
21
- };
22
- },
23
- };
5
+ * Define base TanStack Query options and interceptors on a procedure contract.
6
+ * Applied multiple times, later options are spread-merged with higher priority
7
+ * while interceptors are concatenated. A key explicitly set to `undefined`
8
+ * resets the value from earlier applications instead of merging.
9
+ *
10
+ * Apply them to router utils with {@link ContractOptionsUtilsPlugin}.
11
+ *
12
+ * @see {@link https://orpc.dev/docs/integrations/tanstack-query#contract-options-plugin | TanStack Query Integration - Contract Options Plugin}
13
+ */
14
+ function tanstackQuery(options) {
15
+ return {
16
+ name: "~tanstack-query",
17
+ init(meta) {
18
+ const current = meta["~tanstack-query"];
19
+ return {
20
+ ...meta,
21
+ "~tanstack-query": current ? mergeProcedureUtilsOptions(current, options) : options
22
+ };
23
+ }
24
+ };
24
25
  }
25
- export function getTanstackQueryMeta(procedureOrLazy) {
26
- return procedureOrLazy['~orpc'].meta['~tanstack-query'];
26
+ function getTanstackQueryMeta(procedureOrLazy) {
27
+ return procedureOrLazy["~orpc"].meta["~tanstack-query"];
27
28
  }
28
29
  /**
29
- * Router utils plugin that applies base options defined via {@link tanstackQuery}
30
- * on the given router contract. Meta options act as the base layer: options defined
31
- * on the utils override them, and utils interceptors run after meta interceptors.
32
- *
33
- * The contract shape must match the client the utils are created from,
34
- * so pass the root router contract when utils paths start from the root.
35
- *
36
- * @see {@link https://orpc.dev/docs/integrations/tanstack-query#contract-options-plugin | TanStack Query Integration - Contract Options Plugin}
37
- */
38
- export class ContractOptionsUtilsPlugin {
39
- contract;
40
- name = '~contract-options';
41
- constructor(contract) {
42
- this.contract = contract;
43
- }
44
- initProcedureOptions(path, options) {
45
- const procedure = getRouterContract(this.contract, path);
46
- if (!(procedure instanceof ProcedureContract)) {
47
- return options;
48
- }
49
- const base = getTanstackQueryMeta(procedure);
50
- if (!base) {
51
- return options;
52
- }
53
- return mergeProcedureUtilsOptions(base, options);
54
- }
55
- }
30
+ * Router utils plugin that applies base options defined via {@link tanstackQuery}
31
+ * on the given router contract. Meta options act as the base layer: options defined
32
+ * on the utils override them, and utils interceptors run after meta interceptors.
33
+ *
34
+ * The contract shape must match the client the utils are created from,
35
+ * so pass the root router contract when utils paths start from the root.
36
+ *
37
+ * @see {@link https://orpc.dev/docs/integrations/tanstack-query#contract-options-plugin | TanStack Query Integration - Contract Options Plugin}
38
+ */
39
+ var ContractOptionsUtilsPlugin = class {
40
+ contract;
41
+ name = "~contract-options";
42
+ constructor(contract) {
43
+ this.contract = contract;
44
+ }
45
+ initProcedureOptions(path, options) {
46
+ const procedure = getRouterContract(this.contract, path);
47
+ if (!(procedure instanceof ProcedureContract)) return options;
48
+ const base = getTanstackQueryMeta(procedure);
49
+ if (!base) return options;
50
+ return mergeProcedureUtilsOptions(base, options);
51
+ }
52
+ };
53
+ export { ContractOptionsUtilsPlugin, getTanstackQueryMeta, tanstackQuery };