contentful-management
Version:
Client for Contentful's Content Management API
148 lines (145 loc) • 5.4 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';
/**
* Represents that state of the scheduled action
*/
var ScheduledActionStatus;
(function (ScheduledActionStatus) {
/** action is pending execution */
ScheduledActionStatus["scheduled"] = "scheduled";
/** action has been started and pending completion */
ScheduledActionStatus["inProgress"] = "inProgress";
/** action was completed successfully (terminal state) */
ScheduledActionStatus["succeeded"] = "succeeded";
/** action failed to complete (terminal state) */
ScheduledActionStatus["failed"] = "failed";
/** action was canceled by a user (terminal state) */
ScheduledActionStatus["canceled"] = "canceled";
})(ScheduledActionStatus || (ScheduledActionStatus = {}));
function getInstanceMethods(makeRequest) {
const getParams = (self) => {
const scheduledAction = self.toPlainObject();
return {
spaceId: scheduledAction.sys.space.sys.id,
environmentId: scheduledAction.environment?.sys.id,
scheduledActionId: scheduledAction.sys.id,
version: scheduledAction.sys.version,
};
};
return {
/**
* Cancels the current Scheduled Action schedule.
*
* @example ```javascript
* const contentful = require('contentful-management');
*
* const client = contentful.createClient({
* accessToken: '<content_management_api_key>'
* })
*
* client.getSpace('<space_id>')
* .then((space) => {
* return space.createScheduledAction({
* entity: {
* sys: {
* type: 'Link',
* linkType: 'Entry',
* id: '<entry_id>'
* }
* },
* environment: {
* sys: {
* type: 'Link',
* linkType: 'Environment',
* id: '<environment_id>'
* }
* },
* action: 'publish',
* scheduledFor: {
* datetime: <ISO_date_string>,
* timezone: 'Europe/Berlin'
* }
* })
* .then((scheduledAction) => scheduledAction.delete())
* .then((deletedScheduledAction) => console.log(deletedScheduledAction))
* .catch(console.error);
* ```
*/
async delete() {
const params = getParams(this);
return makeRequest({
entityType: 'ScheduledAction',
action: 'delete',
params,
}).then((data) => wrapScheduledAction(makeRequest, data));
},
/**
* Update the current scheduled action. Currently, only changes made to the `scheduledFor` property will be saved.
*
* @example ```javascript
* const contentful = require('contentful-management');
*
* const client = contentful.createClient({
* accessToken: '<content_management_api_key>'
* })
*
* client.getSpace('<space_id>')
* .then((space) => {
* return space.createScheduledAction({
* entity: {
* sys: {
* type: 'Link',
* linkType: 'Entry',
* id: '<entry_id>'
* }
* },
* environment: {
* sys: {
* type: 'Link',
* linkType: 'Environment',
* id: '<environment_id>'
* }
* },
* action: 'publish',
* scheduledFor: {
* datetime: <ISO_date_string>,
* timezone: 'Europe/Berlin'
* }
* })
* .then((scheduledAction) => {
* scheduledAction.scheduledFor.timezone = 'Europe/Paris';
* return scheduledAction.update();
* })
* .then((scheduledAction) => console.log(scheduledAction))
* .catch(console.error);
* ```
*/
async update() {
const params = getParams(this);
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { sys, ...payload } = this.toPlainObject();
return makeRequest({
entityType: 'ScheduledAction',
action: 'update',
params,
payload,
}).then((data) => wrapScheduledAction(makeRequest, data));
},
};
}
/**
* @internal
*/
function wrapScheduledAction(makeRequest, data) {
const scheduledAction = toPlainObject(copy(data));
const scheduledActionWithMethods = enhanceWithMethods(scheduledAction, getInstanceMethods(makeRequest));
return freezeSys(scheduledActionWithMethods);
}
/**
* @internal
*/
const wrapScheduledActionCollection = wrapCollection(wrapScheduledAction);
export { ScheduledActionStatus, wrapScheduledAction, wrapScheduledActionCollection };
//# sourceMappingURL=scheduled-action.js.map