import express , { Express , NextFunction , Request , Response , Router } from 'express' ; import ejs from 'ejs' ; import { AppControllerRoute , AppViewRoute , BullBoardQueues , ControllerHandlerReturnType , HTTPMethod , IServerAdapter , } from '@bull-board/api/dist/typings/app' ; import { wrapAsync } from './helpers/wrapAsync' ; export class ExpressAdapter implements IServerAdapter { private readonly app : Express ; private basePath = '' ; private bullBoardQueues : BullBoardQueues | undefined ; private errorHandler : ( ( error : Error ) => ControllerHandlerReturnType ) | undefined ; constructor ( ) { this . app = express ( ) ; } public setBasePath ( path ) { this . basePath = path ; return this ; } public setStaticPath ( staticsRoute , staticsPath ) { this . app . use ( staticsRoute , express . static ( staticsPath ) ) ; return this ; } public setViewsPath ( viewPath ) { this . app . set ( 'view engine' , 'ejs' ) . set ( 'views' , viewPath ) ; this . app . engine ( 'ejs' , ejs . renderFile ) ; return this ; } public setErrorHandler ( handler ) { this . errorHandler = handler ; return this ; } public setApiRoutes ( routes ) { if ( ! this . errorHandler ) { throw new Error ( `Please call 'setErrorHandler' before using 'registerPlugin'` ) ; } else if ( ! this . bullBoardQueues ) { throw new Error ( `Please call 'setQueues' before using 'registerPlugin'` ) ; } const router = Router ( ) ; routes . forEach ( ( route ) => ( Array . isArray ( route . method ) ? route . method : [ route . method ] ) . forEach ( ( method ) => { router [ method ] ( route . route , wrapAsync ( async ( req , res ) => { const response = await route . handler ( { queues : this . bullBoardQueues as BullBoardQueues , query : req . query , params : req . params , } ) ; res . status ( response . status || 200 ) . json ( response . body ) ; } ) ) ; } ) ) ; router . use ( ( err , _req , res , next ) => { if ( ! this . errorHandler ) { return next ( ) ; } const response = this . errorHandler ( err ) ; return res . status ( response . status as 500 ) . send ( response . body ) ; } ) ; this . app . use ( router ) ; return this ; } public setEntryRoute ( routeDef ) { const { name } = routeDef . handler ( ) ; const viewHandler = ( _req , res ) => { res . render ( name , { basePath : this . basePath } ) ; } ; this . app [ routeDef . method ] ( routeDef . route , viewHandler ) ; return this ; } public setQueues ( bullBoardQueues ) { this . bullBoardQueues = bullBoardQueues ; return this ; } public getRouter ( ) { return this . app ; } }