@contentstack/management
Version:
The Content Management API is used to manage the content of your Contentstack account
372 lines (363 loc) • 14.1 kB
JavaScript
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
import _regeneratorRuntime from "@babel/runtime/regenerator";
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
import cloneDeep from 'lodash/cloneDeep';
import { create, update, deleteEntity, fetch, fetchAll } from '../../entity';
import error from '../../core/contentstackError';
import ContentstackCollection from '../../contentstackCollection';
import { PublishRules, PublishRulesCollection } from './publishRules';
/**
* Workflow is a tool that allows you to streamline the process of content creation and publishing, and lets you manage the content lifecycle of your project smoothly. Read more about <a href='https://www.contentstack.com/docs/developers/set-up-workflows-and-publish-rules'>Workflows and Publish Rules</a>.
* @namespace Workflow
*/
export function Workflow(http) {
var _this = this;
var data = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
this.stackHeaders = data.stackHeaders;
this.urlPath = "/workflows";
if (data.workflow) {
Object.assign(this, cloneDeep(data.workflow));
this.urlPath = "/workflows/".concat(this.uid);
/**
* @description The Update Workflow request allows you to add a workflow stage or update the details of the existing stages of a workflow.
* @memberof Workflow
* @func update
* @returns {Promise<Workflow>} Promise for Workflow instance
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.stack({ api_key: 'api_key'}).workflow('workflow_uid').fetch()
* .then((workflow) => {
* workflow.name = 'My New Workflow'
* workflow.description = 'Workflow description'
* return workflow.update()
* })
* .then((workflow) => console.log(workflow))
*
*/
this.update = update(http, 'workflow');
/**
* @description The Disable Workflow request allows you to disable a workflow.
* @memberof Workflow
* @func disable
* @async
* @returns {Promise<Object>} Promise for response data object
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.stack({ api_key: 'api_key'}).workflow('workflow_uid').disable()
* .then((workflow) => console.log(workflow))
*
*/
this.disable = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
var response, _t;
return _regeneratorRuntime.wrap(function (_context) {
while (1) switch (_context.prev = _context.next) {
case 0:
_context.prev = 0;
_context.next = 1;
return http.get("/workflows/".concat(this.uid, "/disable"), {
headers: _objectSpread({}, cloneDeep(this.stackHeaders))
});
case 1:
response = _context.sent;
if (!response.data) {
_context.next = 2;
break;
}
return _context.abrupt("return", response.data);
case 2:
throw error(response);
case 3:
_context.next = 5;
break;
case 4:
_context.prev = 4;
_t = _context["catch"](0);
throw error(_t);
case 5:
case "end":
return _context.stop();
}
}, _callee, this, [[0, 4]]);
}));
/**
* @description The Enable Workflow request allows you to enable a workflow.
* @memberof Workflow
* @func enable
* @async
* @returns {Promise<Object>} Promise for response data object
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.stack({ api_key: 'api_key'}).workflow('workflow_uid').enable()
* .then((workflow) => console.log(workflow))
*
*/
this.enable = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee2() {
var response, _t2;
return _regeneratorRuntime.wrap(function (_context2) {
while (1) switch (_context2.prev = _context2.next) {
case 0:
_context2.prev = 0;
_context2.next = 1;
return http.get("/workflows/".concat(this.uid, "/enable"), {
headers: _objectSpread({}, cloneDeep(this.stackHeaders))
});
case 1:
response = _context2.sent;
if (!response.data) {
_context2.next = 2;
break;
}
return _context2.abrupt("return", response.data);
case 2:
throw error(response);
case 3:
_context2.next = 5;
break;
case 4:
_context2.prev = 4;
_t2 = _context2["catch"](0);
throw error(_t2);
case 5:
case "end":
return _context2.stop();
}
}, _callee2, this, [[0, 4]]);
}));
/**
* @description The Delete Workflow call is used to delete an existing Workflow permanently from your Stack.
* @memberof Workflow
* @func delete
* @returns {Promise<Object>} Response Object.
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.stack({ api_key: 'api_key'}).workflow('workflow_uid').delete()
* .then((response) => console.log(response.notice))
*/
this["delete"] = deleteEntity(http);
/**
* @description The fetch workflow retrieves the comprehensive details of a specific Workflow of a stack.
* @memberof Workflow
* @func fetch
* @returns {Promise<Workflow>} Promise for Workflow instance
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.stack({ api_key: 'api_key'}).workflow('workflow_uid').fetch()
* .then((workflow) => console.log(workflow))
*
*/
this.fetch = fetch(http, 'workflow');
} else {
this.contentType = function (contentTypeUID) {
if (contentTypeUID) {
/**
* @description The Get Publish Rules call retrieves publish rules for a content type.
* @memberof Workflow
* @func getPublishRules
* @async
* @param {Object=} params - Query parameters.
* @prop {string=} params.action - Enter the action that has been set in the Publishing Rule. Example:publish/unpublish
* @prop {string=} params.locale - Enter the code of the locale where your Publishing Rule will be applicable.
* @prop {string=} params.environment - Enter the UID of the environment where your Publishing Rule will be applicable.
* @returns {Promise<ContentstackCollection>} Promise for ContentstackCollection instance.
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.stack({ api_key: 'api_key'}).workflow('workflow_uid').contentType('contentType_uid').getPublishRules()
* .then((collection) => console.log(collection))
*/
var getPublishRules = /*#__PURE__*/function () {
var _ref3 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee3(params) {
var headers, response, _t3;
return _regeneratorRuntime.wrap(function (_context3) {
while (1) switch (_context3.prev = _context3.next) {
case 0:
headers = {};
if (this.stackHeaders) {
headers.headers = this.stackHeaders;
}
if (params) {
headers.params = _objectSpread({}, cloneDeep(params));
}
_context3.prev = 1;
_context3.next = 2;
return http.get("/workflows/content_type/".concat(contentTypeUID), headers);
case 2:
response = _context3.sent;
if (!response.data) {
_context3.next = 3;
break;
}
return _context3.abrupt("return", new ContentstackCollection(response, http, this.stackHeaders, PublishRulesCollection));
case 3:
throw error(response);
case 4:
_context3.next = 6;
break;
case 5:
_context3.prev = 5;
_t3 = _context3["catch"](1);
throw error(_t3);
case 6:
case "end":
return _context3.stop();
}
}, _callee3, this, [[1, 5]]);
}));
return function getPublishRules(_x) {
return _ref3.apply(this, arguments);
};
}();
return {
getPublishRules: getPublishRules,
stackHeaders: _objectSpread({}, _this.stackHeaders)
};
}
};
/**
* @description The Create a Workflow request allows you to create a Workflow.
* @memberof Workflow
* @func create
* @returns {Promise<Workflow>} Promise for Workflow instance
*
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* const workflow = {
* workflow_stages: [
* {
* color: "#2196f3",
* SYS_ACL: {
* roles: {
* uids: []
* },
* users: {
* uids: [
* "$all"
* ]
* },
* others: {}
* },
* next_available_stages: [
* "$all"
* ],
* allStages: true,
* allUsers: true,
* specificStages: false,
* specificUsers: false,
* entry_lock: "$none",
* name: "Review"
* },
* {
* color: "#74ba76",
* SYS_ACL: {
* roles: {
* uids: []
* },
* users: {
* uids: [
* "$all"
* ]
* },
* others: {}
* },
* allStages: true,
* allUsers: true,
* specificStages: false,
* specificUsers: false,
* next_available_stages: [
* "$all"
* ],
* entry_lock: "$none",
* name: "Complete"
* }
* ],
* admin_users: {
* users: []
* },
* name: "Workflow Name",
* enabled: true,
* content_types: [
* "$all"
* ]
* }
* client.stack().workflow().create({ workflow })
* .then((workflow) => console.log(workflow))
*/
this.create = create({
http: http
});
/**
* @description The Get all Workflows request retrieves the details of all the Workflows of a stack.
* @memberof Workflow
* @func fetchAll
* @param {Object=} param - Query parameters.
* @prop {Number=} param.limit - The limit parameter will return a specific number of Workflows in the output.
* @prop {Number=} param.skip - The skip parameter will skip a specific number of Workflows in the output.
* @prop {Boolean=} param.include_count - To retrieve the count of Workflows.
* @returns {Promise<ContentstackCollection>} Promise for ContentstackCollection instance.
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.stack({ api_key: 'api_key'}).workflow().fetchAll()
* .then((collection) => console.log(collection))
*
*/
this.fetchAll = fetchAll(http, WorkflowCollection);
/**
* @description The Publish rule allows you to create, fetch, delete, and update publish rules.
* @memberof Workflow
* @func publishRule
* @param {String} ruleUid The UID of the Publish rules you want to get details.
* @returns {PublishRules} Instance of PublishRules.
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.stack({ api_key: 'api_key'}).workflow().publishRule().fetchAll()
* .then((collection) => console.log(collection))
*
* client.stack({ api_key: 'api_key'}).workflow().publishRule('rule_uid').fetch()
* .then((publishrule) => console.log(publishrule))
*
*/
this.publishRule = function () {
var ruleUid = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
var publishInfo = {
stackHeaders: _this.stackHeaders
};
if (ruleUid) {
publishInfo.publishing_rule = {
uid: ruleUid
};
}
return new PublishRules(http, publishInfo);
};
}
}
export function WorkflowCollection(http, data) {
var obj = cloneDeep(data.workflows) || [];
if (!Array.isArray(obj)) {
obj = [obj];
}
return obj.map(function (userData) {
return new Workflow(http, {
workflow: userData,
stackHeaders: data.stackHeaders
});
});
}