contentful-management
Version:
Client for Contentful's Content Management API
146 lines (144 loc) • 5.18 kB
JavaScript
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
import { freezeSys, toPlainObject } from 'contentful-sdk-core';
import copy from 'fast-copy';
import { wrapCollection } from '../common-utils';
import enhanceWithMethods from '../enhance-with-methods';
import { isDraft, isPublished, isUpdated } from '../plain/checks';
import { wrapEditorInterface } from './editor-interface';
import { wrapSnapshot, wrapSnapshotCollection } from './snapshot';
import { omitAndDeleteField } from '../methods/content-type';
/**
* @private
*/
function createContentTypeApi(makeRequest) {
const getParams = self => {
const contentType = self.toPlainObject();
return {
raw: contentType,
params: {
spaceId: contentType.sys.space.sys.id,
environmentId: contentType.sys.environment.sys.id,
contentTypeId: contentType.sys.id
}
};
};
return {
update: function () {
const {
raw,
params
} = getParams(this);
return makeRequest({
entityType: 'ContentType',
action: 'update',
params,
payload: raw
}).then(data => wrapContentType(makeRequest, data));
},
delete: function () {
const {
params
} = getParams(this);
return makeRequest({
entityType: 'ContentType',
action: 'delete',
params
}).then(() => {
// noop
});
},
publish: function () {
const {
raw,
params
} = getParams(this);
return makeRequest({
entityType: 'ContentType',
action: 'publish',
params,
payload: raw
}).then(data => wrapContentType(makeRequest, data));
},
unpublish: function () {
const {
params
} = getParams(this);
return makeRequest({
entityType: 'ContentType',
action: 'unpublish',
params
}).then(data => wrapContentType(makeRequest, data));
},
getEditorInterface: function () {
const {
params
} = getParams(this);
return makeRequest({
entityType: 'EditorInterface',
action: 'get',
params
}).then(data => wrapEditorInterface(makeRequest, data));
},
getSnapshots: function (query = {}) {
const {
params
} = getParams(this);
return makeRequest({
entityType: 'Snapshot',
action: 'getManyForContentType',
params: _objectSpread(_objectSpread({}, params), {}, {
query
})
}).then(data => wrapSnapshotCollection(makeRequest, data));
},
getSnapshot: function (snapshotId) {
const {
params
} = getParams(this);
return makeRequest({
entityType: 'Snapshot',
action: 'getForContentType',
params: _objectSpread(_objectSpread({}, params), {}, {
snapshotId
})
}).then(data => wrapSnapshot(makeRequest, data));
},
isPublished: function () {
return isPublished(this);
},
isUpdated: function () {
return isUpdated(this);
},
isDraft: function () {
return isDraft(this);
},
omitAndDeleteField: function (fieldId) {
const {
raw,
params
} = getParams(this);
return omitAndDeleteField(makeRequest, _objectSpread(_objectSpread({}, params), {}, {
fieldId
}), raw).then(data => wrapContentType(makeRequest, data));
}
};
}
/**
* @private
* @param makeRequest - function to make requests via an adapter
* @param data - Raw content type data
* @return Wrapped content type data
*/
export function wrapContentType(makeRequest, data) {
const contentType = toPlainObject(copy(data));
const contentTypeWithMethods = enhanceWithMethods(contentType, createContentTypeApi(makeRequest));
return freezeSys(contentTypeWithMethods);
}
/**
* @private
*/
export const wrapContentTypeCollection = wrapCollection(wrapContentType);