UNPKG

4.22 kBJavaScriptView Raw
1"use strict";
2/**
3 * Copyright 2018 Google Inc. All Rights Reserved.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17Object.defineProperty(exports, "__esModule", { value: true });
18exports.RichResponse = void 0;
19const simple_1 = require("./simple");
20const card_1 = require("./card");
21const browse_1 = require("./browse");
22const media_1 = require("./media");
23const order_1 = require("./order");
24const linkout_1 = require("./linkout");
25const suggestion_1 = require("./suggestion");
26const html_1 = require("./html");
27const isOptions = (options) => {
28 const test = options;
29 return (typeof test.link === 'object' ||
30 Array.isArray(test.items) ||
31 Array.isArray(test.suggestions) ||
32 test.suggestions instanceof suggestion_1.Suggestions);
33};
34class RichResponse {
35 constructor(options, ...items) {
36 this.items = [];
37 if (!options) {
38 return;
39 }
40 if (Array.isArray(options)) {
41 this.add(...options);
42 return;
43 }
44 if (isOptions(options)) {
45 if (options.items) {
46 this.add(...options.items);
47 }
48 const { link, suggestions } = options;
49 this.linkOutSuggestion = link;
50 if (suggestions) {
51 if (Array.isArray(suggestions)) {
52 this.addSuggestion(...suggestions);
53 }
54 else {
55 this.addSuggestion(suggestions);
56 }
57 }
58 return;
59 }
60 this.add(options, ...items);
61 }
62 /**
63 * Add a RichResponse item
64 * @public
65 */
66 add(...items) {
67 const raw = this.items;
68 for (const item of items) {
69 if (typeof item === 'string') {
70 this.add(new simple_1.SimpleResponse(item));
71 continue;
72 }
73 if (item instanceof linkout_1.LinkOutSuggestion) {
74 this.linkOutSuggestion = item;
75 continue;
76 }
77 if (item instanceof simple_1.SimpleResponse) {
78 raw.push({ simpleResponse: item });
79 continue;
80 }
81 if (item instanceof card_1.BasicCard) {
82 raw.push({ basicCard: item });
83 continue;
84 }
85 if (item instanceof card_1.Table) {
86 raw.push({ tableCard: item });
87 continue;
88 }
89 if (item instanceof browse_1.BrowseCarousel) {
90 raw.push({ carouselBrowse: item });
91 continue;
92 }
93 if (item instanceof media_1.MediaResponse) {
94 raw.push({ mediaResponse: item });
95 continue;
96 }
97 if (item instanceof order_1.OrderUpdate) {
98 raw.push({ structuredResponse: { orderUpdate: item } });
99 continue;
100 }
101 if (item instanceof html_1.HtmlResponse) {
102 raw.push({ htmlResponse: item });
103 continue;
104 }
105 raw.push(item);
106 }
107 return this;
108 }
109 /**
110 * Adds a single suggestion or list of suggestions to list of items.
111 * @public
112 */
113 addSuggestion(...suggestions) {
114 if (!this.suggestions) {
115 this.suggestions = [];
116 }
117 for (const suggestion of suggestions) {
118 if (typeof suggestion === 'string') {
119 this.addSuggestion(new suggestion_1.Suggestions(suggestion));
120 continue;
121 }
122 this.suggestions.push(...suggestion.suggestions);
123 }
124 return this;
125 }
126}
127exports.RichResponse = RichResponse;
128//# sourceMappingURL=rich.js.map
\No newline at end of file