contentful-management
Version:
Client for Contentful's Content Management API
77 lines (67 loc) • 3.28 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';
// PROPS //
// Remove and replace with BLOCKS as soon as rich-text-types supports mentions
export let CommentNode = /*#__PURE__*/function (CommentNode) {
CommentNode["Document"] = "document";
CommentNode["Paragraph"] = "paragraph";
CommentNode["Mention"] = "mention";
return CommentNode;
}({});
// Add "extends Block" as soon as rich-text-types supports mentions
// Add "extends Document" as soon as rich-text-types supports mentions.
// PARAMS //
// We keep this type as explicit as possible until we open up the comments entity further
// NESTED CLIENT //
/**
* @private
*/
export default function createCommentApi(makeRequest) {
const getParams = comment => ({
spaceId: comment.sys.space.sys.id,
environmentId: comment.sys.environment.sys.id,
entryId: comment.sys.parentEntity.sys.id,
commentId: comment.sys.id
});
return {
update: async function () {
const raw = this.toPlainObject();
const data = await makeRequest({
entityType: 'Comment',
action: 'update',
params: getParams(raw),
payload: raw
});
return wrapComment(makeRequest, data);
},
delete: async function () {
const raw = this.toPlainObject();
await makeRequest({
entityType: 'Comment',
action: 'delete',
params: _objectSpread(_objectSpread({}, getParams(raw)), {}, {
version: raw.sys.version
})
});
}
};
}
/**
* @private
*/
export function wrapComment(makeRequest, data) {
const comment = toPlainObject(copy(data));
const commentWithMethods = enhanceWithMethods(comment, createCommentApi(makeRequest));
return freezeSys(commentWithMethods);
}
/**
* @private
*/
export const wrapCommentCollection = wrapCollection(wrapComment);