contentful-management
Version:
Client for Contentful's Content Management API
2,901 lines • 122 kB
JavaScript
import { createRequestConfig } from 'contentful-sdk-core';
import { normalizeCursorPaginationParameters, normalizeCursorPaginationResponse } from './common-utils.mjs';
import { wrapReleaseActionCollection, wrapReleaseAction } from './entities/release-action.mjs';
import { wrapRelease, wrapReleaseCollection } from './entities/release.mjs';
import { wrapTag, wrapTagCollection } from './entities/tag.mjs';
import { wrapUIConfig } from './entities/ui-config.mjs';
import { wrapUserUIConfig } from './entities/user-ui-config.mjs';
import { wrapEnvironmentTemplateInstallationCollection } from './entities/environment-template-installation.mjs';
import { wrapFunctionCollection } from './entities/function.mjs';
import { wrapFunctionLog, wrapFunctionLogCollection } from './entities/function-log.mjs';
import { wrapEnvironment } from './entities/environment.mjs';
import { wrapContentType, wrapContentTypeCursorPaginatedCollection, wrapContentTypeCollection } from './entities/content-type.mjs';
import { wrapEntryCollection, wrapEntry, wrapEntryTypeCursorPaginatedCollection } from './entities/entry.mjs';
import { wrapAsset, wrapAssetTypeCursorPaginatedCollection, wrapAssetCollection } from './entities/asset.mjs';
import { wrapAssetKey } from './entities/asset-key.mjs';
import { wrapLocale, wrapLocaleCollection } from './entities/locale.mjs';
import { wrapSnapshotCollection } from './entities/snapshot.mjs';
import { wrapEditorInterfaceCollection, wrapEditorInterface } from './entities/editor-interface.mjs';
import { wrapUpload } from './entities/upload.mjs';
import { wrapExtension, wrapExtensionCollection } from './entities/extension.mjs';
import { wrapAppInstallationCollection, wrapAppInstallation } from './entities/app-installation.mjs';
import { wrapAppSignedRequest } from './entities/app-signed-request.mjs';
import { wrapAppActionCall } from './entities/app-action-call.mjs';
import { wrapBulkAction } from './entities/bulk-action.mjs';
import { wrapAppAccessToken } from './entities/app-access-token.mjs';
import { wrapResourceTypesForEnvironmentCollection } from './entities/resource-type.mjs';
import { wrapResourceCollection } from './entities/resource.mjs';
import { wrapAiActionInvocation } from './entities/ai-action-invocation.mjs';
import { wrapAgentCollection, wrapAgent } from './entities/agent.mjs';
import { wrapAgentRunCollection, wrapAgentRun, wrapAgentGenerateResponse } from './entities/agent-run.mjs';
import { wrapSemanticDuplicates } from './entities/semantic-duplicates.mjs';
import { wrapSemanticRecommendations } from './entities/semantic-recommendations.mjs';
import { wrapSemanticReferenceSuggestions } from './entities/semantic-reference-suggestions.mjs';
import { wrapSemanticSearch } from './entities/semantic-search.mjs';
import { wrapContentSemanticsIndexCollection } from './entities/content-semantics-index.mjs';
/**
* Creates API object with methods to access the Environment API
* @param {ContentfulEnvironmentAPI} makeRequest - function to make requests via an adapter
* @returns {ContentfulSpaceAPI}
* @internal
*/
function createEnvironmentApi(makeRequest) {
return {
/**
* Deletes the environment
* @returns 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.getEnvironment('<environment-id>'))
* .then((environment) => environment.delete())
* .then(() => console.log('Environment deleted.'))
* .catch(console.error)
* ```
*/
delete: function deleteEnvironment() {
const raw = this.toPlainObject();
return makeRequest({
entityType: 'Environment',
action: 'delete',
params: { spaceId: raw.sys.space.sys.id, environmentId: raw.sys.id },
}).then(() => {
// noop
});
},
/**
* Updates the environment
* @returns Promise for the updated 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) => {
* environment.name = 'New name'
* return environment.update()
* })
* .then((environment) => console.log(`Environment ${environment.sys.id} renamed.`)
* .catch(console.error)
* ```
*/
update: function updateEnvironment() {
const raw = this.toPlainObject();
return makeRequest({
entityType: 'Environment',
action: 'update',
params: { spaceId: raw.sys.space.sys.id, environmentId: raw.sys.id },
payload: raw,
}).then((data) => wrapEnvironment(makeRequest, data));
},
/**
* Creates SDK Entry object (locally) from entry data
* @param entryData - Entry Data
* @returns Entry
* @example ```javascript
* environment.getEntry('entryId').then(entry => {
*
* // Build a plainObject in order to make it usable for React (saving in state or redux)
* const plainObject = entry.toPlainObject();
*
* // The entry is being updated in some way as plainObject:
* const updatedPlainObject = {
* ...plainObject,
* fields: {
* ...plainObject.fields,
* title: {
* 'en-US': 'updatedTitle'
* }
* }
* };
*
* // Rebuild an sdk object out of the updated plainObject:
* const entryWithMethodsAgain = environment.getEntryFromData(updatedPlainObject);
*
* // Update with help of the sdk method:
* entryWithMethodsAgain.update();
*
* });
* ```
**/
getEntryFromData(entryData) {
return wrapEntry(makeRequest, entryData);
},
/**
* Creates SDK Asset object (locally) from entry data
* @param assetData - Asset ID
* @returns Asset
* @example ```javascript
* environment.getAsset('asset_id').then(asset => {
*
* // Build a plainObject in order to make it usable for React (saving in state or redux)
* const plainObject = asset.toPlainObject();
*
* // The asset is being updated in some way as plainObject:
* const updatedPlainObject = {
* ...plainObject,
* fields: {
* ...plainObject.fields,
* title: {
* 'en-US': 'updatedTitle'
* }
* }
* };
*
* // Rebuild an sdk object out of the updated plainObject:
* const assetWithMethodsAgain = environment.getAssetFromData(updatedPlainObject);
*
* // Update with help of the sdk method:
* assetWithMethodsAgain.update();
*
* });
* ```
*/
getAssetFromData(assetData) {
return wrapAsset(makeRequest, assetData);
},
/**
*
* @description Get a BulkAction by ID.
* See: https://www.contentful.com/developers/docs/references/content-management-api/#/reference/bulk-actions/bulk-action
* @param bulkActionId - ID of the BulkAction to fetch
* @returns - Promise with the BulkAction
*
* @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) => environment.getBulkAction('<bulk_action_id>'))
* .then((bulkAction) => console.log(bulkAction))
* ```
*/
getBulkAction(bulkActionId) {
const raw = this.toPlainObject();
return makeRequest({
entityType: 'BulkAction',
action: 'get',
params: {
spaceId: raw.sys.space.sys.id,
environmentId: raw.sys.id,
bulkActionId,
},
}).then((data) => wrapBulkAction(makeRequest, data));
},
/**
* @description Creates a BulkAction that will attempt to publish all items contained in the payload.
* See: https://www.contentful.com/developers/docs/references/content-management-api/#/reference/bulk-actions/publish-bulk-action
* @param {BulkActionPayload} payload - Object containing the items to be processed in the bulkAction
* @returns - Promise with the BulkAction
*
* @example
*
* ```javascript
* const contentful = require('contentful-management')
*
* const client = contentful.createClient({
* accessToken: '<content_management_api_key>'
* })
*
* const payload = {
* entities: {
* sys: { type: 'Array' }
* items: [
* { sys: { type: 'Link', id: '<entry-id>', linkType: 'Entry', version: 2 } }
* ]
* }
* }
*
* // Using Thenables
* client.getSpace('<space_id>')
* .then((space) => space.getEnvironment('<environment_id>'))
* .then((environment) => environment.createPublishBulkAction(payload))
* .then((bulkAction) => console.log(bulkAction.waitProcessing()))
* .catch(console.error)
*
* // Using async/await
* try {
* const space = await client.getSpace('<space_id>')
* const environment = await space.getEnvironment('<environment_id>')
* const bulkActionInProgress = await environment.createPublishBulkAction(payload)
*
* // You can wait for a recently created BulkAction to be processed by using `bulkAction.waitProcessing()`
* const bulkActionCompleted = await bulkActionInProgress.waitProcessing()
* console.log(bulkActionCompleted)
* } catch (error) {
* console.log(error)
* }
* ```
*/
createPublishBulkAction(payload) {
const raw = this.toPlainObject();
return makeRequest({
entityType: 'BulkAction',
action: 'publish',
params: {
spaceId: raw.sys.space.sys.id,
environmentId: raw.sys.id,
},
payload,
}).then((data) => wrapBulkAction(makeRequest, data));
},
/**
* @description Creates a BulkAction that will attempt to validate all items contained in the payload.
* See: https://www.contentful.com/developers/docs/references/content-management-api/#/reference/bulk-actions/validate-bulk-action
* @param {BulkActionPayload} payload - Object containing the items to be processed in the bulkAction
* @returns - Promise with the BulkAction
*
* @example
*
* ```javascript
* const contentful = require('contentful-management')
*
* const client = contentful.createClient({
* accessToken: '<content_management_api_key>'
* })
*
* const payload = {
* action: 'publish',
* entities: {
* sys: { type: 'Array' }
* items: [
* { sys: { type: 'Link', id: '<entry-id>', linkType: 'Entry' } }
* ]
* }
* }
*
* // Using Thenables
* client.getSpace('<space_id>')
* .then((space) => space.getEnvironment('<environment_id>'))
* .then((environment) => environment.createValidateBulkAction(payload))
* .then((bulkAction) => console.log(bulkAction.waitProcessing()))
* .catch(console.error)
*
* // Using async/await
* try {
* const space = await client.getSpace('<space_id>')
* const environment = await space.getEnvironment('<environment_id>')
* const bulkActionInProgress = await environment.createValidateBulkAction(payload)
*
* // You can wait for a recently created BulkAction to be processed by using `bulkAction.waitProcessing()`
* const bulkActionCompleted = await bulkActionInProgress.waitProcessing()
* console.log(bulkActionCompleted)
* } catch (error) {
* console.log(error)
* }
* ```
*/
createValidateBulkAction(payload) {
const raw = this.toPlainObject();
return makeRequest({
entityType: 'BulkAction',
action: 'validate',
params: {
spaceId: raw.sys.space.sys.id,
environmentId: raw.sys.id,
},
payload,
}).then((data) => wrapBulkAction(makeRequest, data));
},
/**
* @description Creates a BulkAction that will attempt to unpublish all items contained in the payload.
* See: https://www.contentful.com/developers/docs/references/content-management-api/#/reference/bulk-actions/unpublish-bulk-action
* @param {BulkActionPayload} payload - Object containing the items to be processed in the bulkAction
* @returns - Promise with the BulkAction
*
* @example
*
* ```javascript
* const contentful = require('contentful-management')
*
* const client = contentful.createClient({
* accessToken: '<content_management_api_key>'
* })
*
* const payload = {
* entities: {
* sys: { type: 'Array' }
* items: [
* { sys: { type: 'Link', id: 'entry-id', linkType: 'Entry' } }
* ]
* }
* }
*
* // Using Thenables
* client.getSpace('<space_id>')
* .then((space) => space.getEnvironment('<environment_id>'))
* .then((environment) => environment.createUnpublishBulkAction(payload))
* .then((bulkAction) => console.log(bulkAction.waitProcessing()))
* .catch(console.error)
*
* // Using async/await
* try {
* const space = await clientgetSpace('<space_id>')
* const environment = await space.getEnvironment('<environment_id>')
* const bulkActionInProgress = await environment.createUnpublishBulkAction(payload)
*
* // You can wait for a recently created BulkAction to be processed by using `bulkAction.waitProcessing()`
* const bulkActionCompleted = await bulkActionInProgress.waitProcessing()
* console.log(bulkActionCompleted)
* } catch (error) {
* console.log(error)
* }
* ```
*/
createUnpublishBulkAction(payload) {
const raw = this.toPlainObject();
return makeRequest({
entityType: 'BulkAction',
action: 'unpublish',
params: {
spaceId: raw.sys.space.sys.id,
environmentId: raw.sys.id,
},
payload,
}).then((data) => wrapBulkAction(makeRequest, data));
},
/**
* Gets a Content Type
* @param contentTypeId - Content Type ID
* @returns Promise for a Content Type
* @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) => environment.getContentType('<content_type_id>'))
* .then((contentType) => console.log(contentType))
* .catch(console.error)
* ```
*/
getContentType(contentTypeId) {
const raw = this.toPlainObject();
return makeRequest({
entityType: 'ContentType',
action: 'get',
params: {
spaceId: raw.sys.space.sys.id,
environmentId: raw.sys.id,
contentTypeId,
},
}).then((data) => wrapContentType(makeRequest, data));
},
/**
* Gets a collection of Content Types
* @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.
* @returns Promise for a collection of Content Types
* @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) => environment.getContentTypes())
* .then((response) => console.log(response.items))
* .catch(console.error)
* ```
*/
getContentTypes(query = {}) {
const raw = this.toPlainObject();
return makeRequest({
entityType: 'ContentType',
action: 'getMany',
params: {
spaceId: raw.sys.space.sys.id,
environmentId: raw.sys.id,
query: createRequestConfig({ query }).params,
},
}).then((data) => wrapContentTypeCollection(makeRequest, data));
},
/**
* Gets a collection of Content Types with cursor based pagination
* @param query - Object with cursor pagination parameters. Check the <a href="https://www.contentful.com/developers/docs/references/content-management-api/#/introduction/cursor-pagination">REST API reference</a> for more details.
* @returns Promise for a collection of Content Types
* @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) => environment.getContentTypesWithCursor())
* .then((response) => console.log(response.items))
* .catch(console.error)
* ```
*/
getContentTypesWithCursor(query = {}) {
const raw = this.toPlainObject();
const normalizedQueryParams = normalizeCursorPaginationParameters(query);
return makeRequest({
entityType: 'ContentType',
action: 'getMany',
params: {
spaceId: raw.sys.space.sys.id,
environmentId: raw.sys.id,
query: createRequestConfig({ query: normalizedQueryParams }).params,
},
}).then((data) => wrapContentTypeCursorPaginatedCollection(makeRequest, normalizeCursorPaginationResponse(data)));
},
/**
* Creates a Content Type
* @param data - Object representation of the Content Type to be created
* @returns Promise for the newly created Content Type
* @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) => environment.createContentType({
* name: 'Blog Post',
* fields: [
* {
* id: 'title',
* name: 'Title',
* required: true,
* localized: false,
* type: 'Text'
* }
* ]
* }))
* .then((contentType) => console.log(contentType))
* .catch(console.error)
* ```
*/
createContentType(data) {
const raw = this.toPlainObject();
return makeRequest({
entityType: 'ContentType',
action: 'create',
params: {
spaceId: raw.sys.space.sys.id,
environmentId: raw.sys.id,
},
payload: data,
}).then((response) => wrapContentType(makeRequest, response));
},
/**
* Creates a Content Type with a custom ID
* @param contentTypeId - Content Type ID
* @param data - Object representation of the Content Type to be created
* @returns Promise for the newly created Content Type
* @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) => environment.createContentTypeWithId('<content-type-id>', {
* name: 'Blog Post',
* fields: [
* {
* id: 'title',
* name: 'Title',
* required: true,
* localized: false,
* type: 'Text'
* }
* ]
* }))
* .then((contentType) => console.log(contentType))
* .catch(console.error)
* ```
*/
createContentTypeWithId(contentTypeId, data) {
const raw = this.toPlainObject();
return makeRequest({
entityType: 'ContentType',
action: 'createWithId',
params: {
spaceId: raw.sys.space.sys.id,
environmentId: raw.sys.id,
contentTypeId,
},
payload: data,
}).then((response) => wrapContentType(makeRequest, response));
},
/**
* Gets an EditorInterface for a ContentType
* @param contentTypeId - Content Type ID
* @returns Promise for an EditorInterface
* @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) => environment.getEditorInterfaceForContentType('<content_type_id>'))
* .then((EditorInterface) => console.log(EditorInterface))
* .catch(console.error)
* ```
*/
getEditorInterfaceForContentType(contentTypeId) {
const raw = this.toPlainObject();
return makeRequest({
entityType: 'EditorInterface',
action: 'get',
params: {
spaceId: raw.sys.space.sys.id,
environmentId: raw.sys.id,
contentTypeId,
},
}).then((response) => wrapEditorInterface(makeRequest, response));
},
/**
* Gets all EditorInterfaces
* @returns Promise for a collection of EditorInterface
* @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) => environment.getEditorInterfaces())
* .then((response) => console.log(response.items))
* .catch(console.error)
* ```
*/
getEditorInterfaces() {
const raw = this.toPlainObject();
return makeRequest({
entityType: 'EditorInterface',
action: 'getMany',
params: {
spaceId: raw.sys.space.sys.id,
environmentId: raw.sys.id,
},
}).then((response) => wrapEditorInterfaceCollection(makeRequest, response));
},
/**
* Gets an Entry
* Warning: if you are using the select operator, when saving, any field that was not selected will be removed
* from your entry in the backend
* @param id - Entry ID
* @param query - Object with search parameters. In this method it's only useful for `locale`.
* @returns Promise for an Entry
* @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) => environment.getEntry('<entry-id>'))
* .then((entry) => console.log(entry))
* .catch(console.error)
* ```
*/
getEntry(id, query = {}) {
const raw = this.toPlainObject();
return makeRequest({
entityType: 'Entry',
action: 'get',
params: {
spaceId: raw.sys.space.sys.id,
environmentId: raw.sys.id,
entryId: id,
query: createRequestConfig({ query: query }).params,
},
}).then((data) => wrapEntry(makeRequest, data));
},
/**
* Deletes an Entry of this environment
* @param id - Entry ID
* @returns 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.getEnvironment('<environment-id>'))
* .then((environment) => environment.deleteEntry("4bmLXiuviAZH3jkj5DLRWE"))
* .then(() => console.log('Entry deleted.'))
* .catch(console.error)
* ```
*/
deleteEntry(id) {
const raw = this.toPlainObject();
return makeRequest({
entityType: 'Entry',
action: 'delete',
params: {
spaceId: raw.sys.space.sys.id,
environmentId: raw.sys.id,
entryId: id,
},
}).then(() => {
// noop
});
},
/**
* Gets a collection of Entries
* Warning: if you are using the select operator, when saving, any field that was not selected will be removed
* from your entry in the backend
* @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.
* @returns Promise for a collection of Entries
* @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) => environment.getEntries({'content_type': 'foo'})) // you can add more queries as 'key': 'value'
* .then((response) => console.log(response.items))
* .catch(console.error)
* ```
*/
getEntries(query = {}) {
const raw = this.toPlainObject();
return makeRequest({
entityType: 'Entry',
action: 'getMany',
params: {
spaceId: raw.sys.space.sys.id,
environmentId: raw.sys.id,
query: createRequestConfig({ query: query }).params,
},
}).then((data) => wrapEntryCollection(makeRequest, data));
},
/**
* Gets a collection of Entries with cursor based pagination
* Warning: if you are using the select operator, when saving, any field that was not selected will be removed
* from your entry in the backend
* @param query - Object with cursor pagination parameters. Check the <a href="https://www.contentful.com/developers/docs/references/content-management-api/#/introduction/cursor-pagination">REST API reference</a> for more details.
* @returns Promise for a collection of Entries
* @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) => environment.getEntriesWithCursor({'content_type': 'foo'})) // you can add more queries as 'key': 'value'
* .then((response) => console.log(response.items))
* .catch(console.error)
* ```
*/
getEntriesWithCursor(query = {}) {
const raw = this.toPlainObject();
const normalizedQueryParams = normalizeCursorPaginationParameters(query);
return makeRequest({
entityType: 'Entry',
action: 'getMany',
params: {
spaceId: raw.sys.space.sys.id,
environmentId: raw.sys.id,
query: createRequestConfig({ query: normalizedQueryParams }).params,
},
}).then((data) => wrapEntryTypeCursorPaginatedCollection(makeRequest, normalizeCursorPaginationResponse(data)));
},
/**
* Gets a collection of published Entries
* @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.
* @returns Promise for a collection of published Entries
* @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) => environment.getPublishedEntries({'content_type': 'foo'})) // you can add more queries as 'key': 'value'
* .then((response) => console.log(response.items))
* .catch(console.error)
* ```
*/
getPublishedEntries(query = {}) {
const raw = this.toPlainObject();
return makeRequest({
entityType: 'Entry',
action: 'getPublished',
params: {
spaceId: raw.sys.space.sys.id,
environmentId: raw.sys.id,
query: createRequestConfig({ query: query }).params,
},
}).then((data) => wrapEntryCollection(makeRequest, data));
},
/**
* Gets a collection of published Entries with cursor based pagination
* @param query - Object with cursor pagination parameters. Check the <a href="https://www.contentful.com/developers/docs/references/content-management-api/#/introduction/cursor-pagination">REST API reference</a> for more details.
* @returns Promise for a collection of published Entries
* @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) => environment.getPublishedEntriesWithCursor())
* .then((response) => console.log(response.items))
* .catch(console.error)
* ```
*/
getPublishedEntriesWithCursor(query = {}) {
const raw = this.toPlainObject();
const normalizedQueryParams = normalizeCursorPaginationParameters(query);
return makeRequest({
entityType: 'Entry',
action: 'getPublished',
params: {
spaceId: raw.sys.space.sys.id,
environmentId: raw.sys.id,
query: createRequestConfig({ query: normalizedQueryParams }).params,
},
}).then((data) => wrapEntryTypeCursorPaginatedCollection(makeRequest, normalizeCursorPaginationResponse(data)));
},
/**
* Creates a Entry
* @param contentTypeId - The Content Type ID of the newly created Entry
* @param data - Object representation of the Entry to be created
* @returns Promise for the newly created Entry
* @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) => environment.createEntry('<content_type_id>', {
* fields: {
* title: {
* 'en-US': 'Entry title'
* }
* }
* }))
* .then((entry) => console.log(entry))
* .catch(console.error)
* ```
*/
createEntry(contentTypeId, data) {
const raw = this.toPlainObject();
return makeRequest({
entityType: 'Entry',
action: 'create',
params: {
spaceId: raw.sys.space.sys.id,
environmentId: raw.sys.id,
contentTypeId: contentTypeId,
},
payload: data,
}).then((response) => wrapEntry(makeRequest, response));
},
/**
* Creates a Entry with a custom ID
* @param contentTypeId - The Content Type of the newly created Entry
* @param id - Entry ID
* @param data - Object representation of the Entry to be created
* @returns Promise for the newly created Entry
* @example ```javascript
* const contentful = require('contentful-management')
*
* const client = contentful.createClient({
* accessToken: '<content_management_api_key>'
* })
*
* // Create entry
* client.getSpace('<space_id>')
* .then((space) => space.getEnvironment('<environment-id>'))
* .then((environment) => environment.createEntryWithId('<content_type_id>', '<entry_id>', {
* fields: {
* title: {
* 'en-US': 'Entry title'
* }
* }
* }))
* .then((entry) => console.log(entry))
* .catch(console.error)
* ```
*/
createEntryWithId(contentTypeId, id, data) {
const raw = this.toPlainObject();
return makeRequest({
entityType: 'Entry',
action: 'createWithId',
params: {
spaceId: raw.sys.space.sys.id,
environmentId: raw.sys.id,
entryId: id,
contentTypeId: contentTypeId,
},
payload: data,
}).then((response) => wrapEntry(makeRequest, response));
},
/**
* Get entry references
* @param entryId - Entry ID
* @param {Object} options.include - Level of the entry descendants from 1 up to 10 maximum
* @returns Promise of Entry references
* @example ```javascript
* const contentful = require('contentful-management');
*
* const client = contentful.createClient({
* accessToken: '<contentful_management_api_key>
* })
*
* // Get entry references
* client.getSpace('<space_id>')
* .then((space) => space.getEnvironment('<environment_id>'))
* .then((environment) => environment.getEntryReferences('<entry_id>', {include: number}))
* .then((entry) => console.log(entry.includes))
* // or
* .then((environment) => environment.getEntry('<entry_id>')).then((entry) => entry.references({include: number}))
* .catch(console.error)
* ```
*/
getEntryReferences(entryId, options) {
const raw = this.toPlainObject();
return makeRequest({
entityType: 'Entry',
action: 'references',
params: {
spaceId: raw.sys.space.sys.id,
environmentId: raw.sys.id,
entryId: entryId,
include: options?.include,
},
}).then((response) => wrapEntryCollection(makeRequest, response));
},
/**
* Gets an Asset
* Warning: if you are using the select operator, when saving, any field that was not selected will be removed
* from your entry in the backend
* @param id - Asset ID
* @param query - Object with search parameters. In this method it's only useful for `locale`.
* @returns Promise for an Asset
* @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) => environment.getAsset('<asset_id>'))
* .then((asset) => console.log(asset))
* .catch(console.error)
* ```
*/
getAsset(id, query = {}) {
const raw = this.toPlainObject();
return makeRequest({
entityType: 'Asset',
action: 'get',
params: {
spaceId: raw.sys.space.sys.id,
environmentId: raw.sys.id,
assetId: id,
query: createRequestConfig({ query: query }).params,
},
}).then((data) => wrapAsset(makeRequest, data));
},
/**
* Gets a collection of Assets
* Warning: if you are using the select operator, when saving, any field that was not selected will be removed
* from your entry in the backend
* @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.
* @returns Promise for a collection of Assets
* @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) => environment.getAssets())
* .then((response) => console.log(response.items))
* .catch(console.error)
* ```
*/
getAssets(query = {}) {
const raw = this.toPlainObject();
return makeRequest({
entityType: 'Asset',
action: 'getMany',
params: {
spaceId: raw.sys.space.sys.id,
environmentId: raw.sys.id,
query: createRequestConfig({ query: query }).params,
},
}).then((data) => wrapAssetCollection(makeRequest, data));
},
/**
* Gets a collection of Assets with cursor based pagination
* Warning: if you are using the select operator, when saving, any field that was not selected will be removed
* from your entry in the backend
* @param query - Object with cursor pagination parameters. Check the <a href="https://www.contentful.com/developers/docs/references/content-management-api/#/introduction/cursor-pagination">REST API reference</a> for more details.
* @returns Promise for a collection of Assets
* @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) => environment.getAssetsWithCursor())
* .then((response) => console.log(response.items))
* .catch(console.error)
* ```
*/
getAssetsWithCursor(query = {}) {
const raw = this.toPlainObject();
const normalizedQueryParams = normalizeCursorPaginationParameters(query);
return makeRequest({
entityType: 'Asset',
action: 'getMany',
params: {
spaceId: raw.sys.space.sys.id,
environmentId: raw.sys.id,
query: createRequestConfig({ query: normalizedQueryParams }).params,
},
}).then((data) => wrapAssetTypeCursorPaginatedCollection(makeRequest, normalizeCursorPaginationResponse(data)));
},
/**
* Gets a collection of published Assets
* @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.
* @returns Promise for a collection of published Assets
* @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) => environment.getPublishedAssets())
* .then((response) => console.log(response.items))
* .catch(console.error)
* ```
*/
getPublishedAssets(query = {}) {
const raw = this.toPlainObject();
return makeRequest({
entityType: 'Asset',
action: 'getPublished',
params: {
spaceId: raw.sys.space.sys.id,
environmentId: raw.sys.id,
query: createRequestConfig({ query: query }).params,
},
}).then((data) => wrapAssetCollection(makeRequest, data));
},
/**
* Gets a collection of published Assets with cursor based pagination
* @param query - Object with cursor pagination parameters. Check the <a href="https://www.contentful.com/developers/docs/references/content-management-api/#/introduction/cursor-pagination">REST API reference</a> for more details.
* @returns Promise for a collection of published Assets
* @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) => environment.getPublishedAssetsWithCursor())
* .then((response) => console.log(response.items))
* .catch(console.error)
* ```
*/
getPublishedAssetsWithCursor(query = {}) {
const raw = this.toPlainObject();
const normalizedQueryParams = normalizeCursorPaginationParameters(query);
return makeRequest({
entityType: 'Asset',
action: 'getPublished',
params: {
spaceId: raw.sys.space.sys.id,
environmentId: raw.sys.id,
query: createRequestConfig({ query: normalizedQueryParams }).params,
},
}).then((data) => wrapAssetTypeCursorPaginatedCollection(makeRequest, normalizeCursorPaginationResponse(data)));
},
/**
* Creates a Asset. After creation, call asset.processForLocale or asset.processForAllLocales to start asset processing.
* @param data - Object representation of the Asset to be created. Note that the field object should have an upload property on asset creation, which will be removed and replaced with an url property when processing is finished.
* @returns Promise for the newly created Asset
* @example ```javascript
* const client = contentful.createClient({
* accessToken: '<content_management_api_key>'
* })
*
* // Create asset
* client.getSpace('<space_id>')
* .then((space) => space.getEnvironment('<environment-id>'))
* .then((environment) => environment.createAsset({
* fields: {
* title: {
* 'en-US': 'Playsam Streamliner'
* },
* file: {
* 'en-US': {
* contentType: 'image/jpeg',
* fileName: 'example.jpeg',
* upload: 'https://example.com/example.jpg'
* }
* }
* }
* }))
* .then((asset) => asset.processForLocale("en-US")) // OR asset.processForAllLocales()
* .then((asset) => console.log(asset))
* .catch(console.error)
* ```
*/
createAsset(data) {
const raw = this.toPlainObject();
return makeRequest({
entityType: 'Asset',
action: 'create',
params: {
spaceId: raw.sys.space.sys.id,
environmentId: raw.sys.id,
},
payload: data,
}).then((response) => wrapAsset(makeRequest, response));
},
/**
* Creates a Asset with a custom ID. After creation, call asset.processForLocale or asset.processForAllLocales to start asset processing.
* @param id - Asset ID
* @param data - Object representation of the Asset to be created. Note that the field object should have an upload property on asset creation, which will be removed and replaced with an url property when processing is finished.
* @returns Promise for the newly created Asset
* @example ```javascript
* const client = contentful.createClient({
* accessToken: '<content_management_api_key>'
* })
*
* // Create asset
* client.getSpace('<space_id>')
* .then((space) => space.getEnvironment('<environment-id>'))
* .then((environment) => environment.createAssetWithId('<asset_id>', {
* title: {
* 'en-US': 'Playsam Streamliner'
* },
* file: {
* 'en-US': {
* contentType: 'image/jpeg',
* fileName: 'example.jpeg',
* upload: 'https://example.com/example.jpg'
* }
* }
* }))
* .then((asset) => asset.process())
* .then((asset) => console.log(asset))
* .catch(console.error)
* ```
*/
createAssetWithId(id, data) {
const raw = this.toPlainObject();
return makeRequest({
entityType: 'Asset',
action: 'createWithId',
params: {
spaceId: raw.sys.space.sys.id,
environmentId: raw.sys.id,
assetId: id,
},
payload: data,
}).then((response) => wrapAsset(makeRequest, response));
},
/**
* Creates a Asset based on files. After creation, call asset.processForLocale or asset.processForAllLocales to start asset processing.
* @param data - Object representation of the Asset to be created. Note that the field object should have an uploadFrom property on asset creation, which will be removed and replaced with an url property when processing is finished.
* @param data.fields.file.[LOCALE].file - Can be a string, an ArrayBuffer or a Stream.
* @returns Promise for the newly created Asset
* @example ```javascript
* const client = contentful.createClient({
* accessToken: '<content_management_api_key>'
* })
*
* client.getSpace('<space_id>')
* .then((space) => space.getEnvironment('<environment-id>'))
* .then((environment) => environment.createAssetFromFiles({
* fields: {
* file: {
* 'en-US': {
* contentType: 'image/jpeg',
* fileName: 'filename_english.jpg',
* file: createReadStream('path/to/filename_english.jpg')
* },
* 'de-DE': {
* contentType: 'image/svg+xml',
* fileName: 'filename_german.svg',
* file: '<svg><path fill="red" d="M50 50h150v50H50z"/></svg>'
* }
* }
* }
* }))
* .then((asset) => console.log(asset))
* .catch(console.error)
* ```
*/
createAssetFromFiles(data, options) {
const raw = this.toPlainObject();
return makeRequest({
entityType: 'Asset',
action: 'createFromFiles',
params: {
spaceId: raw.sys.space.sys.id,
environmentId: raw.sys.id,
uploadTimeout: options?.uploadTimeout,
},
payload: data,
}).then((response) => wrapAsset(makeRequest, response));
},
/**
* Creates an asset key for signing asset URLs (Embargoed Assets)
* @param data Object with request payload
* @param data.expiresAt number a UNIX timestamp in the future (but not more than 48 hours from time of calling)
* @returns Promise for the newly created AssetKey
* @example ```javascript
* const client = contentful.createClient({
* accessToken: '<content_management_api_key>'
* })
*
* // Create assetKey
* now = () => Math.floor(Date.now() / 1000)
* const withExpiryIn1Hour = () => now() + 1 * 60 * 60
* client.getSpace('<space_id>')
* .then((space) => space.getEnvironment('<environment-id>'))
* .then((environment) => environment.createAssetKey({ expiresAt: withExpiryIn1Hour() }))
* .then((policy, secret) => console.log({ policy, secret }))
* .catch(console.error)
* ```
*/
createAssetKey(payload) {
const raw = this.toPlainObject();
return makeRequest({
entityType: 'AssetKey',
action: 'create',
params: {
spaceId: raw.sys.space.sys.id,
environmentId: raw.sys.id,
},
payload,
}).then((data) => wrapAssetKey(makeRequest, data));
},
/**
* Gets an Upload
* @param id - Upload ID
* @returns Promise for an Upload
* @example ```javascript
* const client = contentful.createClient({
* accessToken: '<content_management_api_key>'
* })
* const uploadStream = createReadStream('path/to/filename_english.jpg')
*
* client.getSpace('<space_id>')
* .then((space) => space.getEnvironment('<environment-id>'))
* .then((environment) => environment.getUpload('<upload-id>')
* .then((upload) => console.log(upload))
* .catch(console.error)
*/
getUpload(id) {
const raw = this.toPlainObject();
return makeRequest({
entityType: 'Upload',
action: 'get',
params: {
spaceId: raw.sys.space.sys.id,
environmentId: raw.sys.id,
uploadId: id,
},
}).then((data) => wrapUpload(makeRequest, data));
},
/**
* Creates a Upload.
* @param data - Object with file information.
* @param data.file - Actual file content. Can be a string, an ArrayBuffer or a Stream.
* @returns Upload object containing information about the uploaded file.
* @example ```javascript
* const client = contentful.createClient({
* accessToken: '<content_management_api_key>'
* })
* const uploadStream = createReadStream('path/to/filename_english.jpg')
*
* client.getSpace('<space_id>')
* .then((space) => space.getEnvironment('<environment-id>'))
* .then((environment) => environment.createUpload({file: uploadStream})
* .then((upload) => console.log(upload))
* .catch(console.error)
* ```
*/
createUpload: function createUpload(data) {
const raw = this.toPlainObject();
return makeRequest({
entityType: 'Upload',
action: 'create',
params: {
spaceId: raw.sys.space.sys.id,
environmentId: raw.sys.id,
},
payload: data,
}).then((data) => wrapUpload(makeRequest, data));
},
/**
* Gets a Locale
* @param localeId - Locale ID
* @returns Promise for an Locale
* @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) => environment.getLocale('<locale_id>'))
* .then((locale) => console.log(locale))
* .catch(console.error)
* ```
*/
getLocale(localeId) {
const raw = this.toPlainObject();
return makeRequest({
entityType: 'Locale',
action: 'get',
params: {
spaceId: raw.sys.space.sys.id,
environmentId: raw.sys.id,
localeId,
},
}).then((data) => wrapLocale(makeRequest, data));
},
/**
* Gets a collection of Locales
* @returns Promise for a collection of Locales
* @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) => environment.getLocales())
* .then((response) => console.log(response.items))
* .catch(console.error)
* ```
*/
getLocales(query = {}) {
const raw = this.toPlainObject();
return makeRequest({
entityType: 'Locale',
action: 'getMany',
params: {
spaceId: raw.sys.space.sys.id,
environmentId: raw.sys.id,
query: createRequestConfig({ query }).params,
},
}).then((data) => wrapLocaleCollection(makeRequest, data));
},
/**
* Creates a Locale
* @param data - Object representation of the Locale to be created
* @returns Promise for the newly created Locale
* @example ```javascript
* const contentful = require('contentful-management')
*
* const client = contentful.createClient({
* accessToken: '<content_management_api_key>'
* })
*
* // Create locale
* client.getSpace('<space_id>')
* .then((space) => space.getEnvironment('<environment-id>'))
* .then((environment) => environment.createLocale({
* name: 'German (Austria)',
* code: 'de-AT',
* fallbackCode: 'de-DE',
* optional: true
* }))
* .then((locale) => console.log(locale))
* .catch(console.error)
* ```
*/
createLocale(data) {
const raw = this.toPlainObject();
return makeRequest({
entityType: 'Locale',
action: 'create',
params: {
spaceId: raw.sys.space.sys.id,
environmentId: raw.sys.id,
},
payload: data,
}).then((response) => wrapLocale(makeRequest, response));
},
/**
* Gets an UI Extension
* @param id - Extension ID
* @returns Promise for an UI Extension
* @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) => environment.getUiExtension('<extension-id>'))
* .then((extension) => console.log(extension))
* .catch(console.error)
* ```
*/
getUiExtension(id) {
const raw = this.toPlainObject();
return makeRequest({
entityType: 'Extension',
action: 'get',
params: {
spaceId: raw.sys.space.sys.id,
environmentId: raw.sys.id,
extensionId: id,
},
}).then((data) => wrapExtension(makeRequest, data));
},
/**
* Gets a collection of UI Extension
* @returns Promise for a collection of UI Extensions
* @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) => environment.getUiExtensions()
* .then((response) => console.log(response.items))
* .catch(console.error)
* ```
*/
getUiExtensions() {
const raw = this.toPlainObject();
return makeRequest({
entityType: 'Extension',
action: 'getMany',
params: {
spaceId: raw.sys.space.sys.id,
environmentId: raw.sys.id,
},
}).then((response) => wrapExtensionCollection(makeRequest, response));
},
/**
* Creates a UI Extension
* @param data - Object representation of the UI Extension to be created
* @returns Promise for the newly created UI Extension
* @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) => environment.createUiExtension({
* extension: {
* name: 'My awesome extension',
* src: 'https://example.com/my',
* fieldTypes: [
* {
* type: 'Symbol'
* },
* {
* type: 'Text'
* }
* ],
* sidebar: false
* }
* }))
* .then((extension) => console.log(extension))
* .catch(console.error)
* ```
*/
createUiExtension(data) {
const raw = this.toPlainObject();
return makeRequest({
entityType: 'Extension',
action: 'create',
params: {
spaceId: raw.sys.space.sys.id,
environmentId: raw.sys.id,
},
payload: data,
}).then((response) => wrapExtension(makeRequest, response));
},
/**
* Creates a UI Extension with a custom ID
* @param id - Extension ID
* @param data - Object representation of the UI Extension to be created
* @returns Promise for the newly created UI Extension
* @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) => environment.createUiExtensionWithId('<extension_id>', {
* extension: {
* name: 'My awesome extension',
* src: 'https://example.com/my',
* fieldTypes: [
* {
* type: 'Symbol'
* },
* {
* type: 'Text'
* }
* ],
* sidebar: false
* }
* }))
* .then((extension) => console.log(extension))
* .catch(console.error)
* ```
*/
createUiExtensionWithId(id, data) {
const raw = this.toPlainObject();
return makeRequest({
entityType: 'Extension',
action: 'createWithId',
params: {
spaceId: raw.sys.space.sys.id,
environmentId: raw.sys.id,
extensionId: id,
},
payload: data,
}).then((response) => wrapExtension(makeRequest, response));
},
/**
* Creates an App Installation
* @param appDefinitionId - AppDefinition ID
* @param data - AppInstallation data
* @param options.acceptAllTerms - Flag for accepting Apps' Marketplace EULA, Terms, and Privacy policy (need to pass `{acceptAllTerms: true}` to install a marketplace app)
* @returns Promise for an App Installation
* @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) => environment.createAppInstallation('<app_definition_id>', {
* parameters: {
* someParameter: someValue
* }
* })
* .then((appInstallation) => console.log(appInstallation))
* .catch(console.error)
* ```
*/
createAppInstallation(appDefinitionId, data, { acceptAllTerms } = {}) {
const raw = this.toPlainObject();
return makeRequest({
entityType: 'AppInstallation',
action: 'upsert',
params: {
spaceId: raw.sys.space.sys.id,
environmentId: raw.sys.id,
appDefinitionId,
acceptAllTerms,
},
payload: data,
}).then((payload) => wrapAppInstallation(makeRequest, payload));
},
/**
* Gets an App Installation
* @param id - AppDefinition ID
* @returns Promise for an App Installation
* @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) => environment.getAppInstallation('<app-definition-id>'))
* .then((appInstallation) => console.log(appInstallation))
* .catch(console.error)
* ```
*/
getAppInstallation(id) {
const raw = this.toPlainObject();
return makeRequest({
entityType: 'AppInstallation',
action: 'get',
params: {
spaceId: raw.sys.space.sys.id,
environmentId: raw.sys.id,
appDefinitionId: id,
},
}).then((data) => wrapAppInstallation(makeRequest, data));
},
/**
* Gets a collection of App Installation
* @returns Promise for a collection of App Installations
* @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) => environment.getAppInstallations()
* .then((response) => console.log(response.items))
* .catch(console.error)
* ```
*/
getAppInstallations() {
const raw = this.toPlainObject();
return makeRequest({
entityType: 'AppInstallation',
action: 'getMany',
params: {
spaceId: raw.sys.space.sys.id,
environmentId: raw.sys.id,
},
}).then((data) => wrapAppInstallationCollection(makeRequest, data));
},
/**
* Creates an app action call
* @param appDefinitionId - AppDefinition ID
* @param appActionId - action ID
* @param data - App Action Call data
* @returns Promise for an App Action Call
* @example ```javascript
* const contentful = require('contentful-management')
*
* const client = contentful.createClient({
* accessToken: '<content_management_api_key>'
* })
*
* const data = {
* headers: {
* 'x-my-header': 'some-value'
* },
* body: {
* 'some-body-value': true
* }
* }
*
* client.getSpace('<space_id>')
* .then((space) => space.getEnvironment('<environment-id>'))
* .then((environment) => environment.createAppActionCall('<app_definition_id>', '<action_id>', data)
* .then((appActionCall) => console.log(appActionCall))
* .catch(console.error)
* ```
*/
createAppActionCall(appDefinitionId, appActionId, data) {
const raw = this.toPlainObject();
return makeRequest({
entityType: 'AppActionCall',
action: 'create',
params: {
spaceId: raw.sys.space.sys.id,
environmentId: raw.sys.id,
appDefinitionId,
appActionId,
},
payload: data,
}).then((payload) => wrapAppActionCall(makeRequest, payload));
},
/**
* Gets the raw response (headers/body) for a completed App Action Call
* @param appDefinitionId - AppDefinition ID
* @param appActionId - App Action ID
* @param callId - App Action Call ID
* @returns Promise for the raw response object including `response.body` and optional `response.headers`
* @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) => environment.getAppActionCallResponse('<app_definition_id>', '<app_action_id>', '<call_id>'))
* .then((raw) => console.log(raw.response.body))
* .catch(console.error)
* ```
*/
getAppActionCallResponse(appDefinitionId, appActionId, callId) {
const raw = this.toPlainObject();
return makeRequest({
entityType: 'AppActionCall',
action: 'getResponse',
params: {
spaceId: raw.sys.space.sys.id,
environmentId: raw.sys.id,
appDefinitionId,
appActionId,
callId,
},
});
},
/**
* Creates an app signed request
* @param appDefinitionId - AppDefinition ID
* @param data - SignedRequest data
* @returns Promise for a Signed Request
* @example ```javascript
* const contentful = require('contentful-management')
*
* const client = contentful.createClient({
* accessToken: '<content_management_api_key>'
* })
*
* const data = {
* method: 'POST',
* path: '/request_path',
* body: '{ "key": "data" }',
* headers: {
* 'x-my-header': 'some-value'
* },
* }
*
* client.getSpace('<space_id>')
* .then((space) => space.getEnvironment('<environment-id>'))
* .then((environment) => environment.createAppSignedRequest('<app_definition_id>', data)
* .then((signedRequest) => console.log(signedRequest))
* .catch(console.error)
* ```
*/
createAppSignedRequest(appDefinitionId, data) {
const raw = this.toPlainObject();
return makeRequest({
entityType: 'AppSignedRequest',
action: 'create',
params: {
spaceId: raw.sys.space.sys.id,
environmentId: raw.sys.id,
appDefinitionId,
},
payload: data,
}).then((payload) => wrapAppSignedRequest(makeRequest, payload));
},
/**
* Creates an app access token
* @param appDefinitionId - AppDefinition ID
* @param data - Json Web Token
* @returns Promise for an app access token
* @example ```javascript
* const contentful = require('contentful-management')
* const { sign } = require('jsonwebtoken')
*
* const signOptions = { algorithm: 'RS256', issuer: '<app_definition_id>', expiresIn: '10m' }
*
* const client = contentful.createClient({
* accessToken: '<content_management_api_key>'
* })
*
* const data = {
* jwt: sign({}, '<private_key>', signOptions)
* }
*
* client.getSpace('<space_id>')
* .then((space) => space.getEnvironment('<environment-id>'))
* .then((environment) => environment.createAppAccessToken('<app_definition_id>', data)
* .then((appAccessToken) => console.log(appAccessToken))
* .catch(console.error)
* ```
*/
createAppAccessToken(appDefinitionId, data) {
const raw = this.toPlainObject();
return makeRequest({
entityType: 'AppAccessToken',
action: 'create',
params: {
spaceId: raw.sys.space.sys.id,
environmentId: raw.sys.id,
appDefinitionId,
},
payload: data,
}).then((payload) => wrapAppAccessToken(makeRequest, payload));
},
/**
* Gets a collection of Functions for a given environment
* @param appInstallationId
* @param {import('../common-types').AcceptsQueryOptions} query - optional query parameter for filtering functions by action
* @returns Promise containing wrapped collection of Functions in 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) => environment.getFunctionsForEnvironment('<app-installation-id>', { 'accepts[all]': '<action>' }))
* .then((functions) => console.log(functions.items))
* .catch(console.error)
* ```
*/
getFunctionsForEnvironment(appInstallationId, query) {
const raw = this.toPlainObject();
return makeRequest({
entityType: 'Function',
action: 'getManyForEnvironment',
params: {
spaceId: raw.sys.space.sys.id,
environmentId: raw.sys.id,
appInstallationId,
query,
},
}).then((data) => wrapFunctionCollection(makeRequest, data));
},
/**
* Gets a collection of FunctionLogs for a given app installation id and FunctionId
* @param appInstallationId
* @param functionId
* @param {import('../common-types').CursorBasedParams} query - optional query parameter for pagination (limit, nextPage, prevPage)
* @returns Promise containing wrapped collection of FunctionLogs
* * @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) =>
* environment.getFunctionLogs(
* '<app-installation-id>',
* '<function-id>',
* {
* query: {
* // optional limit
* limit: 10,
* // optional interval query
* 'sys.createdAt[gte]': start,
* 'sys.createdAt[lt]': end,
* // optional cursor based pagination parameters
* pagePrev: '<page_prev>',
* },
* },
* )
* )
* .then((functionLogs) => console.log(functionLog.items))
* .catch(console.error)
* ```
*/
getFunctionLogs(appInstallationId, functionId, query) {
const raw = this.toPlainObject();
return makeRequest({
entityType: 'FunctionLog',
action: 'getMany',
params: {
spaceId: raw.sys.space.sys.id,
environmentId: raw.sys.id,
appInstallationId,
functionId,
query: query ? createRequestConfig({ query }).params : undefined,
},
}).then((data) => wrapFunctionLogCollection(makeRequest, data));
},
/**
* Gets a FunctionLog by appInstallationId, functionId and logId
* @param appInstallationId
* @param functionId
* @param logId
* @returns Promise containing a wrapped FunctionLog
* @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) =>
* environment.getFunctionLog(
* '<app-installation-id>',
* '<function-id>',
* '<log-id>'
* )
* )
* .then((functionLog) => console.log(functionLog))
* .catch(console.error)
* ```
*/
getFunctionLog(appInstallationId, functionId, logId) {
const raw = this.toPlainObject();
return makeRequest({
entityType: 'FunctionLog',
action: 'get',
params: {
spaceId: raw.sys.space.sys.id,
environmentId: raw.sys.id,
appInstallationId,
functionId,
logId,
},
}).then((data) => wrapFunctionLog(makeRequest, data));
},
/**
* Gets all snapshots of an entry
* @func getEntrySnapshots
* @param entryId - Entry ID
* @param query - query additional query paramaters
* @returns Promise for a collection of Entry Snapshots
* @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) => environment.getEntrySnapshots('<entry_id>'))
* .then((snapshots) => console.log(snapshots.items))
* .catch(console.error)
* ```
*/
getEntrySnapshots(entryId, query = {}) {
const raw = this.toPlainObject();
return makeRequest({
entityType: 'Snapshot',
action: 'getManyForEntry',
params: {
spaceId: raw.sys.space.sys.id,
environmentId: raw.sys.id,
entryId,
query,
},
}).then((data) => wrapSnapshotCollection(makeRequest, data));
},
/**
* Gets all snapshots of a contentType
* @func getContentTypeSnapshots
* @param contentTypeId - Content Type ID
* @param query - query additional query paramaters
* @returns Promise for a collection of Content Type Snapshots
* @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) => environment.getContentTypeSnapshots('<contentTypeId>'))
* .then((snapshots) => console.log(snapshots.items))
* .catch(console.error)
* ```
*/
getContentTypeSnapshots(contentTypeId, query = {}) {
const raw = this.toPlainObject();
return makeRequest({
entityType: 'Snapshot',
action: 'getManyForContentType',
params: {
spaceId: raw.sys.space.sys.id,
environmentId: raw.sys.id,
contentTypeId,
query,
},
}).then((data) => wrapSnapshotCollection(makeRequest, data));
},
createTag(id, name, visibility) {
const raw = this.toPlainObject();
return makeRequest({
entityType: 'Tag',
action: 'createWithId',
params: {
spaceId: raw.sys.space.sys.id,
environmentId: raw.sys.id,
tagId: id,
},
payload: {
name,
sys: { visibility: visibility ?? 'private' },
},
}).then((data) => wrapTag(makeRequest, data));
},
getTags(query = {}) {
const raw = this.toPlainObject();
return makeRequest({
entityType: 'Tag',
action: 'getMany',
params: {
spaceId: raw.sys.space.sys.id,
environmentId: raw.sys.id,
query: createRequestConfig({ query }).params,
},
}).then((data) => wrapTagCollection(makeRequest, data));
},
getTag(id) {
const raw = this.toPlainObject();
return makeRequest({
entityType: 'Tag',
action: 'get',
params: {
spaceId: raw.sys.space.sys.id,
environmentId: raw.sys.id,
tagId: id,
},
}).then((data) => wrapTag(makeRequest, data));
},
/**
* Retrieves a Release by ID
* @param releaseId
* @returns Promise containing a wrapped Release
* @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) => environment.getRelease('<release_id>'))
* .then((release) => console.log(release))
* .catch(console.error)
* ```
*/
getRelease(releaseId) {
const raw = this.toPlainObject();
return makeRequest({
entityType: 'Release',
action: 'get',
params: {
spaceId: raw.sys.space.sys.id,
environmentId: raw.sys.id,
releaseId,
},
}).then((data) => wrapRelease(makeRequest, data));
},
/**
* Gets a Collection of Releases,
* @param {ReleaseQueryOptions} query filtering options for the collection result
* @returns Promise containing a wrapped Release Collection
* @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) => environment.getReleases({ 'entities.sys.id[in]': '<asset_id>,<entry_id>' }))
* .then((releases) => console.log(releases))
* .catch(console.error)
* ```
*/
getReleases(query) {
const raw = this.toPlainObject();
return makeRequest({
entityType: 'Release',
action: 'query',
params: {
spaceId: raw.sys.space.sys.id,
environmentId: raw.sys.id,
query,
},
}).then((data) => wrapReleaseCollection(makeRequest, data));
},
/**
* Creates a new Release with the entities and title in the payload
* @param payload Object containing the payload in order to create a Release
* @returns Promise containing a wrapped Release, that has other helper methods within.
* @example ```javascript
* const contentful = require('contentful-management')
*
* const client = contentful.createClient({
* accessToken: '<content_management_api_key>'
* })
*
* const payload = {
* title: 'My Release',
* entities: {
* sys: { type: 'Array' },
* items: [
* { sys: { linkType: 'Entry', type: 'Link', id: '<entry_id>' } }
* ]
* }
* }
*
* client.getSpace('<space_id>')
* .then((space) => space.getEnvironment('<environment-id>'))
* .then((environment) => environment.createRelease(payload))
* .then((release) => console.log(release))
* .catch(console.error)
* ```
*/
createRelease(payload) {
const raw = this.toPlainObject();
return makeRequest({
entityType: 'Release',
action: 'create',
params: {
spaceId: raw.sys.space.sys.id,
environmentId: raw.sys.id,
},
payload,
}).then((data) => wrapRelease(makeRequest, data));
},
/**
* Updates a Release and replaces all the properties.
* @param {object} options,
* @param options.releaseId the ID of the release
* @param options.payload the payload to be updated in the Release
* @param options.version Release sys.version that to be updated
* @returns Promise containing a wrapped Release, that has helper methods within.
*
* @example ```javascript
* const contentful = require('contentful-management')
*
* const client = contentful.createClient({
* accessToken: '<content_management_api_key>'
* })
*
*
* const payload = {
* title: "Updated Release title",
* entities: {
* sys: { type: 'Array' },
* items: [
* { sys: { linkType: 'Entry', type: 'Link', id: '<entry_id>' } }
* ]
* }
* }
*
* client.getSpace('<space_id>')
* .then((space) => space.getEnvironment('<environment-id>'))
* .then((environment) => environment.updateRelease({ releaseId: '<release_id>', version: 1, payload } ))
* .then((release) => console.log(release))
* .catch(console.error)
* ```
*/
updateRelease({ releaseId, payload, version, }) {
const raw = this.toPlainObject();
return makeRequest({
entityType: 'Release',
action: 'update',
params: {
spaceId: raw.sys.space.sys.id,
environmentId: raw.sys.id,
releaseId,
version,
},
payload,
}).then((data) => wrapRelease(makeRequest, data));
},
/**
* Deletes a Release by ID - does not delete any entities.
* @param releaseId the ID of the release
*
* @returns Promise containing a wrapped Release, that has helper methods within.
* @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) => environment.deleteRelease('<release_id>')
* .catch(console.error)
* ```
*/
deleteRelease(releaseId) {
const raw = this.toPlainObject();
return makeRequest({
entityType: 'Release',
action: 'delete',
params: {
spaceId: raw.sys.space.sys.id,
environmentId: raw.sys.id,
releaseId,
},
});
},
/**
* Publishes all Entities contained in a Release.
* @param options.releaseId the ID of the release
* @param options.version the version of the release that is to be published
* @returns Promise containing a wrapped Release, that has helper methods within.
*
* @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) => environment.publishRelease({ releaseId: '<release_id>', version: 1 }))
* .catch(console.error)
* ```
*/
publishRelease({ releaseId, version }) {
const raw = this.toPlainObject();
return makeRequest({
entityType: 'Release',
action: 'publish',
params: {
spaceId: raw.sys.space.sys.id,
environmentId: raw.sys.id,
releaseId,
version,
},
}).then((data) => wrapReleaseAction(makeRequest, data));
},
/**
* Unpublishes all Entities contained in a Release.
* @param options.releaseId the ID of the release
* @param options.version the version of the release that is to be published
* @returns Promise containing a wrapped Release, that has helper methods within.
*
* @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) => environment.unpublishRelease({ releaseId: '<release_id>', version: 1 }))
* .catch(console.error)
* ```
*/
unpublishRelease({ releaseId, version }) {
const raw = this.toPlainObject();
return makeRequest({
entityType: 'Release',
action: 'unpublish',
params: {
spaceId: raw.sys.space.sys.id,
environmentId: raw.sys.id,
releaseId,
version,
},
}).then((data) => wrapReleaseAction(makeRequest, data));
},
/**
* Validates all Entities contained in a Release against an action (publish or unpublish)
* @param options.releaseId the ID of the release
* @param options.payload (optional) the type of action to be validated against
*
* @returns Promise containing a wrapped Release, that has helper methods within.
*
* @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) => environment.validateRelease({ releaseId: '<release_id>', payload: { action: 'unpublish' } }))
* .catch(console.error)
* ```
*/
validateRelease({ releaseId, payload, }) {
const raw = this.toPlainObject();
return makeRequest({
entityType: 'Release',
action: 'validate',
params: {
spaceId: raw.sys.space.sys.id,
environmentId: raw.sys.id,
releaseId,
},
payload,
}).then((data) => wrapReleaseAction(makeRequest, data));
},
/**
* Archives a Release and prevents new operations (publishing, unpublishing adding new entities etc).
* @param options.releaseId the ID of the release
* @param options.version the version of the release that is to be archived
* @returns Promise containing a wrapped Release, that has helper methods within.
*
* @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) => environment.archiveRelease({ releaseId: '<release_id>', version: 1 }))
* .catch(console.error)
* ```
*/
archiveRelease({ releaseId, version }) {
const raw = this.toPlainObject();
return makeRequest({
entityType: 'Release',
action: 'archive',
params: {
spaceId: raw.sys.space.sys.id,
environmentId: raw.sys.id,
releaseId,
version,
},
}).then((data) => wrapRelease(makeRequest, data));
},
/**
* Unarchives a previously archived Release - this enables the release to be published, unpublished etc.
* @param options.releaseId the ID of the release
* @param options.version the version of the release that is to be unarchived
* @returns Promise containing a wrapped Release, that has helper methods within.
*
* @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) => environment.unarchiveRelease({ releaseId: '<release_id>', version: 1 }))
* .catch(console.error)
* ```
*/
unarchiveRelease({ releaseId, version }) {
const raw = this.toPlainObject();
return makeRequest({
entityType: 'Release',
action: 'unarchive',
params: {
spaceId: raw.sys.space.sys.id,
environmentId: raw.sys.id,
releaseId,
version,
},
}).then((data) => wrapRelease(makeRequest, data));
},
/**
* Retrieves a ReleaseAction by ID
* @param params.releaseId The ID of a Release
* @param params.actionId The ID of a Release Action
* @returns Promise containing a wrapped ReleaseAction
* @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) => environment.getReleaseAction({ releaseId: '<release_id>', actionId: '<action_id>' }))
* .then((releaseAction) => console.log(releaseAction))
* .catch(console.error)
* ```
*/
getReleaseAction({ actionId, releaseId }) {
const raw = this.toPlainObject();
return makeRequest({
entityType: 'ReleaseAction',
action: 'get',
params: {
actionId,
spaceId: raw.sys.space.sys.id,
environmentId: raw.sys.id,
releaseId,
},
}).then((data) => wrapReleaseAction(makeRequest, data));
},
/**
* Gets a Collection of ReleaseActions
* @param {string} params.releaseId ID of the Release to fetch the actions from
* @param {ReleaseQueryOptions} params.query filtering options for the collection result
* @returns Promise containing a wrapped ReleaseAction Collection
*
* @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) => environment.getReleaseActions({ query: { 'sys.id[in]': '<id_1>,<id_2>', 'sys.release.sys.id[in]': '<id1>,<id2>' } }))
* .then((releaseActions) => console.log(releaseActions))
* .catch(console.error)
* ```
*/
getReleaseActions({ query }) {
const raw = this.toPlainObject();
return makeRequest({
entityType: 'ReleaseAction',
action: 'getMany',
params: {
spaceId: raw.sys.space.sys.id,
environmentId: raw.sys.id,
query,
},
}).then((data) => wrapReleaseActionCollection(makeRequest, data));
},
async getUIConfig() {
const raw = this.toPlainObject();
const data = await makeRequest({
entityType: 'UIConfig',
action: 'get',
params: {
spaceId: raw.sys.space.sys.id,
environmentId: raw.sys.id,
},
});
return wrapUIConfig(makeRequest, data);
},
async getUserUIConfig() {
const raw = this.toPlainObject();
const data = await makeRequest({
entityType: 'UserUIConfig',
action: 'get',
params: {
spaceId: raw.sys.space.sys.id,
environmentId: raw.sys.id,
},
});
return wrapUserUIConfig(makeRequest, data);
},
/**
* Gets a collection of all environment template installations in the environment for a given template
* @param environmentTemplateId - Environment template ID to return installations for
* @param [options.installationId] - Installation ID to filter for a specific installation
* @returns Promise for a collection of EnvironmentTemplateInstallations
* ```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) => environment.getEnvironmentTemplateInstallations('<environment_template_id>'))
* .then((installations) => console.log(installations.items))
* .catch(console.error)
* ```
*/
async getEnvironmentTemplateInstallations(environmentTemplateId, { installationId, ...query } = {}) {
const raw = this.toPlainObject();
return makeRequest({
entityType: 'EnvironmentTemplateInstallation',
action: 'getForEnvironment',
params: {
environmentTemplateId,
...(installationId && { installationId }),
query: { ...createRequestConfig({ query }).params },
spaceId: raw.sys.space.sys.id,
environmentId: raw.sys.id,
},
}).then((data) => wrapEnvironmentTemplateInstallationCollection(makeRequest, data));
},
/**
* Gets a collection of all resource types based on native external references app installations in the environment
* @param query - BasicCursorPaginationOptions
* @returns Promise for a collection of ResourceTypes
* ```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) => environment.getResourceTypes({limit: 10}))
* .then((installations) => console.log(installations.items))
* .catch(console.error)
* ```
*/
async getResourceTypes(query) {
const raw = this.toPlainObject();
return makeRequest({
entityType: 'ResourceType',
action: 'getForEnvironment',
params: {
query,
spaceId: raw.sys.space.sys.id,
environmentId: raw.sys.id,
},
}).then((data) => wrapResourceTypesForEnvironmentCollection(makeRequest, data));
},
/**
* Gets a collection of all resources for a given resource type based on native external references app installations in the environment
* @param resourceTypeId - Id of the resourceType to get its resources
* @param query - Either LookupQuery options with 'sys.urn[in]' param or a Search query with 'query' param, in both cases you can add pagination options
* @returns Promise for a collection of Resources for a given resourceTypeId
* ```javascript
* const contentful = require('contentful-management')
*
* const client = contentful.createClient({
* accessToken: '<content_management_api_key>'
* })
*
* // Search Query
* client.getSpace('<space_id>')
* .then((space) => space.getEnvironment('<environment_id>'))
* // <search_query> is a string you want to search for in the external resources
* .then((environment) => environment.getResourcesForResourceType('<resource_type_id>', {query: '<search_query>', limit: 10}))
* .then((installations) => console.log(installations.items))
* .catch(console.error)
*
* // Lookup query
*
* client.getSpace('<space_id>')
* .then((space) => space.getEnvironment('<environment_id>'))
* .then((environment) => environment.getResourcesForResourceType('<resource_type_id>', {'sys.urn[in]': '<resource_urn1>,<resource_urn2>', limit: 10}))
* .then((installations) => console.log(installations.items))
* .catch(console.error)
* ```
*/
async getResourcesForResourceType(resourceTypeId, query) {
const raw = this.toPlainObject();
return makeRequest({
entityType: 'Resource',
action: 'getMany',
params: {
query,
spaceId: raw.sys.space.sys.id,
environmentId: raw.sys.id,
resourceTypeId,
},
}).then((data) => wrapResourceCollection(makeRequest, data));
},
/**
* Invokes an AI Action.
* @param aiActionId - The ID of the AI Action to invoke.
* @param payload - The invocation payload.
* @returns Promise for an AI Action Invocation.
* @example ```javascript
* client.getSpace('<space_id>')
* .then(space => space.getEnvironment('<environment_id>'))
* .then(environment => environment.invokeAiAction('<ai_action_id>', {
* variables: [ ... ],
* outputFormat: 'RichText'
* }))
* .then(invocation => console.log(invocation))
* .catch(console.error)
* ```
*/
invokeAiAction(aiActionId, payload) {
const raw = this.toPlainObject();
return makeRequest({
entityType: 'AiAction',
action: 'invoke',
params: { spaceId: raw.sys.space.sys.id, environmentId: raw.sys.id, aiActionId },
payload,
}).then((data) => wrapAiActionInvocation(makeRequest, data));
},
/**
* Retrieves an AI Action Invocation.
* @param params - Object containing the AI Action ID and the Invocation ID.
* @returns Promise for an AI Action Invocation.
* @example ```javascript
* client.getSpace('<space_id>')
* .then(space => space.getEnvironment('<environment_id>'))
* .then(environment => environment.getAiActionInvocation({
* aiActionId: '<ai_action_id>',
* invocationId: '<invocation_id>'
* }))
* .then(invocation => console.log(invocation))
* .catch(console.error)
* ```
*/
getAiActionInvocation({ aiActionId, invocationId, }) {
const raw = this.toPlainObject();
return makeRequest({
entityType: 'AiActionInvocation',
action: 'get',
params: {
spaceId: raw.sys.space.sys.id,
environmentId: raw.sys.id,
aiActionId,
invocationId,
},
}).then((data) => wrapAiActionInvocation(makeRequest, data));
},
/**
* Retrieves Semantic Duplicates for the given entity ID
* @param payload - Object containing the entityId and optional filters
* @returns Promise for Semantic Duplicates
* @example ```javascript
* client.getSpace('<space_id>')
* .then(space => space.getEnvironment('<environment_id>'))
* .then(environment => environment.getSemanticDuplicates({
* entityId: '<entity_id>',
* filters: {
* contentTypeIds: ['<content_type_id1>', '<content_type_id2>'],
* }
* })
*/
getSemanticDuplicates(payload) {
const raw = this.toPlainObject();
return makeRequest({
entityType: 'SemanticDuplicates',
action: 'get',
params: {
spaceId: raw.sys.space.sys.id,
environmentId: raw.sys.id,
},
payload,
}).then((data) => wrapSemanticDuplicates(makeRequest, data));
},
/**
* Retrieves Semantic Recommendations for the given entity IDs
* @param payload - Object containing the entityIds and optional filters
* @returns Promise for Semantic Recommendations
* @example ```javascript
* client.getSpace('<space_id>')
* .then(space => space.getEnvironment('<environment_id>'))
* .then(environment => environment.getSemanticRecommendations({
* entityIds: ['<entity_id>'],
* filters: {
* contentTypeIds: ['<content_type_id1>', '<content_type_id2>'],
* }
* })
*/
getSemanticRecommendations(payload) {
const raw = this.toPlainObject();
return makeRequest({
entityType: 'SemanticRecommendations',
action: 'get',
params: {
spaceId: raw.sys.space.sys.id,
environmentId: raw.sys.id,
},
payload,
}).then((data) => wrapSemanticRecommendations(makeRequest, data));
},
/**
* Retrieves Semantic Reference Suggestions for the given entity ID and its reference field ID
* @param payload - Object containing the entityId and referenceFieldId
* @returns Promise for Semantic Reference Suggestions
* @example ```javascript
* client.getSpace('<space_id>')
* .then(space => space.getEnvironment('<environment_id>'))
* .then(environment => environment.getSemanticReferenceSuggestions({
* entityId: '<entity_id>',
* referenceFieldId: '<reference_field_id>',
* })
*/
getSemanticReferenceSuggestions(payload) {
const raw = this.toPlainObject();
return makeRequest({
entityType: 'SemanticReferenceSuggestions',
action: 'get',
params: {
spaceId: raw.sys.space.sys.id,
environmentId: raw.sys.id,
},
payload,
}).then((data) => wrapSemanticReferenceSuggestions(makeRequest, data));
},
/**
* Retrieves Semantic Search results for the given query
* @param payload - Object containing the search query and optional filters
* @returns Promise for Semantic Search results
* @example ```javascript
* client.getSpace('<space_id>')
* .then(space => space.getEnvironment('<environment_id>'))
* .then(environment => environment.getSemanticSearch({
* query: '<search_query>',
* filters: {
* contentTypeIds: ['<content_type_id1>', '<content_type_id2>'],
* }
* })
*/
getSemanticSearch(payload) {
const raw = this.toPlainObject();
return makeRequest({
entityType: 'SemanticSearch',
action: 'get',
params: {
spaceId: raw.sys.space.sys.id,
environmentId: raw.sys.id,
},
payload,
}).then((data) => wrapSemanticSearch(makeRequest, data));
},
/**
* Gets all content semantics indexes for the environment
* @return Promise for a collection of ContentSemanticsIndex
* @example ```javascript
* client.getSpace('<space_id>')
* .then(space => space.getEnvironment('<environment_id>'))
* .then(environment => environment.getContentSemanticsIndexes())
*/
getContentSemanticsIndexes() {
const raw = this.toPlainObject();
return makeRequest({
entityType: 'ContentSemanticsIndex',
action: 'getManyForEnvironment',
params: {
spaceId: raw.sys.space.sys.id,
environmentId: raw.sys.id,
},
}).then((data) => wrapContentSemanticsIndexCollection(makeRequest, data));
},
/**
* Gets an AI Agent
* @param agentId - AI Agent ID
* @returns Promise for an AI Agent
* @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) => environment.getAgent('<agent_id>'))
* .then((agent) => console.log(agent))
* .catch(console.error)
* ```
*/
getAgent(agentId) {
const raw = this.toPlainObject();
return makeRequest({
entityType: 'Agent',
action: 'get',
params: {
spaceId: raw.sys.space.sys.id,
environmentId: raw.sys.id,
agentId,
},
}).then((data) => wrapAgent(makeRequest, data));
},
/**
* Gets a collection of AI Agents
* @returns Promise for a collection of AI Agents
* @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) => environment.getAgents())
* .then((response) => console.log(response.items))
* .catch(console.error)
* ```
*/
getAgents() {
const raw = this.toPlainObject();
return makeRequest({
entityType: 'Agent',
action: 'getMany',
params: {
spaceId: raw.sys.space.sys.id,
environmentId: raw.sys.id,
},
}).then((data) => wrapAgentCollection(makeRequest, data));
},
/**
* Generates content using an AI Agent
* @param agentId - AI Agent ID
* @param payload - Generation payload
* @returns Promise for a simplified response containing `sys.id`, `sys.type`, and `sys.status`.
* Use `getAgentRun()` with the returned `sys.id` to poll for full results.
* @example ```javascript
* const contentful = require('contentful-management')
*
* async function generateContent() {
* const client = contentful.createClient({
* accessToken: '<content_management_api_key>'
* })
*
* const space = await client.getSpace('<space_id>')
* const environment = await space.getEnvironment('<environment_id>')
*
* // Start generation (returns 202 Accepted)
* const response = await environment.generateWithAgent('<agent_id>', {
* messages: [
* {
* parts: [{ type: 'text', text: 'Write a short poem about Contentful' }],
* role: 'user'
* }
* ]
* })
*
* // Poll for full results
* let run = await environment.getAgentRun(response.sys.id)
* while (run.sys.status === 'IN_PROGRESS') {
* await new Promise((resolve) => setTimeout(resolve, 1000))
* run = await environment.getAgentRun(response.sys.id)
* }
*
* console.log(run)
* }
* ```
*/
generateWithAgent(agentId, payload) {
const raw = this.toPlainObject();
return makeRequest({
entityType: 'Agent',
action: 'generate',
params: {
spaceId: raw.sys.space.sys.id,
environmentId: raw.sys.id,
agentId,
},
payload,
}).then((data) => wrapAgentGenerateResponse(makeRequest, data));
},
/**
* Gets an AI Agent Run
* @param runId - AI Agent Run ID
* @returns Promise for an AI Agent Run
* @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) => environment.getAgentRun('<run_id>'))
* .then((run) => console.log(run))
* .catch(console.error)
* ```
*/
getAgentRun(runId) {
const raw = this.toPlainObject();
return makeRequest({
entityType: 'AgentRun',
action: 'get',
params: {
spaceId: raw.sys.space.sys.id,
environmentId: raw.sys.id,
runId,
},
}).then((data) => wrapAgentRun(makeRequest, data));
},
/**
* Gets a collection of AI Agent Runs with optional filtering
* @param query - Object with search parameters (agentIn, statusIn)
* @returns Promise for a collection of AI Agent Runs
* @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) => environment.getAgentRuns({
* agentIn: ['agent1', 'agent2'],
* statusIn: ['COMPLETED', 'IN_PROGRESS']
* }))
* .then((response) => console.log(response.items))
* .catch(console.error)
* ```
*/
getAgentRuns(query = {}) {
const raw = this.toPlainObject();
return makeRequest({
entityType: 'AgentRun',
action: 'getMany',
params: {
spaceId: raw.sys.space.sys.id,
environmentId: raw.sys.id,
query,
},
}).then((data) => wrapAgentRunCollection(makeRequest, data));
},
};
}
export { createEnvironmentApi as default };
//# sourceMappingURL=create-environment-api.mjs.map