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

93 lines 3.1 kB
import { __awaiter } from "tslib"; import * as contentful from 'contentful'; import { CmsException, ContentId } from '../cms'; import { convertContentfulException } from './delivery-utils'; /** * Manages the {@link Context}, parses Content's Id and ContentType from the Contentful entries... */ export class AdaptorDeliveryApi { constructor(client, options) { this.client = client; this.options = options; } getAsset(id, context, query) { return __awaiter(this, void 0, void 0, function* () { return this.client.getAsset(id, this.queryFromContext(context, query)); }); } getAssets(context, query) { return __awaiter(this, void 0, void 0, function* () { return this.client.getAssets(this.queryFromContext(context, query)); }); } getEntry(id, context, query = {}) { return __awaiter(this, void 0, void 0, function* () { return this.client.getEntry(id, this.queryFromContext(context, query)); }); } getEntries(context, query = {}) { return __awaiter(this, void 0, void 0, function* () { try { return this.client.getEntries(this.queryFromContext(context, query)); } catch (e) { throw convertContentfulException(e, query); } }); } getContentType(id) { return __awaiter(this, void 0, void 0, function* () { try { return this.client.getContentType(id); } catch (e) { console.error(`ERROR in getContentType for id ${id}:`, e); throw e; } }); } getOptions() { return this.options; } queryFromContext(context, query = {}) { const locale = this.options.cmsLocale ? this.options.cmsLocale(context.locale) : context.locale; if (locale) { query['locale'] = locale; } return query; } } export class ContentfulEntryUtils { static getContentId(entry) { return ContentId.create(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`); } return entry.sys.contentType.sys.id; } } 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; } //# sourceMappingURL=delivery-api.js.map