@openinc/parse-server-opendash
Version:
Parse Server Cloud Code for open.INC Stack.
191 lines (190 loc) • 6.31 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ConfigState = void 0;
const table_1 = require("table");
const __1 = require("..");
/**
* Config class to handle configuration values from environment variables.
* It supports different types of values (string, int, float, boolean) and provides methods to get the values.
*
*/
class ConfigState {
constructor(configs) {
this.configs = {};
this.baseConfigs = {};
this.values = {};
this.configs = configs;
this.baseConfigs = __1.baseoptions;
}
async init(log) {
for (const [key, c] of Object.entries(this.baseConfigs)) {
if (c.type === "string") {
this.values[key] = getEnv(c.env, c.default);
}
if (c.type === "int") {
this.values[key] = getEnvInt(c.env, c.default);
}
if (c.type === "float") {
this.values[key] = getEnvFloat(c.env, c.default);
}
if (c.type === "boolean") {
this.values[key] = getEnvBoolean(c.env, c.default);
}
}
//Load values from custom config
for (const [key, c] of Object.entries(this.configs)) {
if (c.type === "string") {
this.values[key] = getEnv(c.env, c.default);
}
if (c.type === "int") {
this.values[key] = getEnvInt(c.env, c.default);
}
if (c.type === "float") {
this.values[key] = getEnvFloat(c.env, c.default);
}
if (c.type === "boolean") {
this.values[key] = getEnvBoolean(c.env, c.default);
}
}
if (log)
this.log();
// validation:
for (const [key, c] of Object.entries(this.configs)) {
if (c.required === true) {
this.validateKey(key);
}
if ("dependencies" in c &&
Array.isArray(c.dependencies) &&
this.getBoolean(key)) {
for (const dependency of c.dependencies) {
this.validateKey(dependency);
}
}
}
}
get(key) {
this.validateKeyType(key, "string");
return this.values[key];
}
getBoolean(key) {
this.validateKeyType(key, "boolean");
return this.values[key];
}
getNumber(key) {
this.validateKeyType(key, "int", "float");
return this.values[key];
}
export() {
const result = {};
for (const [key, c] of Object.entries(this.configs)) {
if (c.secret)
continue;
if (!c.public)
continue;
if (c.type === "string") {
result[key] = this.get(key);
}
if (c.type === "int") {
result[key] = this.getNumber(key);
}
if (c.type === "float") {
result[key] = this.getNumber(key);
}
if (c.type === "boolean") {
result[key] = this.getBoolean(key);
}
}
return result;
}
validateKey(key) {
if (!(key in this.values) || this.values[key] === undefined) {
throw new Error(`Invalid config, '${key}' is required.`);
}
}
validateKeyType(key, ...types) {
const t = this.configs[key]?.type;
if (!types.includes(t)) {
console.error(new Error(`Invalid getter for config key '${key}' is of type ${t} but was called as ${types.join("|")}.`));
process.exit(1);
}
}
log() {
const tableBaseConfig = {
header: {
alignment: "center",
content: "PARSE BASE CONFIGURATION\nStart Parse Server with the following base configuration.",
},
columnDefault: {
paddingLeft: 0,
paddingRight: 0,
width: 25,
wrapWord: true,
},
// singleLine: true,
columns: [
{ width: 50 },
{ width: 54 },
{ width: 15 },
{ width: 40, truncate: 40 },
],
};
const tableDataBaseConfig = Object.entries(this.baseConfigs).map(([key, c]) => {
const value = this.values[key]?.toString() || "undefined";
return [
key,
c.env,
c.type,
this.baseConfigs[key]?.secret && value !== "undefined"
? "****"
: value,
];
});
console.log((0, table_1.table)(tableDataBaseConfig, tableBaseConfig));
const tableConfig = {
header: {
alignment: "center",
content: "PARSE CUSTOM CONFIGURATION\nStart Parse Server with the following custom configuration.",
},
columnDefault: {
paddingLeft: 0,
paddingRight: 0,
width: 25,
wrapWord: true,
},
// singleLine: true,
columns: [
{ width: 50 },
{ width: 54 },
{ width: 15 },
{ width: 40, truncate: 40 },
],
};
const tableData = Object.entries(this.configs).map(([key, c]) => {
const value = this.values[key]?.toString() || "undefined";
return [
key,
c.env,
c.type,
this.configs[key]?.secret && value !== "undefined" ? "****" : value,
];
});
console.log((0, table_1.table)(tableData, tableConfig));
}
}
exports.ConfigState = ConfigState;
function getEnv(key, fallback) {
return process.env[key] || fallback;
}
function getEnvInt(key, fallback = "") {
return parseInt(getEnv(key, fallback));
}
function getEnvFloat(key, fallback = "") {
return parseFloat(getEnv(key, fallback));
}
function getEnvBoolean(key, fallback = "false") {
const value = getEnv(key, fallback);
if (!value || value.toLowerCase() === "false" || value === "0") {
return false;
}
return true;
}