import express from "express" ; export type createLazyLoaderOptions = { /**
     * if preload is true, load router asap
     * Default: false
     */ preload ? : boolean ; } ; param options example export function createLazyRouter ( options = { } ) { const preload = options . preload ?? false ; param resolver return function lazyRouter ( resolver ) { const lazyRouter = express . Router ( ) ; let loadedRouter ; const resolveResolver = ( ) => { return resolver ( ) . then ( ( router ) => { if ( "default" in router ) { loadedRouter = router . default ; } else { loadedRouter = router ; } } ) ; } ; lazyRouter . use ( ( req , res , next ) => { if ( loadedRouter ) { return loadedRouter ( req , res , next ) ; } else { resolveResolver ( ) . then ( ( ) => { return loadedRouter ( req , res , next ) ; } ) . catch ( ( error ) => { next ( error ) ; } ) ; } } ) ; if ( preload ) { resolveResolver ( ) . catch ( ( error ) => { console . error ( "[lazy-router] Fail to preload" , error ) ; } ) ; } return lazyRouter ; } ; }