///
import http from 'http';
import { Stream } from 'stream';
import { URL } from 'url';
export declare type HttpMethod = 'GET' | 'HEAD' | 'POST' | 'PUT' | 'DELETE' | 'OPTIONS' | 'PATCH';
export interface ServerRequest extends http.IncomingMessage {
params: Params;
protocol: 'http' | 'https';
parsedURL: URL;
}
export interface ServerResponse extends http.ServerResponse {
}
export declare type ResponseBodyType = string | object | number | Buffer | Stream | Error | null;
export declare type Handler = (req: Request, res: ServerResponse) => void | ResponseBody | Promise;
export declare type ErrorHandler = (req: ServerRequest, res: ServerResponse, err: unknown) => void | ResponseBodyType | Promise;
export interface ServeOptions {
trustProxy?: boolean;
errorHandler?: ErrorHandler;
}
export declare function serve(handler: Handler, options?: ServeOptions): (req: http.IncomingMessage, res: http.ServerResponse) => Promise;
export declare function getHeader(req: http.IncomingMessage, header: string): string | undefined;
export interface RequestBodyOptions {
limit?: string;
encoding?: string;
}
export declare function buffer(req: http.IncomingMessage, { limit, encoding }?: RequestBodyOptions): Promise;
export declare function text(req: http.IncomingMessage, options?: RequestBodyOptions): Promise;
export declare function json(req: http.IncomingMessage, options?: RequestBodyOptions): Promise;
export declare function send(res: http.ServerResponse, code: number, body?: ResponseBodyType): void;
export declare function notFound(): HttpError;
export declare function router(...handlers: RouteHandler[]): Handler;
export interface RouteHandler extends Handler>> {
method: Method;
route: Route;
compilePath: (params?: RouteParams) => string;
matchPath: (path: string) => false | {
params: RouteParams;
path: string;
index: number;
};
}
export declare function route(method: Method, path: Route, handler: Handler>>): RouteHandler;
export declare class HttpError extends Error {
statusCode: number;
metadata: unknown;
constructor(statusCode: number, message: string, metadata: unknown);
}
export declare function httpError(code: number, message: string, metadata?: unknown): HttpError;
export declare class RedirectError extends Error {
statusCode: number;
location: string;
constructor(statusCode: number, location: string);
}
export declare function redirect(location: string, statusCode?: number): RedirectError;
/**
* Creates a function that caches its results for a given request. Both successful responses
* and errors are cached.
*
* @param fn The function that should be cached.
* @returns The results of calling the function
*/
export declare function fromRequest any>(fn: Fn): Fn;
export declare type RouteParams = T extends `${string}:${infer P}?/${infer Rest}` ? {
[K in P]?: string;
} & RouteParams : T extends `${string}:${infer P}*/${infer Rest}` ? {
[K in P]?: string[];
} & RouteParams : T extends `${string}:${infer P}+/${infer Rest}` ? {
[K in P]: string[];
} & RouteParams : T extends `${string}:${infer P}/${infer Rest}` ? {
[K in P]: string;
} & RouteParams : T extends `${string}:${infer P}?` ? {
[K in P]?: string;
} : T extends `${string}:${infer P}*` ? {
[K in P]?: string[];
} : T extends `${string}:${infer P}+` ? {
[K in P]: string[];
} : T extends `${string}:${infer P}` ? {
[K in P]: string;
} : {};
//# sourceMappingURL=zap.d.ts.map