@botonic/plugin-contentful
Version:
## What Does This Plugin Do?
101 lines • 3.67 kB
JavaScript
import { __awaiter } from "tslib";
import { convertContentfulException } from './delivery-utils';
import { I18nEntryTraverser, } from './traverser';
/**
* It requests contentful to deliver all locales for each entry, and we discard all except the one in the context
*/
export class IgnoreFallbackDecorator {
constructor(api) {
this.api = api;
}
getEntries(context, query = {}) {
return __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 convertContentfulException(e, query);
}
});
}
getEntry(id, context, query = {}) {
return __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 __awaiter(this, void 0, void 0, function* () {
const visitor = new IgnoreFallbackVisitor(context);
return Promise.all(entries.map((item) => __awaiter(this, void 0, void 0, function* () {
const traverser = new 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 __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: '*' });
}
}
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