contentful-management
Version:
Client for Contentful's Content Management API
92 lines (89 loc) • 3.65 kB
JavaScript
import copy from 'fast-copy';
import { post, del as del$1, get as get$1, put } from './raw.js';
import { normalizeSelect } from './utils.js';
const VERSION_HEADER = 'X-Contentful-Version';
const BODY_FORMAT_HEADER = 'x-contentful-comment-body-format';
const PARENT_ENTITY_REFERENCE_HEADER = 'x-contentful-parent-entity-reference';
const PARENT_COMMENT_ID_HEADER = 'x-contentful-parent-id';
const getSpaceEnvBaseUrl = (params) => `/spaces/${params.spaceId}/environments/${params.environmentId}`;
const getEntityCommentUrl = (params) => `${getEntityBaseUrl(params)}/${params.commentId}`;
function getParentPlural(parentEntityType) {
switch (parentEntityType) {
case 'ContentType':
return 'content_types';
case 'Entry':
return 'entries';
case 'Workflow':
return 'workflows';
}
}
/**
* Comments can be added to a content type, an entry, and a workflow. Workflow comments requires a version
* to be set as part of the URL path. Workflow comments only support `create` (with
* versionized URL) and `getMany` (without version). The API might support more methods
* in the future with new use cases being discovered.
*/
const getEntityBaseUrl = (paramsOrg) => {
const params = 'entryId' in paramsOrg
? {
spaceId: paramsOrg.spaceId,
environmentId: paramsOrg.environmentId,
parentEntityType: 'Entry',
parentEntityId: paramsOrg.entryId,
}
: paramsOrg;
const { parentEntityId, parentEntityType } = params;
const parentPlural = getParentPlural(parentEntityType);
const versionPath = 'parentEntityVersion' in params ? `/versions/${params.parentEntityVersion}` : '';
return `${getSpaceEnvBaseUrl(params)}/${parentPlural}/${parentEntityId}${versionPath}/comments`;
};
const get = (http, params) => get$1(http, getEntityCommentUrl(params), {
headers: params.bodyFormat === 'rich-text'
? {
[BODY_FORMAT_HEADER]: params.bodyFormat,
}
: {},
});
const getMany = (http, params) => get$1(http, getEntityBaseUrl(params), {
params: normalizeSelect(params.query),
headers: params.bodyFormat === 'rich-text'
? {
[BODY_FORMAT_HEADER]: params.bodyFormat,
}
: {},
});
const create = (http, params, rawData) => {
const data = copy(rawData);
return post(http, getEntityBaseUrl(params), data, {
headers: {
...(typeof rawData.body !== 'string' ? { [BODY_FORMAT_HEADER]: 'rich-text' } : {}),
...('parentEntityReference' in params && params.parentEntityReference
? { [PARENT_ENTITY_REFERENCE_HEADER]: params.parentEntityReference }
: {}),
...(params.parentCommentId ? { [PARENT_COMMENT_ID_HEADER]: params.parentCommentId } : {}),
},
});
};
const update = (http, params, rawData, headers) => {
const data = copy(rawData);
delete data.sys;
return put(http, getEntityCommentUrl(params), data, {
headers: {
[VERSION_HEADER]: rawData.sys.version ?? 0,
...(typeof rawData.body !== 'string' ? { [BODY_FORMAT_HEADER]: 'rich-text' } : {}),
...headers,
},
});
};
const del = (http, { version, ...params }) => {
return del$1(http, getEntityCommentUrl(params), {
headers: { [VERSION_HEADER]: version },
});
};
// Add a deprecation notice. But `getAll` may never be removed for app compatibility reasons.
/**
* @deprecated use `getMany` instead.
*/
const getAll = getMany;
export { create, del, get, getAll, getMany, update };
//# sourceMappingURL=comment.js.map