import { ErrorRequestHandler , NextFunction , Request , RequestHandler , Response } from "express" ; import Context from "./Context" ; import ExpressRequestAdapter from "./ExpressRequestAdapter" ; import ExpressResponseAdapter from "./ExpressResponseAdapter" ; import IHandlers from "./handlers/IHandlers" ; import deserializerMiddleware from "./middleware/deserializer.middleware" ; import dispatchMiddleware from "./middleware/dispatch.middleware" ; import endMiddleware from "./middleware/end.middleware" ; import errorMiddleware from "./middleware/error.middleware" ; import HandlerMiddlewareFactory from "./middleware/HandlerMiddlewareFactory" ; import serializerMiddleware from "./middleware/serializer.middleware" ; import MiddlewareFactory from "./MiddlewareFactory" ; import ILogger from "./utils/ILogger" ; export class export default class ExpressMiddlewareFactory extends MiddlewareFactory { param { ILogger } logger param { string } contextPath memberof public constructor ( logger , private readonly contextPath = "default_context" ) { super ( logger ) ; } returns { RequestHandler } memberof public createDispatchMiddleware ( ) { return ( req , res , next ) => { const request = new ExpressRequestAdapter ( req ) ; const response = new ExpressResponseAdapter ( res ) ; dispatchMiddleware ( new Context ( res . locals , this . contextPath , request , response ) , request , next , this . logger ) ; } ; } returns { RequestHandler } memberof public createDeserializerMiddleware ( ) { return ( req , res , next ) => { const request = new ExpressRequestAdapter ( req ) ; const response = new ExpressResponseAdapter ( res ) ; deserializerMiddleware ( new Context ( res . locals , this . contextPath , request , response ) , request , next , this . logger ) ; } ; } param { IHandlers } handlers returns { RequestHandler } memberof public createHandlerMiddleware ( handlers ) { const handlerMiddlewareFactory = new HandlerMiddlewareFactory ( handlers , this . logger ) ; return ( req , res , next ) => { const request = new ExpressRequestAdapter ( req ) ; const response = new ExpressResponseAdapter ( res ) ; handlerMiddlewareFactory . createHandlerMiddleware ( ) ( new Context ( res . locals , this . contextPath , request , response ) , next ) ; } ; } returns { RequestHandler } memberof public createSerializerMiddleware ( ) { return ( req , res , next ) => { const request = new ExpressRequestAdapter ( req ) ; const response = new ExpressResponseAdapter ( res ) ; serializerMiddleware ( new Context ( res . locals , this . contextPath , request , response ) , new ExpressResponseAdapter ( res ) , next , this . logger ) ; } ; } returns { ErrorRequestHandler } memberof public createErrorMiddleware ( ) { return ( err , req , res , next ) => { const request = new ExpressRequestAdapter ( req ) ; const response = new ExpressResponseAdapter ( res ) ; errorMiddleware ( new Context ( res . locals , this . contextPath , request , response ) , err , new ExpressRequestAdapter ( req ) , new ExpressResponseAdapter ( res ) , next , this . logger ) ; } ; } returns { RequestHandler } memberof public createEndMiddleware ( ) { return ( req , res ) => { const request = new ExpressRequestAdapter ( req ) ; const response = new ExpressResponseAdapter ( res ) ; endMiddleware ( new Context ( res . locals , this . contextPath , request , response ) , new ExpressResponseAdapter ( res ) , this . logger ) ; } ; } }