UNPKG

@itxch/contentful-import

Version:

This tool allows you to import JSON dump exported by contentful-export

169 lines (168 loc) 4.87 kB
import Promise$1 from "bluebird"; import { logEmitter } from "contentful-batch-libs/dist/logging.js"; const BATCH_CHAR_LIMIT = 1990; const BATCH_SIZE_LIMIT = 100; const METHODS = { contentTypes: { name: "content types", method: "getContentTypes" }, locales: { name: "locales", method: "getLocales" }, entries: { name: "entries", method: "getEntries" }, assets: { name: "assets", method: "getAssets" }, tags: { name: "tags", method: "getTags" } }; async function batchedIdQuery({ environment, type, ids, requestQueue }) { const method = METHODS[type].method; const entityTypeName = METHODS[type].name; const batches = getIdBatches(ids); let totalFetched = 0; const allPendingResponses = batches.map((idBatch) => { return requestQueue.add(async () => { const response = await environment[method]({ "sys.id[in]": idBatch, limit: idBatch.split(",").length }); totalFetched = totalFetched + response.items.length; logEmitter.emit("info", `Fetched ${totalFetched} of ${response.total} ${entityTypeName}`); return response.items; }); }); const responses = await Promise$1.all(allPendingResponses); return responses.flat(); } async function batchedPageQuery({ environment, type, requestQueue }) { const method = METHODS[type].method; const entityTypeName = METHODS[type].name; let totalFetched = 0; const data = await requestQueue.add(async () => { const response = await environment[method]({ skip: 0, limit: BATCH_SIZE_LIMIT }); totalFetched += response.items.length; logEmitter.emit("info", `Fetched ${totalFetched} of ${response.total} ${entityTypeName}`); return { items: response.items, total: response.total }; }); if (!data) { throw "Error"; } const { items, total } = data; const batches = getPagedBatches(totalFetched, total); const remainingTotalResponses = batches.map(({ skip }) => { return requestQueue.add(async () => { const response = await environment[method]({ skip, limit: BATCH_SIZE_LIMIT }); totalFetched = totalFetched + response.items.length; logEmitter.emit("info", `Fetched ${totalFetched} of ${response.total} ${entityTypeName}`); return response.items; }); }); const remainingResponses = await Promise$1.all(remainingTotalResponses); return items.concat(remainingResponses.flat()); } function getIdBatches(ids) { const batches = []; let currentBatch = ""; let currentSize = 0; while (ids.length > 0) { const id = ids.splice(0, 1); currentBatch += id; currentSize = currentSize + 1; if (currentSize === BATCH_SIZE_LIMIT || currentBatch.length > BATCH_CHAR_LIMIT || ids.length === 0) { batches.push(currentBatch); currentBatch = ""; currentSize = 0; } else { currentBatch += ","; } } return batches; } function getPagedBatches(totalFetched, total) { const batches = []; if (totalFetched >= total) { return batches; } let skip = totalFetched; while (skip < total) { batches.push({ skip }); skip += BATCH_SIZE_LIMIT; } return batches; } async function getDestinationData({ client, spaceId, environmentId, sourceData, contentModelOnly, skipLocales, skipContentModel, requestQueue }) { const space = await client.getSpace(spaceId); const environment = await space.getEnvironment(environmentId); const result = { contentTypes: [], tags: [], locales: [], entries: [], assets: [] }; sourceData = { ...result, ...sourceData }; if (!skipContentModel) { const contentTypeIds = sourceData.contentTypes?.map((e) => e.sys.id); if (contentTypeIds) { result.contentTypes = batchedIdQuery({ environment, type: "contentTypes", ids: contentTypeIds, requestQueue }); } if (!skipLocales) { const localeIds = sourceData.locales?.map((e) => e.sys.id); if (localeIds && localeIds.length) { result.locales = batchedPageQuery({ environment, type: "locales", requestQueue }); } } } try { result.tags = await batchedPageQuery({ environment, type: "tags", requestQueue }); } catch (_) { delete result.tags; } if (contentModelOnly) { return Promise$1.props(result); } const entryIds = sourceData.entries?.map((e) => e.sys.id); const assetIds = sourceData.assets?.map((e) => e.sys.id); if (entryIds) { result.entries = batchedIdQuery({ environment, type: "entries", ids: entryIds, requestQueue }); } if (assetIds) { result.assets = batchedIdQuery({ environment, type: "assets", ids: assetIds, requestQueue }); } result.webhooks = []; return Promise$1.props(result); } export { getDestinationData as default };