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.
105 lines (104 loc) • 4.86 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __read = (this && this.__read) || function (o, n) {
var m = typeof Symbol === "function" && o[Symbol.iterator];
if (!m) return o;
var i = m.call(o), r, ar = [], e;
try {
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
}
catch (error) { e = { error: error }; }
finally {
try {
if (r && !r.done && (m = i["return"])) m.call(i);
}
finally { if (e) throw e.error; }
}
return ar;
};
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ArrayParser = void 0;
var errors_1 = require("../errors");
var base_parser_1 = require("./base-parser");
var ArrayParser = (function (_super) {
__extends(ArrayParser, _super);
function ArrayParser(registry) {
var _this = _super.call(this) || this;
_this.registry = registry;
return _this;
}
ArrayParser.prototype.parse = function (context) {
var _this = this;
this._debug.info('Parsing array', context);
var parsed;
try {
parsed = JSON.parse(context.value);
this._debug.info('Parsed JSON successfully', parsed);
}
catch (error) {
this._debug.error('Error parsing JSON', error);
throw new errors_1.EnvironmentParseError(context.envKey, 'Invalid JSON format', context.path, error);
}
if (!Array.isArray(parsed)) {
this._debug.error('Not a valid JSON array', parsed);
throw new errors_1.EnvironmentParseError(context.envKey, 'Not a valid JSON array', context.path);
}
var arraySchema = context.schema;
var value = parsed.map(function (item, index) {
var itemCtx = {
envKey: "".concat(context.envKey, "[").concat(index, "]"),
path: __spreadArray(__spreadArray([], __read(context.path), false), [index.toString()], false),
schema: arraySchema.items,
value: JSON.stringify(item)
};
_this._debug.info("Parsing array item at index ".concat(index), itemCtx);
try {
var parsedItem = _this.registry.parse(itemCtx).value;
_this._debug.info("Parsed array item at index ".concat(index, " successfully"), parsedItem);
return parsedItem;
}
catch (error) {
_this._debug.error("Error parsing array item at index ".concat(index), error);
throw new errors_1.EnvironmentValidationError(context.envKey, "Item ".concat(index, ": ").concat(error.message), context.path);
}
});
this.validate(value, arraySchema, context);
this._debug.info('Parsed array successfully', value);
return { value: value };
};
ArrayParser.prototype.validate = function (value, schema, context) {
if (schema.minItems !== undefined && value.length < schema.minItems) {
throw new errors_1.EnvironmentValidationError(context.envKey, "Array length (".concat(value.length, ") is less than minimum length (").concat(schema.minItems, ")"), context.path);
}
if (schema.maxItems !== undefined && value.length > schema.maxItems) {
throw new errors_1.EnvironmentValidationError(context.envKey, "Array length (".concat(value.length, ") exceeds maximum length (").concat(schema.maxItems, ")"), context.path);
}
this.runCustomValidator(schema.validator, value, context);
this._debug.trace("Validated array for key: ".concat(context.envKey, ", value: ").concat(value));
};
return ArrayParser;
}(base_parser_1.BaseParser));
exports.ArrayParser = ArrayParser;