nestjs-dotenv
Version:
NestJS .env config Module
45 lines (44 loc) • 1.35 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const dotenv = require("dotenv");
const constants_1 = require("./constants");
class ConfigService {
constructor(path) {
this.reload(path);
}
get(key) {
return this.envConfig[key];
}
getWithType(key, type, enumObject) {
const value = this.envConfig[key];
switch (type) {
case 'number':
return Number(value);
case 'string':
return value;
case 'boolean':
return value.toLowerCase() === 'true';
case 'array':
return value.substr(1).substr(-1).split(',');
case 'enum':
if (!enumObject) {
throw new Error(constants_1.NO_ENUMOBJECT_ERROR);
}
return enumObject[value];
case 'object':
try {
const obj = JSON.parse(value);
return obj;
}
catch (error) {
return error;
}
default:
throw new Error(constants_1.UNKNOWN_TYPE);
}
}
reload(path) {
this.envConfig = dotenv.config(path ? { path } : null).parsed;
}
}
exports.ConfigService = ConfigService;