UNPKG

@ebonydevcopy/framework

Version:

A module-based NodeJS chatbot framework.

49 lines 1.47 kB
"use strict"; /** * ebony-framework * * @module routers/ContextRouter * @author Christos Panagiotakopoulos <chrispanag@gmail.com> * @copyright Copyright(c) 2020 Christos Panagiotakopoulos * @license MIT * */ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const lodash_1 = require("lodash"); const BasicRouter_1 = __importDefault(require("./BasicRouter")); /** * A ContextRouter * @extends BasicRouter */ class ContextRouter extends BasicRouter_1.default { /** * Create a ContextRouter * @param options - The options of this ContextRouter */ constructor({ field = 'context' }) { super(); this.field = field; } /** * * @param user - The User Object * @param params - Various parameters passed to the action * @returns The result of the action if the route is found. Else it returns false */ getContextRoute(user, ...params) { if (!(this.field in user)) { throw new Error(`User doesn't have the property: ${this.field}`); } const step = (0, lodash_1.get)(user, this.field); const func = this.getRoute(step); if (func) { return func(user, ...params); } return false; } } exports.default = ContextRouter; //# sourceMappingURL=ContextRouter.js.map