UNPKG

contentful-management

Version:
1,565 lines (1,562 loc) 49.5 kB
/** * Contentful Space API. Contains methods to access any operations at a space * level, such as creating and reading entities contained in a space. */ import { createRequestConfig } from 'contentful-sdk-core'; import entities from './entities'; /** * @private */ /** * Creates API object with methods to access the Space API * @param {MakeRequest} makeRequest - function to make requests via an adapter * @return {ContentfulSpaceAPI} * @private */ export default function createSpaceApi(makeRequest) { const { wrapSpace } = entities.space; const { wrapEnvironment, wrapEnvironmentCollection } = entities.environment; const { wrapWebhook, wrapWebhookCollection } = entities.webhook; const { wrapRole, wrapRoleCollection } = entities.role; const { wrapUser, wrapUserCollection } = entities.user; const { wrapSpaceMember, wrapSpaceMemberCollection } = entities.spaceMember; const { wrapSpaceMembership, wrapSpaceMembershipCollection } = entities.spaceMembership; const { wrapTeamSpaceMembership, wrapTeamSpaceMembershipCollection } = entities.teamSpaceMembership; const { wrapTeamCollection } = entities.team; const { wrapApiKey, wrapApiKeyCollection } = entities.apiKey; const { wrapEnvironmentAlias, wrapEnvironmentAliasCollection } = entities.environmentAlias; const { wrapPreviewApiKey, wrapPreviewApiKeyCollection } = entities.previewApiKey; const { wrapScheduledAction, wrapScheduledActionCollection } = entities.scheduledAction; return { /** * Deletes the space * @return Promise for the deletion. It contains no data, but the Promise error case should be handled. * @example ```javascript * const contentful = require('contentful-management') * * const client = contentful.createClient({ * accessToken: '<content_management_api_key>' * }) * * client.getSpace('<space_id>') * .then((space) => space.delete()) * .then(() => console.log('Space deleted.')) * .catch(console.error) * ``` */ delete: function deleteSpace() { const raw = this.toPlainObject(); return makeRequest({ entityType: 'Space', action: 'delete', params: { spaceId: raw.sys.id } }); }, /** * Updates the space * @return Promise for the updated space. * @example ```javascript * const contentful = require('contentful-management') * * const client = contentful.createClient({ * accessToken: '<content_management_api_key>' * }) * * client.getSpace('<space_id>') * .then((space) => { * space.name = 'New name' * return space.update() * }) * .then((space) => console.log(`Space ${space.sys.id} renamed.`) * .catch(console.error) * ``` */ update: function updateSpace() { const raw = this.toPlainObject(); return makeRequest({ entityType: 'Space', action: 'update', params: { spaceId: raw.sys.id }, payload: raw, headers: {} }).then(data => wrapSpace(makeRequest, data)); }, /** * Gets an environment * @param id - Environment ID * @return Promise for an Environment * @example ```javascript * const contentful = require('contentful-management') * * const client = contentful.createClient({ * accessToken: '<content_management_api_key>' * }) * * client.getSpace('<space_id>') * .then((space) => space.getEnvironment('<environment_id>')) * .then((environment) => console.log(environment)) * .catch(console.error) * ``` */ getEnvironment(environmentId) { const raw = this.toPlainObject(); return makeRequest({ entityType: 'Environment', action: 'get', params: { spaceId: raw.sys.id, environmentId } }).then(data => wrapEnvironment(makeRequest, data)); }, /** * Gets a collection of Environments * @return Promise for a collection of Environment * @example ```javascript * const contentful = require('contentful-management') * * const client = contentful.createClient({ * accessToken: '<content_management_api_key>' * }) * * client.getSpace('<space_id>') * .then((space) => space.getEnvironments()) * .then((response) => console.log(response.items)) * .catch(console.error) * ``` */ getEnvironments(query = {}) { const raw = this.toPlainObject(); return makeRequest({ entityType: 'Environment', action: 'getMany', params: { spaceId: raw.sys.id, query } }).then(data => wrapEnvironmentCollection(makeRequest, data)); }, /** * Creates an environment * @param data - Object representation of the Environment to be created * @return Promise for the newly created Environment * @example ```javascript * const contentful = require('contentful-management') * * const client = contentful.createClient({ * accessToken: '<content_management_api_key>' * }) * * client.getSpace('<space_id>') * .then((space) => space.createEnvironment({ name: 'Staging' })) * .then((environment) => console.log(environment)) * .catch(console.error) * ``` */ createEnvironment(data = {}) { const raw = this.toPlainObject(); return makeRequest({ entityType: 'Environment', action: 'create', params: { spaceId: raw.sys.id }, payload: data }).then(response => wrapEnvironment(makeRequest, response)); }, /** * Creates an Environment with a custom ID * @param id - Environment ID * @param data - Object representation of the Environment to be created * @param sourceEnvironmentId - ID of the source environment that will be copied to create the new environment. Default is "master" * @return Promise for the newly created Environment * @example ```javascript * const contentful = require('contentful-management') * * const client = contentful.createClient({ * accessToken: '<content_management_api_key>' * }) * * client.getSpace('<space_id>') * .then((space) => space.createEnvironmentWithId('<environment-id>', { name: 'Staging'}, 'master')) * .then((environment) => console.log(environment)) * .catch(console.error) * ``` */ createEnvironmentWithId(id, data, sourceEnvironmentId) { const raw = this.toPlainObject(); return makeRequest({ entityType: 'Environment', action: 'createWithId', params: { spaceId: raw.sys.id, environmentId: id, sourceEnvironmentId }, payload: data }).then(response => wrapEnvironment(makeRequest, response)); }, /** * Gets a Webhook * @param id - Webhook ID * @return Promise for a Webhook * @example ```javascript * const contentful = require('contentful-management') * * const client = contentful.createClient({ * accessToken: '<content_management_api_key>' * }) * * client.getSpace('<space_id>') * .then((space) => space.getWebhook('<webhook_id>')) * .then((webhook) => console.log(webhook)) * .catch(console.error) * ``` */ getWebhook(id) { const raw = this.toPlainObject(); return makeRequest({ entityType: 'Webhook', action: 'get', params: { spaceId: raw.sys.id, webhookDefinitionId: id } }).then(data => wrapWebhook(makeRequest, data)); }, /** * Gets a collection of Webhooks * @return Promise for a collection of Webhooks * @example ```javascript * const contentful = require('contentful-management') * * const client = contentful.createClient({ * accessToken: '<content_management_api_key>' * }) * * client.getSpace('<space_id>') * .then((space) => space.getWebhooks()) * .then((response) => console.log(response.items)) * .catch(console.error) * ``` */ getWebhooks() { const raw = this.toPlainObject(); return makeRequest({ entityType: 'Webhook', action: 'getMany', params: { spaceId: raw.sys.id } }).then(data => wrapWebhookCollection(makeRequest, data)); }, /** * Fetch a webhook signing secret * @returns Promise for the redacted webhook signing secret in this space * @example ```javascript * const contentful = require('contentful-management') * * const client = contentful.createClient({ * accessToken: '<content_management_api_key>' * }) * * client.getSpace('<space_id>') * .then((space) => space.getWebhookSigningSecret()) * .then((response) => console.log(response.redactedValue)) * .catch(console.error) * ``` */ getWebhookSigningSecret: function getSigningSecret() { const raw = this.toPlainObject(); return makeRequest({ entityType: 'Webhook', action: 'getSigningSecret', params: { spaceId: raw.sys.id } }); }, /** * Fetch a webhook retry policy * @returns Promise for the redacted webhook retry policy in this space * @example ```javascript * const contentful = require('contentful-management') * * const client = contentful.createClient({ * accessToken: '<content_management_api_key>' * }) * * client.getSpace('<space_id>') * .then((space) => space.getRetryPolicy()) * .then((response) => console.log(response.redactedValue)) * .catch(console.error) * ``` */ getWebhookRetryPolicy: function getWebhookRetryPolicy() { const raw = this.toPlainObject(); return makeRequest({ entityType: 'Webhook', action: 'getRetryPolicy', params: { spaceId: raw.sys.id } }); }, /** * Creates a Webhook * @param data - Object representation of the Webhook to be created * @return Promise for the newly created Webhook * @example ```javascript * const contentful = require('contentful-management') * * client.getSpace('<space_id>') * .then((space) => space.createWebhook({ * 'name': 'My webhook', * 'url': 'https://www.example.com/test', * 'topics': [ * 'Entry.create', * 'ContentType.create', * '*.publish', * 'Asset.*' * ] * })) * .then((webhook) => console.log(webhook)) * .catch(console.error) * ``` */ createWebhook(data) { const raw = this.toPlainObject(); return makeRequest({ entityType: 'Webhook', action: 'create', params: { spaceId: raw.sys.id }, payload: data }).then(data => wrapWebhook(makeRequest, data)); }, /** * Creates a Webhook with a custom ID * @param id - Webhook ID * @param data - Object representation of the Webhook to be created * @return Promise for the newly created Webhook * @example ```javascript * const contentful = require('contentful-management') * * client.getSpace('<space_id>') * .then((space) => space.createWebhookWithId('<webhook_id>', { * 'name': 'My webhook', * 'url': 'https://www.example.com/test', * 'topics': [ * 'Entry.create', * 'ContentType.create', * '*.publish', * 'Asset.*' * ] * })) * .then((webhook) => console.log(webhook)) * .catch(console.error) * ``` */ createWebhookWithId(id, data) { const raw = this.toPlainObject(); return makeRequest({ entityType: 'Webhook', action: 'createWithId', params: { spaceId: raw.sys.id, webhookDefinitionId: id }, payload: data }).then(data => wrapWebhook(makeRequest, data)); }, /** * Create or update the webhook signing secret for this space * @param data 64 character string that will be used to sign the webhook calls * @returns Promise for the redacted webhook signing secret that was created or updated * @example ```javascript * const contentful = require('contentful-management') * const crypto = require('crypto') * * const client = contentful.createClient({ * accessToken: '<content_management_api_key>' * }) * * const signingSecret = client.getSpace('<space_id>') * .then((space) => space.upsertWebhookSigningSecret({ * value: crypto.randomBytes(32).toString('hex') * })) * .then((response) => console.log(response.redactedValue)) * .catch(console.error) * ``` */ upsertWebhookSigningSecret: function getSigningSecret(data) { const raw = this.toPlainObject(); return makeRequest({ entityType: 'Webhook', action: 'upsertSigningSecret', params: { spaceId: raw.sys.id }, payload: data }); }, /** * Create or update the webhook retry policy for this space * @param data the maxRetries with integer value >= 2 and <= 99 value to set in the Retry Policy * @returns Promise for the redacted webhook retry policy that was created or updated * @example ```javascript * const contentful = require('contentful-management') * * const client = contentful.createClient({ * accessToken: '<content_management_api_key>' * }) * * const retryPolicy = client.getSpace('<space_id>') * .then((space) => space.upsertWebhookRetryPolicy({ * maxRetries: 15 * })) * .then((response) => console.log(response.redactedValue)) * .catch(console.error) * ``` */ upsertWebhookRetryPolicy: function upsertWebhookRetryPolicy(data) { const raw = this.toPlainObject(); return makeRequest({ entityType: 'Webhook', action: 'upsertRetryPolicy', params: { spaceId: raw.sys.id }, payload: data }); }, /** * Delete the webhook signing secret for this space * @returns Promise<void> * @example ```javascript * const contentful = require('contentful-management') * * const client = contentful.createClient({ * accessToken: '<content_management_api_key>' * }) * * client.getSpace('<space_id>') * .then((space) => space.deleteWebhookSigningSecret()) * .then(() => console.log("success")) * .catch(console.error) * ``` */ deleteWebhookSigningSecret: function getSigningSecret() { const raw = this.toPlainObject(); return makeRequest({ entityType: 'Webhook', action: 'deleteSigningSecret', params: { spaceId: raw.sys.id } }); }, /** * Delete the webhook retry policy for this space * @returns Promise<void> * @example ```javascript * const contentful = require('contentful-management') * * const client = contentful.createClient({ * accessToken: '<content_management_api_key>' * }) * * client.getSpace('<space_id>') * .then((space) => space.deleteWebhookRetryPolicy()) * .then(() => console.log("success")) * .catch(console.error) * ``` */ deleteWebhookRetryPolicy: function deleteRetryPolicy() { const raw = this.toPlainObject(); return makeRequest({ entityType: 'Webhook', action: 'deleteRetryPolicy', params: { spaceId: raw.sys.id } }); }, /** * Gets a Role * @param id - Role ID * @return Promise for a Role * @example ```javascript * const contentful = require('contentful-management') * * const client = contentful.createClient({ * accessToken: '<content_management_api_key>' * }) * * client.getSpace('<space_id>') * .then((space) => space.createRole({ * fields: { * title: { * 'en-US': 'Role title' * } * } * })) * .then((role) => console.log(role)) * .catch(console.error) * ``` */ getRole(id) { const raw = this.toPlainObject(); return makeRequest({ entityType: 'Role', action: 'get', params: { spaceId: raw.sys.id, roleId: id } }).then(data => wrapRole(makeRequest, data)); }, /** * Gets a collection of Roles * @return Promise for a collection of Roles * @example ```javascript * const contentful = require('contentful-management') * * const client = contentful.createClient({ * accessToken: '<content_management_api_key>' * }) * * client.getSpace('<space_id>') * .then((space) => space.getRoles()) * .then((response) => console.log(response.items)) * .catch(console.error) * ``` */ getRoles(query = {}) { const raw = this.toPlainObject(); return makeRequest({ entityType: 'Role', action: 'getMany', params: { spaceId: raw.sys.id, query: createRequestConfig({ query }).params } }).then(data => wrapRoleCollection(makeRequest, data)); }, /** * Creates a Role * @param data - Object representation of the Role to be created * @return Promise for the newly created Role * @example ```javascript * const contentful = require('contentful-management') * * const client = contentful.createClient({ * accessToken: '<content_management_api_key>' * }) * client.getSpace('<space_id>') * .then((space) => space.createRole({ * name: 'My Role', * description: 'foobar role', * permissions: { * ContentDelivery: 'all', * ContentModel: ['read'], * Settings: [] * }, * policies: [ * { * effect: 'allow', * actions: 'all', * constraint: { * and: [ * { * equals: [ * { doc: 'sys.type' }, * 'Entry' * ] * }, * { * equals: [ * { doc: 'sys.type' }, * 'Asset' * ] * } * ] * } * } * ] * })) * .then((role) => console.log(role)) * .catch(console.error) * ``` */ createRole(data) { const raw = this.toPlainObject(); return makeRequest({ entityType: 'Role', action: 'create', params: { spaceId: raw.sys.id }, payload: data }).then(data => wrapRole(makeRequest, data)); }, /** * Creates a Role with a custom ID * @param id - Role ID * @param data - Object representation of the Role to be created * @return Promise for the newly created Role * @example ```javascript * const contentful = require('contentful-management') * * const client = contentful.createClient({ * accessToken: '<content_management_api_key>' * }) * client.getSpace('<space_id>') * .then((space) => space.createRoleWithId('<role-id>', { * name: 'My Role', * description: 'foobar role', * permissions: { * ContentDelivery: 'all', * ContentModel: ['read'], * Settings: [] * }, * policies: [ * { * effect: 'allow', * actions: 'all', * constraint: { * and: [ * { * equals: [ * { doc: 'sys.type' }, * 'Entry' * ] * }, * { * equals: [ * { doc: 'sys.type' }, * 'Asset' * ] * } * ] * } * } * ] * })) * .then((role) => console.log(role)) * .catch(console.error) * ``` */ createRoleWithId(id, roleData) { const raw = this.toPlainObject(); return makeRequest({ entityType: 'Role', action: 'createWithId', params: { spaceId: raw.sys.id, roleId: id }, payload: roleData }).then(data => wrapRole(makeRequest, data)); }, /** * Gets a User * @param userId - User ID * @return Promise for a User * @example ```javascript * const contentful = require('contentful-management') * * client.getSpace('<space_id>') * .then((space) => space.getSpaceUser('id')) * .then((user) => console.log(user)) * .catch(console.error) * ``` */ getSpaceUser(userId) { const raw = this.toPlainObject(); return makeRequest({ entityType: 'User', action: 'getForSpace', params: { spaceId: raw.sys.id, userId } }).then(data => wrapUser(makeRequest, data)); }, /** * Gets a collection of Users in a space * @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. * @return Promise a collection of Users in a space * @example ```javascript * const contentful = require('contentful-management') * * client.getSpace('<space_id>') * .then((space) => space.getSpaceUsers(query)) * .then((data) => console.log(data)) * .catch(console.error) * ``` */ getSpaceUsers(query = {}) { const raw = this.toPlainObject(); return makeRequest({ entityType: 'User', action: 'getManyForSpace', params: { spaceId: raw.sys.id, query: createRequestConfig({ query }).params } }).then(data => wrapUserCollection(makeRequest, data)); }, /** * Gets a collection of teams for a space * @param query * @return Promise for a collection of teams for a space * @example ```javascript * const contentful = require('contentful-management') * * client.getSpace('<space_id>') * .then((space) => space.getTeams()) * .then((teamsCollection) => console.log(teamsCollection)) * .catch(console.error) * ``` */ getTeams(query = { limit: 100 }) { const raw = this.toPlainObject(); return makeRequest({ entityType: 'Team', action: 'getManyForSpace', params: { spaceId: raw.sys.id, query: createRequestConfig({ query }).params } }).then(data => wrapTeamCollection(makeRequest, data)); }, /** * Gets a Space Member * @param id Get Space Member by user_id * @return Promise for a Space Member * @example ```javascript * const contentful = require('contentful-management') * * client.getSpace('<space_id>') * .then((space) => space.getSpaceMember(id)) * .then((spaceMember) => console.log(spaceMember)) * .catch(console.error) * ``` */ getSpaceMember(id) { const raw = this.toPlainObject(); return makeRequest({ entityType: 'SpaceMember', action: 'get', params: { spaceId: raw.sys.id, spaceMemberId: id } }).then(data => wrapSpaceMember(makeRequest, data)); }, /** * Gets a collection of Space Members * @param query * @return Promise for a collection of Space Members * @example ```javascript * const contentful = require('contentful-management') * * client.getSpace('<space_id>') * .then((space) => space.getSpaceMembers({'limit': 100})) * .then((spaceMemberCollection) => console.log(spaceMemberCollection)) * .catch(console.error) * ``` */ getSpaceMembers(query = {}) { const raw = this.toPlainObject(); return makeRequest({ entityType: 'SpaceMember', action: 'getMany', params: { spaceId: raw.sys.id, query: createRequestConfig({ query }).params } }).then(data => wrapSpaceMemberCollection(makeRequest, data)); }, /** * Gets a Space Membership * Warning: the user attribute in the space membership root is deprecated. The attribute has been moved inside the sys object (i.e. sys.user). * @param id - Space Membership ID * @return Promise for a Space Membership * @example ```javascript * const contentful = require('contentful-management') * * client.getSpace('<space_id>') * .then((space) => space.getSpaceMembership('id')) * .then((spaceMembership) => console.log(spaceMembership)) * .catch(console.error) * ``` */ getSpaceMembership(id) { const raw = this.toPlainObject(); return makeRequest({ entityType: 'SpaceMembership', action: 'get', params: { spaceId: raw.sys.id, spaceMembershipId: id } }).then(data => wrapSpaceMembership(makeRequest, data)); }, /** * Gets a collection of Space Memberships * Warning: the user attribute in the space membership root is deprecated. The attribute has been moved inside the sys object (i.e. sys.user). * @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. * @return Promise for a collection of Space Memberships * @example ```javascript * const contentful = require('contentful-management') * * client.getSpace('<space_id>') * .then((space) => space.getSpaceMemberships({'limit': 100})) // you can add more queries as 'key': 'value' * .then((response) => console.log(response.items)) * .catch(console.error) * ``` */ getSpaceMemberships(query = {}) { const raw = this.toPlainObject(); return makeRequest({ entityType: 'SpaceMembership', action: 'getMany', params: { spaceId: raw.sys.id, query: createRequestConfig({ query }).params } }).then(data => wrapSpaceMembershipCollection(makeRequest, data)); }, /** * Creates a Space Membership * Warning: the user attribute in the space membership root is deprecated. The attribute has been moved inside the sys object (i.e. sys.user). * @param data - Object representation of the Space Membership to be created * @return Promise for the newly created Space Membership * @example ```javascript * const contentful = require('contentful-management') * * const client = contentful.createClient({ * accessToken: '<content_management_api_key>' * }) * * client.getSpace('<space_id>') * .then((space) => space.createSpaceMembership({ * admin: false, * roles: [ * { * type: 'Link', * linkType: 'Role', * id: '<role_id>' * } * ], * email: 'foo@example.com' * })) * .then((spaceMembership) => console.log(spaceMembership)) * .catch(console.error) * ``` */ createSpaceMembership(data) { const raw = this.toPlainObject(); return makeRequest({ entityType: 'SpaceMembership', action: 'create', params: { spaceId: raw.sys.id }, payload: data }).then(response => wrapSpaceMembership(makeRequest, response)); }, /** * Creates a Space Membership with a custom ID * Warning: the user attribute in the space membership root is deprecated. The attribute has been moved inside the sys object (i.e. sys.user). * @param id - Space Membership ID * @param data - Object representation of the Space Membership to be created * @return Promise for the newly created Space Membership * @example ```javascript * const contentful = require('contentful-management') * * const client = contentful.createClient({ * accessToken: '<content_management_api_key>' * }) * * client.getSpace('<space_id>') * .then((space) => space.createSpaceMembershipWithId('<space-membership-id>', { * admin: false, * roles: [ * { * type: 'Link', * linkType: 'Role', * id: '<role_id>' * } * ], * email: 'foo@example.com' * })) * .then((spaceMembership) => console.log(spaceMembership)) * .catch(console.error) * ``` */ createSpaceMembershipWithId(id, data) { const raw = this.toPlainObject(); return makeRequest({ entityType: 'SpaceMembership', action: 'createWithId', params: { spaceId: raw.sys.id, spaceMembershipId: id }, payload: data }).then(response => wrapSpaceMembership(makeRequest, response)); }, /** * Gets a Team Space Membership * @param id - Team Space Membership ID * @return Promise for a Team Space Membership * @example ```javascript * const contentful = require('contentful-management') * * client.getSpace('<space_id>') * .then((space) => space.getTeamSpaceMembership('team_space_membership_id')) * .then((teamSpaceMembership) => console.log(teamSpaceMembership)) * .catch(console.error) * ``` */ getTeamSpaceMembership(teamSpaceMembershipId) { const raw = this.toPlainObject(); return makeRequest({ entityType: 'TeamSpaceMembership', action: 'get', params: { spaceId: raw.sys.id, teamSpaceMembershipId } }).then(data => wrapTeamSpaceMembership(makeRequest, data)); }, /** * Gets a collection of Team Space Memberships * @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. * @return Promise for a collection of Team Space Memberships * @example ```javascript * const contentful = require('contentful-management') * * client.getSpace('<space_id>') * .then((space) => space.getTeamSpaceMemberships()) * .then((response) => console.log(response.items)) * .catch(console.error) * ``` */ getTeamSpaceMemberships(query = {}) { const raw = this.toPlainObject(); return makeRequest({ entityType: 'TeamSpaceMembership', action: 'getMany', params: { spaceId: raw.sys.id, query: createRequestConfig({ query: query }).params } }).then(data => wrapTeamSpaceMembershipCollection(makeRequest, data)); }, /** * Creates a Team Space Membership * @param id - Team ID * @param data - Object representation of the Team Space Membership to be created * @return Promise for the newly created Team Space Membership * @example ```javascript * const contentful = require('contentful-management') * * const client = contentful.createClient({ * accessToken: '<content_management_api_key>' * }) * * client.getSpace('<space_id>') * .then((space) => space.createTeamSpaceMembership('team_id', { * admin: false, * roles: [ * { sys: { * type: 'Link', * linkType: 'Role', * id: '<role_id>' * } * } * ], * })) * .then((teamSpaceMembership) => console.log(teamSpaceMembership)) * .catch(console.error) * ``` */ createTeamSpaceMembership(teamId, data) { const raw = this.toPlainObject(); return makeRequest({ entityType: 'TeamSpaceMembership', action: 'create', params: { spaceId: raw.sys.id, teamId }, payload: data }).then(response => wrapTeamSpaceMembership(makeRequest, response)); }, /** * Gets a Api Key * @param id - API Key ID * @return Promise for a Api Key * @example ```javascript * const contentful = require('contentful-management') * * const client = contentful.createClient({ * accessToken: '<content_management_api_key>' * }) * * client.getSpace('<space_id>') * .then((space) => space.getApiKey('<apikey-id>')) * .then((apikey) => console.log(apikey)) * .catch(console.error) * ``` */ getApiKey(id) { const raw = this.toPlainObject(); return makeRequest({ entityType: 'ApiKey', action: 'get', params: { spaceId: raw.sys.id, apiKeyId: id } }).then(data => wrapApiKey(makeRequest, data)); }, /** * Gets a collection of Api Keys * @return Promise for a collection of Api Keys * @example ```javascript * const contentful = require('contentful-management') * * const client = contentful.createClient({ * accessToken: '<content_management_api_key>' * }) * * client.getSpace('<space_id>') * .then((space) => space.getApiKeys()) * .then((response) => console.log(response.items)) * .catch(console.error) * ``` */ getApiKeys() { const raw = this.toPlainObject(); return makeRequest({ entityType: 'ApiKey', action: 'getMany', params: { spaceId: raw.sys.id } }).then(data => wrapApiKeyCollection(makeRequest, data)); }, /** * Gets a collection of preview Api Keys * @return Promise for a collection of Preview Api Keys * @example ```javascript * const contentful = require('contentful-management') * * const client = contentful.createClient({ * accessToken: '<content_management_api_key>' * }) * * client.getSpace('<space_id>') * .then((space) => space.getPreviewApiKeys()) * .then((response) => console.log(response.items)) * .catch(console.error) * ``` */ getPreviewApiKeys() { const raw = this.toPlainObject(); return makeRequest({ entityType: 'PreviewApiKey', action: 'getMany', params: { spaceId: raw.sys.id } }).then(data => wrapPreviewApiKeyCollection(makeRequest, data)); }, /** * Gets a preview Api Key * @param id - Preview API Key ID * @return Promise for a Preview Api Key * @example ```javascript * const contentful = require('contentful-management') * * const client = contentful.createClient({ * accessToken: '<content_management_api_key>' * }) * * client.getSpace('<space_id>') * .then((space) => space.getPreviewApiKey('<preview-apikey-id>')) * .then((previewApikey) => console.log(previewApikey)) * .catch(console.error) * ``` */ getPreviewApiKey(id) { const raw = this.toPlainObject(); return makeRequest({ entityType: 'PreviewApiKey', action: 'get', params: { spaceId: raw.sys.id, previewApiKeyId: id } }).then(data => wrapPreviewApiKey(makeRequest, data)); }, /** * Creates a Api Key * @param payload - Object representation of the Api Key to be created * @return Promise for the newly created Api Key * @example ```javascript * const contentful = require('contentful-management') * * const client = contentful.createClient({ * accessToken: '<content_management_api_key>' * }) * * client.getSpace('<space_id>') * .then((space) => space.createApiKey({ * name: 'API Key name', * environments:[ * { * sys: { * type: 'Link' * linkType: 'Environment', * id:'<environment_id>' * } * } * ] * } * })) * .then((apiKey) => console.log(apiKey)) * .catch(console.error) * ``` */ createApiKey: function createApiKey(payload) { const raw = this.toPlainObject(); return makeRequest({ entityType: 'ApiKey', action: 'create', params: { spaceId: raw.sys.id }, payload }).then(data => wrapApiKey(makeRequest, data)); }, /** * Creates a Api Key with a custom ID * @param id - Api Key ID * @param payload - Object representation of the Api Key to be created * @return Promise for the newly created Api Key * @example ```javascript * const contentful = require('contentful-management') * * const client = contentful.createClient({ * accessToken: '<content_management_api_key>' * }) * * client.getSpace('<space_id>') * .then((space) => space.createApiKeyWithId('<api-key-id>', { * name: 'API Key name' * environments:[ * { * sys: { * type: 'Link' * linkType: 'Environment', * id:'<environment_id>' * } * } * ] * } * })) * .then((apiKey) => console.log(apiKey)) * .catch(console.error) * ``` */ createApiKeyWithId(id, payload) { const raw = this.toPlainObject(); return makeRequest({ entityType: 'ApiKey', action: 'createWithId', params: { spaceId: raw.sys.id, apiKeyId: id }, payload }).then(data => wrapApiKey(makeRequest, data)); }, /** * Creates an EnvironmentAlias with a custom ID * @param environmentAliasId - EnvironmentAlias ID * @param data - Object representation of the EnvironmentAlias to be created * @return Promise for the newly created EnvironmentAlias * @example ```javascript * const contentful = require('contentful-management') * * const client = contentful.createClient({ * accessToken: '<content_management_api_key>' * }) * * client.getSpace('<space_id>') * .then((space) => space.createEnvironmentAliasWithId('<environment-alias-id>', { * environment: { * sys: { type: 'Link', linkType: 'Environment', id: 'targetEnvironment' } * } * })) * .then((environmentAlias) => console.log(environmentAlias)) * .catch(console.error) * ``` */ createEnvironmentAliasWithId(environmentAliasId, data) { const raw = this.toPlainObject(); return makeRequest({ entityType: 'EnvironmentAlias', action: 'createWithId', params: { spaceId: raw.sys.id, environmentAliasId }, payload: data }).then(response => wrapEnvironmentAlias(makeRequest, response)); }, /** * Gets an Environment Alias * @param Environment Alias ID * @return Promise for an Environment Alias * @example ```javascript * const contentful = require('contentful-management') * * const client = contentful.createClient({ * accessToken: '<content_management_api_key>' * }) * * client.getSpace('<space_id>') * .then((space) => space.getEnvironmentAlias('<alias-id>')) * .then((alias) => console.log(alias)) * .catch(console.error) * ``` */ getEnvironmentAlias(environmentAliasId) { const raw = this.toPlainObject(); return makeRequest({ entityType: 'EnvironmentAlias', action: 'get', params: { spaceId: raw.sys.id, environmentAliasId } }).then(data => wrapEnvironmentAlias(makeRequest, data)); }, /** * Gets a collection of Environment Aliases * @return Promise for a collection of Environment Aliases * @example ```javascript * const contentful = require('contentful-management') * * const client = contentful.createClient({ * accessToken: '<content_management_api_key>' * }) * * client.getSpace('<space_id>') * .then((space) => space.getEnvironmentAliases() * .then((response) => console.log(response.items)) * .catch(console.error) * ``` */ getEnvironmentAliases() { const raw = this.toPlainObject(); return makeRequest({ entityType: 'EnvironmentAlias', action: 'getMany', params: { spaceId: raw.sys.id } }).then(data => wrapEnvironmentAliasCollection(makeRequest, data)); }, /** * Query for scheduled actions in space. * @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. * @return Promise for the scheduled actions query * * @example ```javascript * const contentful = require('contentful-management'); * * const client = contentful.createClient({ * accessToken: '<content_management_api_key>' * }) * * client.getSpace('<space_id>') * .then((space) => space.getScheduledActions({ * 'environment.sys.id': '<environment_id>', * 'sys.status': 'scheduled' * })) * .then((scheduledActionCollection) => console.log(scheduledActionCollection.items)) * .catch(console.error) * ``` */ getScheduledActions(query) { const raw = this.toPlainObject(); return makeRequest({ entityType: 'ScheduledAction', action: 'getMany', params: { spaceId: raw.sys.id, query } }).then(response => wrapScheduledActionCollection(makeRequest, response)); }, /** * Get a Scheduled Action in the current space by environment and ID. * * @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. * @returns Promise with the Scheduled Action * @example ```javascript * const contentful = require('contentful-management'); * * const client = contentful.createClient({ * accessToken: '<content_management_api_key>' * }) * * client.getSpace('<space_id>') * .then((space) => space.getScheduledAction({ * scheduledActionId: '<scheduled-action-id>', * environmentId: '<environmentId>' * })) * .then((scheduledAction) => console.log(scheduledAction)) * .catch(console.error) * ``` */ getScheduledAction({ scheduledActionId, environmentId }) { const space = this.toPlainObject(); return makeRequest({ entityType: 'ScheduledAction', action: 'get', params: { spaceId: space.sys.id, environmentId, scheduledActionId } }).then(scheduledAction => wrapScheduledAction(makeRequest, scheduledAction)); }, /** * Creates a scheduled action * @param data - Object representation of the scheduled action to be created * @return Promise for the newly created scheduled actions * @example ```javascript * const contentful = require('contentful-management'); * * const client = contentful.createClient({ * accessToken: '<content_management_api_key>' * }) * * client.getSpace('<space_id>') * .then((space) => space.createScheduledAction({ * entity: { * sys: { * type: 'Link', * linkType: 'Entry', * id: '<entry_id>' * } * }, * environment: { * sys: { * type: 'Link', * linkType: 'Environment', * id: '<environment_id>' * } * }, * action: 'publish', * scheduledFor: { * datetime: <ISO_date_string>, * timezone: 'Europe/Berlin' * } * })) * .then((scheduledAction) => console.log(scheduledAction)) * .catch(console.error) * ``` */ createScheduledAction(data) { const raw = this.toPlainObject(); return makeRequest({ entityType: 'ScheduledAction', action: 'create', params: { spaceId: raw.sys.id }, payload: data }).then(response => wrapScheduledAction(makeRequest, response)); }, /** * Update a scheduled action * @param {object} options * @param options.scheduledActionId the id of the scheduled action to update * @param options.version the sys.version of the scheduled action to be updated * @param payload the scheduled actions object with updates, omitting sys object * @returns Promise containing a wrapped scheduled action with helper methods * @example ```javascript * const contentful = require('contentful-management'); * * const client = contentful.createClient({ * accessToken: '<content_management_api_key>' * }) * * client.getSpace('<space_id>') * .then((space) => { * return space.createScheduledAction({ * entity: { * sys: { * type: 'Link', * linkType: 'Entry', * id: '<entry_id>' * } * }, * environment: { * sys: { * type: 'Link', * linkType: 'Environment', * id: '<environment_id>' * } * }, * action: 'publish', * scheduledFor: { * datetime: <ISO_date_string>, * timezone: 'Europe/Berlin' * } * }) * .then((scheduledAction) => { * const { _sys, ...payload } = scheduledAction; * return space.updateScheduledAction({ * ...payload, * scheduledFor: { * ...payload.scheduledFor, * timezone: 'Europe/Paris' * } * }) * }) * .then((scheduledAction) => console.log(scheduledAction)) * .catch(console.error); * ``` */ updateScheduledAction({ scheduledActionId, payload, version }) { const spaceProps = this.toPlainObject(); return makeRequest({ entityType: 'ScheduledAction', action: 'update', params: { spaceId: spaceProps.sys.id, version, scheduledActionId }, payload }).then(response => wrapScheduledAction(makeRequest, response)); }, /** * Cancels a Scheduled Action. * Only cancels actions that have not yet executed. * * @param {object} options * @param options.scheduledActionId the id of the scheduled action to be canceled * @param options.environmentId the environment ID of the scheduled action to be canceled * @throws if the Scheduled Action cannot be found or the user doesn't have permissions in the entity in the action. * @returns Promise containing a wrapped Scheduled Action with helper methods * @example ```javascript * const contentful = require('contentful-management'); * * const client = contentful.createClient({ * accessToken: '<content_management_api_key>' * }) * * // Given that an Scheduled Action is scheduled * client.getSpace('<space_id>') * .then((space) => space.deleteScheduledAction({ * environmentId: '<environment-id>', * scheduledActionId: '<scheduled-action-id>' * })) * // The scheduled Action sys.status is now 'canceled' * .then((scheduledAction) => console.log(scheduledAction)) * .catch(console.error); * ``` */ deleteScheduledAction({ scheduledActionId, environmentId }) { const spaceProps = this.toPlainObject(); return makeRequest({ entityType: 'ScheduledAction', action: 'delete', params: { spaceId: spaceProps.sys.id, environmentId, scheduledActionId } }).then(response => wrapScheduledAction(makeRequest, response)); } }; }