UNPKG

4.21 kBTypeScriptView Raw
1/// <reference types="node" />
2import http from 'http';
3import { Stream } from 'stream';
4import { URL } from 'url';
5export declare type HttpMethod = 'GET' | 'HEAD' | 'POST' | 'PUT' | 'DELETE' | 'OPTIONS' | 'PATCH';
6export interface ServerRequest<Params = unknown> extends http.IncomingMessage {
7 params: Params;
8 protocol: 'http' | 'https';
9 parsedURL: URL;
10}
11export interface ServerResponse extends http.ServerResponse {
12}
13export declare type ResponseBodyType = string | object | number | Buffer | Stream | Error | null;
14export declare type Handler<ResponseBody extends ResponseBodyType = ResponseBodyType, Request extends ServerRequest = ServerRequest> = (req: Request, res: ServerResponse) => void | ResponseBody | Promise<ResponseBody>;
15export declare type ErrorHandler = (req: ServerRequest, res: ServerResponse, err: unknown) => void | ResponseBodyType | Promise<ResponseBodyType>;
16export interface ServeOptions {
17 trustProxy?: boolean;
18 errorHandler?: ErrorHandler;
19}
20export declare function serve(handler: Handler, options?: ServeOptions): (req: http.IncomingMessage, res: http.ServerResponse) => Promise<void>;
21export declare function getHeader(req: http.IncomingMessage, header: string): string | undefined;
22export interface RequestBodyOptions {
23 limit?: string;
24 encoding?: string;
25}
26export declare function buffer(req: http.IncomingMessage, { limit, encoding }?: RequestBodyOptions): Promise<Buffer>;
27export declare function text(req: http.IncomingMessage, options?: RequestBodyOptions): Promise<string>;
28export declare function json(req: http.IncomingMessage, options?: RequestBodyOptions): Promise<any>;
29export declare function send(res: http.ServerResponse, code: number, body?: ResponseBodyType): void;
30export declare function notFound(): HttpError;
31export declare function router(...handlers: RouteHandler<HttpMethod, any, ResponseBodyType>[]): Handler;
32export interface RouteHandler<Method extends HttpMethod = HttpMethod, Route extends string = string, ResponseBody extends ResponseBodyType = ResponseBodyType> extends Handler<ResponseBody, ServerRequest<RouteParams<Route>>> {
33 method: Method;
34 route: Route;
35 compilePath: (params?: RouteParams<Route>) => string;
36 matchPath: (path: string) => false | {
37 params: RouteParams<Route>;
38 path: string;
39 index: number;
40 };
41}
42export declare function route<ResponseBody extends ResponseBodyType, Method extends HttpMethod = HttpMethod, Route extends string = string>(method: Method, path: Route, handler: Handler<ResponseBody, ServerRequest<RouteParams<Route>>>): RouteHandler<Method, Route, ResponseBody>;
43export declare class HttpError extends Error {
44 statusCode: number;
45 metadata: unknown;
46 constructor(statusCode: number, message: string, metadata: unknown);
47}
48export declare function httpError(code: number, message: string, metadata?: unknown): HttpError;
49export declare class RedirectError extends Error {
50 statusCode: number;
51 location: string;
52 constructor(statusCode: number, location: string);
53}
54export declare function redirect(location: string, statusCode?: number): RedirectError;
55/**
56 * Creates a function that caches its results for a given request. Both successful responses
57 * and errors are cached.
58 *
59 * @param fn The function that should be cached.
60 * @returns The results of calling the function
61 */
62export declare function fromRequest<Fn extends (req: ServerRequest, ...rest: any[]) => any>(fn: Fn): Fn;
63export declare type RouteParams<T extends string> = T extends `${string}:${infer P}?/${infer Rest}` ? {
64 [K in P]?: string;
65} & RouteParams<Rest> : T extends `${string}:${infer P}*/${infer Rest}` ? {
66 [K in P]?: string[];
67} & RouteParams<Rest> : T extends `${string}:${infer P}+/${infer Rest}` ? {
68 [K in P]: string[];
69} & RouteParams<Rest> : T extends `${string}:${infer P}/${infer Rest}` ? {
70 [K in P]: string;
71} & RouteParams<Rest> : T extends `${string}:${infer P}?` ? {
72 [K in P]?: string;
73} : T extends `${string}:${infer P}*` ? {
74 [K in P]?: string[];
75} : T extends `${string}:${infer P}+` ? {
76 [K in P]: string[];
77} : T extends `${string}:${infer P}` ? {
78 [K in P]: string;
79} : {};
80//# sourceMappingURL=zap.d.ts.map
\No newline at end of file