ts-convict
Version:
Model style decorators for your convict config.
110 lines (109 loc) • 4.03 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.TSConvict = void 0;
const Reflector_1 = __importDefault(require("./Reflector"));
const convict_1 = __importDefault(require("convict"));
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
class TSConvict {
constructor(ConfigClass) {
this.opts = {};
if (!Reflector_1.default.isConfigClass) {
throw new Error("A class must be decorated with Config or at least one Property");
}
const opts = Reflector_1.default.getConvictMetaForClass(ConfigClass);
if (opts !== null) {
if (typeof opts.parser !== "undefined") {
convict_1.default.addParser(opts.parser);
}
if (typeof opts.formats !== "undefined") {
convict_1.default.addFormats(opts.formats);
}
this.opts = opts;
}
this.baseModel = new ConfigClass();
this.schema = this.getSchemaFor(this.baseModel);
this.client = convict_1.default(this.schema);
}
load(config = null) {
let baseFile = null;
if (typeof this.opts.file !== "undefined" &&
fs_1.default.existsSync(path_1.default.resolve(this.opts.file))) {
baseFile = this.opts.file;
}
if (typeof config === "string" || Array.isArray(config)) {
let files;
if (Array.isArray(config)) {
files = config;
}
else {
files = [config];
}
if (baseFile !== null) {
files.unshift(baseFile);
}
this.client.loadFile(files);
}
else if (config === null) {
if (baseFile !== null) {
this.client.loadFile(baseFile);
}
else {
this.client.load({});
}
}
else if (typeof config === "object") {
if (baseFile !== null) {
this.client.loadFile(baseFile);
}
this.client.load(config);
}
else {
throw new Error(`Could not load the config given: ${config}`);
}
const allowed = this.opts.validationMethod === undefined
? "strict"
: this.opts.validationMethod;
this.client.validate({ allowed });
const rawConfig = this.client.getProperties();
return this.applyDataToModel(this.baseModel, rawConfig);
}
getSchemaFor(target) {
const schema = {};
for (const key of Reflector_1.default.getClassProperties(target)) {
const meta = Reflector_1.default.getConvictMetaForProperty(target, key);
if (Reflector_1.default.isConstructor(meta) && Reflector_1.default.isConfigClass(meta)) {
try {
target[key] = new meta();
schema[key] = this.getSchemaFor(target[key]);
}
catch (error) {
console.error(error);
throw new Error("Could not make a new class of " + meta);
}
}
else {
schema[key] = meta;
}
}
return schema;
}
applyDataToModel(target, config) {
for (const key of Reflector_1.default.getClassProperties(target)) {
const meta = Reflector_1.default.getConvictMetaForProperty(target, key);
if (Reflector_1.default.isConstructor(meta) && Reflector_1.default.isConfigClass(meta)) {
this.applyDataToModel(target[key], config[key] || {});
}
else {
if (key in config) {
target[key] = config[key];
}
}
}
return target;
}
}
exports.TSConvict = TSConvict;