contentful-management
Version:
Client for Contentful's Content Management API
56 lines (53 loc) • 1.58 kB
JavaScript
import { toPlainObject, freezeSys } from 'contentful-sdk-core';
import copy from 'fast-copy';
import { wrapCollection } from '../common-utils.js';
import enhanceWithMethods from '../enhance-with-methods.js';
/**
* @internal
*/
function createTaskApi(makeRequest) {
const getParams = (task) => ({
spaceId: task.sys.space.sys.id,
environmentId: task.sys.environment.sys.id,
entryId: task.sys.parentEntity.sys.id,
taskId: task.sys.id,
});
return {
update: function () {
const raw = this.toPlainObject();
return makeRequest({
entityType: 'Task',
action: 'update',
params: getParams(raw),
payload: raw,
}).then((data) => wrapTask(makeRequest, data));
},
delete: function () {
const raw = this.toPlainObject();
return makeRequest({
entityType: 'Task',
action: 'delete',
params: {
...getParams(raw),
version: raw.sys.version,
},
}).then(() => {
// noop
});
},
};
}
/**
* @internal
*/
function wrapTask(makeRequest, data) {
const task = toPlainObject(copy(data));
const taskWithMethods = enhanceWithMethods(task, createTaskApi(makeRequest));
return freezeSys(taskWithMethods);
}
/**
* @internal
*/
const wrapTaskCollection = wrapCollection(wrapTask);
export { wrapTask, wrapTaskCollection };
//# sourceMappingURL=task.js.map