UNPKG

18 kBJavaScriptView Raw
1function 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; }
2function _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; }
3function _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; }
4function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
5function _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); }
6import * as checks from './plain/checks';
7import entities from './entities';
8
9/**
10 * @private
11 */
12
13/**
14 * @private
15 */
16export default function createEntryApi(makeRequest) {
17 const {
18 wrapEntry,
19 wrapEntryCollection
20 } = entities.entry;
21 const {
22 wrapSnapshot,
23 wrapSnapshotCollection
24 } = entities.snapshot;
25 const {
26 wrapTask,
27 wrapTaskCollection
28 } = entities.task;
29 const {
30 wrapComment,
31 wrapCommentCollection
32 } = entities.comment;
33 const getParams = self => {
34 const entry = self.toPlainObject();
35 return {
36 params: {
37 spaceId: entry.sys.space.sys.id,
38 environmentId: entry.sys.environment.sys.id,
39 entryId: entry.sys.id
40 },
41 raw: entry
42 };
43 };
44 return {
45 /**
46 * Sends an update to the server with any changes made to the object's properties
47 * @return Object returned from the server with updated changes.
48 * @example ```javascript
49 * const contentful = require('contentful-management')
50 *
51 * const client = contentful.createClient({
52 * accessToken: '<content_management_api_key>'
53 * })
54 *
55 * client.getSpace('<space_id>')
56 * .then((space) => space.getEnvironment('<environment_id>'))
57 * .then((environment) => environment.getEntry('<entry_id>'))
58 * .then((entry) => {
59 * entry.fields.title['en-US'] = 'New entry title'
60 * return entry.update()
61 * })
62 * .then((entry) => console.log(`Entry ${entry.sys.id} updated.`))
63 * .catch(console.error)
64 * ```
65 */
66 update: function update() {
67 const {
68 raw,
69 params
70 } = getParams(this);
71 return makeRequest({
72 entityType: 'Entry',
73 action: 'update',
74 params,
75 payload: raw
76 }).then(data => wrapEntry(makeRequest, data));
77 },
78 /**
79 * Sends an JSON patch to the server with any changes made to the object's properties
80 * @return Object returned from the server with updated changes.
81 * @example ```javascript
82 * const contentful = require('contentful-management')
83 *
84 * const client = contentful.createClient({
85 * accessToken: '<content_management_api_key>'
86 * })
87 *
88 * client.getSpace('<space_id>')
89 * .then((space) => space.getEnvironment('<environment_id>'))
90 * .then((environment) => environment.getEntry('<entry_id>'))
91 * .then((entry) => entry.patch([
92 * {
93 * op: 'replace',
94 * path: '/fields/title/en-US',
95 * value: 'New entry title'
96 * }
97 * ]))
98 * .then((entry) => console.log(`Entry ${entry.sys.id} updated.`))
99 * .catch(console.error)
100 * ```
101 */
102 patch: function patch(ops) {
103 const {
104 raw,
105 params
106 } = getParams(this);
107 return makeRequest({
108 entityType: 'Entry',
109 action: 'patch',
110 params: _objectSpread(_objectSpread({}, params), {}, {
111 version: raw.sys.version
112 }),
113 payload: ops
114 }).then(data => wrapEntry(makeRequest, data));
115 },
116 /**
117 * Deletes this object on the server.
118 * @return Promise for the deletion. It contains no data, but the Promise error case should be handled.
119 * @example ```javascript
120 * const contentful = require('contentful-management')
121 *
122 * const client = contentful.createClient({
123 * accessToken: '<content_management_api_key>'
124 * })
125 *
126 * client.getSpace('<space_id>')
127 * .then((space) => space.getEnvironment('<environment_id>'))
128 * .then((environment) => environment.getEntry('<entry_id>'))
129 * .then((entry) => entry.delete())
130 * .then(() => console.log(`Entry deleted.`))
131 * .catch(console.error)
132 * ```
133 */
134 delete: function del() {
135 const {
136 params
137 } = getParams(this);
138 return makeRequest({
139 entityType: 'Entry',
140 action: 'delete',
141 params
142 });
143 },
144 /**
145 * Publishes the object
146 * @return Object returned from the server with updated metadata.
147 * @example ```javascript
148 * const contentful = require('contentful-management')
149 *
150 * const client = contentful.createClient({
151 * accessToken: '<content_management_api_key>'
152 * })
153 *
154 * client.getSpace('<space_id>')
155 * .then((space) => space.getEnvironment('<environment_id>'))
156 * .then((environment) => environment.getEntry('<entry_id>'))
157 * .then((entry) => entry.publish())
158 * .then((entry) => console.log(`Entry ${entry.sys.id} published.`))
159 * .catch(console.error)
160 * ```
161 */
162 publish: function publish() {
163 const {
164 raw,
165 params
166 } = getParams(this);
167 return makeRequest({
168 entityType: 'Entry',
169 action: 'publish',
170 params,
171 payload: raw
172 }).then(data => wrapEntry(makeRequest, data));
173 },
174 /**
175 * Unpublishes the object
176 * @return Object returned from the server with updated metadata.
177 * @example ```javascript
178 * const contentful = require('contentful-management')
179 *
180 * const client = contentful.createClient({
181 * accessToken: '<content_management_api_key>'
182 * })
183 *
184 * client.getSpace('<space_id>')
185 * .then((space) => space.getEnvironment('<environment_id>'))
186 * .then((environment) => environment.getEntry('<entry_id>'))
187 * .then((entry) => entry.unpublish())
188 * .then((entry) => console.log(`Entry ${entry.sys.id} unpublished.`))
189 * .catch(console.error)
190 * ```
191 */
192
193 unpublish: function unpublish() {
194 const {
195 params
196 } = getParams(this);
197 return makeRequest({
198 entityType: 'Entry',
199 action: 'unpublish',
200 params
201 }).then(data => wrapEntry(makeRequest, data));
202 },
203 /**
204 * Archives the object
205 * @return Object returned from the server with updated metadata.
206 * @example ```javascript
207 * const contentful = require('contentful-management')
208 *
209 * const client = contentful.createClient({
210 * accessToken: '<content_management_api_key>'
211 * })
212 *
213 * client.getSpace('<space_id>')
214 * .then((space) => space.getEnvironment('<environment_id>'))
215 * .then((environment) => environment.getEntry('<entry_id>'))
216 * .then((entry) => entry.archive())
217 * .then((entry) => console.log(`Entry ${entry.sys.id} archived.`))
218 * .catch(console.error)
219 * ```
220 */
221 archive: function archive() {
222 const {
223 params
224 } = getParams(this);
225 return makeRequest({
226 entityType: 'Entry',
227 action: 'archive',
228 params
229 }).then(data => wrapEntry(makeRequest, data));
230 },
231 /**
232 * Unarchives the object
233 * @return Object returned from the server with updated metadata.
234 * @example ```javascript
235 * const contentful = require('contentful-management')
236 *
237 * const client = contentful.createClient({
238 * accessToken: '<content_management_api_key>'
239 * })
240 *
241 * client.getSpace('<space_id>')
242 * .then((space) => space.getEnvironment('<environment_id>'))
243 * .then((environment) => environment.getEntry('<entry_id>'))
244 * .then((entry) => entry.unarchive())
245 * .then((entry) => console.log(`Entry ${entry.sys.id} unarchived.`))
246 * .catch(console.error)
247 * ```
248 */
249 unarchive: function unarchive() {
250 const {
251 params
252 } = getParams(this);
253 return makeRequest({
254 entityType: 'Entry',
255 action: 'unarchive',
256 params
257 }).then(data => wrapEntry(makeRequest, data));
258 },
259 /**
260 * Gets all snapshots of an entry
261 * @example ```javascript
262 * const contentful = require('contentful-management')
263 *
264 * const client = contentful.createClient({
265 * accessToken: '<content_management_api_key>'
266 * })
267 *
268 * client.getSpace('<space_id>')
269 * .then((space) => space.getEnvironment('<environment_id>'))
270 * .then((environment) => environment.getEntry('<entry_id>'))
271 * .then((entry) => entry.getSnapshots())
272 * .then((snapshots) => console.log(snapshots.items))
273 * .catch(console.error)
274 * ```
275 */
276 getSnapshots: function (query = {}) {
277 const {
278 params
279 } = getParams(this);
280 return makeRequest({
281 entityType: 'Snapshot',
282 action: 'getManyForEntry',
283 params: _objectSpread(_objectSpread({}, params), {}, {
284 query
285 })
286 }).then(data => wrapSnapshotCollection(makeRequest, data));
287 },
288 /**
289 * Gets a snapshot of an entry
290 * @param snapshotId - Id of the snapshot
291 * @example ```javascript
292 * const contentful = require('contentful-management')
293 *
294 * const client = contentful.createClient({
295 * accessToken: '<content_management_api_key>'
296 * })
297 *
298 * client.getSpace('<space_id>')
299 * .then((space) => space.getEnvironment('<environment_id>'))
300 * .then((environment) => environment.getEntry('<entry_id>'))
301 * .then((entry) => entry.getSnapshot('<snapshot_id>'))
302 * .then((snapshot) => console.log(snapshot))
303 * .catch(console.error)
304 * ```
305 */
306 getSnapshot: function (snapshotId) {
307 const {
308 params
309 } = getParams(this);
310 return makeRequest({
311 entityType: 'Snapshot',
312 action: 'getForEntry',
313 params: _objectSpread(_objectSpread({}, params), {}, {
314 snapshotId
315 })
316 }).then(data => wrapSnapshot(makeRequest, data));
317 },
318 /**
319 * Creates a new comment for an entry
320 * @param data Object representation of the Comment to be created
321 * @returns Promise for the newly created Comment
322 * @example ```javascript
323 * const contentful = require('contentful-management')
324 *
325 * const client = contentful.createClient({
326 * accessToken: '<content_management_api_key>'
327 * })
328 *
329 * client.getSpace('<space_id>')
330 * .then((space) => space.getEnvironment('<environment-id>'))
331 * .then((environment) => environment.getEntry('<entry-id>'))
332 * .then((entry) => entry.createComment({
333 * body: 'Something left to do'
334 * }))
335 * .then((comment) => console.log(comment))
336 * .catch(console.error)
337 * ```
338 */
339 createComment: function (data) {
340 const {
341 params
342 } = getParams(this);
343 return makeRequest({
344 entityType: 'Comment',
345 action: 'create',
346 params: {
347 spaceId: params.spaceId,
348 environmentId: params.environmentId,
349 parentEntityId: params.entryId,
350 parentEntityType: 'Entry'
351 },
352 payload: data
353 }).then(data => wrapComment(makeRequest, data));
354 },
355 /**
356 * Gets all comments of an entry
357 * @returns
358 * const contentful = require('contentful-management')
359 *
360 * const client = contentful.createClient({
361 * accessToken: '<content_management_api_key>'
362 * })
363 *
364 * client.getSpace('<space_id>')
365 * .then((space) => space.getEnvironment('<environment-id>'))
366 * .then((environment) => environment.getEntry('<entry-id>'))
367 * .then((entry) => entry.getComments())
368 * .then((comments) => console.log(comments))
369 * .catch(console.error)
370 * ```
371 */
372 getComments: function () {
373 const {
374 params
375 } = getParams(this);
376 return makeRequest({
377 entityType: 'Comment',
378 action: 'getMany',
379 params
380 }).then(data => wrapCommentCollection(makeRequest, data));
381 },
382 /**
383 * Gets a comment of an entry
384 * @returns
385 * const contentful = require('contentful-management')
386 *
387 * const client = contentful.createClient({
388 * accessToken: '<content_management_api_key>'
389 * })
390 *
391 * client.getSpace('<space_id>')
392 * .then((space) => space.getEnvironment('<environment-id>'))
393 * .then((environment) => environment.getEntry('<entry-id>'))
394 * .then((entry) => entry.getComment(`<comment-id>`))
395 * .then((comment) => console.log(comment))
396 * .catch(console.error)
397 * ```
398 */
399 getComment: function (id) {
400 const {
401 params
402 } = getParams(this);
403 return makeRequest({
404 entityType: 'Comment',
405 action: 'get',
406 params: _objectSpread(_objectSpread({}, params), {}, {
407 commentId: id
408 })
409 }).then(data => wrapComment(makeRequest, data));
410 },
411 /**
412 * Creates a new task for an entry
413 * @param data Object representation of the Task to be created
414 * @returns Promise for the newly created Task
415 * @example ```javascript
416 * const contentful = require('contentful-management')
417 *
418 * const client = contentful.createClient({
419 * accessToken: '<content_management_api_key>'
420 * })
421 *
422 * client.getSpace('<space_id>')
423 * .then((space) => space.getEnvironment('<environment-id>'))
424 * .then((environment) => environment.getEntry('<entry-id>'))
425 * .then((entry) => entry.createTask({
426 * body: 'Something left to do',
427 * assignedTo: '<user-id>',
428 * status: 'active'
429 * }))
430 * .then((task) => console.log(task))
431 * .catch(console.error)
432 * ```
433 */
434 createTask: function (data) {
435 const {
436 params
437 } = getParams(this);
438 return makeRequest({
439 entityType: 'Task',
440 action: 'create',
441 params,
442 payload: data
443 }).then(data => wrapTask(makeRequest, data));
444 },
445 /**
446 * Gets all tasks of an entry
447 * @returns
448 * const contentful = require('contentful-management')
449 *
450 * const client = contentful.createClient({
451 * accessToken: '<content_management_api_key>'
452 * })
453 *
454 * client.getSpace('<space_id>')
455 * .then((space) => space.getEnvironment('<environment-id>'))
456 * .then((environment) => environment.getEntry('<entry-id>'))
457 * .then((entry) => entry.getTasks())
458 * .then((tasks) => console.log(tasks))
459 * .catch(console.error)
460 * ```
461 */
462 getTasks: function (query = {}) {
463 const {
464 params
465 } = getParams(this);
466 return makeRequest({
467 entityType: 'Task',
468 action: 'getMany',
469 params: _objectSpread(_objectSpread({}, params), {}, {
470 query
471 })
472 }).then(data => wrapTaskCollection(makeRequest, data));
473 },
474 /**
475 * Gets a task of an entry
476 * @returns
477 * const contentful = require('contentful-management')
478 *
479 * const client = contentful.createClient({
480 * accessToken: '<content_management_api_key>'
481 * })
482 *
483 * client.getSpace('<space_id>')
484 * .then((space) => space.getEnvironment('<environment-id>'))
485 * .then((environment) => environment.getEntry('<entry-id>'))
486 * .then((entry) => entry.getTask(`<task-id>`))
487 * .then((task) => console.log(task))
488 * .catch(console.error)
489 * ```
490 */
491 getTask: function (id) {
492 const {
493 params
494 } = getParams(this);
495 return makeRequest({
496 entityType: 'Task',
497 action: 'get',
498 params: _objectSpread(_objectSpread({}, params), {}, {
499 taskId: id
500 })
501 }).then(data => wrapTask(makeRequest, data));
502 },
503 /**
504 * Checks if the entry is published. A published entry might have unpublished changes
505 */
506 isPublished: function isPublished() {
507 const raw = this.toPlainObject();
508 return checks.isPublished(raw);
509 },
510 /**
511 * Checks if the entry is updated. This means the entry was previously published but has unpublished changes.
512 */
513 isUpdated: function isUpdated() {
514 const raw = this.toPlainObject();
515 return checks.isUpdated(raw);
516 },
517 /**
518 * Checks if the entry is in draft mode. This means it is not published.
519 */
520 isDraft: function isDraft() {
521 const raw = this.toPlainObject();
522 return checks.isDraft(raw);
523 },
524 /**
525 * Checks if entry is archived. This means it's not exposed to the Delivery/Preview APIs.
526 */
527 isArchived: function isArchived() {
528 const raw = this.toPlainObject();
529 return checks.isArchived(raw);
530 },
531 /**
532 * Recursively collects references of an entry and their descendants
533 */
534 references: function references(options) {
535 const raw = this.toPlainObject();
536 return makeRequest({
537 entityType: 'Entry',
538 action: 'references',
539 params: {
540 spaceId: raw.sys.space.sys.id,
541 environmentId: raw.sys.environment.sys.id,
542 entryId: raw.sys.id,
543 include: options === null || options === void 0 ? void 0 : options.include
544 }
545 }).then(response => wrapEntryCollection(makeRequest, response));
546 }
547 };
548}
\No newline at end of file