contentful-management
Version:
Client for Contentful's Content Management API
61 lines (57 loc) • 2.04 kB
JavaScript
/* eslint-disable @typescript-eslint/no-explicit-any */
import { freezeSys, toPlainObject } from 'contentful-sdk-core';
import copy from 'fast-copy';
import enhanceWithMethods from '../enhance-with-methods';
import { pollAsyncActionStatus } from '../methods/action';
/** Entity types supported by the BulkAction API */
/** Represents the state of the BulkAction */
export let BulkActionStatus = /*#__PURE__*/function (BulkActionStatus) {
/** BulkAction is pending execution */
BulkActionStatus["created"] = "created";
/** BulkAction has been started and pending completion */
BulkActionStatus["inProgress"] = "inProgress";
/** BulkAction was completed successfully (terminal state) */
BulkActionStatus["succeeded"] = "succeeded";
/** BulkAction failed to complete (terminal state) */
BulkActionStatus["failed"] = "failed";
return BulkActionStatus;
}({});
const STATUSES = Object.values(BulkActionStatus);
/** The object returned by the BulkActions API */
/**
* @private
*/
function createBulkActionApi(makeRequest) {
const getParams = self => {
const bulkAction = self.toPlainObject();
return {
spaceId: bulkAction.sys.space.sys.id,
environmentId: bulkAction.sys.environment.sys.id,
bulkActionId: bulkAction.sys.id
};
};
return {
async get() {
const params = getParams(this);
return makeRequest({
entityType: 'BulkAction',
action: 'get',
params
}).then(bulkAction => wrapBulkAction(makeRequest, bulkAction));
},
async waitProcessing(options) {
return pollAsyncActionStatus(async () => this.get(), options);
}
};
}
/**
* @private
* @param makeRequest - function to make requests via an adapter
* @param data - Raw BulkAction data
* @return Wrapped BulkAction data
*/
export function wrapBulkAction(makeRequest, data) {
const bulkAction = toPlainObject(copy(data));
const bulkActionWithApiMethods = enhanceWithMethods(bulkAction, createBulkActionApi(makeRequest));
return freezeSys(bulkActionWithApiMethods);
}