import { PartialDeep, MaybeOptionalOptions, Public } from '@orpc/shared'; import { ClientContext, Client, AnyNestedClient } from '@orpc/client'; import { SWRSubscriptionOptions } from 'swr/subscription'; interface OperationKeyPrefixOptions { /** * Prepended as the first element of the key when present. * Use this to avoid key conflicts when multiple router utils share the same client. */ prefix?: string; } type OperationKeyOptions = OperationKeyPrefixOptions & { input?: PartialDeep; }; type OperationKey = [path: string[], options: Omit, 'prefix'>] | [prefix: string, path: string[], options: Omit, 'prefix'>]; /** * Generate a **full matching** key for SWR operations. * * @see {@link https://orpc.dev/docs/integrations/swr#data-fetching SWR Key Docs} */ declare function generateOperationKey(path: string[], options?: OperationKeyOptions): OperationKey; type InferSubscriberOutput = TOutput extends AsyncIterable ? U[] : never; type InferLiveSubscriberOutput = TOutput extends AsyncIterable ? U : never; type OperationType = 'fetcher' | 'mutator' | 'subscriber' | 'liveSubscriber'; declare const OPERATION_CONTEXT_SYMBOL: unique symbol; interface OperationContext { [OPERATION_CONTEXT_SYMBOL]?: { key: unknown; type: OperationType; }; } type KeyOptions = undefined extends TInput ? { input?: TInput; } : { input: TInput; }; type MatcherStrategy = 'exact' | 'partial'; type MatcherOptions = ('partial' extends TStrategy ? { input?: PartialDeep; } : undefined extends TInput ? { input?: TInput; } : { input: TInput; }) & { strategy?: TStrategy; }; type Matcher = (key?: unknown) => boolean; type FetcherOptions = object extends TClientContext ? { context?: TClientContext; } : { context: TClientContext; }; type SubscriberOptions = FetcherOptions & { /** * Determines how data is handled when the subscription is subscribed again * * - `'reset'`: Clears existing data before streaming new data. * - `'append'`: Adds new streamed chunks to the existing data. * - `'replace'`: Buffers streamed data and replaces existing data after the stream completes. * * @default 'reset' */ refetchMode?: 'append' | 'reset' | 'replace'; /** * Maximum number of chunks to store. * When exceeded, the oldest chunks will be removed. */ maxChunks?: number; }; type MutatorOptions = FetcherOptions; type Fetcher = (key: OperationKey) => Promise; type Mutator = (key: unknown, options: Readonly<{ arg: TInput; }>) => Promise; type Subscriber = (key: OperationKey, options: SWRSubscriptionOptions) => (() => void); declare class SharedUtils { protected readonly path: string[]; protected readonly options: OperationKeyPrefixOptions; constructor(path: string[], options: OperationKeyPrefixOptions); /** * Generate a matcher function that returns `true` if the key matches the specified conditions. * * @see {@link https://orpc.dev/docs/integrations/swr#manual-revalidation SWR Manual Revalidation Docs} */ matcher(...rest: MaybeOptionalOptions>): Matcher; } interface ProcedureUtilsOptions extends OperationKeyPrefixOptions { } declare class ProcedureUtils extends SharedUtils { /** * Calling corresponding procedure client * * @see {@link https://orpc.dev/docs/integrations/swr#calling-clients SWR Calling Procedure Client Docs} */ call: Client; constructor(path: string[], client: Client, options?: ProcedureUtilsOptions); /** * Generate a **full matching** key for SWR operations. * * @see {@link https://orpc.dev/docs/integrations/swr#data-fetching SWR Key Docs} */ key(...rest: MaybeOptionalOptions>): OperationKey; /** * Generate a fetcher function for use with useSWR, useSWRInfinite, and other SWR hooks. * * @see {@link https://orpc.dev/docs/integrations/swr#data-fetching SWR Data Fetching Docs} */ fetcher(...rest: MaybeOptionalOptions>): Fetcher; /** * Generate a subscriber function that subscribes to an [AsyncIteratorObject](https://orpc.dev/docs/async-iterator-object) for use with useSWRSubscription, etc. * * @see {@link https://orpc.dev/docs/integrations/swr#subscriptions SWR Subscriptions Docs} */ subscriber(...rest: MaybeOptionalOptions>): Subscriber, TError>; /** * Generate a live subscriber that subscribes to the latest events from an [AsyncIteratorObject](https://orpc.dev/docs/async-iterator-object) for use with useSWRSubscription, etc. * * @see {@link https://orpc.dev/docs/integrations/swr#subscriptions SWR Subscriptions Docs} */ liveSubscriber(...rest: MaybeOptionalOptions>): Subscriber, TError>; /** * Generate a mutator function for use with useSWRMutation, etc. * * @see {@link https://orpc.dev/docs/integrations/swr#mutations SWR Mutations Docs} */ mutator(...rest: MaybeOptionalOptions>): Mutator; } type RouterUtils = T extends Client ? Public> : { [K in keyof T]: T[K] extends AnyNestedClient ? RouterUtils : never; } & Public>; interface RouterUtilsOptions extends OperationKeyPrefixOptions { /** * Base path for all keys. * * @internal */ path?: string[] | undefined; } /** * Create a swr router utils from a client. * * @info Both client-side and server-side clients are supported. * @see {@link https://orpc.dev/docs/integrations/swr SWR Integration} */ declare function createRouterUtils(client: T, options?: RouterUtilsOptions): RouterUtils; declare function isSubsetOf(subsetKey: unknown, fullKey: unknown): boolean; export { OPERATION_CONTEXT_SYMBOL, ProcedureUtils, OPERATION_CONTEXT_SYMBOL as SWR_OPERATION_CONTEXT_SYMBOL, SharedUtils, createRouterUtils, createRouterUtils as createSWRUtils, generateOperationKey, isSubsetOf }; export type { Fetcher, FetcherOptions, InferLiveSubscriberOutput, InferSubscriberOutput, KeyOptions, Matcher, MatcherOptions, MatcherStrategy, Mutator, MutatorOptions, OperationContext, OperationKey, OperationKeyOptions, OperationKeyPrefixOptions, OperationType, ProcedureUtilsOptions, RouterUtils, RouterUtilsOptions, OperationContext as SWROperationContext, Subscriber, SubscriberOptions };