Skip to content

Commit

Permalink
feat: add ConfigService#getOrThrow()
Browse files Browse the repository at this point in the history
  • Loading branch information
jonahsnider committed Jun 2, 2022
1 parent 495ae12 commit e55f335
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions lib/config.service.ts
Expand Up @@ -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<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, R = PathValue<T, P>>(
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 no default value was provided an exception will be thrown.
* @param propertyPath
* @param defaultValue
*/
getOrThrow<T = any>(propertyPath: KeyOf<K>, defaultValue: NoInferType<T>): 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<T = K, P extends Path<T> = any, R = PathValue<T, P>>(
propertyPath: P,
defaultValue: NoInferType<R>,
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<T = any>(
propertyPath: KeyOf<K>,
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<T = any>(
propertyPath: KeyOf<K>,
defaultValue?: T,
Expand Down

1 comment on commit e55f335

@swang2019
Copy link

@swang2019 swang2019 commented on e55f335 Jul 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly what I want, nice job!

Please sign in to comment.