UNPKG

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