@quiltjs/quilt
Version:
Lightweight, type-safe handler and router abstraction for Node HTTP servers.
36 lines • 2.99 kB
TypeScript
import { Handler } from './Handler.js';
import { type ExecuteHandlerHooks } from './executeHandler.js';
export type HTTPMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'OPTIONS' | 'HEAD';
export interface ServerEngineAdapter<RequestType, ResponseType> {
get(path: string, handler: (req: RequestType, res: ResponseType) => Promise<void>): void;
post(path: string, handler: (req: RequestType, res: ResponseType) => Promise<void>): void;
put(path: string, handler: (req: RequestType, res: ResponseType) => Promise<void>): void;
patch(path: string, handler: (req: RequestType, res: ResponseType) => Promise<void>): void;
delete(path: string, handler: (req: RequestType, res: ResponseType) => Promise<void>): void;
options(path: string, handler: (req: RequestType, res: ResponseType) => Promise<void>): void;
head(path: string, handler: (req: RequestType, res: ResponseType) => Promise<void>): void;
}
export type HttpContext<RequestType, ResponseType> = {
req: RequestType;
res: ResponseType;
};
export type QuiltHooks<RequestType, ResponseType> = ExecuteHandlerHooks<HttpContext<RequestType, ResponseType>>;
type ErrorHandler<RequestType, ResponseType> = (error: Error, ctx: HttpContext<RequestType, ResponseType>) => void | Promise<void>;
export declare class Quilt<RequestType = any, ResponseType = any> {
private adapter;
private errorHandler?;
private hooks?;
constructor(adapter: ServerEngineAdapter<RequestType, ResponseType>);
get<O, D extends Record<string, Handler<any, HttpContext<RequestType, ResponseType>, any>>>(path: string, handler: Handler<O, HttpContext<RequestType, ResponseType>, D>): void;
post<O, D extends Record<string, Handler<any, HttpContext<RequestType, ResponseType>, any>>>(path: string, handler: Handler<O, HttpContext<RequestType, ResponseType>, D>): void;
put<O, D extends Record<string, Handler<any, HttpContext<RequestType, ResponseType>, any>>>(path: string, handler: Handler<O, HttpContext<RequestType, ResponseType>, D>): void;
patch<O, D extends Record<string, Handler<any, HttpContext<RequestType, ResponseType>, any>>>(path: string, handler: Handler<O, HttpContext<RequestType, ResponseType>, D>): void;
delete<O, D extends Record<string, Handler<any, HttpContext<RequestType, ResponseType>, any>>>(path: string, handler: Handler<O, HttpContext<RequestType, ResponseType>, D>): void;
options<O, D extends Record<string, Handler<any, HttpContext<RequestType, ResponseType>, any>>>(path: string, handler: Handler<O, HttpContext<RequestType, ResponseType>, D>): void;
head<O, D extends Record<string, Handler<any, HttpContext<RequestType, ResponseType>, any>>>(path: string, handler: Handler<O, HttpContext<RequestType, ResponseType>, D>): void;
setErrorHandler(errorHandler: ErrorHandler<RequestType, ResponseType>): void;
setHooks(hooks: QuiltHooks<RequestType, ResponseType>): void;
private handleRequest;
}
export {};
//# sourceMappingURL=Quilt.d.ts.map