import type { SchemaSource } from './schema.ts'; /** Key-value pair on the Rust boundary. */ export interface KvPair { key: string; value: string; } /** Multipart part metadata coming from Rust. */ export interface PartMeta { name?: string | null; filename?: string | null; contentType?: string | null; } /** Request/response body bridge (the napi `BodyIo` class). */ export interface BodyIo { read(): Promise; write(chunk: Buffer): Promise; endWrite(): void; nextPart(): Promise; readPart(): Promise; /** Resolves when the request ends: `true` if the client disconnected first. */ waitAbort(): Promise; } /** A matched request received from Rust. */ export interface NativeRequest { /** Ticket for `takeBody`/`respond` on the native bridge (§19). */ reqId: number; leafId: number; method: string; path: string; queryString?: string | null; params: Record; query: KvPair[]; headers: KvPair[]; ip: string; ips: string[]; country?: string | null; id: string; validBody?: string | null; validQuery?: string | null; validParams?: string | null; } /** The response returned to Rust. */ export interface NativeResponse { status: number; headers: KvPair[]; body?: string; streamed?: boolean; } /** Response body source for `c.body()`. */ export type BodySource = Buffer | Uint8Array | ReadableStream | AsyncIterable; /** `Set-Cookie` options (§6d B1). */ export interface CookieOptions { maxAge?: number; domain?: string; path?: string; expires?: Date; httpOnly?: boolean; secure?: boolean; sameSite?: 'strict' | 'lax' | 'none' | 'Strict' | 'Lax' | 'None'; } /** A part of a multipart request (§9a). */ export interface MultipartPart { name: string | undefined; filename: string | undefined; contentType: string | undefined; readonly stream: ReadableStream; arrayBuffer(): Promise; text(): Promise; } /** Contextual logger (§6d B3). */ export interface Logger { debug(msg: string, extra?: Record): void; info(msg: string, extra?: Record): void; warn(msg: string, extra?: Record): void; error(msg: string, extra?: Record): void; } /** Where to look for validated values. */ export type ValidLocation = 'body' | 'query' | 'params'; /** The request inside the context (§7). */ export interface ContextRequest { readonly method: string; readonly path: string; readonly rawPath: string; readonly url: string; readonly params: Record; readonly query: Record; queries(key: string): string[]; header(name: string): string | undefined; readonly headers: Record; readonly ip: string; readonly ips: string[]; readonly country: string | undefined; readonly id: string; /** The request's AbortSignal (timeout/disconnect) — set by the dispatcher. */ signal: AbortSignal | undefined; cookie(name: string): string | undefined; readonly stream: ReadableStream; arrayBuffer(): Promise; text(): Promise; json(): Promise; parseBody(): Promise>; parts(): AsyncGenerator; formData(): Promise; valid(loc: ValidLocation): T; /** @internal values from native validation */ _rustValid: Record; /** @internal valibot transform result layered on the native values */ _valid: Record; } /** The mutable part of the response. */ export interface ContextResponse { status: number | undefined; headers: ResHeaders; sent: boolean; } /** The request context `c` (§7). */ export interface Context { readonly req: ContextRequest; readonly res: ContextResponse; aborted: boolean; error: unknown; readonly log: Logger; set(key: string, value: unknown): Context; get(key: string): T | undefined; status(code: number): Context; header(name: string, value: string): Context; cookie(name: string, value: string, opts?: CookieOptions): Context; json(value: unknown, status?: number): Context; text(value: string, status?: number): Context; body(data: BodySource | string | null | undefined, status?: number): Context; redirect(location: string, status?: number): Context; notFound(): Context; /** @internal */ _responseStrip: ResponseStrip | null; /** @internal */ _bodyIo: BodyIo; /** @internal */ _body: string | undefined; /** @internal */ _stream: BodySource | undefined; /** @internal */ _finalized: boolean; /** @internal */ _settled: boolean; /** @internal */ _store: Map; } /** Map of "status → allowed field set" for schema-based response stripping (§6b). */ export type ResponseStrip = Record>; /** Context construction options. */ export interface BuildContextOptions { baseUrl?: string; requestIdHeader?: string; bodyLimit?: number | undefined; responseStrip?: ResponseStrip | null; } /** Route schemas (§6b). */ export interface RouteSchema { body?: SchemaSource; query?: SchemaSource; params?: SchemaSource; response?: Record; } /** An error carrying an HTTP status (caught by the dispatcher → sent to the client). */ export declare class HttpError extends Error { readonly status: number; constructor(status: number, message?: string); } /** Response headers: lowercase, set/append, multiple values (set-cookie). */ export declare class ResHeaders { #private; set(name: string, value: string | number): this; append(name: string, value: string | number): this; get(name: string): string | undefined; has(name: string): boolean; delete(name: string): this; /** Flat list of pairs (duplicates allowed) for the native layer. */ toPairs(): KvPair[]; } /** Parse the Cookie header into a `{ name: value }` object. */ export declare function parseCookies(cookieHeader: string | undefined): Record; /** Serialize Set-Cookie (§6d B1). */ export declare function serializeCookie(name: string, value: string, opts?: CookieOptions): string; /** A `BodyIo` or a thunk fetching it from the native registry (§19). */ export type BodyIoSource = BodyIo | (() => BodyIo | null); /** Build the context `c` from a native request. `bodyIo` may be a thunk fetching the * native BodyIo lazily (§19) — it is resolved at most once, on first body use. */ export declare function buildContext(nreq: NativeRequest, bodyIo: BodyIoSource, opts?: BuildContextOptions): Context; /** Apply the handler's return value as sugar (string→text, stream→body, else json). */ export declare function applyReturnValue(c: Context, returnValue: unknown): void; /** Reduce the context to a native JsResponse (starts the pump for a streamed body). */ export declare function buildNativeResponse(c: Context): NativeResponse; //# sourceMappingURL=context.d.ts.map