/** * Client-side transport runtime for calling server functions. * * When the build tools transform a `"use server"` module for the client * bundle, each exported function is replaced with a stub created by * `createServerReference(fnId, callServer)`. This module provides that factory. */ import { type ClientRuntime } from "../shared/runtime-config.js"; export type { RuntimeTransportOptions } from "../shared/runtime-config.js"; /** * Request context passed through server calls. * * Keep this limited to per-call controls. HTTP request defaults such as * headers and credentials belong on TransportOptions. */ export interface RequestContext { /** Signal for aborting the request. */ signal?: AbortSignal; } export interface TransportAdapter { /** Execute a server function call. */ send(fnId: string, args: unknown[], context?: RequestContext): Promise; } type MaybePromise = T | Promise; export type HeaderFactory = (context: RequestContext) => MaybePromise; interface BaseTransportOptions { /** Base URL for framework server calls. Defaults to the current page origin. */ baseUrl?: string; /** Credentials policy for framework server requests. */ credentials?: RequestCredentials; } export interface TransportOptions extends BaseTransportOptions { /** Static headers or a factory evaluated for each transport call. */ headers?: HeadersInit | HeaderFactory; /** Server function endpoint override for standalone/custom runtimes. */ functions?: { /** Path or URL for the server function endpoint. */ endpoint?: string; }; /** Adapter capabilities for custom runtimes or protocols. */ adapter?: TransportAdapter; /** Suppress warnings when re-initializing transport. Useful for HMR. */ silent?: boolean; } /** * Configure the transport runtime. Call once at app startup if you need to * customize HTTP request defaults, server origin, or adapter capabilities. */ export declare function initTransport(options?: TransportOptions): void; /** * Initialize the default HTTP transport from the generated client runtime. * * Framework-managed runtimes call this before loading user modules. Embedded * runtime metadata wins over global defaults; absent both, the default * transport is captured. The first framework call owns that configuration for * the realm. Explicit initTransport() calls still win and are never rewritten. */ export declare function initTransportFromRuntime(runtime: Pick): void; /** Minimal callable shape for server function stubs. */ type AnyFn = (...args: unknown[]) => unknown; /** * A server function stub augmented with query metadata. * * These properties are attached at runtime by the * `createServerReference` call. TypeScript source types won't reflect them, * so use this type when you need typed access to the metadata. * * @example * import type { ServerFunction } from "@evjs/client"; * import { getUsers } from "./api/users.server"; * * // Runtime properties (added by build system): * getUsers.queryKey() // → [""] * getUsers.fnId // → "" * getUsers.fnName // → "getUsers" * getUsers.fnArity // → declared parameter count * * // For typed access in generic code: * function invalidate(fn: T) { * queryClient.invalidateQueries({ queryKey: fn.queryKey() }); * } */ export interface ServerFunction { (...args: TArgs): Promise; /** Build a TanStack Query key from the function ID + arguments. */ queryKey(...args: TArgs): unknown[]; /** Returns a QueryOptions object with queryKey and queryFn for TanStack Query loaders/prefetching. */ queryOptions(...args: TArgs): { queryKey: unknown[]; queryFn: (ctx?: { signal?: AbortSignal; }) => Promise; }; /** The internal function ID (stable SHA-256 hash). */ readonly fnId: string; /** The human-readable export name. */ readonly fnName: string; /** The number of declared parameters in the original server function. */ readonly fnArity?: number; } /** * Look up the human-readable export name for a function ID. * Falls back to the fnId itself if no name is registered. */ export declare function getFnName(fnId: string): string; /** * Create a server reference stub for a server function. * * Returns a callable function that forwards calls to `callServerFn(fnId, args)`. * The returned function is augmented with `.queryKey()`, `.queryOptions()`, * `.fnId`, and `.fnName` metadata for use with TanStack Query. * * Follows the React Server Components convention. * * @param fnId - The unique function hash ID. * @param exportName - The human-readable export name. * @returns An augmented server function stub. */ export declare function createServerReference(fnId: string, exportName?: string, arity?: number): ServerFunction; /** * Look up the internal function ID for a server function stub. * Returns undefined if the function is not a registered server function. */ export declare function getFnId(fn: AnyFn): string | undefined; export declare function getServerFunction(fn: (...args: TArgs) => Promise): ServerFunction | undefined; //# sourceMappingURL=transport-runtime.d.ts.map