UNPKG

contentful-management

Version:
51 lines (49 loc) 1.53 kB
import { toPlainObject, freezeSys } from 'contentful-sdk-core'; import copy from 'fast-copy'; import enhanceWithMethods from '../enhance-with-methods'; import { wrapCollection } from '../common-utils'; /** * @private */ function createAppInstallationApi(makeRequest) { const getParams = data => ({ spaceId: data.sys.space.sys.id, environmentId: data.sys.environment.sys.id, appDefinitionId: data.sys.appDefinition.sys.id }); return { update: function update() { const data = this.toPlainObject(); return makeRequest({ entityType: 'AppInstallation', action: 'upsert', params: getParams(data), headers: {}, payload: data }).then(data => wrapAppInstallation(makeRequest, data)); }, delete: function del() { const data = this.toPlainObject(); return makeRequest({ entityType: 'AppInstallation', action: 'delete', params: getParams(data) }); } }; } /** * @private * @param makeRequest - function to make requests via an adapter * @param data - Raw App Installation data * @return Wrapped App installation data */ export function wrapAppInstallation(makeRequest, data) { const appInstallation = toPlainObject(copy(data)); const appInstallationWithMethods = enhanceWithMethods(appInstallation, createAppInstallationApi(makeRequest)); return freezeSys(appInstallationWithMethods); } /** * @private */ export const wrapAppInstallationCollection = wrapCollection(wrapAppInstallation);