@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
105 lines • 3.9 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.IgnoreFallbackDecorator = void 0;
const tslib_1 = require("tslib");
const delivery_utils_1 = require("./delivery-utils");
const traverser_1 = require("./traverser");
/**
* It requests contentful to deliver all locales for each entry, and we discard all except the one in the context
*/
class IgnoreFallbackDecorator {
constructor(api) {
this.api = api;
}
getEntries(context, query = {}) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
if (!context.ignoreFallbackLocale) {
return this.api.getEntries(context, query);
}
try {
let entries = yield this.api.getEntries(this.i18nContext(context), query);
entries = Object.assign({}, entries);
entries.items = yield this.traverseEntries(context, entries.items);
return entries;
}
catch (e) {
throw (0, delivery_utils_1.convertContentfulException)(e, query);
}
});
}
getEntry(id, context, query = {}) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
if (!context.ignoreFallbackLocale) {
return this.api.getEntry(id, context, query);
}
const entry = yield this.api.getEntry(id, this.i18nContext(context), query);
return (yield this.traverseEntries(context, [entry]))[0];
});
}
traverseEntries(context, entries) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const visitor = new IgnoreFallbackVisitor(context);
return Promise.all(entries.map((item) => tslib_1.__awaiter(this, void 0, void 0, function* () {
const traverser = new traverser_1.I18nEntryTraverser(this.api, visitor);
return yield traverser.traverse(item, context);
})));
});
}
getAsset(id, context, query) {
console.warn('IgnoreFallbackDecorator does not any special treatment for getAsset');
return this.api.getAsset(id, context, query);
}
getAssets(context, query) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
console.warn('IgnoreFallbackDecorator does not any special treatment for getAssets');
return this.api.getAssets(context, query);
});
}
getContentType(id) {
return this.api.getContentType(id);
}
getOptions() {
return this.api.getOptions();
}
i18nContext(context) {
return Object.assign(Object.assign({}, context), { locale: '*' });
}
}
exports.IgnoreFallbackDecorator = IgnoreFallbackDecorator;
class IgnoreFallbackVisitor {
constructor(context) {
this.context = context;
if (!context.locale) {
throw new Error('Context.ignoreFallbackLocale set but Context.locale not set');
}
this.contextForContentful = Object.assign(Object.assign({}, context), { locale: '*' });
}
visitEntry(entry) {
return entry;
}
visitOtherField(vf) {
return this.hackType(vf.value[vf.locale], undefined);
}
visitStringField(vf) {
return this.hackType(vf.value[vf.locale], '');
}
hackType(t, defaultValue) {
if (defaultValue != undefined) {
t = t !== null && t !== void 0 ? t : defaultValue;
}
return t;
}
visitMultipleStringField(vf) {
return this.hackType(vf.value[vf.locale], []);
}
visitSingleReference(vf) {
return this.hackType(vf.value[vf.locale], undefined);
}
visitMultipleReference(vf) {
return this.hackType(vf.value[vf.locale]);
}
name() {
return 'ignoreFallbackLocale';
}
}
//# sourceMappingURL=ignore-fallback-decorator.js.map