@@ -14,29 +14,23 @@ import { LoggingModule } from '../modules/logging/logging.module.js';
1414import { PrismaModule } from '../modules/prisma/prisma.module.js' ;
1515import { ValidationPipe } from '../pipes/validation.pipe.js' ;
1616
17- import type { RuntimeEnv } from '../../config/schema.js' ;
17+ import type { BaseEnv } from '../../config/schema.js' ;
1818import type { PrismaModuleOptions } from '../modules/prisma/prisma.config.js' ;
1919
2020type ImportedModule = DynamicModule | Type < any > ;
2121
22- type ConditionalImport = {
22+ type ConditionalImport < TEnv extends BaseEnv = BaseEnv > = {
2323 module : ImportedModule ;
24- when : ConditionalKeys < RuntimeEnv , boolean | undefined > ;
24+ when : ConditionalKeys < TEnv , boolean | undefined > ;
2525} ;
2626
27- const isConditionalImport = ( value : { [ key : string ] : any } ) : value is ConditionalImport => {
28- return isObjectLike ( value . module ) && typeof value . when === 'string' ;
29- } ;
30-
31- type Import = ConditionalImport | ImportedModule ;
32-
3327export type DynamicAppModule = DynamicModule & {
3428 module : typeof AppModule ;
3529} ;
3630
37- export type CreateAppModuleOptions = {
38- envConfig : RuntimeEnv ;
39- imports ?: Import [ ] ;
31+ export type CreateAppModuleOptions < TEnv extends BaseEnv = BaseEnv > = {
32+ envConfig : TEnv ;
33+ imports ?: ( ConditionalImport < TEnv > | ImportedModule ) [ ] ;
4034 prisma : PrismaModuleOptions ;
4135 providers ?: Provider [ ] ;
4236} ;
@@ -45,7 +39,12 @@ export class AppModule implements NestModule {
4539 @Inject ( )
4640 private readonly configService : ConfigService ;
4741
48- static create ( { envConfig, imports = [ ] , prisma, providers = [ ] } : CreateAppModuleOptions ) : DynamicAppModule {
42+ static create < TEnv extends BaseEnv > ( {
43+ envConfig,
44+ imports : imports_ = [ ] ,
45+ prisma,
46+ providers = [ ]
47+ } : CreateAppModuleOptions < TEnv > ) : DynamicAppModule {
4948 const coreImports : ImportedModule [ ] = [
5049 ConfigModule . forRoot ( { envConfig } ) ,
5150 CryptoModule . forRoot ( ) ,
@@ -89,20 +88,19 @@ export class AppModule implements NestModule {
8988 } ) ;
9089 }
9190
91+ const imports : ImportedModule [ ] = [ ] ;
92+ for ( const import_ of imports_ as { [ key : string ] : any } [ ] ) {
93+ if ( isObjectLike ( import_ . module ) && typeof import_ . when === 'string' ) {
94+ if ( envConfig [ import_ . when as keyof typeof envConfig ] ) {
95+ imports . push ( import_ . module as ImportedModule ) ;
96+ }
97+ } else {
98+ imports . push ( import_ as ImportedModule ) ;
99+ }
100+ }
101+
92102 return {
93- imports : [
94- ...coreImports ,
95- ...imports
96- . map ( ( value ) => {
97- if ( ! isConditionalImport ( value ) ) {
98- return value ;
99- } else if ( envConfig [ value . when ] ) {
100- return value . module ;
101- }
102- return null ;
103- } )
104- . filter ( ( value ) => value !== null )
105- ] ,
103+ imports : [ ...coreImports , ...imports ] ,
106104 module : AppModule ,
107105 providers : [ ...coreProviders , ...providers ]
108106 } ;
0 commit comments