UNPKG

@botonic/plugin-contentful

Version:

Botonic Plugin Contentful is one of the **[available](https://github.com/hubtype/botonic/tree/master/packages)** plugins for Botonic. **[Contentful](http://www.contentful.com)** is a CMS (Content Management System) which manages contents of a great variet

195 lines 6.91 kB
import { ContentType } from '../cms'; import { HandoffAgentEmail } from '../contents'; import { CarouselBuilder, DocumentBuilder, ElementBuilder, HandoffBuilder, InputBuilder, MediaBuilder, StartUpBuilder, TextBuilder, } from '../factories/content-factories'; import { Button, ButtonStyle, CmsException, CommonFields, ContentCallback, Text, } from '../index'; /** * The Builder classes below create Content instances with minimal effort * to speedup the implementation of unit tests by setting random values for the * non specified fields. * They are exported by the plugin so that the can be used in bots' unit tests */ export function rndStr() { return Math.random().toString(); } export function rndBool() { return Math.random() >= 0.5; } export class ContentCallbackBuilder { withContentId(contentId) { this.callback = ContentCallback.ofContentId(contentId); return this; } build() { return this.callback || new ContentCallback(ContentType.TEXT, rndStr()); } } export class RndButtonsBuilder { constructor() { this.buttons = []; this.callbackBuilder = new ContentCallbackBuilder(); } build() { return this.buttons; } withCallback(callback) { this.callback = callback; return this; } withName(name) { this.name = name; return this; } withText(text) { this.text = text; return this; } addButton() { var _a, _b, _c; this.buttons.push(new Button(rndStr(), (_a = this.name) !== null && _a !== void 0 ? _a : rndStr(), (_b = this.text) !== null && _b !== void 0 ? _b : rndStr(), (_c = this.callback) !== null && _c !== void 0 ? _c : this.callbackBuilder.build())); return this; } } export class RndTopContentBuilder { constructor() { this.keywords = []; } withRandomFields(builder) { builder.shortText = rndStr(); builder.keywords = [rndStr(), rndStr()]; builder.followUp = rndBool() ? undefined : new Text(new CommonFields(rndStr(), rndStr()), rndStr(), []); } } export class RndTextBuilder extends TextBuilder { constructor(name = rndStr(), text = rndStr()) { super(rndStr(), name, text); this.buttonsBuilder = new RndButtonsBuilder(); this.topComponentBuilder = new RndTopContentBuilder(); } /** @deprecated use buttonsBuilder */ withButtonsBuilder() { return this.buttonsBuilder; } build() { const buttons = this.buttonsBuilder.build(); // TODO too complex. resurrect withButtonsBuilder? if (buttons.length > 0) { if (this.buttons.length > 0) { throw new CmsException('Not supported to add buttons with both .buttons & buttonsBuilder'); } this.buttonsBuilder.buttons = []; this.buttons = buttons; } return super.build(); } withRandomFields() { this.buttonsBuilder.addButton().addButton(); this.topComponentBuilder.withRandomFields(this); this.buttonsStyle = rndBool() ? ButtonStyle.QUICK_REPLY : ButtonStyle.BUTTON; return this; } } export class RndElementBuilder extends ElementBuilder { constructor() { super(rndStr()); } withButtonsBuilder() { if (this.buttons.length > 0) { throw new CmsException('cannot use withButtonsBuilder if addButtons was previously called'); } if (!this.buttonsBuilder) { this.buttonsBuilder = new RndButtonsBuilder(); } return this.buttonsBuilder; } withButtons(buttons) { if (this.buttonsBuilder) { throw new CmsException('cannot use addButtons if withButtonsBuilder was previously called'); } return super.withButtons(buttons); } withRandomFields() { if (!this.buttonsBuilder) { this.buttonsBuilder = new RndButtonsBuilder().addButton().addButton(); } this.title = rndStr(); this.subtitle = rndStr(); this.imgUrl = rndStr(); return this; } build() { if (this.buttonsBuilder) { this.buttons = this.buttonsBuilder.build(); } return super.build(); } } export class RndCarouselBuilder extends CarouselBuilder { constructor(name = rndStr()) { super(rndStr(), name); this.topComponentBuilder = new RndTopContentBuilder(); this.elementBuilder = new RndElementBuilder(); } withRandomFields(numElements = 2) { for (let i = 0; i < numElements; i++) { this.elementBuilder.withRandomFields(); this.addElement(); } this.topComponentBuilder.withRandomFields(this); return this; } } export class RndStartUpBuilder extends StartUpBuilder { constructor(name = rndStr(), text = rndStr()) { super(rndStr(), name, text); this.topComponentBuilder = new RndTopContentBuilder(); this.buttonsBuilder = new RndButtonsBuilder(); } withRandomFields() { this.buttons = this.buttonsBuilder.addButton().addButton().build(); this.topComponentBuilder.withRandomFields(this); return this; } } export class RndMediaBuilder extends MediaBuilder { constructor(name = rndStr(), mediaUrl = 'http://' + rndStr()) { super(rndStr(), name, mediaUrl); this.topComponentBuilder = new RndTopContentBuilder(); } withRandomFields() { this.topComponentBuilder.withRandomFields(this); return this; } } export class RndDocumentBuilder extends DocumentBuilder { constructor(name = rndStr(), docUrl = 'http://' + rndStr()) { super(rndStr(), name, docUrl); this.topComponentBuilder = new RndTopContentBuilder(); } withRandomFields() { this.topComponentBuilder.withRandomFields(this); return this; } } export class RndHandoffBuilder extends HandoffBuilder { constructor(name = rndStr(), onFinish = new ContentCallbackBuilder().build()) { super(rndStr(), name, onFinish); this.topComponentBuilder = new RndTopContentBuilder(); } withRandomFields() { this.message = new Text(new CommonFields(rndStr(), rndStr()), rndStr(), []); this.failMessage = new Text(new CommonFields(rndStr(), rndStr()), rndStr(), []); this.agent = new HandoffAgentEmail(rndStr()); this.shadowing = rndBool(); this.topComponentBuilder.withRandomFields(this); return this; } } export class RndInputBuilder extends InputBuilder { constructor(name = rndStr(), keywords = [rndStr(), rndStr()], target = new ContentCallbackBuilder().build()) { super(rndStr(), name, rndStr(), keywords, target); this.topComponentBuilder = new RndTopContentBuilder(); } } //# sourceMappingURL=builders.js.map