import express, { Router } from "express"; import { AppMasterRouterType } from "./types"; import { AppMasterMiddleware, AppMasterMiddlewareConfig, AppMasterMiddlewareSetter, AppMasterMiddlewareKey } from "../am-middleware"; import { AppMasterCoreType } from "../../am-core"; export interface AppMasterRouter extends AppMasterRouterType {} export class AppMasterRouter { constructor(config:AppMasterMiddlewareConfig,main:AppMasterMiddlewareConfig["main"],m:AppMasterCoreType){ const app = express(); app.disable("x-powered-by"); const middleware = new AppMasterMiddleware(); if(config){ const custom = config.custom(m); const customNames = Object.keys(custom); const setCustomMiddleware = (k:string,b:1|0) => { customNames.forEach(s => { const handler = custom[s]; handler.before == k && b?app.use(handler.func(m)): handler.after == k && !b?app.use(handler.func(m)): null;});}; for(const k in config){ if(k !== "custom"){ if(k == "main"){app.use(m.vars.entry,main?main(m):Router());} else{ const opts:any = config[k as keyof AppMasterMiddlewareConfig]; const setter = middleware[k as keyof AppMasterMiddleware] as AppMasterMiddlewareSetter; setCustomMiddleware(k,1); setter?setter(app,opts,m):null; setCustomMiddleware(k,0);}}}} middleware.pageNotFound(app,null,m); middleware.finalHandler(app,null,m); return app;}} export default AppMasterRouter;