contentful-management
Version:
Client for Contentful's Content Management API
209 lines (206 loc) • 8.48 kB
JavaScript
import { errorHandler } from 'contentful-sdk-core';
import copy from 'fast-copy';
import { getUploadHttpClient } from '../../../upload-http-client.js';
import { put, post, del as del$1, get as get$2 } from './raw.js';
import { create as create$2 } from './upload.js';
import { normalizeSelect } from './utils.js';
import { create as create$1, createFromFiles as createFromFiles$1, createWithId as createWithId$1, get as get$1, getMany as getMany$1, processForAllLocales as processForAllLocales$1, processForLocale as processForLocale$1, update as update$1 } from './release-asset.js';
const get = (http, params, rawData, headers) => {
if (params.releaseId) {
return get$1(http, params);
}
return get$2(http, `/spaces/${params.spaceId}/environments/${params.environmentId}/assets/${params.assetId}`, {
params: normalizeSelect(params.query),
headers: headers ? { ...headers } : undefined,
});
};
const getPublished = (http, params, rawData, headers) => {
return get$2(http, `/spaces/${params.spaceId}/environments/${params.environmentId}/public/assets`, {
params: normalizeSelect(params.query),
headers: headers ? { ...headers } : undefined,
});
};
const getMany = (http, params, rawData, headers) => {
if (params.releaseId) {
return getMany$1(http, params);
}
return get$2(http, `/spaces/${params.spaceId}/environments/${params.environmentId}/assets`, {
params: normalizeSelect(params.query),
headers: headers ? { ...headers } : undefined,
});
};
const update = (http, params, rawData, headers) => {
if (params.releaseId) {
return update$1(http, params, rawData, headers ?? {});
}
const data = copy(rawData);
delete data.sys;
return put(http, `/spaces/${params.spaceId}/environments/${params.environmentId}/assets/${params.assetId}`, data, {
headers: {
'X-Contentful-Version': rawData.sys.version ?? 0,
...headers,
},
});
};
const del = (http, params) => {
return del$1(http, `/spaces/${params.spaceId}/environments/${params.environmentId}/assets/${params.assetId}`);
};
const publish = (http, params, rawData) => {
const payload = params.locales?.length ? { add: { fields: { '*': params.locales } } } : null;
return put(http, `/spaces/${params.spaceId}/environments/${params.environmentId}/assets/${params.assetId}/published`, payload, {
headers: {
'X-Contentful-Version': rawData.sys.version ?? 0,
},
});
};
const unpublish = (http, params, rawData) => {
if (params.locales?.length) {
const payload = { remove: { fields: { '*': params.locales } } };
return put(http, `/spaces/${params.spaceId}/environments/${params.environmentId}/assets/${params.assetId}/published`, payload, {
headers: {
'X-Contentful-Version': rawData?.sys.version,
},
});
}
else {
return del$1(http, `/spaces/${params.spaceId}/environments/${params.environmentId}/assets/${params.assetId}/published`);
}
};
const archive = (http, params) => {
return put(http, `/spaces/${params.spaceId}/environments/${params.environmentId}/assets/${params.assetId}/archived`);
};
const unarchive = (http, params) => {
return del$1(http, `/spaces/${params.spaceId}/environments/${params.environmentId}/assets/${params.assetId}/archived`);
};
const create = (http, params, rawData) => {
if (params.releaseId) {
return create$1(http, params, rawData, {});
}
const data = copy(rawData);
return post(http, `/spaces/${params.spaceId}/environments/${params.environmentId}/assets`, data);
};
const createWithId = (http, params, rawData) => {
if (params.releaseId) {
return createWithId$1(http, params, rawData, {});
}
const data = copy(rawData);
return put(http, `/spaces/${params.spaceId}/environments/${params.environmentId}/assets/${params.assetId}`, data);
};
const createFromFiles = async (http, params, data) => {
if (params.releaseId) {
return createFromFiles$1(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$2(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 }) => {
if (asset.sys.release) {
return processForLocale$1(http, {
asset: asset,
locale,
options: { processingCheckRetries, processingCheckWait },
...params,
});
}
return put(http, `/spaces/${params.spaceId}/environments/${params.environmentId}/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,
}, {
resolve,
reject,
locale,
processingCheckWait,
processingCheckRetries,
}));
});
};
const processForAllLocales = async (http, { asset, options = {}, ...params }) => {
if (asset.sys.release) {
return processForAllLocales$1(http, {
asset: 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 { archive, create, createFromFiles, createWithId, del, get, getMany, getPublished, processForAllLocales, processForLocale, publish, unarchive, unpublish, update };
//# sourceMappingURL=asset.js.map