diff --git a/lib/config.service.ts b/lib/config.service.ts index f92dac21..ba172c6d 100644 --- a/lib/config.service.ts +++ b/lib/config.service.ts @@ -122,6 +122,70 @@ export class ConfigService< return defaultValue as T; } + /** + * Get a configuration value (either custom configuration or process environment variable) + * based on property path (you can use dot notation to traverse nested object, e.g. "database.host"). + * @param propertyPath + */ + getOrThrow(propertyPath: KeyOf): Exclude; + /** + * Get a configuration value (either custom configuration or process environment variable) + * based on property path (you can use dot notation to traverse nested object, e.g. "database.host"). + * @param propertyPath + * @param options + */ + getOrThrow = any, R = PathValue>( + propertyPath: P, + options: ConfigGetOptions, + ): Exclude; + /** + * Get a configuration value (either custom configuration or process environment variable) + * based on property path (you can use dot notation to traverse nested object, e.g. "database.host"). + * It returns a default value if the key does not exist. + * If no default value was provided an exception will be thrown. + * @param propertyPath + * @param defaultValue + */ + getOrThrow(propertyPath: KeyOf, defaultValue: NoInferType): T; + /** + * Get a configuration value (either custom configuration or process environment variable) + * based on property path (you can use dot notation to traverse nested object, e.g. "database.host"). + * It returns a default value if the key does not exist. + * If no default value was provided an exception will be thrown. + * @param propertyPath + * @param defaultValue + * @param options + */ + getOrThrow = any, R = PathValue>( + propertyPath: P, + defaultValue: NoInferType, + options: ConfigGetOptions, + ): R; + /** + * Get a configuration value (either custom configuration or process environment variable) + * based on property path (you can use dot notation to traverse nested object, e.g. "database.host"). + * It returns a default value if the key does not exist. + * If no default value was provided an exception will be thrown. + * @param propertyPath + * @param defaultValueOrOptions + */ + getOrThrow( + propertyPath: KeyOf, + defaultValueOrOptions?: T | ConfigGetOptions, + options?: ConfigGetOptions, + ): T { + // @ts-expect-error Bypass method overloads + const value = this.get(propertyPath, defaultValueOrOptions, options) as T | undefined; + + if (isUndefined(value)) { + throw new TypeError( + `Configuration key "${propertyPath}" does not exist`, + ); + } + + return value; + } + private getFromCache( propertyPath: KeyOf, defaultValue?: T,