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

100 lines 4.46 kB
import { __awaiter } from "tslib"; import * as contentful from 'contentful'; import { CmsException, CommonFields, ContentId, DateRangeContent, SearchableBy, } from '../cms'; import { ResourceTypeNotFoundCmsException } from '../cms/exceptions'; import * as time from '../time'; import { SearchableByKeywordsDelivery, } from './search/searchable-by'; export class ContentfulEntryUtils { static getContentId(entry) { return new 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 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 SearchableBy(fields.searchableBy.map(searchableBy => SearchableByKeywordsDelivery.fromEntry(searchableBy))); const dateRange = fields.dateRange && ContentfulEntryUtils.fromDateRangeEntry(fields.dateRange); return new 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 DateRangeContent(ContentfulEntryUtils.commonFieldsFromEntry(entry), dateRange); } } export 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; } export 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 ResourceTypeNotFoundCmsException(query['content_type'] || 'not set', e); } return e; } //supported types: string, number and boolean export function addCustomFields(content, entryFields, referenceDelivery, ignoreFields) { return __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; }); } export function isReferenceField(field) { var _a; return (_a = field === null || field === void 0 ? void 0 : field.sys) === null || _a === void 0 ? void 0 : _a.id; } //# sourceMappingURL=delivery-utils.js.map