contentful-management
Version:
Client for Contentful's Content Management API
74 lines (71 loc) • 2.62 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';
import { wrapAiActionInvocation } from './ai-action-invocation.js';
function createAiActionApi(makeRequest) {
const getParams = (data) => ({
spaceId: data.sys.space.sys.id,
aiActionId: data.sys.id,
});
return {
update: function update() {
const self = this;
return makeRequest({
entityType: 'AiAction',
action: 'update',
params: getParams(self),
payload: self,
}).then((data) => wrapAiAction(makeRequest, data));
},
delete: function del() {
const self = this;
return makeRequest({
entityType: 'AiAction',
action: 'delete',
params: getParams(self),
});
},
publish: function publish() {
const self = this;
return makeRequest({
entityType: 'AiAction',
action: 'publish',
params: {
aiActionId: self.sys.id,
spaceId: self.sys.space.sys.id,
version: self.sys.version,
},
}).then((data) => wrapAiAction(makeRequest, data));
},
unpublish: function unpublish() {
const self = this;
return makeRequest({
entityType: 'AiAction',
action: 'unpublish',
params: getParams(self),
}).then((data) => wrapAiAction(makeRequest, data));
},
invoke: function invoke(environmentId, payload) {
const self = this;
return makeRequest({
entityType: 'AiAction',
action: 'invoke',
params: {
spaceId: self.sys.space.sys.id,
environmentId,
aiActionId: self.sys.id,
},
payload,
}).then((data) => wrapAiActionInvocation(makeRequest, data));
},
};
}
function wrapAiAction(makeRequest, data) {
const aiAction = toPlainObject(copy(data));
const aiActionWithMethods = enhanceWithMethods(aiAction, createAiActionApi(makeRequest));
return freezeSys(aiActionWithMethods);
}
const wrapAiActionCollection = wrapCollection(wrapAiAction);
export { wrapAiAction, wrapAiActionCollection };
//# sourceMappingURL=ai-action.js.map