UNPKG

@miup/nest-config

Version:

<p align="center"> <a href="http://nestjs.com/" target="blank"><img src="https://nestjs.com/img/logo_text.svg" width="320" alt="Nest Logo" /></a> </p>

78 lines (77 loc) 2.19 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ConfigService = void 0; const dotenv = require("dotenv-extended"); const path = require("path"); const common_1 = require("@nestjs/common"); class ConfigService { constructor(option) { this.logger = new common_1.Logger(ConfigService.name); const defaultOption = { path: path.join(process.cwd(), 'env'), encoding: 'utf-8', basename: 'environment', default: 'default', env: process.env.NODE_ENV, }; option = Object.assign(defaultOption, option); const dotenvOp = { encoding: option.encoding, silent: true, defaults: path.join(option.path, `${option.basename}.${option.default}.env`), errorOnMissing: false, errorOnExtra: false, includeProcessEnv: false, assignToProcessEnv: true, overrideProcessEnv: false, }; if (option.env) { dotenvOp.path = path.join(option.path, `${option.basename}.${option.env}.env`); } this.envConfig = dotenv.load(dotenvOp); } get(key, defaultValue) { const v = this.envConfig[key]; if (v) { return v; } else { return defaultValue; } } getString(key, defaultValue) { const v = this.envConfig[key]; if (v) { return v; } else { return defaultValue; } } getNumber(key, defaultValue) { const v = this.envConfig[key]; if (v) { return parseFloat(v); } else { return defaultValue; } } getBoolean(key, defaultValue) { const v = this.envConfig[key]; if (v) { return v.toUpperCase() === 'TRUE'; } else { return defaultValue; } } getStringArray(key, defaultValue) { const v = this.envConfig[key]; if (!v) { return defaultValue || []; } return v.split(','); } } exports.ConfigService = ConfigService;