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

189 lines 6.9 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TopContentId = exports.AssetId = exports.ContentId = exports.ResourceId = exports.ContentCallback = exports.Callback = void 0; const tslib_1 = require("tslib"); const escape_string_regexp_1 = tslib_1.__importDefault(require("escape-string-regexp")); const enums_1 = require("../util/enums"); const cms_1 = require("./cms"); const exceptions_1 = require("./exceptions"); class Callback { // TODO add path /** * @param payload may contain the reference of a {@link Content}. See {@link ContentCallback} * @param url for hardcoded URLs (otherwise, use a {@link Url}) * @param empty eg. if locale fallback is disabled, we may get empty * fields. */ constructor(payload, url, empty = false) { this.payload = payload; this.url = url; this.empty = empty; if (!empty && !payload && !url) { throw new exceptions_1.CmsException(`Callback cannot have both 'URL' and 'payload' fields empty`, undefined); } if (payload && url) { throw new exceptions_1.CmsException(`Callback cannot have both 'URL' and 'payload' fields informed`); } } static ofPayload(payload) { if (ContentCallback.payloadReferencesContent(payload)) { return ContentCallback.ofPayload(payload); } else { return new Callback(payload, undefined); } } static ofUrl(url) { return new Callback(undefined, url); } static empty() { return new Callback(undefined, undefined, true); } asContentId() { return undefined; } equals(other) { return this.payload === other.payload && this.url === other.url; } toString() { if (this.payload) { return `'payload:${this.payload}'`; } // eslint-disable-next-line @typescript-eslint/restrict-template-expressions return `'URL:${this.url}'`; } } exports.Callback = Callback; class ContentCallback extends Callback { constructor(model, id) { super(model + ContentCallback.PAYLOAD_SEPARATOR + id); this.model = model; this.id = id; } asContentId() { return new TopContentId(this.model, this.id); } static payloadReferencesContent(payload) { return payload.includes(ContentCallback.PAYLOAD_SEPARATOR); } static ofPayload(payload) { const [type, id] = payload.split(ContentCallback.PAYLOAD_SEPARATOR); if (!id) { throw new Error(`Callback payload '${payload}' does not contain a model reference`); } return new ContentCallback(ContentCallback.checkDeliverableModel(type), id); } static ofContentId(contentId) { return new ContentCallback(contentId.model, contentId.id); } static regexForModel(modelType) { return new RegExp('^' + (0, escape_string_regexp_1.default)(modelType + ContentCallback.PAYLOAD_SEPARATOR) + '[a-zA-Z0-9]*$'); } static checkDeliverableModel(modelType) { if ((0, enums_1.isOfType)(modelType, cms_1.TopContentType)) { return modelType; } else { throw new Error(`${modelType} is not a model type than can be delivered from CMS`); } } } exports.ContentCallback = ContentCallback; ContentCallback.PAYLOAD_SEPARATOR = '$'; class ResourceId { constructor(resourceType, id) { this.resourceType = resourceType; this.id = id; } toString() { return `'${this.resourceType}' with id '${this.id}'`; } equals(other) { return this.resourceType === other.resourceType && this.id === other.id; } static create(resourceType, id) { if ((0, enums_1.isOfType)(resourceType, cms_1.ContentType)) { return ContentId.create(resourceType, id); } return new ResourceId(resourceType, id); } } exports.ResourceId = ResourceId; class ContentId extends ResourceId { constructor(model, id) { super(model, id); this.model = model; } static create(model, id) { if ((0, enums_1.isOfType)(model, cms_1.TopContentType)) { return new TopContentId(model, id); } return new ContentId(model, id); } deliver(cms, context) { switch (this.model) { case cms_1.ContentType.BUTTON: return cms.button(this.id, context); case cms_1.ContentType.ELEMENT: return cms.element(this.id, context); case cms_1.CustomContentType.CUSTOM: return cms.custom(this.id, context); default: return new TopContentId(this.model, this.id).deliver(cms, context); } } } exports.ContentId = ContentId; class AssetId extends ResourceId { constructor(id, assetType) { super(`${String(assetType)} asset`, id); this.assetType = assetType; } } exports.AssetId = AssetId; class TopContentId extends ContentId { constructor(model, id) { super(model, id); this.model = model; } deliver(cms, context) { switch (this.model) { case cms_1.ContentType.CAROUSEL: return cms.carousel(this.id, context); case cms_1.ContentType.DOCUMENT: return cms.document(this.id, context); case cms_1.ContentType.TEXT: return cms.text(this.id, context); case cms_1.ContentType.CHITCHAT: return cms.chitchat(this.id, context); case cms_1.ContentType.STARTUP: return cms.startUp(this.id, context); case cms_1.ContentType.IMAGE: return cms.image(this.id, context); case cms_1.ContentType.VIDEO: return cms.video(this.id, context); case cms_1.ContentType.DATE_RANGE: return cms.dateRange(this.id); case cms_1.ContentType.QUEUE: return cms.queue(this.id, context); case cms_1.ContentType.URL: return cms.url(this.id, context); case cms_1.ContentType.PAYLOAD: return cms.payload(this.id, context); case cms_1.ContentType.SCHEDULE: return cms.schedule(this.id); case cms_1.ContentType.HANDOFF: return cms.handoff(this.id, context); case cms_1.ContentType.INPUT: return cms.input(this.id, context); default: throw new Error( // eslint-disable-next-line @typescript-eslint/restrict-template-expressions `Type '${this.model}' not supported for callback with id '${this.id}'`); } } } exports.TopContentId = TopContentId; //# sourceMappingURL=callback.js.map