import { type ContractProcedure, type InferContractProcedure, type IsContractProcedure } from '@ts-pf/contract'; import { type CallInterceptor } from './call-interceptor.js'; import { createErrorFactory } from './error-factory.js'; import type { MiddlewareFn } from './middleware.js'; export type HandlerFn = (opts: { input: unknown; context: unknown; errors: ReturnType; path: string[]; signal?: AbortSignal; }) => unknown | Promise; /** * The `{ input, output, errors }` of a contract procedure type, or `never` * for anything else: contract's `InferContractProcedure` minus `meta`, so * procedures that differ only in `.meta()` share a brand. Brands compare this * shape, not the procedure type itself, so a handler for `star.find` cannot * fill the `planet.find` slot even when the two types are assignable. */ export type ContractShape = [InferContractProcedure] extends [never] ? never : Pick, 'input' | 'output' | 'errors'>; /** String keys of a contract router, minus the `~pf` brand. */ export type ContractKeys = Exclude; type ContractBrand = unknown extends TContract ? {} : { readonly '~contract'?: ((shape: ContractShape) => ContractShape) | undefined; }; type ContextBrand = unknown extends TCtx ? {} : { readonly '~context'?: ((context: TCtx) => void) | undefined; }; /** * A handler bound to a contract procedure. `TContract` is the contract * procedure type it was built from (`typeof contract.planet.find`); `TCtx` is * the `$context()` type it expects. Both are phantom. The defaults mean * "unbranded": accepted anywhere, and consumers infer `unknown` from them. */ export type ImplementedProcedure = { readonly '~pf': { type: 'implemented-procedure'; contract: ContractProcedure; use: MiddlewareFn[]; useAfter: MiddlewareFn[]; handler: HandlerFn; path: string[]; }; } & ContractBrand & ContextBrand; /** The implementation tree `impl.router()` expects for contract `T`. */ export type RouterImpl = { [K in ContractKeys]: IsContractProcedure extends true ? ImplementedProcedure : RouterImpl; }; declare const contractBrand: unique symbol; declare const contextBrand: unique symbol; type AnyImplementedRouter = { readonly [key: string]: ImplementedProcedure | AnyImplementedRouter | undefined; }; type RouterContractBrand = unknown extends T ? {} : { readonly [contractBrand]?: T | undefined; }; type RouterContextBrand = unknown extends TCtx ? {} : { readonly [contextBrand]?: ((context: TCtx) => void) | undefined; }; /** * A finished implementation tree. `T` is the contract and `TCtx` the * `$context()` type every procedure in it expects; adapters infer `TCtx` * from the context brand (`FetchHandler`, `createLocalClient`, message * handlers, Astro). Only this router-level brand is read: a tree not produced * by `impl.router()` (a plain object of procedures) infers `unknown` even if * its leaves are branded. Both parameters default to `unknown`. */ export type ImplementedRouter = (unknown extends T ? AnyImplementedRouter : Readonly>) & RouterContractBrand & RouterContextBrand; /** Contract keys that `I` lacks, each typed as a message literal. */ export type MissingProcedures = T extends unknown ? { [K in ContractKeys as K extends keyof I ? IsContractProcedure extends true ? never : K : K]: K extends keyof I ? MissingProcedures : `Missing implementation for "${K}"`; } : never; /** Keys of `I` that the contract lacks, each typed as a message literal. */ export type UnexpectedKeys = I extends unknown ? { [K in ContractKeys as K extends ContractKeys ? IsContractProcedure extends true ? never : K : K]: K extends ContractKeys ? UnexpectedKeys : `"${K}" is not in the contract`; } : never; /** The branded slot type for every contract key that `I` provides. */ export type PresentProcedures = T extends unknown ? { [K in ContractKeys as K extends keyof I ? K : never]: IsContractProcedure extends true ? ImplementedProcedure : K extends keyof I ? PresentProcedures : never; } : never; export type RouterImplCheck = MissingProcedures & UnexpectedKeys & PresentProcedures; export declare function isImplementedProcedure(value: unknown): value is ImplementedProcedure; export type ProcedureValidation = { /** Set false to skip the input schema. Default true. */ input?: boolean; /** Set true to check the output schema, including stream output items. Default false. */ output?: boolean; }; export type RunProcedureOptions = { signal?: AbortSignal; interceptors?: readonly CallInterceptor[]; validation?: ProcedureValidation; }; export declare function runProcedure(proc: ImplementedProcedure, rawInput: unknown, context: unknown, options?: RunProcedureOptions): Promise; export declare function lookupProcedure(router: ImplementedRouter, path: string[]): ImplementedProcedure | undefined; export {}; //# sourceMappingURL=runtime.d.ts.map