UNPKG

typed-environment-loader

Version:

typed-environment-loader is a lightweight utility for loading environment variables in a typed manner, ensuring type safety and consistency in your Node.js applications.

54 lines (53 loc) 2.53 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ParserRegistry = void 0; var debug_1 = require("../debug"); var errors_1 = require("../errors"); var _1 = require("./"); var ParserRegistry = (function () { function ParserRegistry() { this._parsers = new Map(); this._debug = (0, debug_1.createDebugLogger)(this.constructor.name); this._debug.info('Initializing ParserRegistry'); this._registerDefaultParsers(); this._debug.info('ParserRegistry initialized with default parsers'); } Object.defineProperty(ParserRegistry.prototype, "parsers", { get: function () { this._debug.info('Retrieving all registered parsers'); return this._parsers; }, enumerable: false, configurable: true }); ParserRegistry.prototype.register = function (type, parserClass) { this._debug.info("Attempting to register parser for type: ".concat(type)); this._parsers.set(type, parserClass); this._debug.info("Registered parser for type: ".concat(type, " (").concat(parserClass.name, ")")); return this; }; ParserRegistry.prototype.parse = function (context) { this._debug.info("Parsing ".concat(context.envKey, " with type: ").concat(context.schema.type)); var ParserClass = this._parsers.get(context.schema.type); if (!ParserClass) { this._debug.error("No parser registered for type: ".concat(context.schema.type)); throw new errors_1.EnvironmentParseError(context.envKey, "No parser registered for type: ".concat(context.schema.type), context.path); } this._debug.info("Found parser for type: ".concat(context.schema.type, " (").concat(ParserClass.name, ")")); var parser = new ParserClass(this); var result = parser.parse(context); this._debug.info("Successfully parsed ".concat(context.envKey, " with type: ").concat(context.schema.type)); return result; }; ParserRegistry.prototype._registerDefaultParsers = function () { this._debug.info('Registering default parsers'); this.register('string', _1.StringParser) .register('number', _1.NumberParser) .register('boolean', _1.BooleanParser) .register('enum', _1.EnumParser) .register('array', _1.ArrayParser); this._debug.info('Default parsers registered'); }; return ParserRegistry; }()); exports.ParserRegistry = ParserRegistry;