UNPKG

contentful-management

Version:
101 lines (99 loc) 5.4 kB
const _excluded = ["version"]; function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var s = Object.getOwnPropertySymbols(e); for (r = 0; r < s.length; r++) o = s[r], t.includes(o) || {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; } function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (e.includes(n)) continue; t[n] = r[n]; } return t; } 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 copy from 'fast-copy'; import * as raw from './raw'; import { normalizeSelect } from './utils'; 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`; }; export const get = (http, params) => raw.get(http, getEntityCommentUrl(params), { headers: params.bodyFormat === 'rich-text' ? { [BODY_FORMAT_HEADER]: params.bodyFormat } : {} }); export const getMany = (http, params) => raw.get(http, getEntityBaseUrl(params), { params: normalizeSelect(params.query), headers: params.bodyFormat === 'rich-text' ? { [BODY_FORMAT_HEADER]: params.bodyFormat } : {} }); export const create = (http, params, rawData) => { const data = copy(rawData); return raw.post(http, getEntityBaseUrl(params), data, { headers: _objectSpread(_objectSpread(_objectSpread({}, 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 } : {}) }); }; export const update = (http, params, rawData, headers) => { var _rawData$sys$version; const data = copy(rawData); delete data.sys; return raw.put(http, getEntityCommentUrl(params), data, { headers: _objectSpread(_objectSpread({ [VERSION_HEADER]: (_rawData$sys$version = rawData.sys.version) !== null && _rawData$sys$version !== void 0 ? _rawData$sys$version : 0 }, typeof rawData.body !== 'string' ? { [BODY_FORMAT_HEADER]: 'rich-text' } : {}), headers) }); }; export const del = (http, _ref) => { let { version } = _ref, params = _objectWithoutProperties(_ref, _excluded); return raw.del(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. */ export const getAll = getMany;