import { Application , Container } from '@tiejs/common' ; import { PluginStoreBuilder } from './builders/PluginStoreBuilder' ; import { MiddlewareStoreBuilder } from './builders/MiddlewareStoreBuilder' ; export interface Options { port : number ; } export class Loader { constructor ( private app ) { this . app = app ; } async init ( ) { const pluginStoreBuilder = Container . get ( PluginStoreBuilder ) ; this . app . pluginStore = await pluginStoreBuilder . createPluginStore ( ) ; await this . initPluginStore ( ) ; const middlewareStoreBuilder = Container . get ( MiddlewareStoreBuilder ) ; this . app . middlewareStore = await middlewareStoreBuilder . createMiddlewareStore ( ) ; this . applyBeforeMiddleware ( ) ; await this . applyMiddlewareDidReady ( ) ; } private async initPluginStore ( ) { for ( const item of this . app . pluginStore ) { if ( item . configDidLoad ) { await item . configDidLoad . call ( item . instance , this . app ) ; } } for ( const item of this . app . pluginStore ) { if ( item . appDidReady ) { await item . appDidReady . call ( item . instance , this . app ) ; } } } private async applyBeforeMiddleware ( ) { for ( const item of this . app . middlewareStore ) { if ( ! item . enable ) continue ; if ( ! item . matcher ) { this . app . use ( item . use as any ) ; } else { this . app . use ( async ( req , res , next ) => { if ( req . path . includes ( item . matcher ?. path || '' ) ) { await ( item . use as any ) ( req , res , next ) ; } else { next ( ) ; } } ) ; } } } private async applyMiddlewareDidReady ( ) { for ( const item of this . app . pluginStore ) { if ( item . middlewareDidReady ) { await item . middlewareDidReady . call ( item . instance , this . app ) ; } } } }