express-service-bootstrap
Version:
This is a convenience package for starting a express API with security, health checks, process exits etc.
75 lines • 3.21 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.EnvironmentVariables = void 0;
/**
* EnvironmentVariables class is a wrapper around NodeJS.ProcessEnv to provide type-safe access to environment variables.
*/
class EnvironmentVariables {
/**
* Creates a new instance of EnvironmentVariables.
* @param env The NodeJS.ProcessEnv object.
* @returns A new instance of EnvironmentVariables.
*/
constructor(env = process.env) {
this.env = env;
}
/**
* Gets the value of the environment variable.
* @param key The key of the environment variable.
* @returns The value of the environment variable or undefined.
*/
getString(key) {
return this.env[key];
}
/**
* Gets the value of the environment variable or the default value if the environment variable is not set.
* @param key The key of the environment variable.
* @param defaultValue The default value to return if the environment variable is not set.
* @returns The value of the environment variable or the default value.
*/
getStringOrDefault(key, defaultValue) {
return this.env[key] || defaultValue;
}
/**
* Gets the value of the environment variable as a number.
* @param key The key of the environment variable.
* @param defaultValue The default value to return if the environment variable is not set.
* @returns The value of the environment variable as a number or the default value.
*/
getNumber(key, defaultValue) {
const value = this.getString(key);
return value === undefined ? defaultValue : parseInt(value, 10);
}
/**
* Gets the value of the environment variable as a boolean.
* @param key The key of the environment variable.
* @param defaultValue The default value to return if the environment variable is not set.
* @returns The value of the environment variable as a boolean or the default value.
*/
getBoolean(key, defaultValue) {
const value = this.getString(key);
return value === undefined ? defaultValue : value === 'true';
}
/**
* Gets the value of the environment variable as an array.
* @param key The key of the environment variable.
* @param defaultValue The default value to return if the environment variable is not set.
* @returns The value of the environment variable as an array or the default value.
*/
getArray(key, defaultValue) {
const value = this.getString(key);
return value === undefined ? defaultValue : JSON.parse(value);
}
/**
* Gets the value of the environment variable as an object.
* @param key The key of the environment variable.
* @param defaultValue The default value to return if the environment variable is not set.
* @returns The value of the environment variable as an object or the default value.
*/
getObject(key, defaultValue) {
const value = this.getString(key);
return value === undefined ? defaultValue : JSON.parse(value);
}
}
exports.EnvironmentVariables = EnvironmentVariables;
//# sourceMappingURL=environment-variables.js.map