@botonic/plugin-contentful
Version:
## What Does This Plugin Do?
47 lines • 1.63 kB
JavaScript
import { __awaiter } from "tslib";
import { ContentType } from '../cms';
import { LocaleInfo } from '../cms/cms-info';
import { isOfType } from '../util/enums';
import { createContentfulClientApi } from './delivery-utils';
export class ContentfulInfo {
constructor(options) {
this.options = options;
this.client = createContentfulClientApi(options);
}
contentTypes() {
return __awaiter(this, void 0, void 0, function* () {
const models = yield this.client.getContentTypes();
return models.items
.map(m => {
if (isOfType(m.sys.id, ContentType)) {
return m.sys.id;
}
return undefined;
})
.filter(m => !!m);
});
}
defaultLocale() {
return __awaiter(this, void 0, void 0, function* () {
const locales = yield this.locales();
for (const locale of Object.values(locales)) {
if (locale.isDefault) {
return locale;
}
}
throw new Error(`No default locale found`);
});
}
locales() {
return __awaiter(this, void 0, void 0, function* () {
const locales = (yield this.client.getLocales()).items;
return locales
.map(l => new LocaleInfo(l.code, l.name, l.fallbackCode || undefined, l.default))
.reduce((newObj, l) => {
newObj[l.code] = l;
return newObj;
}, {});
});
}
}
//# sourceMappingURL=info-contentful.js.map