diff --git a/lib/config.module.ts b/lib/config.module.ts index 91364fac..dfcfbda5 100644 --- a/lib/config.module.ts +++ b/lib/config.module.ts @@ -125,7 +125,7 @@ export class ConfigModule { * Registers configuration object (partial registration). * @param config */ - static forFeature(config: ConfigFactory) { + static forFeature(config: ConfigFactory): DynamicModule { const configProvider = createConfigProvider( config as ConfigFactory & ConfigFactoryKeyHost, ); diff --git a/lib/config.service.ts b/lib/config.service.ts index d59931b7..b49a32e2 100644 --- a/lib/config.service.ts +++ b/lib/config.service.ts @@ -19,7 +19,7 @@ export class ConfigService> { this._isCacheEnabled = value; } - private readonly cache: K = {} as any; + private readonly cache: Partial = {} as any; private _isCacheEnabled = false; constructor( @@ -52,20 +52,12 @@ export class ConfigService> { * @param defaultValue */ get(propertyPath: keyof K, defaultValue?: T): T | undefined { - if ( - this.isCacheEnabled && - has(this.cache as Record, propertyPath) - ) { - const cachedValue = this.getFromCache(propertyPath, defaultValue); - return !isUndefined(cachedValue) ? cachedValue : defaultValue; - } - const validatedEnvValue = this.getFromValidatedEnv(propertyPath); if (!isUndefined(validatedEnvValue)) { return validatedEnvValue; } - const processEnvValue = this.getFromProcessEnv(propertyPath); + const processEnvValue = this.getFromProcessEnv(propertyPath, defaultValue); if (!isUndefined(processEnvValue)) { return processEnvValue; } @@ -93,26 +85,35 @@ export class ConfigService> { this.internalConfig[VALIDATED_ENV_PROPNAME], propertyPath, ); - this.setInCache(propertyPath, validatedEnvValue); - return (validatedEnvValue as unknown) as T; } - private getFromProcessEnv(propertyPath: keyof K): T | undefined { + private getFromProcessEnv( + propertyPath: keyof K, + defaultValue: any, + ): T | undefined { + if ( + this.isCacheEnabled && + has(this.cache as Record, propertyPath) + ) { + const cachedValue = this.getFromCache(propertyPath, defaultValue); + return !isUndefined(cachedValue) ? cachedValue : defaultValue; + } const processValue = get(process.env, propertyPath); - this.setInCache(propertyPath, processValue); + this.setInCacheIfDefined(propertyPath, processValue); return (processValue as unknown) as T; } private getFromInternalConfig(propertyPath: keyof K): T | undefined { const internalValue = get(this.internalConfig, propertyPath); - this.setInCache(propertyPath, internalValue); - return internalValue; } - private setInCache(propertyPath: keyof K, value: any): void { + private setInCacheIfDefined(propertyPath: keyof K, value: any): void { + if (typeof value === 'undefined') { + return; + } set(this.cache as Record, propertyPath, value); } } diff --git a/tests/src/app.module.ts b/tests/src/app.module.ts index 93df7be1..e2bced52 100644 --- a/tests/src/app.module.ts +++ b/tests/src/app.module.ts @@ -23,6 +23,7 @@ export class AppModule { ConfigModule.forRoot({ cache: true, envFilePath: join(__dirname, '.env'), + load: [databaseConfig], }), ], };