UNPKG

contentful-management

Version:
61 lines (60 loc) 1.94 kB
import { freezeSys, toPlainObject } from 'contentful-sdk-core'; import copy from 'fast-copy'; import { wrapCollection } from '../common-utils'; import enhanceWithMethods from '../enhance-with-methods'; /** * @private */ function createFunctionApi(makeRequest) { return { getFunction: function getFunction() { const raw = this.toPlainObject(); return makeRequest({ entityType: 'Function', action: 'get', params: { organizationId: raw.sys.organization.sys.id, appDefinitionId: raw.sys.appDefinition.sys.id, functionId: raw.sys.id } }).then(data => wrapFunction(makeRequest, data)); }, getManyFunctions: function getManyFunctions() { const raw = this.toPlainObject(); return makeRequest({ entityType: 'Function', action: 'getMany', params: { appDefinitionId: raw.sys.appDefinition.sys.id, organizationId: raw.sys.organization.sys.id } }).then(data => wrapFunctionCollection(makeRequest, data)); }, getManyFunctionsForEnvironment(spaceId, environmentId, appInstallationId) { return makeRequest({ entityType: 'Function', action: 'getManyForEnvironment', params: { spaceId: spaceId, environmentId: environmentId, appInstallationId: appInstallationId } }).then(data => wrapFunctionCollection(makeRequest, data)); } }; } /** * @private * @param makeRequest - (real) function to make requests via an adapter * @param data - raw contentful-Function data * @return Wrapped Function data */ export function wrapFunction(makeRequest, data) { const func = toPlainObject(copy(data)); const funcWithMethods = enhanceWithMethods(func, createFunctionApi(makeRequest)); return freezeSys(funcWithMethods); } /** * @private */ export const wrapFunctionCollection = wrapCollection(wrapFunction);