jovo-v4-community-plugin-jexl-output
Version:
A Jovo V4 framework plugin which adds Jexl support for output strings.
139 lines • 6.28 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.JexlOutputPlugin = void 0;
const framework_1 = require("@jovotech/framework");
const _ = __importStar(require("lodash"));
const JexlOutputProcessor_1 = require("./JexlOutputProcessor");
/**
* This plugin will search and process the output object for
* 'message' and 'reprompt' entries in case they contain
* 'Jexl' expressions (https://www.npmjs.com/package/jexl).
*/
class JexlOutputPlugin extends framework_1.Plugin {
mount(extensible) {
if (!(extensible instanceof framework_1.HandleRequest)) {
throw new framework_1.InvalidParentError(this.constructor.name, framework_1.HandleRequest);
}
extensible.middlewareCollection.use("before.response.output", (jovo) => {
this.processJexlExpressionsInOutput(jovo);
});
}
processJexlExpressionsInOutput(jovo) {
const jexlContext = {
// $processEnv: process.env,
$component: jovo.$component,
$data: jovo.$data,
$device: jovo.$device,
$entities: jovo.$entities,
$input: jovo.$input,
$request: jovo.$request,
$route: jovo.$route,
$session: jovo.$session,
$state: jovo.$state,
$user: jovo.$user,
$platform: jovo.$platform,
};
jovo.$output.forEach((entry) => {
if ("message" in entry) {
if (Array.isArray(entry.message)) {
let messageArray = entry.message;
messageArray = messageArray.map((message) => {
return this.processOutputElementIncludingJexl(message, jexlContext);
});
entry.message = messageArray;
}
else {
entry.message = this.processOutputElementIncludingJexl(entry.message, jexlContext);
}
}
if ("reprompt" in entry) {
if (Array.isArray(entry.reprompt)) {
let repromptArray = entry.reprompt;
repromptArray = repromptArray.map((message) => {
return this.processOutputElementIncludingJexl(message, jexlContext);
});
entry.reprompt = repromptArray;
}
else {
entry.reprompt = this.processOutputElementIncludingJexl(entry.reprompt, jexlContext);
}
}
if ("quickReplies" in entry) {
const regEx = new RegExp(/^\${(.+?)}$/);
if (regEx.test(entry.quickReplies) &&
entry.quickReplies !== undefined &&
entry.quickReplies.length > 0) {
// something like "${some.path.notation.and.nothing.around}"
const expression = entry.quickReplies[0];
const keypath = expression.slice(2, expression.length - 1); // ${a.b.c} ==> a.b.c
const result = _.get(jovo, keypath);
entry.quickReplies = result;
}
if (Array.isArray(entry.quickReplies)) {
let quickRepliesArray = entry.quickReplies;
quickRepliesArray = quickRepliesArray.map((message) => {
return this.processQuickReplyValueIncludingJexl(message, jexlContext);
});
entry.quickReplies = quickRepliesArray;
}
}
});
}
/**
*
* @param outputElementIncludingJexl may be a string or an object like {text: string, speech: string} but no Array
* @returns
*/
processOutputElementIncludingJexl(outputElementIncludingJexl, jexlContext) {
if (typeof outputElementIncludingJexl === "string") {
outputElementIncludingJexl = (0, JexlOutputProcessor_1.processJexlExpression)(outputElementIncludingJexl, jexlContext);
}
else if (typeof outputElementIncludingJexl === "object") {
// structure is something like {text: string, speech: string}
if ("text" in outputElementIncludingJexl) {
outputElementIncludingJexl.text = (0, JexlOutputProcessor_1.processJexlExpression)(outputElementIncludingJexl.text, jexlContext);
}
if ("speech" in outputElementIncludingJexl) {
outputElementIncludingJexl.speech = (0, JexlOutputProcessor_1.processJexlExpression)(outputElementIncludingJexl.speech, jexlContext);
}
}
return outputElementIncludingJexl;
}
/**
*
* @param quickReplyValueIncludingJexl may be a string or an object like {text: string, speech: string} but no Array
* @returns
*/
processQuickReplyValueIncludingJexl(quickReplyValueIncludingJexl, jexlContext) {
quickReplyValueIncludingJexl = (0, JexlOutputProcessor_1.processJexlExpression)(quickReplyValueIncludingJexl, jexlContext);
return quickReplyValueIncludingJexl;
}
getDefaultConfig() {
return {};
}
}
exports.JexlOutputPlugin = JexlOutputPlugin;
//# sourceMappingURL=JexlOutputPlugin.js.map