@becklyn/contentful-adapter
Version:
[](https://github.com/Becklyn-Studios/contentful-adapter/actions/workflows/ci.yml)
122 lines (121 loc) • 5.89 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.findEntriesByIds = exports.findOneEntryBySys = exports.findOneAsset = exports.findOneEntry = exports.findEntries = exports.findAllEntries = exports.connectToContentfulDeliveryApi = exports.getExecutedMigrations = exports.getDefaultLocale = exports.connectToContentfulManagementApi = void 0;
const contentful_management_1 = require("contentful-management");
const migration_1 = require("../migrations/migration");
const util_1 = require("./util");
const contentful_1 = require("contentful");
const connectToContentfulManagementApi = async (config) => {
const client = (0, contentful_management_1.createClient)({ accessToken: config.management.accessToken });
const space = await client.getSpace(config.spaceId);
return await space.getEnvironment(config.environmentId);
};
exports.connectToContentfulManagementApi = connectToContentfulManagementApi;
const getDefaultLocale = async (environment) => {
const { items: locales } = await environment.getLocales();
const defaultLocale = locales.find(locale => locale.default);
return defaultLocale ? defaultLocale.code : "en-US";
};
exports.getDefaultLocale = getDefaultLocale;
const getExecutedMigrations = async (environment, locale) => {
let isInitialMigration = false;
await environment.getContentType(migration_1.MIGRATIONS_MODEL_NAME).catch(() => {
isInitialMigration = true;
});
if (isInitialMigration) {
return [];
}
let allVersions = [];
let total = null;
let skip = 0;
while (total === null || total > allVersions.length) {
const { items: versions, total: newTotal } = await environment.getEntries({
content_type: migration_1.MIGRATIONS_MODEL_NAME,
limit: 1000,
skip,
});
total = newTotal;
skip += versions.length;
allVersions = [...allVersions, ...versions];
}
return allVersions.map(version => version.fields.version[locale]).filter(version => !!version);
};
exports.getExecutedMigrations = getExecutedMigrations;
const connectToContentfulDeliveryApi = (config, preview = false) => {
return (0, contentful_1.createClient)({
space: config.spaceId,
accessToken: preview ? config.delivery.previewAccessToken : config.delivery.accessToken,
environment: config.environmentId,
host: preview ? "preview.contentful.com" : undefined,
});
};
exports.connectToContentfulDeliveryApi = connectToContentfulDeliveryApi;
const findAllEntries = async (client, { contentType, select, where, depth }) => {
let allEntries = [];
let total = null;
let skip = 0;
while (total === null || total > allEntries.length) {
const { items, total: newTotal } = await client.getEntries(Object.assign(Object.assign({}, (0, util_1.getContentfulWhereObject)(where)), { content_type: contentType, select: (0, util_1.getContentfulSelectString)(select), include: depth ? depth : 0, limit: 1000, skip }));
total = newTotal;
skip += items.length;
allEntries = [...allEntries, ...items];
}
return allEntries;
};
exports.findAllEntries = findAllEntries;
const findEntries = async (client, { contentType, select, where, depth, limit, skip }) => {
const entries = await client.getEntries(Object.assign(Object.assign({}, (0, util_1.getContentfulWhereObject)(where)), { content_type: contentType, select: (0, util_1.getContentfulSelectString)(select), include: depth ? depth : 0, limit,
skip }));
return entries.items;
};
exports.findEntries = findEntries;
const findOneEntry = async (client, { contentType, select, where, depth, throwError }) => {
const entries = await client.getEntries(Object.assign(Object.assign({}, (0, util_1.getContentfulWhereObject)(where)), { content_type: contentType, select: (0, util_1.getContentfulSelectString)(select), limit: 1, include: depth ? depth : 0 }));
if (0 === entries.items.length) {
if (throwError) {
throw new Error(`Could not find a single ${contentType}`);
}
else {
return null;
}
}
if (1 < entries.items.length && throwError) {
throw new Error(`Found multiple ${contentType} entries`);
}
return entries.items[0];
};
exports.findOneEntry = findOneEntry;
const findOneAsset = async (assetId, client) => {
try {
return await client.getAsset(assetId);
}
catch (e) {
return null;
}
};
exports.findOneAsset = findOneAsset;
const findOneEntryBySys = async (sys, client, { select, where, depth }) => {
if (!sys) {
return null;
}
try {
return await client.getEntry(sys.id, Object.assign(Object.assign({}, (0, util_1.getContentfulWhereObject)(where)), { content_type: sys.contentType && sys.contentType.sys ? sys.contentType.sys.id : undefined, select: (0, util_1.getContentfulSelectString)(select), include: depth ? depth : 0 }));
}
catch (e) {
return null;
}
};
exports.findOneEntryBySys = findOneEntryBySys;
const findEntriesByIds = async (client, options) => {
return await findAllEntriesByIds(client, options);
};
exports.findEntriesByIds = findEntriesByIds;
const findAllEntriesByIds = async (client, options, skip = 0, foundEntries = []) => {
const { contentType, select, where, ids, depth } = options;
const limit = 1000;
const entries = await client.getEntries(Object.assign(Object.assign({}, (0, util_1.getContentfulWhereObject)(where)), { content_type: contentType, select: (0, util_1.getContentfulSelectString)(select), "sys.id[in]": ids.join(","), limit, include: depth ? depth : 0 }));
foundEntries = [...foundEntries, ...entries.items];
return foundEntries.length < entries.total
? findAllEntriesByIds(client, options, skip + limit, foundEntries)
: foundEntries;
};