UNPKG

49.5 kBJavaScriptView Raw
1/**
2 * Contentful Space API. Contains methods to access any operations at a space
3 * level, such as creating and reading entities contained in a space.
4 */
5
6import { createRequestConfig } from 'contentful-sdk-core';
7import entities from './entities';
8
9/**
10 * @private
11 */
12
13/**
14 * Creates API object with methods to access the Space API
15 * @param {MakeRequest} makeRequest - function to make requests via an adapter
16 * @return {ContentfulSpaceAPI}
17 * @private
18 */
19export default function createSpaceApi(makeRequest) {
20 const {
21 wrapSpace
22 } = entities.space;
23 const {
24 wrapEnvironment,
25 wrapEnvironmentCollection
26 } = entities.environment;
27 const {
28 wrapWebhook,
29 wrapWebhookCollection
30 } = entities.webhook;
31 const {
32 wrapRole,
33 wrapRoleCollection
34 } = entities.role;
35 const {
36 wrapUser,
37 wrapUserCollection
38 } = entities.user;
39 const {
40 wrapSpaceMember,
41 wrapSpaceMemberCollection
42 } = entities.spaceMember;
43 const {
44 wrapSpaceMembership,
45 wrapSpaceMembershipCollection
46 } = entities.spaceMembership;
47 const {
48 wrapTeamSpaceMembership,
49 wrapTeamSpaceMembershipCollection
50 } = entities.teamSpaceMembership;
51 const {
52 wrapTeamCollection
53 } = entities.team;
54 const {
55 wrapApiKey,
56 wrapApiKeyCollection
57 } = entities.apiKey;
58 const {
59 wrapEnvironmentAlias,
60 wrapEnvironmentAliasCollection
61 } = entities.environmentAlias;
62 const {
63 wrapPreviewApiKey,
64 wrapPreviewApiKeyCollection
65 } = entities.previewApiKey;
66 const {
67 wrapScheduledAction,
68 wrapScheduledActionCollection
69 } = entities.scheduledAction;
70 return {
71 /**
72 * Deletes the space
73 * @return Promise for the deletion. It contains no data, but the Promise error case should be handled.
74 * @example ```javascript
75 * const contentful = require('contentful-management')
76 *
77 * const client = contentful.createClient({
78 * accessToken: '<content_management_api_key>'
79 * })
80 *
81 * client.getSpace('<space_id>')
82 * .then((space) => space.delete())
83 * .then(() => console.log('Space deleted.'))
84 * .catch(console.error)
85 * ```
86 */
87 delete: function deleteSpace() {
88 const raw = this.toPlainObject();
89 return makeRequest({
90 entityType: 'Space',
91 action: 'delete',
92 params: {
93 spaceId: raw.sys.id
94 }
95 });
96 },
97 /**
98 * Updates the space
99 * @return Promise for the updated space.
100 * @example ```javascript
101 * const contentful = require('contentful-management')
102 *
103 * const client = contentful.createClient({
104 * accessToken: '<content_management_api_key>'
105 * })
106 *
107 * client.getSpace('<space_id>')
108 * .then((space) => {
109 * space.name = 'New name'
110 * return space.update()
111 * })
112 * .then((space) => console.log(`Space ${space.sys.id} renamed.`)
113 * .catch(console.error)
114 * ```
115 */
116 update: function updateSpace() {
117 const raw = this.toPlainObject();
118 return makeRequest({
119 entityType: 'Space',
120 action: 'update',
121 params: {
122 spaceId: raw.sys.id
123 },
124 payload: raw,
125 headers: {}
126 }).then(data => wrapSpace(makeRequest, data));
127 },
128 /**
129 * Gets an environment
130 * @param id - Environment ID
131 * @return Promise for an Environment
132 * @example ```javascript
133 * const contentful = require('contentful-management')
134 *
135 * const client = contentful.createClient({
136 * accessToken: '<content_management_api_key>'
137 * })
138 *
139 * client.getSpace('<space_id>')
140 * .then((space) => space.getEnvironment('<environment_id>'))
141 * .then((environment) => console.log(environment))
142 * .catch(console.error)
143 * ```
144 */
145 getEnvironment(environmentId) {
146 const raw = this.toPlainObject();
147 return makeRequest({
148 entityType: 'Environment',
149 action: 'get',
150 params: {
151 spaceId: raw.sys.id,
152 environmentId
153 }
154 }).then(data => wrapEnvironment(makeRequest, data));
155 },
156 /**
157 * Gets a collection of Environments
158 * @return Promise for a collection of Environment
159 * @example ```javascript
160 * const contentful = require('contentful-management')
161 *
162 * const client = contentful.createClient({
163 * accessToken: '<content_management_api_key>'
164 * })
165 *
166 * client.getSpace('<space_id>')
167 * .then((space) => space.getEnvironments())
168 * .then((response) => console.log(response.items))
169 * .catch(console.error)
170 * ```
171 */
172 getEnvironments(query = {}) {
173 const raw = this.toPlainObject();
174 return makeRequest({
175 entityType: 'Environment',
176 action: 'getMany',
177 params: {
178 spaceId: raw.sys.id,
179 query
180 }
181 }).then(data => wrapEnvironmentCollection(makeRequest, data));
182 },
183 /**
184 * Creates an environment
185 * @param data - Object representation of the Environment to be created
186 * @return Promise for the newly created Environment
187 * @example ```javascript
188 * const contentful = require('contentful-management')
189 *
190 * const client = contentful.createClient({
191 * accessToken: '<content_management_api_key>'
192 * })
193 *
194 * client.getSpace('<space_id>')
195 * .then((space) => space.createEnvironment({ name: 'Staging' }))
196 * .then((environment) => console.log(environment))
197 * .catch(console.error)
198 * ```
199 */
200 createEnvironment(data = {}) {
201 const raw = this.toPlainObject();
202 return makeRequest({
203 entityType: 'Environment',
204 action: 'create',
205 params: {
206 spaceId: raw.sys.id
207 },
208 payload: data
209 }).then(response => wrapEnvironment(makeRequest, response));
210 },
211 /**
212 * Creates an Environment with a custom ID
213 * @param id - Environment ID
214 * @param data - Object representation of the Environment to be created
215 * @param sourceEnvironmentId - ID of the source environment that will be copied to create the new environment. Default is "master"
216 * @return Promise for the newly created Environment
217 * @example ```javascript
218 * const contentful = require('contentful-management')
219 *
220 * const client = contentful.createClient({
221 * accessToken: '<content_management_api_key>'
222 * })
223 *
224 * client.getSpace('<space_id>')
225 * .then((space) => space.createEnvironmentWithId('<environment-id>', { name: 'Staging'}, 'master'))
226 * .then((environment) => console.log(environment))
227 * .catch(console.error)
228 * ```
229 */
230 createEnvironmentWithId(id, data, sourceEnvironmentId) {
231 const raw = this.toPlainObject();
232 return makeRequest({
233 entityType: 'Environment',
234 action: 'createWithId',
235 params: {
236 spaceId: raw.sys.id,
237 environmentId: id,
238 sourceEnvironmentId
239 },
240 payload: data
241 }).then(response => wrapEnvironment(makeRequest, response));
242 },
243 /**
244 * Gets a Webhook
245 * @param id - Webhook ID
246 * @return Promise for a Webhook
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.getWebhook('<webhook_id>'))
256 * .then((webhook) => console.log(webhook))
257 * .catch(console.error)
258 * ```
259 */
260 getWebhook(id) {
261 const raw = this.toPlainObject();
262 return makeRequest({
263 entityType: 'Webhook',
264 action: 'get',
265 params: {
266 spaceId: raw.sys.id,
267 webhookDefinitionId: id
268 }
269 }).then(data => wrapWebhook(makeRequest, data));
270 },
271 /**
272 * Gets a collection of Webhooks
273 * @return Promise for a collection of Webhooks
274 * @example ```javascript
275 * const contentful = require('contentful-management')
276 *
277 * const client = contentful.createClient({
278 * accessToken: '<content_management_api_key>'
279 * })
280 *
281 * client.getSpace('<space_id>')
282 * .then((space) => space.getWebhooks())
283 * .then((response) => console.log(response.items))
284 * .catch(console.error)
285 * ```
286 */
287 getWebhooks() {
288 const raw = this.toPlainObject();
289 return makeRequest({
290 entityType: 'Webhook',
291 action: 'getMany',
292 params: {
293 spaceId: raw.sys.id
294 }
295 }).then(data => wrapWebhookCollection(makeRequest, data));
296 },
297 /**
298 * Fetch a webhook signing secret
299 * @returns Promise for the redacted webhook signing secret in this space
300 * @example ```javascript
301 * const contentful = require('contentful-management')
302 *
303 * const client = contentful.createClient({
304 * accessToken: '<content_management_api_key>'
305 * })
306 *
307 * client.getSpace('<space_id>')
308 * .then((space) => space.getWebhookSigningSecret())
309 * .then((response) => console.log(response.redactedValue))
310 * .catch(console.error)
311 * ```
312 */
313 getWebhookSigningSecret: function getSigningSecret() {
314 const raw = this.toPlainObject();
315 return makeRequest({
316 entityType: 'Webhook',
317 action: 'getSigningSecret',
318 params: {
319 spaceId: raw.sys.id
320 }
321 });
322 },
323 /**
324 * Fetch a webhook retry policy
325 * @returns Promise for the redacted webhook retry policy in this space
326 * @example ```javascript
327 * const contentful = require('contentful-management')
328 *
329 * const client = contentful.createClient({
330 * accessToken: '<content_management_api_key>'
331 * })
332 *
333 * client.getSpace('<space_id>')
334 * .then((space) => space.getRetryPolicy())
335 * .then((response) => console.log(response.redactedValue))
336 * .catch(console.error)
337 * ```
338 */
339 getWebhookRetryPolicy: function getWebhookRetryPolicy() {
340 const raw = this.toPlainObject();
341 return makeRequest({
342 entityType: 'Webhook',
343 action: 'getRetryPolicy',
344 params: {
345 spaceId: raw.sys.id
346 }
347 });
348 },
349 /**
350 * Creates a Webhook
351 * @param data - Object representation of the Webhook to be created
352 * @return Promise for the newly created Webhook
353 * @example ```javascript
354 * const contentful = require('contentful-management')
355 *
356 * client.getSpace('<space_id>')
357 * .then((space) => space.createWebhook({
358 * 'name': 'My webhook',
359 * 'url': 'https://www.example.com/test',
360 * 'topics': [
361 * 'Entry.create',
362 * 'ContentType.create',
363 * '*.publish',
364 * 'Asset.*'
365 * ]
366 * }))
367 * .then((webhook) => console.log(webhook))
368 * .catch(console.error)
369 * ```
370 */
371 createWebhook(data) {
372 const raw = this.toPlainObject();
373 return makeRequest({
374 entityType: 'Webhook',
375 action: 'create',
376 params: {
377 spaceId: raw.sys.id
378 },
379 payload: data
380 }).then(data => wrapWebhook(makeRequest, data));
381 },
382 /**
383 * Creates a Webhook with a custom ID
384 * @param id - Webhook ID
385 * @param data - Object representation of the Webhook to be created
386 * @return Promise for the newly created Webhook
387 * @example ```javascript
388 * const contentful = require('contentful-management')
389 *
390 * client.getSpace('<space_id>')
391 * .then((space) => space.createWebhookWithId('<webhook_id>', {
392 * 'name': 'My webhook',
393 * 'url': 'https://www.example.com/test',
394 * 'topics': [
395 * 'Entry.create',
396 * 'ContentType.create',
397 * '*.publish',
398 * 'Asset.*'
399 * ]
400 * }))
401 * .then((webhook) => console.log(webhook))
402 * .catch(console.error)
403 * ```
404 */
405 createWebhookWithId(id, data) {
406 const raw = this.toPlainObject();
407 return makeRequest({
408 entityType: 'Webhook',
409 action: 'createWithId',
410 params: {
411 spaceId: raw.sys.id,
412 webhookDefinitionId: id
413 },
414 payload: data
415 }).then(data => wrapWebhook(makeRequest, data));
416 },
417 /**
418 * Create or update the webhook signing secret for this space
419 * @param data 64 character string that will be used to sign the webhook calls
420 * @returns Promise for the redacted webhook signing secret that was created or updated
421 * @example ```javascript
422 * const contentful = require('contentful-management')
423 * const crypto = require('crypto')
424 *
425 * const client = contentful.createClient({
426 * accessToken: '<content_management_api_key>'
427 * })
428 *
429 * const signingSecret = client.getSpace('<space_id>')
430 * .then((space) => space.upsertWebhookSigningSecret({
431 * value: crypto.randomBytes(32).toString('hex')
432 * }))
433 * .then((response) => console.log(response.redactedValue))
434 * .catch(console.error)
435 * ```
436 */
437 upsertWebhookSigningSecret: function getSigningSecret(data) {
438 const raw = this.toPlainObject();
439 return makeRequest({
440 entityType: 'Webhook',
441 action: 'upsertSigningSecret',
442 params: {
443 spaceId: raw.sys.id
444 },
445 payload: data
446 });
447 },
448 /**
449 * Create or update the webhook retry policy for this space
450 * @param data the maxRetries with integer value >= 2 and <= 99 value to set in the Retry Policy
451 * @returns Promise for the redacted webhook retry policy that was created or updated
452 * @example ```javascript
453 * const contentful = require('contentful-management')
454 *
455 * const client = contentful.createClient({
456 * accessToken: '<content_management_api_key>'
457 * })
458 *
459 * const retryPolicy = client.getSpace('<space_id>')
460 * .then((space) => space.upsertWebhookRetryPolicy({
461 * maxRetries: 15
462 * }))
463 * .then((response) => console.log(response.redactedValue))
464 * .catch(console.error)
465 * ```
466 */
467 upsertWebhookRetryPolicy: function upsertWebhookRetryPolicy(data) {
468 const raw = this.toPlainObject();
469 return makeRequest({
470 entityType: 'Webhook',
471 action: 'upsertRetryPolicy',
472 params: {
473 spaceId: raw.sys.id
474 },
475 payload: data
476 });
477 },
478 /**
479 * Delete the webhook signing secret for this space
480 * @returns Promise<void>
481 * @example ```javascript
482 * const contentful = require('contentful-management')
483 *
484 * const client = contentful.createClient({
485 * accessToken: '<content_management_api_key>'
486 * })
487 *
488 * client.getSpace('<space_id>')
489 * .then((space) => space.deleteWebhookSigningSecret())
490 * .then(() => console.log("success"))
491 * .catch(console.error)
492 * ```
493 */
494 deleteWebhookSigningSecret: function getSigningSecret() {
495 const raw = this.toPlainObject();
496 return makeRequest({
497 entityType: 'Webhook',
498 action: 'deleteSigningSecret',
499 params: {
500 spaceId: raw.sys.id
501 }
502 });
503 },
504 /**
505 * Delete the webhook retry policy for this space
506 * @returns Promise<void>
507 * @example ```javascript
508 * const contentful = require('contentful-management')
509 *
510 * const client = contentful.createClient({
511 * accessToken: '<content_management_api_key>'
512 * })
513 *
514 * client.getSpace('<space_id>')
515 * .then((space) => space.deleteWebhookRetryPolicy())
516 * .then(() => console.log("success"))
517 * .catch(console.error)
518 * ```
519 */
520 deleteWebhookRetryPolicy: function deleteRetryPolicy() {
521 const raw = this.toPlainObject();
522 return makeRequest({
523 entityType: 'Webhook',
524 action: 'deleteRetryPolicy',
525 params: {
526 spaceId: raw.sys.id
527 }
528 });
529 },
530 /**
531 * Gets a Role
532 * @param id - Role ID
533 * @return Promise for a Role
534 * @example ```javascript
535 * const contentful = require('contentful-management')
536 *
537 * const client = contentful.createClient({
538 * accessToken: '<content_management_api_key>'
539 * })
540 *
541 * client.getSpace('<space_id>')
542 * .then((space) => space.createRole({
543 * fields: {
544 * title: {
545 * 'en-US': 'Role title'
546 * }
547 * }
548 * }))
549 * .then((role) => console.log(role))
550 * .catch(console.error)
551 * ```
552 */
553 getRole(id) {
554 const raw = this.toPlainObject();
555 return makeRequest({
556 entityType: 'Role',
557 action: 'get',
558 params: {
559 spaceId: raw.sys.id,
560 roleId: id
561 }
562 }).then(data => wrapRole(makeRequest, data));
563 },
564 /**
565 * Gets a collection of Roles
566 * @return Promise for a collection of Roles
567 * @example ```javascript
568 * const contentful = require('contentful-management')
569 *
570 * const client = contentful.createClient({
571 * accessToken: '<content_management_api_key>'
572 * })
573 *
574 * client.getSpace('<space_id>')
575 * .then((space) => space.getRoles())
576 * .then((response) => console.log(response.items))
577 * .catch(console.error)
578 * ```
579 */
580 getRoles(query = {}) {
581 const raw = this.toPlainObject();
582 return makeRequest({
583 entityType: 'Role',
584 action: 'getMany',
585 params: {
586 spaceId: raw.sys.id,
587 query: createRequestConfig({
588 query
589 }).params
590 }
591 }).then(data => wrapRoleCollection(makeRequest, data));
592 },
593 /**
594 * Creates a Role
595 * @param data - Object representation of the Role to be created
596 * @return Promise for the newly created Role
597 * @example ```javascript
598 * const contentful = require('contentful-management')
599 *
600 * const client = contentful.createClient({
601 * accessToken: '<content_management_api_key>'
602 * })
603 * client.getSpace('<space_id>')
604 * .then((space) => space.createRole({
605 * name: 'My Role',
606 * description: 'foobar role',
607 * permissions: {
608 * ContentDelivery: 'all',
609 * ContentModel: ['read'],
610 * Settings: []
611 * },
612 * policies: [
613 * {
614 * effect: 'allow',
615 * actions: 'all',
616 * constraint: {
617 * and: [
618 * {
619 * equals: [
620 * { doc: 'sys.type' },
621 * 'Entry'
622 * ]
623 * },
624 * {
625 * equals: [
626 * { doc: 'sys.type' },
627 * 'Asset'
628 * ]
629 * }
630 * ]
631 * }
632 * }
633 * ]
634 * }))
635 * .then((role) => console.log(role))
636 * .catch(console.error)
637 * ```
638 */
639 createRole(data) {
640 const raw = this.toPlainObject();
641 return makeRequest({
642 entityType: 'Role',
643 action: 'create',
644 params: {
645 spaceId: raw.sys.id
646 },
647 payload: data
648 }).then(data => wrapRole(makeRequest, data));
649 },
650 /**
651 * Creates a Role with a custom ID
652 * @param id - Role ID
653 * @param data - Object representation of the Role to be created
654 * @return Promise for the newly created Role
655 * @example ```javascript
656 * const contentful = require('contentful-management')
657 *
658 * const client = contentful.createClient({
659 * accessToken: '<content_management_api_key>'
660 * })
661 * client.getSpace('<space_id>')
662 * .then((space) => space.createRoleWithId('<role-id>', {
663 * name: 'My Role',
664 * description: 'foobar role',
665 * permissions: {
666 * ContentDelivery: 'all',
667 * ContentModel: ['read'],
668 * Settings: []
669 * },
670 * policies: [
671 * {
672 * effect: 'allow',
673 * actions: 'all',
674 * constraint: {
675 * and: [
676 * {
677 * equals: [
678 * { doc: 'sys.type' },
679 * 'Entry'
680 * ]
681 * },
682 * {
683 * equals: [
684 * { doc: 'sys.type' },
685 * 'Asset'
686 * ]
687 * }
688 * ]
689 * }
690 * }
691 * ]
692 * }))
693 * .then((role) => console.log(role))
694 * .catch(console.error)
695 * ```
696 */
697 createRoleWithId(id, roleData) {
698 const raw = this.toPlainObject();
699 return makeRequest({
700 entityType: 'Role',
701 action: 'createWithId',
702 params: {
703 spaceId: raw.sys.id,
704 roleId: id
705 },
706 payload: roleData
707 }).then(data => wrapRole(makeRequest, data));
708 },
709 /**
710 * Gets a User
711 * @param userId - User ID
712 * @return Promise for a User
713 * @example ```javascript
714 * const contentful = require('contentful-management')
715 *
716 * client.getSpace('<space_id>')
717 * .then((space) => space.getSpaceUser('id'))
718 * .then((user) => console.log(user))
719 * .catch(console.error)
720 * ```
721 */
722 getSpaceUser(userId) {
723 const raw = this.toPlainObject();
724 return makeRequest({
725 entityType: 'User',
726 action: 'getForSpace',
727 params: {
728 spaceId: raw.sys.id,
729 userId
730 }
731 }).then(data => wrapUser(makeRequest, data));
732 },
733 /**
734 * Gets a collection of Users in a space
735 * @param query - Object with search parameters. Check the <a href="https://www.contentful.com/developers/docs/javascript/tutorials/using-js-cda-sdk/#retrieving-entries-with-search-parameters">JS SDK tutorial</a> and the <a href="https://www.contentful.com/developers/docs/references/content-delivery-api/#/reference/search-parameters">REST API reference</a> for more details.
736 * @return Promise a collection of Users in a space
737 * @example ```javascript
738 * const contentful = require('contentful-management')
739 *
740 * client.getSpace('<space_id>')
741 * .then((space) => space.getSpaceUsers(query))
742 * .then((data) => console.log(data))
743 * .catch(console.error)
744 * ```
745 */
746 getSpaceUsers(query = {}) {
747 const raw = this.toPlainObject();
748 return makeRequest({
749 entityType: 'User',
750 action: 'getManyForSpace',
751 params: {
752 spaceId: raw.sys.id,
753 query: createRequestConfig({
754 query
755 }).params
756 }
757 }).then(data => wrapUserCollection(makeRequest, data));
758 },
759 /**
760 * Gets a collection of teams for a space
761 * @param query
762 * @return Promise for a collection of teams for a space
763 * @example ```javascript
764 * const contentful = require('contentful-management')
765 *
766 * client.getSpace('<space_id>')
767 * .then((space) => space.getTeams())
768 * .then((teamsCollection) => console.log(teamsCollection))
769 * .catch(console.error)
770 * ```
771 */
772 getTeams(query = {
773 limit: 100
774 }) {
775 const raw = this.toPlainObject();
776 return makeRequest({
777 entityType: 'Team',
778 action: 'getManyForSpace',
779 params: {
780 spaceId: raw.sys.id,
781 query: createRequestConfig({
782 query
783 }).params
784 }
785 }).then(data => wrapTeamCollection(makeRequest, data));
786 },
787 /**
788 * Gets a Space Member
789 * @param id Get Space Member by user_id
790 * @return Promise for a Space Member
791 * @example ```javascript
792 * const contentful = require('contentful-management')
793 *
794 * client.getSpace('<space_id>')
795 * .then((space) => space.getSpaceMember(id))
796 * .then((spaceMember) => console.log(spaceMember))
797 * .catch(console.error)
798 * ```
799 */
800 getSpaceMember(id) {
801 const raw = this.toPlainObject();
802 return makeRequest({
803 entityType: 'SpaceMember',
804 action: 'get',
805 params: {
806 spaceId: raw.sys.id,
807 spaceMemberId: id
808 }
809 }).then(data => wrapSpaceMember(makeRequest, data));
810 },
811 /**
812 * Gets a collection of Space Members
813 * @param query
814 * @return Promise for a collection of Space Members
815 * @example ```javascript
816 * const contentful = require('contentful-management')
817 *
818 * client.getSpace('<space_id>')
819 * .then((space) => space.getSpaceMembers({'limit': 100}))
820 * .then((spaceMemberCollection) => console.log(spaceMemberCollection))
821 * .catch(console.error)
822 * ```
823 */
824 getSpaceMembers(query = {}) {
825 const raw = this.toPlainObject();
826 return makeRequest({
827 entityType: 'SpaceMember',
828 action: 'getMany',
829 params: {
830 spaceId: raw.sys.id,
831 query: createRequestConfig({
832 query
833 }).params
834 }
835 }).then(data => wrapSpaceMemberCollection(makeRequest, data));
836 },
837 /**
838 * Gets a Space Membership
839 * Warning: the user attribute in the space membership root is deprecated. The attribute has been moved inside the sys object (i.e. sys.user).
840 * @param id - Space Membership ID
841 * @return Promise for a Space Membership
842 * @example ```javascript
843 * const contentful = require('contentful-management')
844 *
845 * client.getSpace('<space_id>')
846 * .then((space) => space.getSpaceMembership('id'))
847 * .then((spaceMembership) => console.log(spaceMembership))
848 * .catch(console.error)
849 * ```
850 */
851 getSpaceMembership(id) {
852 const raw = this.toPlainObject();
853 return makeRequest({
854 entityType: 'SpaceMembership',
855 action: 'get',
856 params: {
857 spaceId: raw.sys.id,
858 spaceMembershipId: id
859 }
860 }).then(data => wrapSpaceMembership(makeRequest, data));
861 },
862 /**
863 * Gets a collection of Space Memberships
864 * Warning: the user attribute in the space membership root is deprecated. The attribute has been moved inside the sys object (i.e. sys.user).
865 * @param query - Object with search parameters. Check the <a href="https://www.contentful.com/developers/docs/javascript/tutorials/using-js-cda-sdk/#retrieving-entries-with-search-parameters">JS SDK tutorial</a> and the <a href="https://www.contentful.com/developers/docs/references/content-delivery-api/#/reference/search-parameters">REST API reference</a> for more details.
866 * @return Promise for a collection of Space Memberships
867 * @example ```javascript
868 * const contentful = require('contentful-management')
869 *
870 * client.getSpace('<space_id>')
871 * .then((space) => space.getSpaceMemberships({'limit': 100})) // you can add more queries as 'key': 'value'
872 * .then((response) => console.log(response.items))
873 * .catch(console.error)
874 * ```
875 */
876 getSpaceMemberships(query = {}) {
877 const raw = this.toPlainObject();
878 return makeRequest({
879 entityType: 'SpaceMembership',
880 action: 'getMany',
881 params: {
882 spaceId: raw.sys.id,
883 query: createRequestConfig({
884 query
885 }).params
886 }
887 }).then(data => wrapSpaceMembershipCollection(makeRequest, data));
888 },
889 /**
890 * Creates a Space Membership
891 * Warning: the user attribute in the space membership root is deprecated. The attribute has been moved inside the sys object (i.e. sys.user).
892 * @param data - Object representation of the Space Membership to be created
893 * @return Promise for the newly created Space Membership
894 * @example ```javascript
895 * const contentful = require('contentful-management')
896 *
897 * const client = contentful.createClient({
898 * accessToken: '<content_management_api_key>'
899 * })
900 *
901 * client.getSpace('<space_id>')
902 * .then((space) => space.createSpaceMembership({
903 * admin: false,
904 * roles: [
905 * {
906 * type: 'Link',
907 * linkType: 'Role',
908 * id: '<role_id>'
909 * }
910 * ],
911 * email: 'foo@example.com'
912 * }))
913 * .then((spaceMembership) => console.log(spaceMembership))
914 * .catch(console.error)
915 * ```
916 */
917 createSpaceMembership(data) {
918 const raw = this.toPlainObject();
919 return makeRequest({
920 entityType: 'SpaceMembership',
921 action: 'create',
922 params: {
923 spaceId: raw.sys.id
924 },
925 payload: data
926 }).then(response => wrapSpaceMembership(makeRequest, response));
927 },
928 /**
929 * Creates a Space Membership with a custom ID
930 * Warning: the user attribute in the space membership root is deprecated. The attribute has been moved inside the sys object (i.e. sys.user).
931 * @param id - Space Membership ID
932 * @param data - Object representation of the Space Membership to be created
933 * @return Promise for the newly created Space Membership
934 * @example ```javascript
935 * const contentful = require('contentful-management')
936 *
937 * const client = contentful.createClient({
938 * accessToken: '<content_management_api_key>'
939 * })
940 *
941 * client.getSpace('<space_id>')
942 * .then((space) => space.createSpaceMembershipWithId('<space-membership-id>', {
943 * admin: false,
944 * roles: [
945 * {
946 * type: 'Link',
947 * linkType: 'Role',
948 * id: '<role_id>'
949 * }
950 * ],
951 * email: 'foo@example.com'
952 * }))
953 * .then((spaceMembership) => console.log(spaceMembership))
954 * .catch(console.error)
955 * ```
956 */
957 createSpaceMembershipWithId(id, data) {
958 const raw = this.toPlainObject();
959 return makeRequest({
960 entityType: 'SpaceMembership',
961 action: 'createWithId',
962 params: {
963 spaceId: raw.sys.id,
964 spaceMembershipId: id
965 },
966 payload: data
967 }).then(response => wrapSpaceMembership(makeRequest, response));
968 },
969 /**
970 * Gets a Team Space Membership
971 * @param id - Team Space Membership ID
972 * @return Promise for a Team Space Membership
973 * @example ```javascript
974 * const contentful = require('contentful-management')
975 *
976 * client.getSpace('<space_id>')
977 * .then((space) => space.getTeamSpaceMembership('team_space_membership_id'))
978 * .then((teamSpaceMembership) => console.log(teamSpaceMembership))
979 * .catch(console.error)
980 * ```
981 */
982 getTeamSpaceMembership(teamSpaceMembershipId) {
983 const raw = this.toPlainObject();
984 return makeRequest({
985 entityType: 'TeamSpaceMembership',
986 action: 'get',
987 params: {
988 spaceId: raw.sys.id,
989 teamSpaceMembershipId
990 }
991 }).then(data => wrapTeamSpaceMembership(makeRequest, data));
992 },
993 /**
994 * Gets a collection of Team Space Memberships
995 * @param query - Object with search parameters. Check the <a href="https://www.contentful.com/developers/docs/javascript/tutorials/using-js-cda-sdk/#retrieving-entries-with-search-parameters">JS SDK tutorial</a> and the <a href="https://www.contentful.com/developers/docs/references/content-delivery-api/#/reference/search-parameters">REST API reference</a> for more details.
996 * @return Promise for a collection of Team Space Memberships
997 * @example ```javascript
998 * const contentful = require('contentful-management')
999 *
1000 * client.getSpace('<space_id>')
1001 * .then((space) => space.getTeamSpaceMemberships())
1002 * .then((response) => console.log(response.items))
1003 * .catch(console.error)
1004 * ```
1005 */
1006 getTeamSpaceMemberships(query = {}) {
1007 const raw = this.toPlainObject();
1008 return makeRequest({
1009 entityType: 'TeamSpaceMembership',
1010 action: 'getMany',
1011 params: {
1012 spaceId: raw.sys.id,
1013 query: createRequestConfig({
1014 query: query
1015 }).params
1016 }
1017 }).then(data => wrapTeamSpaceMembershipCollection(makeRequest, data));
1018 },
1019 /**
1020 * Creates a Team Space Membership
1021 * @param id - Team ID
1022 * @param data - Object representation of the Team Space Membership to be created
1023 * @return Promise for the newly created Team Space Membership
1024 * @example ```javascript
1025 * const contentful = require('contentful-management')
1026 *
1027 * const client = contentful.createClient({
1028 * accessToken: '<content_management_api_key>'
1029 * })
1030 *
1031 * client.getSpace('<space_id>')
1032 * .then((space) => space.createTeamSpaceMembership('team_id', {
1033 * admin: false,
1034 * roles: [
1035 * {
1036 sys: {
1037 * type: 'Link',
1038 * linkType: 'Role',
1039 * id: '<role_id>'
1040 * }
1041 * }
1042 * ],
1043 * }))
1044 * .then((teamSpaceMembership) => console.log(teamSpaceMembership))
1045 * .catch(console.error)
1046 * ```
1047 */
1048 createTeamSpaceMembership(teamId, data) {
1049 const raw = this.toPlainObject();
1050 return makeRequest({
1051 entityType: 'TeamSpaceMembership',
1052 action: 'create',
1053 params: {
1054 spaceId: raw.sys.id,
1055 teamId
1056 },
1057 payload: data
1058 }).then(response => wrapTeamSpaceMembership(makeRequest, response));
1059 },
1060 /**
1061 * Gets a Api Key
1062 * @param id - API Key ID
1063 * @return Promise for a Api Key
1064 * @example ```javascript
1065 * const contentful = require('contentful-management')
1066 *
1067 * const client = contentful.createClient({
1068 * accessToken: '<content_management_api_key>'
1069 * })
1070 *
1071 * client.getSpace('<space_id>')
1072 * .then((space) => space.getApiKey('<apikey-id>'))
1073 * .then((apikey) => console.log(apikey))
1074 * .catch(console.error)
1075 * ```
1076 */
1077 getApiKey(id) {
1078 const raw = this.toPlainObject();
1079 return makeRequest({
1080 entityType: 'ApiKey',
1081 action: 'get',
1082 params: {
1083 spaceId: raw.sys.id,
1084 apiKeyId: id
1085 }
1086 }).then(data => wrapApiKey(makeRequest, data));
1087 },
1088 /**
1089 * Gets a collection of Api Keys
1090 * @return Promise for a collection of Api Keys
1091 * @example ```javascript
1092 * const contentful = require('contentful-management')
1093 *
1094 * const client = contentful.createClient({
1095 * accessToken: '<content_management_api_key>'
1096 * })
1097 *
1098 * client.getSpace('<space_id>')
1099 * .then((space) => space.getApiKeys())
1100 * .then((response) => console.log(response.items))
1101 * .catch(console.error)
1102 * ```
1103 */
1104 getApiKeys() {
1105 const raw = this.toPlainObject();
1106 return makeRequest({
1107 entityType: 'ApiKey',
1108 action: 'getMany',
1109 params: {
1110 spaceId: raw.sys.id
1111 }
1112 }).then(data => wrapApiKeyCollection(makeRequest, data));
1113 },
1114 /**
1115 * Gets a collection of preview Api Keys
1116 * @return Promise for a collection of Preview Api Keys
1117 * @example ```javascript
1118 * const contentful = require('contentful-management')
1119 *
1120 * const client = contentful.createClient({
1121 * accessToken: '<content_management_api_key>'
1122 * })
1123 *
1124 * client.getSpace('<space_id>')
1125 * .then((space) => space.getPreviewApiKeys())
1126 * .then((response) => console.log(response.items))
1127 * .catch(console.error)
1128 * ```
1129 */
1130 getPreviewApiKeys() {
1131 const raw = this.toPlainObject();
1132 return makeRequest({
1133 entityType: 'PreviewApiKey',
1134 action: 'getMany',
1135 params: {
1136 spaceId: raw.sys.id
1137 }
1138 }).then(data => wrapPreviewApiKeyCollection(makeRequest, data));
1139 },
1140 /**
1141 * Gets a preview Api Key
1142 * @param id - Preview API Key ID
1143 * @return Promise for a Preview Api Key
1144 * @example ```javascript
1145 * const contentful = require('contentful-management')
1146 *
1147 * const client = contentful.createClient({
1148 * accessToken: '<content_management_api_key>'
1149 * })
1150 *
1151 * client.getSpace('<space_id>')
1152 * .then((space) => space.getPreviewApiKey('<preview-apikey-id>'))
1153 * .then((previewApikey) => console.log(previewApikey))
1154 * .catch(console.error)
1155 * ```
1156 */
1157 getPreviewApiKey(id) {
1158 const raw = this.toPlainObject();
1159 return makeRequest({
1160 entityType: 'PreviewApiKey',
1161 action: 'get',
1162 params: {
1163 spaceId: raw.sys.id,
1164 previewApiKeyId: id
1165 }
1166 }).then(data => wrapPreviewApiKey(makeRequest, data));
1167 },
1168 /**
1169 * Creates a Api Key
1170 * @param payload - Object representation of the Api Key to be created
1171 * @return Promise for the newly created Api Key
1172 * @example ```javascript
1173 * const contentful = require('contentful-management')
1174 *
1175 * const client = contentful.createClient({
1176 * accessToken: '<content_management_api_key>'
1177 * })
1178 *
1179 * client.getSpace('<space_id>')
1180 * .then((space) => space.createApiKey({
1181 * name: 'API Key name',
1182 * environments:[
1183 * {
1184 * sys: {
1185 * type: 'Link'
1186 * linkType: 'Environment',
1187 * id:'<environment_id>'
1188 * }
1189 * }
1190 * ]
1191 * }
1192 * }))
1193 * .then((apiKey) => console.log(apiKey))
1194 * .catch(console.error)
1195 * ```
1196 */
1197 createApiKey: function createApiKey(payload) {
1198 const raw = this.toPlainObject();
1199 return makeRequest({
1200 entityType: 'ApiKey',
1201 action: 'create',
1202 params: {
1203 spaceId: raw.sys.id
1204 },
1205 payload
1206 }).then(data => wrapApiKey(makeRequest, data));
1207 },
1208 /**
1209 * Creates a Api Key with a custom ID
1210 * @param id - Api Key ID
1211 * @param payload - Object representation of the Api Key to be created
1212 * @return Promise for the newly created Api Key
1213 * @example ```javascript
1214 * const contentful = require('contentful-management')
1215 *
1216 * const client = contentful.createClient({
1217 * accessToken: '<content_management_api_key>'
1218 * })
1219 *
1220 * client.getSpace('<space_id>')
1221 * .then((space) => space.createApiKeyWithId('<api-key-id>', {
1222 * name: 'API Key name'
1223 * environments:[
1224 * {
1225 * sys: {
1226 * type: 'Link'
1227 * linkType: 'Environment',
1228 * id:'<environment_id>'
1229 * }
1230 * }
1231 * ]
1232 * }
1233 * }))
1234 * .then((apiKey) => console.log(apiKey))
1235 * .catch(console.error)
1236 * ```
1237 */
1238 createApiKeyWithId(id, payload) {
1239 const raw = this.toPlainObject();
1240 return makeRequest({
1241 entityType: 'ApiKey',
1242 action: 'createWithId',
1243 params: {
1244 spaceId: raw.sys.id,
1245 apiKeyId: id
1246 },
1247 payload
1248 }).then(data => wrapApiKey(makeRequest, data));
1249 },
1250 /**
1251 * Creates an EnvironmentAlias with a custom ID
1252 * @param environmentAliasId - EnvironmentAlias ID
1253 * @param data - Object representation of the EnvironmentAlias to be created
1254 * @return Promise for the newly created EnvironmentAlias
1255 * @example ```javascript
1256 * const contentful = require('contentful-management')
1257 *
1258 * const client = contentful.createClient({
1259 * accessToken: '<content_management_api_key>'
1260 * })
1261 *
1262 * client.getSpace('<space_id>')
1263 * .then((space) => space.createEnvironmentAliasWithId('<environment-alias-id>', {
1264 * environment: {
1265 * sys: { type: 'Link', linkType: 'Environment', id: 'targetEnvironment' }
1266 * }
1267 * }))
1268 * .then((environmentAlias) => console.log(environmentAlias))
1269 * .catch(console.error)
1270 * ```
1271 */
1272 createEnvironmentAliasWithId(environmentAliasId, data) {
1273 const raw = this.toPlainObject();
1274 return makeRequest({
1275 entityType: 'EnvironmentAlias',
1276 action: 'createWithId',
1277 params: {
1278 spaceId: raw.sys.id,
1279 environmentAliasId
1280 },
1281 payload: data
1282 }).then(response => wrapEnvironmentAlias(makeRequest, response));
1283 },
1284 /**
1285 * Gets an Environment Alias
1286 * @param Environment Alias ID
1287 * @return Promise for an Environment Alias
1288 * @example ```javascript
1289 * const contentful = require('contentful-management')
1290 *
1291 * const client = contentful.createClient({
1292 * accessToken: '<content_management_api_key>'
1293 * })
1294 *
1295 * client.getSpace('<space_id>')
1296 * .then((space) => space.getEnvironmentAlias('<alias-id>'))
1297 * .then((alias) => console.log(alias))
1298 * .catch(console.error)
1299 * ```
1300 */
1301 getEnvironmentAlias(environmentAliasId) {
1302 const raw = this.toPlainObject();
1303 return makeRequest({
1304 entityType: 'EnvironmentAlias',
1305 action: 'get',
1306 params: {
1307 spaceId: raw.sys.id,
1308 environmentAliasId
1309 }
1310 }).then(data => wrapEnvironmentAlias(makeRequest, data));
1311 },
1312 /**
1313 * Gets a collection of Environment Aliases
1314 * @return Promise for a collection of Environment Aliases
1315 * @example ```javascript
1316 * const contentful = require('contentful-management')
1317 *
1318 * const client = contentful.createClient({
1319 * accessToken: '<content_management_api_key>'
1320 * })
1321 *
1322 * client.getSpace('<space_id>')
1323 * .then((space) => space.getEnvironmentAliases()
1324 * .then((response) => console.log(response.items))
1325 * .catch(console.error)
1326 * ```
1327 */
1328 getEnvironmentAliases() {
1329 const raw = this.toPlainObject();
1330 return makeRequest({
1331 entityType: 'EnvironmentAlias',
1332 action: 'getMany',
1333 params: {
1334 spaceId: raw.sys.id
1335 }
1336 }).then(data => wrapEnvironmentAliasCollection(makeRequest, data));
1337 },
1338 /**
1339 * Query for scheduled actions in space.
1340 * @param query - Object with search parameters. The enviroment id field is mandatory. Check the <a href="https://www.contentful.com/developers/docs/references/content-management-api/#/reference/scheduled-actions/scheduled-actions-collection">REST API reference</a> for more details.
1341 * @return Promise for the scheduled actions query
1342 *
1343 * @example ```javascript
1344 * const contentful = require('contentful-management');
1345 *
1346 * const client = contentful.createClient({
1347 * accessToken: '<content_management_api_key>'
1348 * })
1349 *
1350 * client.getSpace('<space_id>')
1351 * .then((space) => space.getScheduledActions({
1352 * 'environment.sys.id': '<environment_id>',
1353 * 'sys.status': 'scheduled'
1354 * }))
1355 * .then((scheduledActionCollection) => console.log(scheduledActionCollection.items))
1356 * .catch(console.error)
1357 * ```
1358 */
1359 getScheduledActions(query) {
1360 const raw = this.toPlainObject();
1361 return makeRequest({
1362 entityType: 'ScheduledAction',
1363 action: 'getMany',
1364 params: {
1365 spaceId: raw.sys.id,
1366 query
1367 }
1368 }).then(response => wrapScheduledActionCollection(makeRequest, response));
1369 },
1370 /**
1371 * Get a Scheduled Action in the current space by environment and ID.
1372 *
1373 * @throws if the Scheduled Action cannot be found or the user doesn't have permission to read schedules from the entity of the scheduled action itself.
1374 * @returns Promise with the Scheduled Action
1375 * @example ```javascript
1376 * const contentful = require('contentful-management');
1377 *
1378 * const client = contentful.createClient({
1379 * accessToken: '<content_management_api_key>'
1380 * })
1381 *
1382 * client.getSpace('<space_id>')
1383 * .then((space) => space.getScheduledAction({
1384 * scheduledActionId: '<scheduled-action-id>',
1385 * environmentId: '<environmentId>'
1386 * }))
1387 * .then((scheduledAction) => console.log(scheduledAction))
1388 * .catch(console.error)
1389 * ```
1390 */
1391 getScheduledAction({
1392 scheduledActionId,
1393 environmentId
1394 }) {
1395 const space = this.toPlainObject();
1396 return makeRequest({
1397 entityType: 'ScheduledAction',
1398 action: 'get',
1399 params: {
1400 spaceId: space.sys.id,
1401 environmentId,
1402 scheduledActionId
1403 }
1404 }).then(scheduledAction => wrapScheduledAction(makeRequest, scheduledAction));
1405 },
1406 /**
1407 * Creates a scheduled action
1408 * @param data - Object representation of the scheduled action to be created
1409 * @return Promise for the newly created scheduled actions
1410 * @example ```javascript
1411 * const contentful = require('contentful-management');
1412 *
1413 * const client = contentful.createClient({
1414 * accessToken: '<content_management_api_key>'
1415 * })
1416 *
1417 * client.getSpace('<space_id>')
1418 * .then((space) => space.createScheduledAction({
1419 * entity: {
1420 * sys: {
1421 * type: 'Link',
1422 * linkType: 'Entry',
1423 * id: '<entry_id>'
1424 * }
1425 * },
1426 * environment: {
1427 * sys: {
1428 * type: 'Link',
1429 * linkType: 'Environment',
1430 * id: '<environment_id>'
1431 * }
1432 * },
1433 * action: 'publish',
1434 * scheduledFor: {
1435 * datetime: <ISO_date_string>,
1436 * timezone: 'Europe/Berlin'
1437 * }
1438 * }))
1439 * .then((scheduledAction) => console.log(scheduledAction))
1440 * .catch(console.error)
1441 * ```
1442 */
1443 createScheduledAction(data) {
1444 const raw = this.toPlainObject();
1445 return makeRequest({
1446 entityType: 'ScheduledAction',
1447 action: 'create',
1448 params: {
1449 spaceId: raw.sys.id
1450 },
1451 payload: data
1452 }).then(response => wrapScheduledAction(makeRequest, response));
1453 },
1454 /**
1455 * Update a scheduled action
1456 * @param {object} options
1457 * @param options.scheduledActionId the id of the scheduled action to update
1458 * @param options.version the sys.version of the scheduled action to be updated
1459 * @param payload the scheduled actions object with updates, omitting sys object
1460 * @returns Promise containing a wrapped scheduled action with helper methods
1461 * @example ```javascript
1462 * const contentful = require('contentful-management');
1463 *
1464 * const client = contentful.createClient({
1465 * accessToken: '<content_management_api_key>'
1466 * })
1467 *
1468 * client.getSpace('<space_id>')
1469 * .then((space) => {
1470 * return space.createScheduledAction({
1471 * entity: {
1472 * sys: {
1473 * type: 'Link',
1474 * linkType: 'Entry',
1475 * id: '<entry_id>'
1476 * }
1477 * },
1478 * environment: {
1479 * sys: {
1480 * type: 'Link',
1481 * linkType: 'Environment',
1482 * id: '<environment_id>'
1483 * }
1484 * },
1485 * action: 'publish',
1486 * scheduledFor: {
1487 * datetime: <ISO_date_string>,
1488 * timezone: 'Europe/Berlin'
1489 * }
1490 * })
1491 * .then((scheduledAction) => {
1492 * const { _sys, ...payload } = scheduledAction;
1493 * return space.updateScheduledAction({
1494 * ...payload,
1495 * scheduledFor: {
1496 * ...payload.scheduledFor,
1497 * timezone: 'Europe/Paris'
1498 * }
1499 * })
1500 * })
1501 * .then((scheduledAction) => console.log(scheduledAction))
1502 * .catch(console.error);
1503 * ```
1504 */
1505 updateScheduledAction({
1506 scheduledActionId,
1507 payload,
1508 version
1509 }) {
1510 const spaceProps = this.toPlainObject();
1511 return makeRequest({
1512 entityType: 'ScheduledAction',
1513 action: 'update',
1514 params: {
1515 spaceId: spaceProps.sys.id,
1516 version,
1517 scheduledActionId
1518 },
1519 payload
1520 }).then(response => wrapScheduledAction(makeRequest, response));
1521 },
1522 /**
1523 * Cancels a Scheduled Action.
1524 * Only cancels actions that have not yet executed.
1525 *
1526 * @param {object} options
1527 * @param options.scheduledActionId the id of the scheduled action to be canceled
1528 * @param options.environmentId the environment ID of the scheduled action to be canceled
1529 * @throws if the Scheduled Action cannot be found or the user doesn't have permissions in the entity in the action.
1530 * @returns Promise containing a wrapped Scheduled Action with helper methods
1531 * @example ```javascript
1532 * const contentful = require('contentful-management');
1533 *
1534 * const client = contentful.createClient({
1535 * accessToken: '<content_management_api_key>'
1536 * })
1537 *
1538 * // Given that an Scheduled Action is scheduled
1539 * client.getSpace('<space_id>')
1540 * .then((space) => space.deleteScheduledAction({
1541 * environmentId: '<environment-id>',
1542 * scheduledActionId: '<scheduled-action-id>'
1543 * }))
1544 * // The scheduled Action sys.status is now 'canceled'
1545 * .then((scheduledAction) => console.log(scheduledAction))
1546 * .catch(console.error);
1547 * ```
1548 */
1549 deleteScheduledAction({
1550 scheduledActionId,
1551 environmentId
1552 }) {
1553 const spaceProps = this.toPlainObject();
1554 return makeRequest({
1555 entityType: 'ScheduledAction',
1556 action: 'delete',
1557 params: {
1558 spaceId: spaceProps.sys.id,
1559 environmentId,
1560 scheduledActionId
1561 }
1562 }).then(response => wrapScheduledAction(makeRequest, response));
1563 }
1564 };
1565}
\No newline at end of file