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

179 lines 6.29 kB
import escapeStringRegexp from 'escape-string-regexp'; import { isOfType } from '../util/enums'; import { ContentType, CustomContentType, TopContentType } from './cms'; import { CmsException } from './exceptions'; export 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 CmsException(`Callback cannot have both 'URL' and 'payload' fields empty`, undefined); } if (payload && url) { throw new 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}'`; } } export 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('^' + escapeStringRegexp(modelType + ContentCallback.PAYLOAD_SEPARATOR) + '[a-zA-Z0-9]*$'); } static checkDeliverableModel(modelType) { if (isOfType(modelType, TopContentType)) { return modelType; } else { throw new Error(`${modelType} is not a model type than can be delivered from CMS`); } } } ContentCallback.PAYLOAD_SEPARATOR = '$'; export 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 (isOfType(resourceType, ContentType)) { return ContentId.create(resourceType, id); } return new ResourceId(resourceType, id); } } export class ContentId extends ResourceId { constructor(model, id) { super(model, id); this.model = model; } static create(model, id) { if (isOfType(model, TopContentType)) { return new TopContentId(model, id); } return new ContentId(model, id); } deliver(cms, context) { switch (this.model) { case ContentType.BUTTON: return cms.button(this.id, context); case ContentType.ELEMENT: return cms.element(this.id, context); case CustomContentType.CUSTOM: return cms.custom(this.id, context); default: return new TopContentId(this.model, this.id).deliver(cms, context); } } } export class AssetId extends ResourceId { constructor(id, assetType) { super(`${String(assetType)} asset`, id); this.assetType = assetType; } } export class TopContentId extends ContentId { constructor(model, id) { super(model, id); this.model = model; } deliver(cms, context) { switch (this.model) { case ContentType.CAROUSEL: return cms.carousel(this.id, context); case ContentType.DOCUMENT: return cms.document(this.id, context); case ContentType.TEXT: return cms.text(this.id, context); case ContentType.CHITCHAT: return cms.chitchat(this.id, context); case ContentType.STARTUP: return cms.startUp(this.id, context); case ContentType.IMAGE: return cms.image(this.id, context); case ContentType.VIDEO: return cms.video(this.id, context); case ContentType.DATE_RANGE: return cms.dateRange(this.id); case ContentType.QUEUE: return cms.queue(this.id, context); case ContentType.URL: return cms.url(this.id, context); case ContentType.PAYLOAD: return cms.payload(this.id, context); case ContentType.SCHEDULE: return cms.schedule(this.id); case ContentType.HANDOFF: return cms.handoff(this.id, context); case 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}'`); } } } //# sourceMappingURL=callback.js.map