UNPKG

@jargon/actions-on-google

Version:

The Jargon Actions on Google SDK makes it easy to manage the content of your Google Action

104 lines 3.85 kB
"use strict"; /* * Copyright 2018 Jargon, Inc. or its affiliates. All Rights Reserved. * Licensed under the Apache License, Version 2.0 (the "License"). * You may not use this file except in compliance with the License. * A copy of the License is located at * http://www.apache.org/licenses/LICENSE-2.0 * * or in the "license" file accompanying this file. This file is distributed * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either * express or implied. See the License for the specific language governing * permissions and limitations under the License. */ Object.defineProperty(exports, "__esModule", { value: true }); const actions_on_google_1 = require("actions-on-google"); const jBasicCardFields = ['title', 'subtitle', 'text']; const jBrowseCarouselItemFields = ['title', 'description', 'footer']; const jButtonFields = ['title']; const jImageFields = ['alt']; const jLinkOutSuggestionFields = ['name']; const jMediaObjectFields = ['description', 'name']; const jSimpleResponseFields = ['speech', 'text']; const jTableOptionsFields = ['title', 'subtitle']; const jTableColumnFields = ['header']; /** * The core implementation of ResponseFactory */ class CommonResponseFactory { constructor(_rm) { this._rm = _rm; } async basicCard(options) { const card = await this._render(options, jBasicCardFields); return new actions_on_google_1.BasicCard(card); } async browseCarouselItem(options) { const bci = await this._render(options, jBrowseCarouselItemFields); return new actions_on_google_1.BrowseCarouselItem(bci); } async button(options) { const button = await this._render(options, jButtonFields); return new actions_on_google_1.Button(button); } async image(options) { const image = await this._render(options, jImageFields); return new actions_on_google_1.Image(image); } async linkOutSuggestion(options) { const los = await this._render(options, jLinkOutSuggestionFields); return new actions_on_google_1.LinkOutSuggestion(los); } async mediaObject(options) { const mos = await this._render(options, jMediaObjectFields); return new actions_on_google_1.MediaObject(mos); } async simple(options) { if (isRI(options)) { return new actions_on_google_1.SimpleResponse(await this._rm.render(options)); } const sro = await this._render(options, jSimpleResponseFields); return new actions_on_google_1.SimpleResponse(sro); } async suggestions(...suggestions) { const rendered = await this._rm.renderBatch(suggestions); return new actions_on_google_1.Suggestions(rendered); } async table(options) { const to = await this._render(options, jTableOptionsFields); return new actions_on_google_1.Table(to); } async tableColumn(column) { const c = await this._render(column, jTableColumnFields); return c; } async tableRow(row) { let cells; if (row.cells) { cells = await this._rm.renderBatch(row.cells); } return Object.assign({}, row, { cells: cells }); } async _render(jOpts, fields) { let ris = []; for (const f of fields) { const val = jOpts[f]; val && ris.push(val); } const rendered = await this._rm.renderBatch(ris); let pg = {}; let i = 0; for (const f of fields) { if (jOpts[f]) { pg[f] = rendered[i++]; } } pg = Object.assign({}, jOpts, pg); return pg; } } exports.CommonResponseFactory = CommonResponseFactory; function isRI(ri) { return typeof ri.key === 'string'; } //# sourceMappingURL=responseFactory.js.map