UNPKG

@raintech-oss/jovo-dew

Version:
111 lines 5.43 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; import { DependencyInjector } from '@jovotech/framework'; import { missingInterpolationHandler } from './missingInterpolationHandler'; import { OutputProcessor, ViewVariablesProcessor } from './processors'; export const 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'; export class DewViewEngine { constructor(config, jovo) { this.config = config; this.jovo = jovo; this.processors = {}; this.data = {}; this.audioItems = []; this.i18nOptions = { missingInterpolationHandler: missingInterpolationHandler.bind(this) }; this._returnResourcePathKeysOnly = []; const outputProcessor = new OutputProcessor(this, jovo); const viewVariablesProcessor = new ViewVariablesProcessor(this, jovo); this.registerProcessor(Suffix.Message, outputProcessor); this.registerProcessor(Suffix.Reprompt, outputProcessor); this.registerProcessor(Suffix.Listen, outputProcessor); this.registerProcessor(Suffix.QuickReplies, outputProcessor); this.registerProcessor(Suffix.Card, outputProcessor); this.registerProcessor(Suffix.Carousel, outputProcessor); this.registerProcessor(Suffix.ViewVariables, viewVariablesProcessor); this.registerProcessor(Suffix.Platforms, viewVariablesProcessor); this.registerProcessor(Suffix.APL, viewVariablesProcessor); this.audioItems = this.getAudioItems(); } init() { return __awaiter(this, void 0, void 0, function* () { if (this.config.viewVariables) { this.viewVariables = yield 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; } instantiateViewVariables(jovo) { return __awaiter(this, void 0, void 0, function* () { return yield DependencyInjector.instantiateClass(jovo, this.config.viewVariables, jovo); }); } registerProcessor(name, processor) { this.processors[name] = processor; } getOutput(path) { return __awaiter(this, void 0, void 0, function* () { 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); } yield this.jovo.$handleRequest.middlewareCollection.run(GET_OUTPUT_MIDDLEWARE, this.jovo, payload); return outputs; }); } } //# sourceMappingURL=DewViewEngine.js.map