import { expParse } from "./util/path_exp.ts";import { Req, Res, Routes } from "./types.ts";
class Router { public routes: Routes[] = []; public hook: ((req: Req, res: Res) => void)[] = [];
public use = async (hand: (req: Req, res: Res) => void) => { await this.hook.push(hand); };
public all = ( path: string, hand: (req: Req, res: Res) => void, hook: ((req: Req, res: Res) => void)[] = [], ) => { const e: Routes = { path, reg: / /, method: "ALL", hand: [...this.hook, ...hook, hand], }; this.routes.push(e); };
public get = ( path: string, hand: (req: Req, res: Res) => void, hook: ((req: Req, res: Res) => void)[] = [], ) => { const e: Routes = { path, reg: / /, method: "GET", hand: [...this.hook, ...hook, hand], }; this.routes.push(e); };
public post = ( path: string, hand: (req: Req, res: Res) => void, hook: ((req: Req, res: Res) => void)[] = [], ) => { const e: Routes = { path, reg: / /, method: "POST", hand: [...this.hook, ...hook, hand], }; this.routes.push(e); };
public put = ( path: string, hand: (req: Req, res: Res) => void, hook: ((req: Req, res: Res) => void)[] = [], ) => { const e: Routes = { path, reg: / /, method: "PUT", hand: [...this.hook, ...hook, hand], }; this.routes.push(e); };
public delete = ( path: string, hand: (req: Req, res: Res) => void, hook: ((req: Req, res: Res) => void)[] = [], ) => { const e: Routes = { path, reg: / /, method: "DELETE", hand: [...this.hook, ...hook, hand], }; this.routes.push(e); };
public options = ( path: string, hand: (req: Req, res: Res) => void, hook: ((req: Req, res: Res) => void)[] = [], ) => { const e: Routes = { path, reg: / /, method: "OPTIONS", hand: [...this.hook, ...hook, hand], }; this.routes.push(e); };
public head = ( path: string, hand: (req: Req, res: Res) => void, hook: ((req: Req, res: Res) => void)[] = [], ) => { const e: Routes = { path, reg: / /, method: "HEAD", hand: [...this.hook, ...hook, hand], }; this.routes.push(e); };
public patch = ( path: string, hand: (req: Req, res: Res) => void, hook: ((req: Req, res: Res) => void)[] = [], ) => { const e: Routes = { path, reg: / /, method: "PATCH", hand: [...this.hook, ...hook, hand], }; this.routes.push(e); };
public getRoutes = (m?: boolean) => { for (const r of this.routes) { r.reg = expParse(r.path, m); } return this.routes; };
public pre = (path: string, r: Router) => { for (const rp of r.routes) { const e: Routes = { path: (path + (rp.path !== "/" ? rp.path : "")), reg: rp.reg, method: rp.method, hand: [...this.hook, ...rp.hand], }; this.routes.push(e); } };}
export { Router };