@botonic/plugin-contentful
Version:
## What Does This Plugin Do?
244 lines • 11.8 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Contentful = void 0;
const tslib_1 = require("tslib");
const cms_1 = require("../cms");
const plugin_1 = require("../plugin");
const asset_1 = require("./contents/asset");
const button_1 = require("./contents/button");
const carousel_1 = require("./contents/carousel");
const contents_1 = require("./contents/contents");
const custom_1 = require("./contents/custom");
const date_range_1 = require("./contents/date-range");
const document_1 = require("./contents/document");
const handoff_1 = require("./contents/handoff");
const image_1 = require("./contents/image");
const input_1 = require("./contents/input");
const payload_1 = require("./contents/payload");
const queue_1 = require("./contents/queue");
const reference_1 = require("./contents/reference");
const schedule_1 = require("./contents/schedule");
const startup_1 = require("./contents/startup");
const text_1 = require("./contents/text");
const url_1 = require("./contents/url");
const video_1 = require("./contents/video");
const cache_1 = require("./delivery/cache");
const fallback_cache_1 = require("./delivery/fallback-cache");
const delivery_api_1 = require("./delivery-api");
const delivery_utils_1 = require("./delivery-utils");
const ignore_fallback_decorator_1 = require("./ignore-fallback-decorator");
const keywords_1 = require("./search/keywords");
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 = (0, delivery_utils_1.createContentfulClientApi)(options);
if (!options.disableFallbackCache) {
client = new fallback_cache_1.FallbackCachedClientApi(client, (_b = options.fallbackCacheLimitKB) !== null && _b !== void 0 ? _b : plugin_1.DEFAULT_FALLBACK_CACHE_LIMIT_KB, reporter, logger);
}
if (!options.disableCache) {
client = new cache_1.CachedClientApi(client, options.cacheTtlMs, reporter);
}
const deliveryApi = new delivery_api_1.AdaptorDeliveryApi(client, options);
const resumeErrors = options.resumeErrors || false;
const delivery = new ignore_fallback_decorator_1.IgnoreFallbackDecorator(deliveryApi);
this._contents = new contents_1.ContentsDelivery(delivery, resumeErrors);
this._delivery = delivery;
this._button = new button_1.ButtonDelivery(delivery, resumeErrors);
this._carousel = new carousel_1.CarouselDelivery(delivery, this._button, resumeErrors);
this._document = new document_1.DocumentDelivery(delivery, resumeErrors);
this._text = new text_1.TextDelivery(delivery, this._button, resumeErrors);
this._startUp = new startup_1.StartUpDelivery(delivery, this._button, resumeErrors);
this._url = new url_1.UrlDelivery(delivery, resumeErrors);
this._payload = new payload_1.PayloadDelivery(delivery, resumeErrors);
this._image = new image_1.ImageDelivery(delivery, resumeErrors);
this._video = new video_1.VideoDelivery(delivery, resumeErrors);
this._asset = new asset_1.AssetDelivery(delivery, resumeErrors);
this._schedule = new schedule_1.ScheduleDelivery(delivery, resumeErrors);
this._queue = new queue_1.QueueDelivery(delivery, this._schedule, resumeErrors);
this._handoff = new handoff_1.HandoffDelivery(delivery, this._queue, this._text, resumeErrors);
this._input = new input_1.InputDelivery(delivery, resumeErrors);
this._custom = new custom_1.CustomDelivery(delivery, resumeErrors);
this._dateRange = new date_range_1.DateRangeDelivery(delivery, resumeErrors);
const followUp = new reference_1.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 keywords_1.KeywordsDelivery(delivery);
}
button(id, context = cms_1.DEFAULT_CONTEXT) {
return this._button.button(id, context);
}
element(id, context = cms_1.DEFAULT_CONTEXT) {
return this._carousel.element(id, context);
}
carousel(id, context = cms_1.DEFAULT_CONTEXT) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
return this._carousel.carousel(id, context);
});
}
document(id, context = cms_1.DEFAULT_CONTEXT) {
return this._document.document(id, context);
}
text(id, context = cms_1.DEFAULT_CONTEXT) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
return this._text.text(id, context);
});
}
startUp(id, context = cms_1.DEFAULT_CONTEXT) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
return this._startUp.startUp(id, context);
});
}
url(id, context = cms_1.DEFAULT_CONTEXT) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
return this._url.url(id, context);
});
}
payload(id, context = cms_1.DEFAULT_CONTEXT) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
return this._payload.payload(id, context);
});
}
queue(id, context = cms_1.DEFAULT_CONTEXT) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
return this._queue.queue(id, context);
});
}
image(id, context = cms_1.DEFAULT_CONTEXT) {
return this._image.image(id, context);
}
video(id, context = cms_1.DEFAULT_CONTEXT) {
return this._video.video(id, context);
}
chitchat(id, context = cms_1.DEFAULT_CONTEXT) {
return this._text.text(id, context);
}
handoff(id, context = cms_1.DEFAULT_CONTEXT) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
return this._handoff.handoff(id, context);
});
}
input(id, context = cms_1.DEFAULT_CONTEXT) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
return this._input.input(id, context);
});
}
custom(id, context = cms_1.DEFAULT_CONTEXT) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
return this._custom.custom(id, context);
});
}
topContents(model, context = cms_1.DEFAULT_CONTEXT, filter, paging = new cms_1.PagingOptions()) {
return this._contents.topContents(model, context, (entry, ctxt) => this.topContentFromEntry(entry, ctxt), filter, paging);
}
content(id, context = cms_1.DEFAULT_CONTEXT, referencesToInclude = cms_1.DEFAULT_REFERENCES_TO_INCLUDE) {
return tslib_1.__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 = cms_1.DEFAULT_CONTEXT, paging = new cms_1.PagingOptions()) {
return this._contents.contents(contentType, context, (entry, ctxt) => this.contentFromEntry(entry, ctxt), paging);
}
topContentFromEntry(entry, context) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const model = delivery_utils_1.ContentfulEntryUtils.getContentModel(entry);
const retype = (c) => c;
switch (model) {
case cms_1.ContentType.CAROUSEL:
return retype(yield this._carousel.fromEntry(entry, context));
case cms_1.ContentType.DOCUMENT:
return retype(yield this._document.fromEntry(entry, context));
case cms_1.ContentType.QUEUE:
return retype(yield this._queue.fromEntry(entry, context));
case cms_1.ContentType.CHITCHAT:
case cms_1.ContentType.TEXT:
return retype(yield this._text.fromEntry(entry, context));
case cms_1.ContentType.IMAGE:
return retype(yield this._image.fromEntry(entry, context));
case cms_1.ContentType.VIDEO:
return retype(yield this._video.fromEntry(entry, context));
case cms_1.ContentType.HANDOFF:
return retype(yield this._handoff.fromEntry(entry, context));
case cms_1.ContentType.INPUT:
return retype(yield this._input.fromEntry(entry, context));
case cms_1.ContentType.URL:
return retype(yield this._url.fromEntry(entry, context));
case cms_1.ContentType.PAYLOAD:
return retype(yield this._payload.fromEntry(entry, context));
case cms_1.ContentType.STARTUP:
return retype(yield this._startUp.fromEntry(entry, context));
case cms_1.ContentType.SCHEDULE:
return retype(yield this._schedule.fromEntry(entry, context));
case cms_1.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 tslib_1.__awaiter(this, void 0, void 0, function* () {
const model = delivery_utils_1.ContentfulEntryUtils.getContentModel(entry);
const retype = (c) => c;
switch (model) {
case cms_1.ContentType.BUTTON:
return retype(this._button.fromEntry(entry, context));
case cms_1.ContentType.ELEMENT:
return retype(yield this._carousel.elementFromEntry(entry, context));
default:
return retype(yield this.topContentFromEntry(entry, context));
}
});
}
contentsWithKeywords(context = cms_1.DEFAULT_CONTEXT, paging = new cms_1.PagingOptions()) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
return this._keywords.contentsWithKeywords(context, paging);
});
}
schedule(id, context) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
return this._schedule.schedule(id, context);
});
}
asset(id, context = cms_1.DEFAULT_CONTEXT) {
return this._asset.asset(id, context);
}
assets(context = cms_1.DEFAULT_CONTEXT) {
return this._asset.assets(context);
}
dateRange(id, context) {
return this._dateRange.dateRange(id, context);
}
}
exports.Contentful = Contentful;
//# sourceMappingURL=cms-contentful.js.map