UNPKG

@thi.ng/server

Version:

Minimal HTTP server with declarative routing, static file serving and freely extensible via pre/post interceptors

117 lines 4.36 kB
import { type Fn } from "@thi.ng/api"; import { type ILogger } from "@thi.ng/logger"; import { Router, type RouteMatch } from "@thi.ng/router"; import * as http from "node:http"; import type { CompiledHandler, CompiledServerRoute, RequestCtx, ServerOpts, ServerRoute } from "./api.js"; export declare class Server<CTX extends RequestCtx = RequestCtx> { opts: Partial<ServerOpts<CTX>>; logger: ILogger; router: Router<CompiledServerRoute<CTX>>; server: http.Server<typeof http.IncomingMessage, typeof ServerResponse>; host: string; protected augmentCtx: Fn<RequestCtx, CTX>; protected methodAdapter: ServerOpts["method"]; constructor(opts?: Partial<ServerOpts<CTX>>); start(): Promise<boolean>; stop(): Promise<boolean>; protected listener(req: http.IncomingMessage, res: ServerResponse): Promise<void>; protected runHandler({ fn, pre, post }: CompiledHandler, ctx: CTX): Promise<void>; protected compileRoute(route: ServerRoute<CTX>): CompiledServerRoute<CTX>; addRoutes(routes: ServerRoute<CTX>[]): void; sendFile({ req, res }: RequestCtx, path: string, headers?: http.OutgoingHttpHeaders, compress?: boolean): Promise<void>; redirectToRoute(res: ServerResponse, route: RouteMatch): void; } export declare const server: <CTX extends RequestCtx>(opts?: Partial<ServerOpts<CTX>>) => Server<CTX>; /** * Extended version of the default NodeJS ServerResponse with additional methods * for various commonly used HTTP statuses/errors. */ export declare class ServerResponse extends http.ServerResponse<http.IncomingMessage> { /** * Writes a HTTP 204 header (plus given `headers`) and ends the response. * * @param headers */ noContent(headers?: http.OutgoingHttpHeaders): void; /** * Writes a HTTP 302 header to redirect to given URL, add given additional * `headers` and ends the response. * * @remarks * Also see {@link ServerResponse.seeOther}. * * @param headers */ redirectTo(location: string, headers?: http.OutgoingHttpHeaders): void; /** * Writes a HTTP 303 header to redirect to given URL, add given additional * `headers` and ends the response. * * @remarks * Also see {@link ServerResponse.redirectTo}. * * @param headers */ seeOther(location: string, headers?: http.OutgoingHttpHeaders): void; /** * Writes a HTTP 304 header (plus given `headers`) and ends the response. * * @param headers */ unmodified(headers?: http.OutgoingHttpHeaders): void; /** * Writes a HTTP 400 header (plus given `headers`) and ends the response * (with optional `body`). * * @param headers */ badRequest(headers?: http.OutgoingHttpHeaders, body?: any): void; /** * Writes a HTTP 401 header (plus given `headers`) and ends the response * (with optional `body`). * * @param headers */ unauthorized(headers?: http.OutgoingHttpHeaders, body?: any): void; /** * Writes a HTTP 403 header (plus given `headers`) and ends the response * (with optional `body`). * * @param headers */ forbidden(headers?: http.OutgoingHttpHeaders, body?: any): void; /** * Writes a HTTP 404 header (plus given `headers`) and ends the response * (with optional `body`). * * @param headers */ missing(headers?: http.OutgoingHttpHeaders, body?: any): void; /** * Writes a HTTP 405 header (plus given `headers`) and ends the response * (with optional `body`). * * @param headers */ notAllowed(headers?: http.OutgoingHttpHeaders, body?: any): void; /** * Writes a HTTP 406 header (plus given `headers`) and ends the response * (with optional `body`). * * @param headers */ notAcceptable(headers?: http.OutgoingHttpHeaders, body?: any): void; /** * Writes a HTTP 429 header (plus given `headers`) and ends the response * (with optional `body`). * * @param headers */ rateLimit(headers?: http.OutgoingHttpHeaders, body?: any): void; /** * HTTP 444. Indicates the server has returned no information to the client and closed * the connection (useful as a deterrent for malware) */ noResponse(): void; } //# sourceMappingURL=server.d.ts.map