1 |
|
2 | import http from 'http';
|
3 | import { Stream } from 'stream';
|
4 | import { URL } from 'url';
|
5 | export declare type HttpMethod = 'GET' | 'HEAD' | 'POST' | 'PUT' | 'DELETE' | 'OPTIONS' | 'PATCH';
|
6 | export interface ServerRequest<Params = unknown> extends http.IncomingMessage {
|
7 | params: Params;
|
8 | protocol: 'http' | 'https';
|
9 | parsedURL: URL;
|
10 | }
|
11 | export interface ServerResponse extends http.ServerResponse {
|
12 | }
|
13 | export declare type ResponseBodyType = string | object | number | Buffer | Stream | Error | null;
|
14 | export declare type Handler<ResponseBody extends ResponseBodyType = ResponseBodyType, Request extends ServerRequest = ServerRequest> = (req: Request, res: ServerResponse) => void | ResponseBody | Promise<ResponseBody>;
|
15 | export declare type ErrorHandler = (req: ServerRequest, res: ServerResponse, err: unknown) => void | ResponseBodyType | Promise<ResponseBodyType>;
|
16 | export interface ServeOptions {
|
17 | trustProxy?: boolean;
|
18 | errorHandler?: ErrorHandler;
|
19 | }
|
20 | export declare function serve(handler: Handler, options?: ServeOptions): (req: http.IncomingMessage, res: http.ServerResponse) => Promise<void>;
|
21 | export declare function getHeader(req: http.IncomingMessage, header: string): string | undefined;
|
22 | export interface RequestBodyOptions {
|
23 | limit?: string;
|
24 | encoding?: string;
|
25 | }
|
26 | export declare function buffer(req: http.IncomingMessage, { limit, encoding }?: RequestBodyOptions): Promise<Buffer>;
|
27 | export declare function text(req: http.IncomingMessage, options?: RequestBodyOptions): Promise<string>;
|
28 | export declare function json(req: http.IncomingMessage, options?: RequestBodyOptions): Promise<any>;
|
29 | export declare function send(res: http.ServerResponse, code: number, body?: ResponseBodyType): void;
|
30 | export declare function notFound(): HttpError;
|
31 | export declare function router(...handlers: RouteHandler<HttpMethod, any, ResponseBodyType>[]): Handler;
|
32 | export 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 | }
|
42 | export 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>;
|
43 | export declare class HttpError extends Error {
|
44 | statusCode: number;
|
45 | metadata: unknown;
|
46 | constructor(statusCode: number, message: string, metadata: unknown);
|
47 | }
|
48 | export declare function httpError(code: number, message: string, metadata?: unknown): HttpError;
|
49 | export declare class RedirectError extends Error {
|
50 | statusCode: number;
|
51 | location: string;
|
52 | constructor(statusCode: number, location: string);
|
53 | }
|
54 | export 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 | */
|
62 | export declare function fromRequest<Fn extends (req: ServerRequest, ...rest: any[]) => any>(fn: Fn): Fn;
|
63 | export 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 |
|
\ | No newline at end of file |