Skip to content

Commit 2f04ff8

Browse files
committed
fix: circular type ref
1 parent d1d6e00 commit 2f04ff8

File tree

2 files changed

+25
-27
lines changed

2 files changed

+25
-27
lines changed

src/core/app/app.factory.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import type { CreateAppModuleOptions } from './app.module.js';
1414
type EnvSchema = z.ZodType<BaseEnv, z.ZodTypeDef, { [key: string]: string }>;
1515

1616
export type CreateAppOptions<TEnvSchema extends EnvSchema = EnvSchema> = Simplify<
17-
Omit<CreateAppModuleOptions, 'envConfig'> &
17+
Omit<CreateAppModuleOptions<z.TypeOf<TEnvSchema>>, 'envConfig'> &
1818
Pick<CreateAppContainerOptions, 'docs' | 'version'> & {
1919
envSchema: TEnvSchema;
2020
}

src/core/app/app.module.ts

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,29 +14,23 @@ import { LoggingModule } from '../modules/logging/logging.module.js';
1414
import { PrismaModule } from '../modules/prisma/prisma.module.js';
1515
import { ValidationPipe } from '../pipes/validation.pipe.js';
1616

17-
import type { RuntimeEnv } from '../../config/schema.js';
17+
import type { BaseEnv } from '../../config/schema.js';
1818
import type { PrismaModuleOptions } from '../modules/prisma/prisma.config.js';
1919

2020
type 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-
3327
export 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

Comments
 (0)