UNPKG

contentful-management

Version:
82 lines (79 loc) • 2.83 kB
import { toPlainObject, freezeSys } from 'contentful-sdk-core'; import { copy } from 'fast-copy'; import { wrapCollection } from '../common-utils.mjs'; import enhanceWithMethods from '../enhance-with-methods.mjs'; import { wrapAiActionInvocation } from './ai-action-invocation.mjs'; const AiActionScope = { Entry: 'Entry', EntryField: 'EntryField', }; const AiActionOutputType = { Transform: 'Transform', Suggestion: 'Suggestion', }; 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 { AiActionOutputType, AiActionScope, wrapAiAction, wrapAiActionCollection }; //# sourceMappingURL=ai-action.mjs.map