contentful-management
Version:
Client for Contentful's Content Management API
316 lines • 13 kB
JavaScript
const _excluded = ["spaceId", "environmentId"];
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';
export function createEnvironmentTemplateApi(makeRequest, organizationId) {
const {
wrapEnvironmentTemplate,
wrapEnvironmentTemplateCollection
} = entities.environmentTemplate;
const {
wrapEnvironmentTemplateInstallationCollection
} = entities.environmentTemplateInstallation;
return {
/**
* Updates a environment template
* @return Promise for new version of the template
* ```javascript
* const contentful = require('contentful-management')
*
* const client = contentful.createClient({
* accessToken: '<content_management_api_key>'
* })
*
* client.getEnvironmentTemplate('<organization_id>', '<environment_template_id>')
* .then((environmentTemplate) => {
* environmentTemplate.name = 'New name'
* return environmentTemplate.update()
* })
* .then((environmentTemplate) =>
* console.log(`Environment template ${environmentTemplate.sys.id} renamed.`)
* ).catch(console.error)
* ```
*/
update: function updateEnvironmentTemplate() {
const raw = this.toPlainObject();
return makeRequest({
entityType: 'EnvironmentTemplate',
action: 'update',
params: {
organizationId,
environmentTemplateId: raw.sys.id
},
payload: raw
}).then(data => wrapEnvironmentTemplate(makeRequest, data, organizationId));
},
/**
* Updates environment template version data
* @param version.versionName - Name of the environment template version
* @param version.versionDescription - Description of the environment template version
* @return Promise for an updated EnvironmentTemplate
* ```javascript
* const contentful = require('contentful-management')
*
* const client = contentful.createClient({
* accessToken: '<content_management_api_key>'
* })
*
* client.getEnvironmentTemplate('<organization_id>', '<environment_template_id>')
* .then((environmentTemplate) => {
* return environmentTemplate.updateVersion({
* versionName: 'New Name',
* versionDescription: 'New Description',
* })
* })
* .then((environmentTemplate) =>
* console.log(`Environment template version ${environmentTemplate.sys.id} renamed.`)
* ).catch(console.error)
* ```
*/
updateVersion: function updateEnvironmentTemplateVersion({
versionName,
versionDescription
}) {
const raw = this.toPlainObject();
return makeRequest({
entityType: 'EnvironmentTemplate',
action: 'versionUpdate',
params: {
organizationId,
environmentTemplateId: raw.sys.id,
version: raw.sys.version
},
payload: {
versionName,
versionDescription
}
}).then(data => wrapEnvironmentTemplate(makeRequest, data, organizationId));
},
/**
* Deletes the environment template
* @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.getEnvironmentTemplate('<organization_id>', '<environment_template_id>')
* .then((environmentTemplate) => environmentTemplate.delete())
* .then(() => console.log('Environment template deleted.'))
* .catch(console.error)
* ```
*/
delete: function deleteEnvironmentTemplate() {
const raw = this.toPlainObject();
return makeRequest({
entityType: 'EnvironmentTemplate',
action: 'delete',
params: {
organizationId,
environmentTemplateId: raw.sys.id
}
});
},
/**
* Gets a collection of all versions for the environment template
* @return Promise for a EnvironmentTemplate
* ```javascript
* const contentful = require('contentful-management')
*
* const client = contentful.createClient({
* accessToken: '<content_management_api_key>'
* })
* client.getEnvironmentTemplate('<organization_id>', '<environment_template_id>')
* .then((environmentTemplate) => environmentTemplate.getVersions())
* .then((environmentTemplateVersions) => console.log(environmentTemplateVersions.items))
* .catch(console.error)
* ```
*/
getVersions: function getEnvironmentTemplateVersions() {
const raw = this.toPlainObject();
return makeRequest({
entityType: 'EnvironmentTemplate',
action: 'versions',
params: {
organizationId,
environmentTemplateId: raw.sys.id
}
}).then(data => wrapEnvironmentTemplateCollection(makeRequest, data, organizationId));
},
/**
* Gets a collection of all installations for the environment template
* @param [installationParams.spaceId] - Space ID to filter installations by space and environment
* @param [installationParams.environmentId] - Environment ID to filter installations by space and environment
* @return Promise for a collection of EnvironmentTemplateInstallations
* ```javascript
* const contentful = require('contentful-management')
*
* const client = contentful.createClient({
* accessToken: '<content_management_api_key>'
* })
*
* client.getEnvironmentTemplate('<organization_id>', '<environment_template_id>')
* .then((environmentTemplate) => environmentTemplate.getInstallations())
* .then((environmentTemplateInstallations) =>
* console.log(environmentTemplateInstallations.items)
* )
* .catch(console.error)
* ```
*/
getInstallations: function getEnvironmentTemplateInstallations(_ref = {}) {
let {
spaceId,
environmentId
} = _ref,
query = _objectWithoutProperties(_ref, _excluded);
const raw = this.toPlainObject();
return makeRequest({
entityType: 'EnvironmentTemplateInstallation',
action: 'getMany',
params: {
organizationId,
environmentTemplateId: raw.sys.id,
query: _objectSpread({}, createRequestConfig({
query
}).params),
spaceId,
environmentId
}
}).then(data => wrapEnvironmentTemplateInstallationCollection(makeRequest, data));
},
/**
* Validates an environment template against a given space and environment
* @param params.spaceId - Space ID where the template should be installed into
* @param params.environmentId - Environment ID where the template should be installed into
* @param [params.version] - Version of the template
* @param [params.installation.takeover] - Already existing Content types to takeover in the target environment
* @param [params.changeSet] - Change set which should be applied
* @return Promise for a EnvironmentTemplateValidation
* ```javascript
* const contentful = require('contentful-management')
*
* const client = contentful.createClient({
* accessToken: '<content_management_api_key>'
* })
*
* client.getEnvironmentTemplate('<organization_id>', '<environment_template_id>')
* .then((environmentTemplate) => environmentTemplate.validate({
* spaceId: '<space_id>',
* environmentId: '<environment_id>',
* version: <version>,
* }))
* .then((validationResult) => console.log(validationResult))
* .catch(console.error)
* ```
*/
validate: function validateEnvironmentTemplate({
spaceId,
environmentId,
version,
takeover,
changeSet
}) {
const raw = this.toPlainObject();
return makeRequest({
entityType: 'EnvironmentTemplate',
action: 'validate',
params: {
spaceId,
version,
environmentId,
environmentTemplateId: raw.sys.id
},
payload: _objectSpread(_objectSpread({}, takeover && {
takeover
}), changeSet && {
changeSet
})
});
},
/**
* Installs a template against a given space and environment
* @param params.spaceId - Space ID where the template should be installed into
* @param params.environmentId - Environment ID where the template should be installed into
* @param params.installation.version- Template version which should be installed
* @param [params.installation.takeover] - Already existing Content types tp takeover in the target environment
* @param [params.changeSet] - Change set which should be applied
* @return Promise for a EnvironmentTemplateInstallation
* ```javascript
* const contentful = require('contentful-management')
*
* const client = contentful.createClient({
* accessToken: '<content_management_api_key>'
* })
*
* client.getEnvironmentTemplate('<organization_id>', '<environment_template_id>')
* .then((environmentTemplate) => environmentTemplate.validate({
* spaceId: '<space_id>',
* environmentId: '<environment_id>',
* installation: {
* version: <version>,
* }
* }))
* .then((installation) => console.log(installation))
* .catch(console.error)
* ```
*/
install: function installEnvironmentTemplate({
spaceId,
environmentId,
installation
}) {
const raw = this.toPlainObject();
return makeRequest({
entityType: 'EnvironmentTemplate',
action: 'install',
params: {
spaceId,
environmentId,
environmentTemplateId: raw.sys.id
},
payload: installation
});
},
/**
* Disconnects the template from a given environment
* @param params.spaceId - Space ID where the template should be installed into
* @param params.environmentId - Environment ID where the template should be installed into
* @return Promise for the disconnection with no data
* ```javascript
* const contentful = require('contentful-management')
*
* const client = contentful.createClient({
* accessToken: '<content_management_api_key>'
* })
*
* client.getEnvironmentTemplate('<organization_id>', '<environment_template_id>')
* .then(environmentTemplate) => environmentTemplate.disconnected())
* .then(() => console.log('Template disconnected'))
* .catch(console.error)
* ```
*/
disconnect: function disconnectEnvironmentTemplate({
spaceId,
environmentId
}) {
const raw = this.toPlainObject();
return makeRequest({
entityType: 'EnvironmentTemplate',
action: 'disconnect',
params: {
spaceId,
environmentId,
environmentTemplateId: raw.sys.id
}
});
}
};
}