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

108 lines 4.95 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.isReferenceField = exports.addCustomFields = exports.convertContentfulException = exports.createContentfulClientApi = exports.ContentfulEntryUtils = void 0; const tslib_1 = require("tslib"); const contentful = tslib_1.__importStar(require("contentful")); const cms_1 = require("../cms"); const exceptions_1 = require("../cms/exceptions"); const time = tslib_1.__importStar(require("../time")); const searchable_by_1 = require("./search/searchable-by"); class ContentfulEntryUtils { static getContentId(entry) { return new cms_1.ContentId(ContentfulEntryUtils.getContentModel(entry), entry.sys.id); } /** * Will be false for broken references, or when we have only fetched * the full Entry tree */ static isFullEntry(entry) { return !!entry.fields; } static getContentModel(entry) { // https://blog.oio.de/2014/02/28/typescript-accessing-enum-values-via-a-string/ if (!entry.sys.contentType) { throw new cms_1.CmsException(`Entry '${entry.sys.id}' not fully loaded or referencing a deleted content`); } const typ = entry.sys.contentType.sys.id; return typ; } static commonFieldsFromEntry(entry, followUp) { const fields = entry.fields; const searchableBy = fields.searchableBy && new cms_1.SearchableBy(fields.searchableBy.map(searchableBy => searchable_by_1.SearchableByKeywordsDelivery.fromEntry(searchableBy))); const dateRange = fields.dateRange && ContentfulEntryUtils.fromDateRangeEntry(fields.dateRange); return new cms_1.CommonFields(entry.sys.id, fields.name || '', { keywords: fields.keywords, shortText: fields.shortText, partitions: fields.partitions, searchableBy, dateRange, followUp, //customFields cannot be easily told apart from standard fields until the content is created. see addCustomFields }); } /** Cannot be in date-range to avoid circular dependency */ static fromDateRangeEntry(entry) { const dateRange = new time.DateRange(entry.fields.name, new Date(Date.parse(entry.fields.from)), new Date(Date.parse(entry.fields.to))); return new cms_1.DateRangeContent(ContentfulEntryUtils.commonFieldsFromEntry(entry), dateRange); } } exports.ContentfulEntryUtils = ContentfulEntryUtils; function createContentfulClientApi(options) { const params = { space: options.spaceId, accessToken: options.accessToken, timeout: options.timeoutMs, }; if (options.environment) { params.environment = options.environment; } const client = contentful.createClient(params); return client; } exports.createContentfulClientApi = createContentfulClientApi; function convertContentfulException(e, query) { var _a; const errors = (_a = e === null || e === void 0 ? void 0 : e.details) === null || _a === void 0 ? void 0 : _a.errors; if (Array.isArray(errors) && errors.length && errors[0].name === 'unknownContentType') { return new exceptions_1.ResourceTypeNotFoundCmsException(query['content_type'] || 'not set', e); } return e; } exports.convertContentfulException = convertContentfulException; //supported types: string, number and boolean function addCustomFields(content, entryFields, referenceDelivery, ignoreFields) { return tslib_1.__awaiter(this, void 0, void 0, function* () { const customKeys = Object.keys(entryFields).filter(f => !Object.keys(content).includes(f) && !Object.keys(content.common).includes(f) && !(ignoreFields === null || ignoreFields === void 0 ? void 0 : ignoreFields.includes(f)) && //contentful: followup, plugin: followUp 'followup' != f); for (const customKey of customKeys) { const customField = entryFields[customKey]; if (isReferenceField(customField)) { if (referenceDelivery) { content.common.customFields[customKey] = yield referenceDelivery.delivery.fromEntry(customField, referenceDelivery.context); } else { console.error(`Warning: entry with id ${content.common.id}: type ${content.contentType} can't have custom reference fields`); } } else { content.common.customFields[customKey] = entryFields[customKey]; } } return content; }); } exports.addCustomFields = addCustomFields; function isReferenceField(field) { var _a; return (_a = field === null || field === void 0 ? void 0 : field.sys) === null || _a === void 0 ? void 0 : _a.id; } exports.isReferenceField = isReferenceField; //# sourceMappingURL=delivery-utils.js.map