UNPKG

@expressots/core

Version:

Expressots - modern, fast, lightweight nodejs web framework (@core)

101 lines (100 loc) 4.3 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.EnvValidatorProvider = void 0; const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); const inversify_1 = require("../../di/inversify"); const logger_provider_1 = require("../logger/logger.provider"); const shared_1 = require("@expressots/shared"); /** * The EnvValidatorProvider class provides utility methods for working with environment variables. * It validates, loads, and retrieves environment variables from the .env file. * @provide EnvValidatorProvider * @public API */ let EnvValidatorProvider = class EnvValidatorProvider { constructor() { this.name = "Env Validator Provider"; this.version = "3.0.0"; this.author = "Richard Zampieri"; this.repo = "https://github.com/expressots/expressots"; this.logger = new logger_provider_1.Logger(); } /** * Retrieves the value of an environment variable, or a default value if the 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 if not set. * @public API */ get(key, defaultValue = undefined) { return process.env[key] ?? defaultValue; } /** * Validates and loads all environment variables from the .env file. * If the .env file does not exist or any environment variables are not set, the process will exit with an error. * @param envFile - * @public API */ checkFile(envFile) { // Get the full path of the .env file const envFilePath = path_1.default.join(process.cwd(), ".", envFile); // Check if the .env file exists if (!fs_1.default.existsSync(envFilePath)) { this.logger.error(`Environment file [${envFile}] is not defined.`, "environment-provider"); process.exit(1); } const dotEnvParsed = (0, shared_1.parse)(fs_1.default.readFileSync(envFilePath, "utf8")); /* Verify if all environment variables are defined */ let hasError = false; if (dotEnvParsed) { for (const key of Object.keys(dotEnvParsed)) { // Check if the environment variable is not defined or is an empty string if (!process.env[key] || process.env[key] === "") { this.logger.error(`Environment variable [ ${key} ] is not defined.`, "environment-provider"); hasError = true; } } } if (hasError) { process.exit(1); } } }; exports.EnvValidatorProvider = EnvValidatorProvider; exports.EnvValidatorProvider = EnvValidatorProvider = __decorate([ (0, inversify_1.injectable)(), __metadata("design:paramtypes", []) ], EnvValidatorProvider); String.prototype.AsBoolean = function () { switch (this.toLowerCase().trim()) { case "true": case "1": case "yes": return true; case "false": case "0": case "no": return false; default: return undefined; } }; String.prototype.AsNumber = function () { return Number(this); }; String.prototype.AsString = function () { return String(this); };