contentful-management
Version:
Client for Contentful's Content Management API
65 lines (62 loc) • 2.3 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';
/**
* @internal
*/
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));
},
};
}
/**
* @internal
* @param makeRequest - (real) function to make requests via an adapter
* @param data - raw contentful-Function data
* @returns Wrapped Function data
*/
function wrapFunction(makeRequest, data) {
const func = toPlainObject(copy(data));
const funcWithMethods = enhanceWithMethods(func, createFunctionApi(makeRequest));
return freezeSys(funcWithMethods);
}
/**
* @internal
*/
const wrapFunctionCollection = wrapCollection(wrapFunction);
export { wrapFunction, wrapFunctionCollection };
//# sourceMappingURL=function.js.map