import AdminJS from 'adminjs' ; import { Injectable } from '@nestjs/common' ; import { AbstractHttpAdapter } from '@nestjs/core' ; import { loadPackage } from '@nestjs/common/utils/load-package.util' ; import { AdminModuleOptions } from '../interfaces/admin-module-options.interface' ; import { AbstractLoader } from './abstract.loader' ; @ Injectable ( ) export class ExpressLoader extends AbstractLoader { public register ( admin , httpAdapter , options ) { const app = httpAdapter . getInstance ( ) ; loadPackage ( 'express' , '@adminjs/nestjs' ) ; const adminJsExpressjs = loadPackage ( '@adminjs/express' , '@adminjs/nestjs' , ( ) => require ( '@adminjs/express' ) ) ; loadPackage ( 'express-formidable' , '@adminjs/nestjs' ) ; let router ; if ( 'auth' in options ) { loadPackage ( 'express-session' , '@adminjs/nestjs' ) ; router = adminJsExpressjs . buildAuthenticatedRouter ( admin , options . auth , undefined , options . sessionOptions , options . formidableOptions ) ; } else { router = adminJsExpressjs . buildRouter ( admin , undefined , options . formidableOptions ) ; } app . use ( options . adminJsOptions . rootPath , function admin ( req , res , next ) { return router ( req , res , next ) ; } ) ; this . reorderRoutes ( app ) ; } private reorderRoutes ( app ) { let jsonParser ; let urlencodedParser ; let admin ; if ( app && app . _router && app . _router . stack ) { const jsonParserIndex = app . _router . stack . findIndex ( ( layer ) => layer . name === 'jsonParser' ) ; if ( jsonParserIndex >= 0 ) { jsonParser = app . _router . stack . splice ( jsonParserIndex , 1 ) ; } const urlencodedParserIndex = app . _router . stack . findIndex ( ( layer ) => layer . name === 'urlencodedParser' ) ; if ( urlencodedParserIndex >= 0 ) { urlencodedParser = app . _router . stack . splice ( urlencodedParserIndex , 1 ) ; } const adminIndex = app . _router . stack . findIndex ( ( layer ) => layer . name === 'admin' ) ; if ( adminIndex >= 0 ) { admin = app . _router . stack . splice ( adminIndex , 1 ) ; } const corsIndex = app . _router . stack . findIndex ( ( layer ) => layer . name === 'corsMiddleware' ) ; const expressInitIndex = app . _router . stack . findIndex ( ( layer ) => layer . name === 'expressInit' ) ; const initIndex = ( corsIndex >= 0 ? corsIndex : expressInitIndex ) + 1 ; app . _router . stack . splice ( initIndex , 0 , ... admin , ... jsonParser , ... urlencodedParser ) ; } } }