Skip to content

Commit

Permalink
Merge pull request #941 from jonahsnider/get-or-throw
Browse files Browse the repository at this point in the history
feat: add ConfigService#getOrThrow()
  • Loading branch information
kamilmysliwiec committed Jun 2, 2022
2 parents 20993a1 + c954b99 commit 7df0717
Showing 1 changed file with 67 additions and 0 deletions.
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

0 comments on commit 7df0717

Please sign in to comment.