ts-api-core
Version:
Nodejs api framework core
64 lines • 2.73 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.BaseConfig = void 0;
const model_helper_1 = require("../utils/model.helper");
/** 应用基础配置 */
class BaseConfig {
/** 解析环境变量中的数字 */
static parseEnvNumber(env, defaultValue = 0) {
const v = Number(env === null || env === void 0 ? void 0 : env.trim());
return Number.isNaN(v) ? defaultValue : v;
}
/** 解析环境变量中的字符串 */
static parseEnvStr(env, defaultValue = '') {
return env !== null && env !== void 0 ? env : defaultValue;
}
/** 解析环境变量中的对象 */
static parseEnvObject(env, target, defaultValue, mergeAllField = true) {
// eslint-disable-next-line no-console
console.log(target.name, env);
if (env) {
const data = JSON.parse(env);
const value = new target();
model_helper_1.ModelHelper.initModel(data, value, mergeAllField, false, false);
return value;
}
else {
return defaultValue !== null && defaultValue !== void 0 ? defaultValue : new target();
}
}
/** 初始化数据库配置 */
static initDBConfig(env, defaultConfig) {
var _a, _b, _c, _d, _e;
// eslint-disable-next-line no-console
console.log('dbConfig', env);
if (env) {
const data = JSON.parse(env);
return {
host: (_a = data.host) !== null && _a !== void 0 ? _a : '',
port: (_b = data.port) !== null && _b !== void 0 ? _b : defaultConfig.port,
user: (_c = data.user) !== null && _c !== void 0 ? _c : "root",
password: (_d = data.password) !== null && _d !== void 0 ? _d : '',
database: data.database,
connectTimeout: (_e = data.connectTimeout) !== null && _e !== void 0 ? _e : 15000,
};
}
return defaultConfig;
}
/** 初始化阿里云OSS配置 */
static initOssConfig(env, defaultConfig) {
var _a, _b, _c, _d;
if (env) {
const data = JSON.parse(env);
return {
accessKeyId: (_a = data.accessKeyId) !== null && _a !== void 0 ? _a : '',
accessKeySecret: (_b = data.accessKeySecret) !== null && _b !== void 0 ? _b : '',
region: (_c = data.region) !== null && _c !== void 0 ? _c : '',
bucket: (_d = data.bucket) !== null && _d !== void 0 ? _d : '',
};
}
return defaultConfig;
}
}
exports.BaseConfig = BaseConfig;
//# sourceMappingURL=base.config.js.map