48 lines
1.7 KiB
TypeScript
48 lines
1.7 KiB
TypeScript
import { IncomingMessage, ServerResponse } from "node:http";
|
|
export interface Request extends Omit<IncomingMessage, 'url'> {
|
|
url: {
|
|
pathname: string;
|
|
split: string[];
|
|
searchParams: URLSearchParams;
|
|
qs: Record<string, string>;
|
|
};
|
|
cookies?: Record<string, string>;
|
|
params?: Record<string, string>;
|
|
post?: Record<string, string>;
|
|
}
|
|
export interface Response extends ServerResponse {
|
|
reply: (options: {
|
|
code?: number;
|
|
type?: string;
|
|
body: string;
|
|
}) => void;
|
|
status: (code: number) => Response;
|
|
json: (body: any, code?: number) => void;
|
|
html: (body: string, code?: number) => void;
|
|
redirect: (target: string, code?: number) => void;
|
|
}
|
|
export type Handler = (req: Request, res: Response, next?: () => void) => void | Promise<void>;
|
|
export default class Router {
|
|
private routes;
|
|
private mimes;
|
|
constructor();
|
|
importRoutesFromPath(p: string): Promise<this>;
|
|
group(basePath: string | RegExp, callback: (methods: any) => void): this;
|
|
private combinePaths;
|
|
use(obj: Router): void;
|
|
get(path: string | RegExp, ...callback: Handler[]): this;
|
|
post(path: string | RegExp, ...callback: Handler[]): this;
|
|
head(path: string | RegExp, ...callback: Handler[]): this;
|
|
put(path: string | RegExp, ...callback: Handler[]): this;
|
|
delete(path: string | RegExp, ...callback: Handler[]): this;
|
|
patch(path: string | RegExp, ...callback: Handler[]): this;
|
|
private registerRoute;
|
|
getRoute(path: string, method: string): any;
|
|
private sortRoutes;
|
|
private readMimes;
|
|
static({ dir, route }: {
|
|
dir?: string;
|
|
route?: RegExp;
|
|
}): this;
|
|
}
|