r1-io
Version:
Simple jsx based library for creation of vk bots, based on vk-io
37 lines • 1.83 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.createMiddlewareConfigurator = void 0;
const paramsMiddleware_1 = require("../contextExtensions/paramsMiddleware");
const send_1 = require("../contextExtensions/send/send");
const createMiddlewareConfigurator = ({ getCurrentMenu, applyUserMiddleware, actions }) => async (context, next) => {
const builderContext = await applyUserMiddleware(context, next);
const findAndCallAction = (payload) => actions.findAndCall(payload, { builderContext, context });
const findAndCallAllActions = (actions) => Promise.all(actions.map(findAndCallAction));
if (!builderContext)
return;
const getCurrentMenuAndBuildKeyboard = () => getCurrentMenu(builderContext).build(builderContext);
const customSendBuilded = (0, send_1.customSend)({
buildKeyboard: getCurrentMenuAndBuildKeyboard,
});
(0, paramsMiddleware_1.asyncAttachToContext)('send', customSendBuilded, context);
const beforeMenu = getCurrentMenu(builderContext);
const actionStatus = await findAndCallAction(context.messagePayload);
const afterMenu = getCurrentMenu(builderContext);
if (beforeMenu !== afterMenu) {
if (beforeMenu.onMenuExit?.length) {
await findAndCallAllActions(beforeMenu.onMenuExit);
}
if (afterMenu.onMenuEntering?.length) {
await findAndCallAllActions(afterMenu.onMenuEntering);
}
}
if (actionStatus === 'PayloadNotFound') {
const { fallbackActions } = getCurrentMenu(builderContext);
if (fallbackActions?.length) {
await findAndCallAllActions(fallbackActions);
}
}
return builderContext;
};
exports.createMiddlewareConfigurator = createMiddlewareConfigurator;
//# sourceMappingURL=configureMiddleware.js.map