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

99 lines 3.5 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createContentfulClientApi = exports.ContentfulEntryUtils = exports.AdaptorDeliveryApi = void 0; const tslib_1 = require("tslib"); const contentful = tslib_1.__importStar(require("contentful")); const cms_1 = require("../cms"); const delivery_utils_1 = require("./delivery-utils"); /** * Manages the {@link Context}, parses Content's Id and ContentType from the Contentful entries... */ class AdaptorDeliveryApi { constructor(client, options) { this.client = client; this.options = options; } getAsset(id, context, query) { return tslib_1.__awaiter(this, void 0, void 0, function* () { return this.client.getAsset(id, this.queryFromContext(context, query)); }); } getAssets(context, query) { return tslib_1.__awaiter(this, void 0, void 0, function* () { return this.client.getAssets(this.queryFromContext(context, query)); }); } getEntry(id, context, query = {}) { return tslib_1.__awaiter(this, void 0, void 0, function* () { return this.client.getEntry(id, this.queryFromContext(context, query)); }); } getEntries(context, query = {}) { return tslib_1.__awaiter(this, void 0, void 0, function* () { try { return this.client.getEntries(this.queryFromContext(context, query)); } catch (e) { throw (0, delivery_utils_1.convertContentfulException)(e, query); } }); } getContentType(id) { return tslib_1.__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; } } exports.AdaptorDeliveryApi = AdaptorDeliveryApi; class ContentfulEntryUtils { static getContentId(entry) { return cms_1.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 cms_1.CmsException(`Entry '${entry.sys.id}' not fully loaded or referencing a deleted content`); } return entry.sys.contentType.sys.id; } } 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; //# sourceMappingURL=delivery-api.js.map