import { type Server, type IncomingMessage, type ServerResponse } from 'node:http'; import type { AddressInfo } from 'node:net'; import type { Duplex } from 'node:stream'; import type { AuthMiddleware, AuthenticatedRequest } from './middleware/AuthMiddleware'; import type { RuntimeHost, RuntimeListenEndpoint } from '../runtime/host/types'; /** * Route handler function */ export type RouteHandler = (request: AuthenticatedRequest, response: ServerResponse, params: Record) => Promise; /** * Route definition */ export interface Route { method: string; pattern: RegExp; paramNames: string[]; handler: RouteHandler; /** If true, skip authentication */ public?: boolean; /** If true, authenticate when credentials are present, otherwise continue unauthenticated */ optionalAuth?: boolean; /** If true, match all methods instead of one method. */ allMethods?: boolean; } export interface ApiServerOptions { port?: number; host?: string; socketPath?: string; listenEndpoint?: RuntimeListenEndpoint; runtimeHost?: RuntimeHost; authMiddleware: AuthMiddleware; corsOrigins?: string[]; } export type UpgradeHandler = (request: IncomingMessage, socket: Duplex, head: Buffer) => void; /** * Standalone API Server */ export declare class ApiServer { private readonly logger; private readonly runtimeHost; private readonly listenEndpoint; private readonly authMiddleware; private readonly corsOrigins; private readonly routes; private readonly upgradeHandlers; private readonly shutdownHandlers; private responseHeaders; private server?; constructor(options: ApiServerOptions); /** * Register a route */ route(method: string, path: string, handler: RouteHandler, options?: { /** If true, skip authentication for this route */ public?: boolean; /** If true, authenticate when credentials are present, otherwise continue unauthenticated */ optionalAuth?: boolean; /** If true, match all methods instead of one method */ allMethods?: boolean; }): void; /** * Convenience methods for common HTTP methods */ get(path: string, handler: RouteHandler, options?: { public?: boolean; optionalAuth?: boolean; }): void; post(path: string, handler: RouteHandler, options?: { public?: boolean; optionalAuth?: boolean; }): void; put(path: string, handler: RouteHandler, options?: { public?: boolean; optionalAuth?: boolean; }): void; delete(path: string, handler: RouteHandler, options?: { public?: boolean; optionalAuth?: boolean; }): void; patch(path: string, handler: RouteHandler, options?: { public?: boolean; optionalAuth?: boolean; }): void; all(path: string, handler: RouteHandler, options?: { public?: boolean; optionalAuth?: boolean; }): void; addUpgradeHandler(handler: UpgradeHandler): void; addShutdownHandler(handler: () => void | Promise): void; addResponseHeaders(headers: Record): void; /** * Start the server */ start(): Promise; /** * Stop the server */ stop(): Promise; /** * Get the underlying HTTP server (for WebSocket upgrade) */ getHttpServer(): Server | undefined; address(): AddressInfo | string | null; private handleRequest; private findRoute; private pathToRegex; private handleCors; }