contentful-management
Version:
Client for Contentful's Content Management API
1,379 lines (1,377 loc) • 79.2 kB
JavaScript
const _excluded = ["installationId"];
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var s = Object.getOwnPropertySymbols(e); for (r = 0; r < s.length; r++) o = s[r], t.includes(o) || {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (e.includes(n)) continue; t[n] = r[n]; } return t; }
import { createRequestConfig } from 'contentful-sdk-core';
import entities from './entities';
import { wrapReleaseAction, wrapReleaseActionCollection } from './entities/release-action';
import { wrapRelease, wrapReleaseCollection } from './entities/release';
import { wrapTag, wrapTagCollection } from './entities/tag';
import { wrapUIConfig } from './entities/ui-config';
import { wrapUserUIConfig } from './entities/user-ui-config';
import { wrapEnvironmentTemplateInstallationCollection } from './entities/environment-template-installation';
/**
* @private
*/
/**
* Creates API object with methods to access the Environment API
* @param {ContentfulEnvironmentAPI} makeRequest - function to make requests via an adapter
* @return {ContentfulSpaceAPI}
* @private
*/
export default function createEnvironmentApi(makeRequest) {
const {
wrapEnvironment
} = entities.environment;
const {
wrapContentType,
wrapContentTypeCollection
} = entities.contentType;
const {
wrapEntry,
wrapEntryCollection
} = entities.entry;
const {
wrapAsset,
wrapAssetCollection
} = entities.asset;
const {
wrapAssetKey
} = entities.assetKey;
const {
wrapLocale,
wrapLocaleCollection
} = entities.locale;
const {
wrapSnapshotCollection
} = entities.snapshot;
const {
wrapEditorInterface,
wrapEditorInterfaceCollection
} = entities.editorInterface;
const {
wrapUpload
} = entities.upload;
const {
wrapExtension,
wrapExtensionCollection
} = entities.extension;
const {
wrapAppInstallation,
wrapAppInstallationCollection
} = entities.appInstallation;
const {
wrapAppSignedRequest
} = entities.appSignedRequest;
const {
wrapAppActionCall
} = entities.appActionCall;
const {
wrapBulkAction
} = entities.bulkAction;
const {
wrapAppAccessToken
} = entities.appAccessToken;
return {
/**
* Deletes the environment
* @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.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
* @return 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
* @return 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
* @return 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
* @return 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.
* @return 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));
},
/**
* Creates a Content Type
* @param data - Object representation of the Content Type to be created
* @return 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
* @return 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
* @return 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
* @return 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`.
* @return 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
* @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.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.
* @return 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 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.
* @return 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));
},
/**
* Creates a Entry
* @param contentTypeId - The Content Type ID of the newly created Entry
* @param data - Object representation of the Entry to be created
* @return 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
* @return 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 === null || options === void 0 ? void 0 : 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`.
* @return 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.
* @return 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 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.
* @return 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));
},
/**
* 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.
* @return 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.
* @return 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.
* @return 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 === null || options === void 0 ? void 0 : 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)
* @return 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
* @return 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.
* @return 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
* @return 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
* @return 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() {
const raw = this.toPlainObject();
return makeRequest({
entityType: 'Locale',
action: 'getMany',
params: {
spaceId: raw.sys.space.sys.id,
environmentId: raw.sys.id
}
}).then(data => wrapLocaleCollection(makeRequest, data));
},
/**
* Creates a Locale
* @param data - Object representation of the Locale to be created
* @return 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
* @return 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
* @return 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
* @return 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.spac