@fangcha/backend-kit
Version:
Backend Kit.
73 lines • 3.52 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.FangchaApp = void 0;
const tslib_1 = require("tslib");
const _FangchaState_1 = require("./_FangchaState");
const CustomRequestFollower_1 = require("./CustomRequestFollower");
const logger_1 = require("@fangcha/logger");
const bot_kit_1 = require("@fangcha/bot-kit");
class FangchaApp {
constructor(protocol) {
this.protocol = protocol;
}
launch() {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
_FangchaState_1._FangchaState.appName = this.protocol.appName;
_FangchaState_1._FangchaState.env = this.protocol.env;
_FangchaState_1._FangchaState.tags = this.protocol.tags || [];
_FangchaState_1._FangchaState.retainHealthWord = this.protocol.retainHealthWord || '';
_FangchaState_1._FangchaState._checkHealthHandler = this.protocol.checkHealth || (() => tslib_1.__awaiter(this, void 0, void 0, function* () { }));
(0, logger_1.initLoggerForApp)(this.protocol.appName);
if (this.protocol.feishuBotKey) {
const proxy = new bot_kit_1.FeishuBot({});
if (!['staging', 'production'].includes(this.protocol.env)) {
proxy.setMuteMode(true);
}
proxy.setTag(this.protocol.env);
proxy.setRetainedBotKey(this.protocol.feishuBotKey);
proxy.setAppName(this.protocol.appName);
_FangchaState_1._FangchaState.botProxy = proxy;
CustomRequestFollower_1.CustomRequestFollower.botProxy = proxy;
}
else if (this.protocol.wecomBotKey) {
const proxy = new bot_kit_1.WecomProxy({});
if (!['staging', 'production'].includes(this.protocol.env)) {
proxy.setMuteMode(true);
}
proxy.setTag(this.protocol.env);
proxy.setRetainedBotKey(this.protocol.wecomBotKey);
proxy.setAppName(this.protocol.appName);
_FangchaState_1._FangchaState.botProxy = proxy;
CustomRequestFollower_1.CustomRequestFollower.botProxy = proxy;
}
const plugins = this.protocol.plugins || [];
for (const plugin of plugins) {
if (plugin.appWillLoad) {
yield plugin.appWillLoad(this.protocol);
}
}
const appDidLoad = this.protocol.appDidLoad || (() => tslib_1.__awaiter(this, void 0, void 0, function* () { }));
yield appDidLoad().catch((err) => {
console.error(err);
if (_FangchaState_1._FangchaState.botProxy) {
_FangchaState_1._FangchaState.botProxy.notify(['App Loading Error', err.message].join('\n'));
}
throw err;
});
for (const plugin of plugins) {
yield plugin.appDidLoad(this.protocol);
}
if (this.protocol.checkHealth) {
yield this.protocol.checkHealth();
}
for (const plugin of plugins) {
if (plugin.checkHealth) {
yield plugin.checkHealth();
}
}
_FangchaState_1._FangchaState.botProxy.notify(`[${_FangchaState_1._FangchaState.tags.join(', ')}] App launched.`);
});
}
}
exports.FangchaApp = FangchaApp;
//# sourceMappingURL=FangchaApp.js.map