@botonic/plugin-contentful
Version:
## What Does This Plugin Do?
76 lines • 3.23 kB
JavaScript
import { __awaiter } from "tslib";
import { AssetId, CmsException, ContentId, isCustomModel, isSameModel, } from '../cms';
import { asyncMap } from '../util/async';
import { ContentfulEntryUtils } from './delivery-utils';
export class ResourceDelivery {
constructor(delivery, resumeErrors) {
this.delivery = delivery;
this.resumeErrors = resumeErrors;
}
urlFromAssetRequired(assetField) {
if (!assetField.fields.file) {
throw new 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 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 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 CmsException(doing, reason, resourceId);
}
asyncMap(context, entries, factory) {
return 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 (ContentfulEntryUtils.isFullEntry(entry)) {
return ContentfulEntryUtils.getContentId(entry);
}
return new ContentId('<UNKNOWN MODEL TYPE>', entry.sys.id);
}
}
export class ContentDelivery extends ResourceDelivery {
constructor(modelType, delivery, resumeErrors) {
super(delivery, resumeErrors);
this.modelType = modelType;
}
getEntry(id, context, query = {}) {
return __awaiter(this, void 0, void 0, function* () {
const entry = yield this.delivery.getEntry(id, context, query);
const gotType = ContentfulEntryUtils.getContentModel(entry);
if (!isCustomModel(gotType, this.modelType) &&
!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;
}
}
export class TopContentDelivery extends ContentDelivery {
}
//# sourceMappingURL=content-delivery.js.map