UNPKG

contentful-management

Version:
144 lines (141 loc) 5.86 kB
import { errorHandler } from 'contentful-sdk-core'; import copy from 'fast-copy'; import { getUploadHttpClient } from '../../../upload-http-client.js'; import { post, put, get as get$1 } from './raw.js'; import { create as create$1 } from './upload.js'; import { normalizeSelect } from './utils.js'; const get = (http, params, rawData, headers) => { return get$1(http, `/spaces/${params.spaceId}/environments/${params.environmentId}/releases/${params.releaseId}/assets/${params.assetId}`, { params: normalizeSelect(params.query), headers: headers ? { ...headers } : undefined, }); }; const getMany = (http, params, rawData, headers) => { return get$1(http, `/spaces/${params.spaceId}/environments/${params.environmentId}/releases/${params.releaseId}/assets`, { params: normalizeSelect(params.query), headers: headers ? { ...headers } : undefined, }); }; const update = (http, params, rawData, headers) => { const data = copy(rawData); delete data.sys; return put(http, `/spaces/${params.spaceId}/environments/${params.environmentId}/releases/${params.releaseId}/assets/${params.assetId}`, data, { headers: { 'X-Contentful-Version': rawData.sys.version ?? 0, ...headers, }, }); }; const create = (http, params, rawData, headers) => { const data = copy(rawData); return post(http, `/spaces/${params.spaceId}/environments/${params.environmentId}/releases/${params.releaseId}/assets`, data, { headers, }); }; const createWithId = (http, params, rawData, headers) => { const data = copy(rawData); return put(http, `/spaces/${params.spaceId}/environments/${params.environmentId}/releases/${params.releaseId}/assets/${params.assetId}`, data, { headers, }); }; const createFromFiles = async (http, params, data) => { const httpUpload = getUploadHttpClient(http, { uploadTimeout: params.uploadTimeout }); const { file } = data.fields; return Promise.all(Object.keys(file).map(async (locale) => { const { contentType, fileName } = file[locale]; return create$1(httpUpload, params, file[locale]).then((upload) => { return { [locale]: { contentType, fileName, uploadFrom: { sys: { type: 'Link', linkType: 'Upload', id: upload.sys.id, }, }, }, }; }); })) .then((uploads) => { const file = uploads.reduce((fieldsData, upload) => ({ ...fieldsData, ...upload }), {}); const asset = { ...data, fields: { ...data.fields, file, }, }; return create(http, params, asset, {}); }) .catch(errorHandler); }; /** * Asset processing */ const ASSET_PROCESSING_CHECK_WAIT = 3000; const ASSET_PROCESSING_CHECK_RETRIES = 10; async function checkIfAssetHasUrl(http, params, { resolve, reject, locale, processingCheckWait = ASSET_PROCESSING_CHECK_WAIT, processingCheckRetries = ASSET_PROCESSING_CHECK_RETRIES, checkCount = 0, }) { return get(http, params).then((asset) => { if (asset.fields.file[locale].url) { resolve(asset); } else if (checkCount === processingCheckRetries) { const error = new Error(); error.name = 'AssetProcessingTimeout'; error.message = 'Asset is taking longer then expected to process.'; reject(error); } else { checkCount++; setTimeout(() => checkIfAssetHasUrl(http, params, { resolve: resolve, reject: reject, locale: locale, checkCount: checkCount, processingCheckWait, processingCheckRetries, }), processingCheckWait); } }); } const processForLocale = async (http, { asset, locale, options: { processingCheckRetries, processingCheckWait } = {}, ...params }) => { return put(http, `/spaces/${params.spaceId}/environments/${params.environmentId}/releases/${asset.sys.release.sys.id}/assets/${asset.sys.id}/files/${locale}/process`, null, { headers: { 'X-Contentful-Version': asset.sys.version, }, }) .then(() => { return new Promise((resolve, reject) => checkIfAssetHasUrl(http, { spaceId: params.spaceId, environmentId: params.environmentId, assetId: asset.sys.id, releaseId: asset.sys.release.sys.id, }, { resolve, reject, locale, processingCheckWait, processingCheckRetries, })); }); }; const processForAllLocales = async (http, { asset, options = {}, ...params }) => { const locales = Object.keys(asset.fields.file || {}); let mostUpToDateAssetVersion = asset; // Let all the locales process // Since they all resolve at different times, // we need to pick the last resolved value // to reflect the most recent state const allProcessingLocales = locales.map((locale) => processForLocale(http, { ...params, asset, locale, options }).then((result) => { // Side effect of always setting the most up to date asset version // The last one to call this will be the last one that finished // and thus the most up to date mostUpToDateAssetVersion = result; })); return Promise.all(allProcessingLocales).then(() => mostUpToDateAssetVersion); }; export { create, createFromFiles, createWithId, get, getMany, processForAllLocales, processForLocale, update }; //# sourceMappingURL=release-asset.js.map