Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add ConfigService#getOrThrow() #941

Merged
merged 4 commits into from Jun 2, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
67 changes: 67 additions & 0 deletions lib/config.service.ts
Expand Up @@ -122,6 +122,73 @@ 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<T = any>(propertyPath: KeyOf<K>): Exclude<T, undefined>;
/**
* 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<T = K, P extends Path<T> = any>(
propertyPath: P,
options: ConfigGetOptions,
): Exclude<T, undefined>;
/**
* 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 the default value is undefined an exception will be thrown.
* @param propertyPath
* @param defaultValue
*/
getOrThrow<T = any>(
propertyPath: KeyOf<K>,
defaultValue: NoInferType<T>,
): Exclude<T, undefined>;
/**
* 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 the default value is undefined an exception will be thrown.
* @param propertyPath
* @param defaultValue
* @param options
*/
getOrThrow<T = K, P extends Path<T> = any, R = PathValue<T, P>>(
propertyPath: P,
defaultValue: NoInferType<R>,
options: ConfigGetOptions,
): Exclude<R, undefined>;
/**
* 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 the default value is undefined an exception will be thrown.
* @param propertyPath
* @param defaultValueOrOptions
*/
getOrThrow<T = any>(
propertyPath: KeyOf<K>,
defaultValueOrOptions?: T | ConfigGetOptions,
options?: ConfigGetOptions,
): Exclude<T, undefined> {
// @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.toString()}" does not exist`);
}

return value as Exclude<T, undefined>;
}

private getFromCache<T = any>(
propertyPath: KeyOf<K>,
defaultValue?: T,
Expand Down