contentful-management
Version:
Client for Contentful's Content Management API
150 lines (146 loc) • 5.32 kB
JavaScript
const _excluded = ["sys"];
function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var s = Object.getOwnPropertySymbols(e); for (r = 0; r < s.length; r++) o = s[r], t.includes(o) || {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (e.includes(n)) continue; t[n] = r[n]; } return t; }
import { freezeSys, toPlainObject } from 'contentful-sdk-core';
import copy from 'fast-copy';
import { wrapCollection } from '../common-utils';
import enhanceWithMethods from '../enhance-with-methods';
/**
* Represents that state of the scheduled action
*/
export let ScheduledActionStatus = /*#__PURE__*/function (ScheduledActionStatus) {
ScheduledActionStatus["scheduled"] = "scheduled";
ScheduledActionStatus["inProgress"] = "inProgress";
ScheduledActionStatus["succeeded"] = "succeeded";
ScheduledActionStatus["failed"] = "failed";
ScheduledActionStatus["canceled"] = "canceled";
return ScheduledActionStatus;
}({});
export default function getInstanceMethods(makeRequest) {
const getParams = self => {
var _scheduledAction$envi;
const scheduledAction = self.toPlainObject();
return {
spaceId: scheduledAction.sys.space.sys.id,
environmentId: (_scheduledAction$envi = scheduledAction.environment) === null || _scheduledAction$envi === void 0 ? void 0 : _scheduledAction$envi.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 _this$toPlainObject = this.toPlainObject(),
{
sys
} = _this$toPlainObject,
payload = _objectWithoutProperties(_this$toPlainObject, _excluded);
return makeRequest({
entityType: 'ScheduledAction',
action: 'update',
params,
payload
}).then(data => wrapScheduledAction(makeRequest, data));
}
};
}
/**
* @private
*/
export function wrapScheduledAction(makeRequest, data) {
const scheduledAction = toPlainObject(copy(data));
const scheduledActionWithMethods = enhanceWithMethods(scheduledAction, getInstanceMethods(makeRequest));
return freezeSys(scheduledActionWithMethods);
}
/**
* @private
*/
export const wrapScheduledActionCollection = wrapCollection(wrapScheduledAction);