contentful-management
Version:
Client for Contentful's Content Management API
135 lines (132 loc) • 4.6 kB
JavaScript
import copy from 'fast-copy';
import { toPlainObject, freezeSys } from 'contentful-sdk-core';
import enhanceWithMethods from '../enhance-with-methods.js';
import { wrapCursorPaginatedCollection, wrapCollection } from '../common-utils.js';
import { isArchived, isDraft, isUpdated, isPublished } from '../plain/checks.js';
/**
* @internal
*/
function createAssetApi(makeRequest) {
const getParams = (raw) => {
return {
spaceId: raw.sys.space.sys.id,
environmentId: raw.sys.environment.sys.id,
assetId: raw.sys.id,
};
};
return {
processForLocale: function processForLocale(locale, options) {
const raw = this.toPlainObject();
return makeRequest({
entityType: 'Asset',
action: 'processForLocale',
params: {
...getParams(raw),
locale,
options,
asset: raw,
},
}).then((data) => wrapAsset(makeRequest, data));
},
processForAllLocales: function processForAllLocales(options) {
const raw = this.toPlainObject();
return makeRequest({
entityType: 'Asset',
action: 'processForAllLocales',
params: {
...getParams(raw),
asset: raw,
options,
},
}).then((data) => wrapAsset(makeRequest, data));
},
update: function update() {
const raw = this.toPlainObject();
return makeRequest({
entityType: 'Asset',
action: 'update',
params: getParams(raw),
payload: raw,
headers: {},
}).then((data) => wrapAsset(makeRequest, data));
},
delete: function del() {
const raw = this.toPlainObject();
return makeRequest({
entityType: 'Asset',
action: 'delete',
params: getParams(raw),
});
},
publish: function publish() {
const raw = this.toPlainObject();
return makeRequest({
entityType: 'Asset',
action: 'publish',
params: getParams(raw),
payload: raw,
}).then((data) => wrapAsset(makeRequest, data));
},
unpublish: function unpublish() {
const raw = this.toPlainObject();
return makeRequest({
entityType: 'Asset',
action: 'unpublish',
params: getParams(raw),
}).then((data) => wrapAsset(makeRequest, data));
},
archive: function archive() {
const raw = this.toPlainObject();
return makeRequest({
entityType: 'Asset',
action: 'archive',
params: getParams(raw),
}).then((data) => wrapAsset(makeRequest, data));
},
unarchive: function unarchive() {
const raw = this.toPlainObject();
return makeRequest({
entityType: 'Asset',
action: 'unarchive',
params: getParams(raw),
}).then((data) => wrapAsset(makeRequest, data));
},
isPublished: function isPublished$1() {
const raw = this.toPlainObject();
return isPublished(raw);
},
isUpdated: function isUpdated$1() {
const raw = this.toPlainObject();
return isUpdated(raw);
},
isDraft: function isDraft$1() {
const raw = this.toPlainObject();
return isDraft(raw);
},
isArchived: function isArchived$1() {
const raw = this.toPlainObject();
return isArchived(raw);
},
};
}
/**
* @internal
* @param makeRequest - function to make requests via an adapter
* @param data - Raw asset data
* @returns Wrapped asset data
*/
function wrapAsset(makeRequest, data) {
const asset = toPlainObject(copy(data));
const assetWithMethods = enhanceWithMethods(asset, createAssetApi(makeRequest));
return freezeSys(assetWithMethods);
}
/**
* @internal
*/
const wrapAssetCollection = wrapCollection(wrapAsset);
/**
* @internal
*/
const wrapAssetTypeCursorPaginatedCollection = wrapCursorPaginatedCollection(wrapAsset);
export { wrapAsset, wrapAssetCollection, wrapAssetTypeCursorPaginatedCollection };
//# sourceMappingURL=asset.js.map