n8n-nodes-feishu-lark
Version:
n8n custom nodes for n8n to interact with Feishu/Lark, including Lark Bot, Lark MCP, and Lark Trigger.
86 lines • 3.44 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.EventDispatcher = void 0;
const cache_1 = require("./cache");
const request_handle_1 = __importDefault(require("./request-handle"));
const consts_1 = require("../consts");
class EventDispatcher {
constructor(params) {
this.verificationToken = '';
this.encryptKey = '';
this.handles = new Map();
const { encryptKey, verificationToken, logger } = params;
this.logger = logger;
this.isAnyEvent = params.isAnyEvent;
this.encryptKey = encryptKey || '';
this.verificationToken = verificationToken || '';
this.requestHandle = new request_handle_1.default({
logger: this.logger,
encryptKey,
verificationToken,
});
this.cache = cache_1.internalCache;
this.registerAppTicketHandle();
this.logger.info('event-dispatch is ready');
}
registerAppTicketHandle() {
this.register({
app_ticket: async (data) => {
const { app_ticket, app_id } = data;
if (app_ticket) {
await this.cache.set(consts_1.CAppTicket, app_ticket, undefined, {
namespace: app_id,
});
this.logger.debug('set app ticket');
}
else {
this.logger.warn('response not include app ticket');
}
},
});
}
register(handles) {
Object.keys(handles).forEach((key) => {
if (this.handles.has(key) && key !== consts_1.CAppTicketHandle) {
this.logger.debug(`this ${key} handle is registered`);
}
const handle = handles[key];
if (handle) {
this.handles.set(key, handle);
}
else {
this.logger.warn(`Handle for key ${key} is undefined and will not be registered`);
}
this.logger.debug(`register ${key} handle`);
});
return this;
}
async invoke(data, params) {
var _a, _b;
const needCheck = (params === null || params === void 0 ? void 0 : params.needCheck) === false ? false : true;
if (needCheck && !((_a = this.requestHandle) === null || _a === void 0 ? void 0 : _a.checkIsEventValidated(data))) {
this.logger.warn('event verification failed');
return undefined;
}
const targetData = (_b = this.requestHandle) === null || _b === void 0 ? void 0 : _b.parse(data);
this.logger.debug(`Event data: ${JSON.stringify(targetData)}`);
if (this.isAnyEvent) {
const ret = await this.handles.get(consts_1.ANY_EVENT)(targetData);
this.logger.info(`execute any_event handle`);
return ret;
}
const type = targetData[consts_1.CEventType];
if (this.handles.has(type)) {
const ret = await this.handles.get(type)(targetData);
this.logger.info(`execute ${type} handle`);
return ret;
}
this.logger.warn(`no ${type} handle`);
return undefined;
}
}
exports.EventDispatcher = EventDispatcher;
//# sourceMappingURL=event-handler.js.map