contentful-management
Version:
Client for Contentful's Content Management API
49 lines (47 loc) • 1.28 kB
JavaScript
import copy from 'fast-copy';
import { freezeSys, toPlainObject } from 'contentful-sdk-core';
import enhanceWithMethods from '../enhance-with-methods';
import { wrapCollection } from '../common-utils';
/**
* @private
*/
function createTeamApi(makeRequest) {
const getParams = data => ({
teamId: data.sys.id,
organizationId: data.sys.organization.sys.id
});
return {
update: function update() {
const raw = this.toPlainObject();
return makeRequest({
entityType: 'Team',
action: 'update',
params: getParams(raw),
payload: raw
}).then(data => wrapTeam(makeRequest, data));
},
delete: function del() {
const raw = this.toPlainObject();
return makeRequest({
entityType: 'Team',
action: 'delete',
params: getParams(raw)
});
}
};
}
/**
* @private
* @param makeRequest - function to make requests via an adapter
* @param data - Raw team data
* @return Wrapped team data
*/
export function wrapTeam(makeRequest, data) {
const team = toPlainObject(copy(data));
const teamWithMethods = enhanceWithMethods(team, createTeamApi(makeRequest));
return freezeSys(teamWithMethods);
}
/**
* @private
*/
export const wrapTeamCollection = wrapCollection(wrapTeam);