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

240 lines 11.3 kB
import { __awaiter } from "tslib"; import { ContentType, DEFAULT_CONTEXT, DEFAULT_REFERENCES_TO_INCLUDE, PagingOptions, } from '../cms'; import { DEFAULT_FALLBACK_CACHE_LIMIT_KB } from '../plugin'; import { AssetDelivery } from './contents/asset'; import { ButtonDelivery } from './contents/button'; import { CarouselDelivery } from './contents/carousel'; import { ContentsDelivery } from './contents/contents'; import { CustomDelivery } from './contents/custom'; import { DateRangeDelivery } from './contents/date-range'; import { DocumentDelivery } from './contents/document'; import { HandoffDelivery } from './contents/handoff'; import { ImageDelivery } from './contents/image'; import { InputDelivery } from './contents/input'; import { PayloadDelivery } from './contents/payload'; import { QueueDelivery } from './contents/queue'; import { ReferenceDelivery } from './contents/reference'; import { ScheduleDelivery } from './contents/schedule'; import { StartUpDelivery } from './contents/startup'; import { TextDelivery } from './contents/text'; import { UrlDelivery } from './contents/url'; import { VideoDelivery } from './contents/video'; import { CachedClientApi } from './delivery/cache'; import { FallbackCachedClientApi } from './delivery/fallback-cache'; import { AdaptorDeliveryApi } from './delivery-api'; import { ContentfulEntryUtils, createContentfulClientApi, } from './delivery-utils'; import { IgnoreFallbackDecorator } from './ignore-fallback-decorator'; import { KeywordsDelivery } from './search/keywords'; export class Contentful { /** * * See https://www.contentful.com/developers/docs/references/content-delivery-api/#/introduction/api-rate-limits * for API rate limits * * https://www.contentful.com/developers/docs/javascript/tutorials/using-js-cda-sdk/ */ constructor(options) { var _a, _b; const logger = (_a = options.logger) !== null && _a !== void 0 ? _a : console.error; const reporter = (msg, func, args, error) => { logger(`${msg}. '${func}(${String(args)})' threw '${String(error)}'`); return Promise.resolve(); }; let client = createContentfulClientApi(options); if (!options.disableFallbackCache) { client = new FallbackCachedClientApi(client, (_b = options.fallbackCacheLimitKB) !== null && _b !== void 0 ? _b : DEFAULT_FALLBACK_CACHE_LIMIT_KB, reporter, logger); } if (!options.disableCache) { client = new CachedClientApi(client, options.cacheTtlMs, reporter); } const deliveryApi = new AdaptorDeliveryApi(client, options); const resumeErrors = options.resumeErrors || false; const delivery = new IgnoreFallbackDecorator(deliveryApi); this._contents = new ContentsDelivery(delivery, resumeErrors); this._delivery = delivery; this._button = new ButtonDelivery(delivery, resumeErrors); this._carousel = new CarouselDelivery(delivery, this._button, resumeErrors); this._document = new DocumentDelivery(delivery, resumeErrors); this._text = new TextDelivery(delivery, this._button, resumeErrors); this._startUp = new StartUpDelivery(delivery, this._button, resumeErrors); this._url = new UrlDelivery(delivery, resumeErrors); this._payload = new PayloadDelivery(delivery, resumeErrors); this._image = new ImageDelivery(delivery, resumeErrors); this._video = new VideoDelivery(delivery, resumeErrors); this._asset = new AssetDelivery(delivery, resumeErrors); this._schedule = new ScheduleDelivery(delivery, resumeErrors); this._queue = new QueueDelivery(delivery, this._schedule, resumeErrors); this._handoff = new HandoffDelivery(delivery, this._queue, this._text, resumeErrors); this._input = new InputDelivery(delivery, resumeErrors); this._custom = new CustomDelivery(delivery, resumeErrors); this._dateRange = new DateRangeDelivery(delivery, resumeErrors); const followUp = new ReferenceDelivery(this._delivery, this._carousel, this._text, this._image, this._startUp, this._video, this._document, this._url, this._handoff, this._queue, this._schedule, this._input, this._payload, this._dateRange); [ this._carousel, this._text, this._image, this._startUp, this._video, this._document, this._url, this._handoff, this._queue, this._schedule, this._input, this._payload, this._dateRange, ].forEach(d => d.setReference(followUp)); this._keywords = new KeywordsDelivery(delivery); } button(id, context = DEFAULT_CONTEXT) { return this._button.button(id, context); } element(id, context = DEFAULT_CONTEXT) { return this._carousel.element(id, context); } carousel(id, context = DEFAULT_CONTEXT) { return __awaiter(this, void 0, void 0, function* () { return this._carousel.carousel(id, context); }); } document(id, context = DEFAULT_CONTEXT) { return this._document.document(id, context); } text(id, context = DEFAULT_CONTEXT) { return __awaiter(this, void 0, void 0, function* () { return this._text.text(id, context); }); } startUp(id, context = DEFAULT_CONTEXT) { return __awaiter(this, void 0, void 0, function* () { return this._startUp.startUp(id, context); }); } url(id, context = DEFAULT_CONTEXT) { return __awaiter(this, void 0, void 0, function* () { return this._url.url(id, context); }); } payload(id, context = DEFAULT_CONTEXT) { return __awaiter(this, void 0, void 0, function* () { return this._payload.payload(id, context); }); } queue(id, context = DEFAULT_CONTEXT) { return __awaiter(this, void 0, void 0, function* () { return this._queue.queue(id, context); }); } image(id, context = DEFAULT_CONTEXT) { return this._image.image(id, context); } video(id, context = DEFAULT_CONTEXT) { return this._video.video(id, context); } chitchat(id, context = DEFAULT_CONTEXT) { return this._text.text(id, context); } handoff(id, context = DEFAULT_CONTEXT) { return __awaiter(this, void 0, void 0, function* () { return this._handoff.handoff(id, context); }); } input(id, context = DEFAULT_CONTEXT) { return __awaiter(this, void 0, void 0, function* () { return this._input.input(id, context); }); } custom(id, context = DEFAULT_CONTEXT) { return __awaiter(this, void 0, void 0, function* () { return this._custom.custom(id, context); }); } topContents(model, context = DEFAULT_CONTEXT, filter, paging = new PagingOptions()) { return this._contents.topContents(model, context, (entry, ctxt) => this.topContentFromEntry(entry, ctxt), filter, paging); } content(id, context = DEFAULT_CONTEXT, referencesToInclude = DEFAULT_REFERENCES_TO_INCLUDE) { return __awaiter(this, void 0, void 0, function* () { const entry = yield this._delivery.getEntry(id, context, { include: referencesToInclude, }); return this.contentFromEntry(entry, context); }); } contents(contentType, context = DEFAULT_CONTEXT, paging = new PagingOptions()) { return this._contents.contents(contentType, context, (entry, ctxt) => this.contentFromEntry(entry, ctxt), paging); } topContentFromEntry(entry, context) { return __awaiter(this, void 0, void 0, function* () { const model = ContentfulEntryUtils.getContentModel(entry); const retype = (c) => c; switch (model) { case ContentType.CAROUSEL: return retype(yield this._carousel.fromEntry(entry, context)); case ContentType.DOCUMENT: return retype(yield this._document.fromEntry(entry, context)); case ContentType.QUEUE: return retype(yield this._queue.fromEntry(entry, context)); case ContentType.CHITCHAT: case ContentType.TEXT: return retype(yield this._text.fromEntry(entry, context)); case ContentType.IMAGE: return retype(yield this._image.fromEntry(entry, context)); case ContentType.VIDEO: return retype(yield this._video.fromEntry(entry, context)); case ContentType.HANDOFF: return retype(yield this._handoff.fromEntry(entry, context)); case ContentType.INPUT: return retype(yield this._input.fromEntry(entry, context)); case ContentType.URL: return retype(yield this._url.fromEntry(entry, context)); case ContentType.PAYLOAD: return retype(yield this._payload.fromEntry(entry, context)); case ContentType.STARTUP: return retype(yield this._startUp.fromEntry(entry, context)); case ContentType.SCHEDULE: return retype(yield this._schedule.fromEntry(entry, context)); case ContentType.DATE_RANGE: return retype(yield this._dateRange.fromEntry(entry, context)); default: throw new Error(`${model} is not a Content type`); } // no need to wrap in an CMSException with the content id because asyncMap // already does it }); } // TODO move all delivery instances to a class contentFromEntry(entry, context) { return __awaiter(this, void 0, void 0, function* () { const model = ContentfulEntryUtils.getContentModel(entry); const retype = (c) => c; switch (model) { case ContentType.BUTTON: return retype(this._button.fromEntry(entry, context)); case ContentType.ELEMENT: return retype(yield this._carousel.elementFromEntry(entry, context)); default: return retype(yield this.topContentFromEntry(entry, context)); } }); } contentsWithKeywords(context = DEFAULT_CONTEXT, paging = new PagingOptions()) { return __awaiter(this, void 0, void 0, function* () { return this._keywords.contentsWithKeywords(context, paging); }); } schedule(id, context) { return __awaiter(this, void 0, void 0, function* () { return this._schedule.schedule(id, context); }); } asset(id, context = DEFAULT_CONTEXT) { return this._asset.asset(id, context); } assets(context = DEFAULT_CONTEXT) { return this._asset.assets(context); } dateRange(id, context) { return this._dateRange.dateRange(id, context); } } //# sourceMappingURL=cms-contentful.js.map