@raintech-oss/jovo-dew
Version:
Dew View Engine for Jovo
100 lines • 4.71 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DewViewEngine = exports.Suffix = void 0;
const framework_1 = require("@jovotech/framework");
const missingInterpolationHandler_1 = require("./missingInterpolationHandler");
const processors_1 = require("./processors");
exports.Suffix = {
Message: 'message',
Reprompt: 'reprompt',
Listen: 'listen',
QuickReplies: 'quickReplies',
Card: 'card',
Carousel: 'carousel',
ViewVariables: 'vv',
Platforms: 'platforms',
APL: 'apl',
};
const GET_OUTPUT_MIDDLEWARE = 'event.DewViewEnginePlugin.getOutput';
class DewViewEngine {
constructor(config, jovo) {
this.config = config;
this.jovo = jovo;
this.processors = {};
this.data = {};
this.audioItems = [];
this.i18nOptions = { missingInterpolationHandler: missingInterpolationHandler_1.missingInterpolationHandler.bind(this) };
this._returnResourcePathKeysOnly = [];
const outputProcessor = new processors_1.OutputProcessor(this, jovo);
const viewVariablesProcessor = new processors_1.ViewVariablesProcessor(this, jovo);
this.registerProcessor(exports.Suffix.Message, outputProcessor);
this.registerProcessor(exports.Suffix.Reprompt, outputProcessor);
this.registerProcessor(exports.Suffix.Listen, outputProcessor);
this.registerProcessor(exports.Suffix.QuickReplies, outputProcessor);
this.registerProcessor(exports.Suffix.Card, outputProcessor);
this.registerProcessor(exports.Suffix.Carousel, outputProcessor);
this.registerProcessor(exports.Suffix.ViewVariables, viewVariablesProcessor);
this.registerProcessor(exports.Suffix.Platforms, viewVariablesProcessor);
this.registerProcessor(exports.Suffix.APL, viewVariablesProcessor);
this.audioItems = this.getAudioItems();
}
async init() {
if (this.config.viewVariables) {
this.viewVariables = await this.instantiateViewVariables(this.jovo);
}
}
get returnResourcePathKeysOnly() {
return this._returnResourcePathKeysOnly;
}
set returnResourcePathKeysOnly(value) {
this._returnResourcePathKeysOnly = value;
}
getAudioItems() {
var _a, _b, _c, _d, _e, _f;
const requestLocale = this.jovo.$request.getLocale();
const locale = requestLocale ? requestLocale : (_a = this.config.audio) === null || _a === void 0 ? void 0 : _a.fallbackLocale;
const fallbackLocale = (_b = this.config.audio) === null || _b === void 0 ? void 0 : _b.fallbackLocale;
let audioResources = requestLocale && ((_d = (_c = this.config) === null || _c === void 0 ? void 0 : _c.audio) === null || _d === void 0 ? void 0 : _d.resources)
? this.config.audio.resources[requestLocale]
: undefined;
if (!audioResources) {
audioResources =
fallbackLocale && ((_f = (_e = this.config) === null || _e === void 0 ? void 0 : _e.audio) === null || _f === void 0 ? void 0 : _f.resources)
? this.config.audio.resources[fallbackLocale]
: undefined;
}
if (!audioResources) {
throw new Error(`No audio resources for locale '${locale}' and audio.fallbackLocale is not found.`);
}
return audioResources;
}
async instantiateViewVariables(jovo) {
return await framework_1.DependencyInjector.instantiateClass(jovo, this.config.viewVariables, jovo);
}
registerProcessor(name, processor) {
this.processors[name] = processor;
}
async getOutput(path) {
const paths = Array.isArray(path) ? path : [path];
const outputs = [];
const data = Object.assign({}, this.data, this.i18nOptions);
const payload = { paths: [] };
for (const path of paths) {
const obj = this.jovo.$t(path, { skipInterpolation: true });
const keys = Object.keys(obj);
payload.paths.push({ path, keys });
let output = {};
for (const key of keys) {
if (this.processors[key]) {
const result = this.processors[key].process(key, obj, path, data);
output = Object.assign({}, output, result);
}
}
outputs.push(output);
}
await this.jovo.$handleRequest.middlewareCollection.run(GET_OUTPUT_MIDDLEWARE, this.jovo, payload);
return outputs;
}
}
exports.DewViewEngine = DewViewEngine;
//# sourceMappingURL=DewViewEngine.js.map