UNPKG

13 kBJavaScriptView Raw
1const _excluded = ["spaceId", "environmentId"];
2function 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; }
3function _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; }
4function _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; }
5function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
6function _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); }
7function _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; }
8function _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; }
9import { createRequestConfig } from 'contentful-sdk-core';
10import entities from './entities';
11export function createEnvironmentTemplateApi(makeRequest, organizationId) {
12 const {
13 wrapEnvironmentTemplate,
14 wrapEnvironmentTemplateCollection
15 } = entities.environmentTemplate;
16 const {
17 wrapEnvironmentTemplateInstallationCollection
18 } = entities.environmentTemplateInstallation;
19 return {
20 /**
21 * Updates a environment template
22 * @return Promise for new version of the template
23 * ```javascript
24 * const contentful = require('contentful-management')
25 *
26 * const client = contentful.createClient({
27 * accessToken: '<content_management_api_key>'
28 * })
29 *
30 * client.getEnvironmentTemplate('<organization_id>', '<environment_template_id>')
31 * .then((environmentTemplate) => {
32 * environmentTemplate.name = 'New name'
33 * return environmentTemplate.update()
34 * })
35 * .then((environmentTemplate) =>
36 * console.log(`Environment template ${environmentTemplate.sys.id} renamed.`)
37 * ).catch(console.error)
38 * ```
39 */
40 update: function updateEnvironmentTemplate() {
41 const raw = this.toPlainObject();
42 return makeRequest({
43 entityType: 'EnvironmentTemplate',
44 action: 'update',
45 params: {
46 organizationId,
47 environmentTemplateId: raw.sys.id
48 },
49 payload: raw
50 }).then(data => wrapEnvironmentTemplate(makeRequest, data, organizationId));
51 },
52 /**
53 * Updates environment template version data
54 * @param version.versionName - Name of the environment template version
55 * @param version.versionDescription - Description of the environment template version
56 * @return Promise for an updated EnvironmentTemplate
57 * ```javascript
58 * const contentful = require('contentful-management')
59 *
60 * const client = contentful.createClient({
61 * accessToken: '<content_management_api_key>'
62 * })
63 *
64 * client.getEnvironmentTemplate('<organization_id>', '<environment_template_id>')
65 * .then((environmentTemplate) => {
66 * return environmentTemplate.updateVersion({
67 * versionName: 'New Name',
68 * versionDescription: 'New Description',
69 * })
70 * })
71 * .then((environmentTemplate) =>
72 * console.log(`Environment template version ${environmentTemplate.sys.id} renamed.`)
73 * ).catch(console.error)
74 * ```
75 */
76 updateVersion: function updateEnvironmentTemplateVersion({
77 versionName,
78 versionDescription
79 }) {
80 const raw = this.toPlainObject();
81 return makeRequest({
82 entityType: 'EnvironmentTemplate',
83 action: 'versionUpdate',
84 params: {
85 organizationId,
86 environmentTemplateId: raw.sys.id,
87 version: raw.sys.version
88 },
89 payload: {
90 versionName,
91 versionDescription
92 }
93 }).then(data => wrapEnvironmentTemplate(makeRequest, data, organizationId));
94 },
95 /**
96 * Deletes the environment template
97 * @return Promise for the deletion. It contains no data, but the Promise error case should be handled.
98 * @example ```javascript
99 * const contentful = require('contentful-management')
100 *
101 * const client = contentful.createClient({
102 * accessToken: '<content_management_api_key>'
103 * })
104 *
105 * client.getEnvironmentTemplate('<organization_id>', '<environment_template_id>')
106 * .then((environmentTemplate) => environmentTemplate.delete())
107 * .then(() => console.log('Environment template deleted.'))
108 * .catch(console.error)
109 * ```
110 */
111 delete: function deleteEnvironmentTemplate() {
112 const raw = this.toPlainObject();
113 return makeRequest({
114 entityType: 'EnvironmentTemplate',
115 action: 'delete',
116 params: {
117 organizationId,
118 environmentTemplateId: raw.sys.id
119 }
120 });
121 },
122 /**
123 * Gets a collection of all versions for the environment template
124 * @return Promise for a EnvironmentTemplate
125 * ```javascript
126 * const contentful = require('contentful-management')
127 *
128 * const client = contentful.createClient({
129 * accessToken: '<content_management_api_key>'
130 * })
131 * client.getEnvironmentTemplate('<organization_id>', '<environment_template_id>')
132 * .then((environmentTemplate) => environmentTemplate.getVersions())
133 * .then((environmentTemplateVersions) => console.log(environmentTemplateVersions.items))
134 * .catch(console.error)
135 * ```
136 */
137 getVersions: function getEnvironmentTemplateVersions() {
138 const raw = this.toPlainObject();
139 return makeRequest({
140 entityType: 'EnvironmentTemplate',
141 action: 'versions',
142 params: {
143 organizationId,
144 environmentTemplateId: raw.sys.id
145 }
146 }).then(data => wrapEnvironmentTemplateCollection(makeRequest, data, organizationId));
147 },
148 /**
149 * Gets a collection of all installations for the environment template
150 * @param [installationParams.spaceId] - Space ID to filter installations by space and environment
151 * @param [installationParams.environmentId] - Environment ID to filter installations by space and environment
152 * @return Promise for a collection of EnvironmentTemplateInstallations
153 * ```javascript
154 * const contentful = require('contentful-management')
155 *
156 * const client = contentful.createClient({
157 * accessToken: '<content_management_api_key>'
158 * })
159 *
160 * client.getEnvironmentTemplate('<organization_id>', '<environment_template_id>')
161 * .then((environmentTemplate) => environmentTemplate.getInstallations())
162 * .then((environmentTemplateInstallations) =>
163 * console.log(environmentTemplateInstallations.items)
164 * )
165 * .catch(console.error)
166 * ```
167 */
168 getInstallations: function getEnvironmentTemplateInstallations(_ref = {}) {
169 let {
170 spaceId,
171 environmentId
172 } = _ref,
173 query = _objectWithoutProperties(_ref, _excluded);
174 const raw = this.toPlainObject();
175 return makeRequest({
176 entityType: 'EnvironmentTemplateInstallation',
177 action: 'getMany',
178 params: {
179 organizationId,
180 environmentTemplateId: raw.sys.id,
181 query: _objectSpread({}, createRequestConfig({
182 query
183 }).params),
184 spaceId,
185 environmentId
186 }
187 }).then(data => wrapEnvironmentTemplateInstallationCollection(makeRequest, data));
188 },
189 /**
190 * Validates an environment template against a given space and environment
191 * @param params.spaceId - Space ID where the template should be installed into
192 * @param params.environmentId - Environment ID where the template should be installed into
193 * @param [params.version] - Version of the template
194 * @param [params.installation.takeover] - Already existing Content types to takeover in the target environment
195 * @param [params.changeSet] - Change set which should be applied
196 * @return Promise for a EnvironmentTemplateValidation
197 * ```javascript
198 * const contentful = require('contentful-management')
199 *
200 * const client = contentful.createClient({
201 * accessToken: '<content_management_api_key>'
202 * })
203 *
204 * client.getEnvironmentTemplate('<organization_id>', '<environment_template_id>')
205 * .then((environmentTemplate) => environmentTemplate.validate({
206 * spaceId: '<space_id>',
207 * environmentId: '<environment_id>',
208 * version: <version>,
209 * }))
210 * .then((validationResult) => console.log(validationResult))
211 * .catch(console.error)
212 * ```
213 */
214 validate: function validateEnvironmentTemplate({
215 spaceId,
216 environmentId,
217 version,
218 takeover,
219 changeSet
220 }) {
221 const raw = this.toPlainObject();
222 return makeRequest({
223 entityType: 'EnvironmentTemplate',
224 action: 'validate',
225 params: {
226 spaceId,
227 version,
228 environmentId,
229 environmentTemplateId: raw.sys.id
230 },
231 payload: _objectSpread(_objectSpread({}, takeover && {
232 takeover
233 }), changeSet && {
234 changeSet
235 })
236 });
237 },
238 /**
239 * Installs a template against a given space and environment
240 * @param params.spaceId - Space ID where the template should be installed into
241 * @param params.environmentId - Environment ID where the template should be installed into
242 * @param params.installation.version- Template version which should be installed
243 * @param [params.installation.takeover] - Already existing Content types tp takeover in the target environment
244 * @param [params.changeSet] - Change set which should be applied
245 * @return Promise for a EnvironmentTemplateInstallation
246 * ```javascript
247 * const contentful = require('contentful-management')
248 *
249 * const client = contentful.createClient({
250 * accessToken: '<content_management_api_key>'
251 * })
252 *
253 * client.getEnvironmentTemplate('<organization_id>', '<environment_template_id>')
254 * .then((environmentTemplate) => environmentTemplate.validate({
255 * spaceId: '<space_id>',
256 * environmentId: '<environment_id>',
257 * installation: {
258 * version: <version>,
259 * }
260 * }))
261 * .then((installation) => console.log(installation))
262 * .catch(console.error)
263 * ```
264 */
265 install: function installEnvironmentTemplate({
266 spaceId,
267 environmentId,
268 installation
269 }) {
270 const raw = this.toPlainObject();
271 return makeRequest({
272 entityType: 'EnvironmentTemplate',
273 action: 'install',
274 params: {
275 spaceId,
276 environmentId,
277 environmentTemplateId: raw.sys.id
278 },
279 payload: installation
280 });
281 },
282 /**
283 * Disconnects the template from a given environment
284 * @param params.spaceId - Space ID where the template should be installed into
285 * @param params.environmentId - Environment ID where the template should be installed into
286 * @return Promise for the disconnection with no data
287 * ```javascript
288 * const contentful = require('contentful-management')
289 *
290 * const client = contentful.createClient({
291 * accessToken: '<content_management_api_key>'
292 * })
293 *
294 * client.getEnvironmentTemplate('<organization_id>', '<environment_template_id>')
295 * .then(environmentTemplate) => environmentTemplate.disconnected())
296 * .then(() => console.log('Template disconnected'))
297 * .catch(console.error)
298 * ```
299 */
300 disconnect: function disconnectEnvironmentTemplate({
301 spaceId,
302 environmentId
303 }) {
304 const raw = this.toPlainObject();
305 return makeRequest({
306 entityType: 'EnvironmentTemplate',
307 action: 'disconnect',
308 params: {
309 spaceId,
310 environmentId,
311 environmentTemplateId: raw.sys.id
312 }
313 });
314 }
315 };
316}
\No newline at end of file