feat(core): Added new method getEnvVarOnDemandStrict to throw error for unset env vars

This commit is contained in:
2024-11-22 18:47:50 +01:00
parent 5b1e3a184b
commit e25406662f
5 changed files with 7757 additions and 3940 deletions

View File

@@ -3,6 +3,6 @@
*/
export const commitinfo = {
name: '@push.rocks/qenv',
version: '6.0.8',
version: '6.1.0',
description: 'A module for easily handling environment variables in Node.js projects with support for .yml and .json configuration.'
}

View File

@@ -111,6 +111,21 @@ export class Qenv {
}
}
/**
* Like getEnvVarOnDemand, but throws an error if the env var is not set.
* @param envVarNameOrNames
* @returns
*/
public async getEnvVarOnDemandStrict(
envVarNameOrNames: TEnvVarRef | TEnvVarRef[]
): Promise<string> {
const value = await this.getEnvVarOnDemand(envVarNameOrNames);
if (!value) {
throw new Error(`Env var ${envVarNameOrNames} is not set!`);
}
return value;
}
public getEnvVarOnDemandSync(envVarNameOrNames: string | string[]): string | undefined {
console.warn('requesting env var sync leaves out potentially important async env sources.');