contentful-management
Version:
Client for Contentful's Content Management API
123 lines (120 loc) • 4.42 kB
JavaScript
import { toPlainObject, freezeSys } from 'contentful-sdk-core';
import copy from 'fast-copy';
import { wrapCursorPaginatedCollection, wrapCollection } from '../common-utils.js';
import enhanceWithMethods from '../enhance-with-methods.js';
import { isDraft, isUpdated, isPublished } from '../plain/checks.js';
import { wrapEditorInterface } from './editor-interface.js';
import { wrapSnapshot, wrapSnapshotCollection } from './snapshot.js';
import { omitAndDeleteField } from '../methods/content-type.js';
/**
* @internal
*/
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: { ...params, query },
}).then((data) => wrapSnapshotCollection(makeRequest, data));
},
getSnapshot: function (snapshotId) {
const { params } = getParams(this);
return makeRequest({
entityType: 'Snapshot',
action: 'getForContentType',
params: { ...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, { ...params, fieldId }, raw).then((data) => wrapContentType(makeRequest, data));
},
};
}
/**
* @internal
* @param makeRequest - function to make requests via an adapter
* @param data - Raw content type data
* @returns Wrapped content type data
*/
function wrapContentType(makeRequest, data) {
const contentType = toPlainObject(copy(data));
const contentTypeWithMethods = enhanceWithMethods(contentType, createContentTypeApi(makeRequest));
return freezeSys(contentTypeWithMethods);
}
/**
* @internal
*/
const wrapContentTypeCollection = wrapCollection(wrapContentType);
/**
* @internal
*/
const wrapContentTypeCursorPaginatedCollection = wrapCursorPaginatedCollection(wrapContentType);
export { wrapContentType, wrapContentTypeCollection, wrapContentTypeCursorPaginatedCollection };
//# sourceMappingURL=content-type.js.map