import {RequestHandler} from "express"; import {NextFunction, Request, Response} from "express-serve-static-core"; import Application from "./Application.js"; import {Type} from "./Utils.js"; export default abstract class Middleware { public constructor( protected readonly app: Application, ) { } protected abstract handle(req: Request, res: Response, next: NextFunction): Promise; public getRequestHandler(): RequestHandler { return async (req, res, next): Promise => { try { if (req.middlewares.find(m => m.constructor === this.constructor)) { next(); } else { req.middlewares.push(this); return await this.handle(req, res, next); } } catch (e) { next(e); } }; } } export interface MiddlewareType extends Type { new(app: Application): M; }