UNPKG

contentful-management

Version:
69 lines 2.15 kB
import { freezeSys, toPlainObject } from 'contentful-sdk-core'; import copy from 'fast-copy'; import { wrapCollection } from '../common-utils'; import enhanceWithMethods from '../enhance-with-methods'; import { wrapAiActionInvocation } from './ai-action-invocation'; 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)); } }; } export function wrapAiAction(makeRequest, data) { const aiAction = toPlainObject(copy(data)); const aiActionWithMethods = enhanceWithMethods(aiAction, createAiActionApi(makeRequest)); return freezeSys(aiActionWithMethods); } export const wrapAiActionCollection = wrapCollection(wrapAiAction);