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

82 lines 3.58 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TopContentDelivery = exports.ContentDelivery = exports.ResourceDelivery = void 0; const tslib_1 = require("tslib"); const cms_1 = require("../cms"); const async_1 = require("../util/async"); const delivery_utils_1 = require("./delivery-utils"); class ResourceDelivery { constructor(delivery, resumeErrors) { this.delivery = delivery; this.resumeErrors = resumeErrors; } urlFromAssetRequired(assetField) { if (!assetField.fields.file) { throw new cms_1.CmsException(`found empty ${assetField.sys.type} asset. Missing localization?`); } return 'https:' + assetField.fields.file.url; } urlFromAssetOptional(assetField = undefined, context) { if (!assetField) { return undefined; } if (!assetField.fields.file) { this.logOrThrow(`found empty asset. Missing localization?`, context, undefined, new cms_1.AssetId(assetField.sys.id, assetField.sys.type)); return undefined; } return this.urlFromAssetRequired(assetField); } checkEntry(entry) { if (entry.fields == undefined) { // this can also happen when the chain of content references is too long. // Try increasing the {include} key in the call to getEntry of the top component throw new cms_1.CmsException(`Cannot find '${entry.sys.type}' with id '${entry.sys.id}' Broken reference? Not published?`); } } logOrThrow(doing, context, reason, resourceId) { if (this.resumeErrors) { console.error(`ERROR: ${doing} on content ${resourceId.toString()} on locale '${String(context.locale)}'. Returning content with partial data.`); return; } throw new cms_1.CmsException(doing, reason, resourceId); } asyncMap(context, entries, factory) { return (0, async_1.asyncMap)(context, entries, factory, undefined, (entry, e) => { const contentId = this.getContentIdForLogs(entry); this.logOrThrow(`Loading content failed`, context, e, contentId); return undefined; }); } getContentIdForLogs(entry) { if (delivery_utils_1.ContentfulEntryUtils.isFullEntry(entry)) { return delivery_utils_1.ContentfulEntryUtils.getContentId(entry); } return new cms_1.ContentId('<UNKNOWN MODEL TYPE>', entry.sys.id); } } exports.ResourceDelivery = ResourceDelivery; class ContentDelivery extends ResourceDelivery { constructor(modelType, delivery, resumeErrors) { super(delivery, resumeErrors); this.modelType = modelType; } getEntry(id, context, query = {}) { return tslib_1.__awaiter(this, void 0, void 0, function* () { const entry = yield this.delivery.getEntry(id, context, query); const gotType = delivery_utils_1.ContentfulEntryUtils.getContentModel(entry); if (!(0, cms_1.isCustomModel)(gotType, this.modelType) && !(0, cms_1.isSameModel)(gotType, this.modelType)) { throw new Error(`Requested model with id '${id}' of type '${this.modelType}' but got '${gotType}'`); } return entry; }); } entryId(entry) { return entry.fields.name + '/' + entry.sys.id; } } exports.ContentDelivery = ContentDelivery; class TopContentDelivery extends ContentDelivery { } exports.TopContentDelivery = TopContentDelivery; //# sourceMappingURL=content-delivery.js.map