Skip to content

Commit caff6cb

Browse files
author
Tim Beyer
authoredAug 18, 2020
feat: Add methods for managing custom environment aliases (#426)
1 parent 26ad7db commit caff6cb

File tree

4 files changed

+105
-6
lines changed

4 files changed

+105
-6
lines changed
 

‎lib/create-space-api.ts

+28
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { QueryOptions } from './common-types'
2222
import { UIExtensionProps } from './entities/ui-extension'
2323
import { CreateApiKeyProps } from './entities/api-key'
2424
import { ScheduledActionQueryOptions, ScheduledActionProps } from './entities/scheduled-action'
25+
import { EnvironmentAliasProps } from './entities/environment-alias'
2526

2627
function raiseDeprecationWarning(method: string) {
2728
console.warn(
@@ -1709,6 +1710,33 @@ export default function createSpaceApi({
17091710
.get(`content_types/${contentTypeId}/snapshots`, createRequestConfig({ query: query }))
17101711
.then((response) => wrapSnapshotCollection(http, response.data), errorHandler)
17111712
},
1713+
/**
1714+
* Creates an EnvironmentAlias with a custom ID
1715+
* @param id - EnvironmentAlias ID
1716+
* @param data - Object representation of the EnvironmentAlias to be created
1717+
* @return Promise for the newly created EnvironmentAlias
1718+
* @example ```javascript
1719+
* const contentful = require('contentful-management')
1720+
*
1721+
* const client = contentful.createClient({
1722+
* accessToken: '<content_management_api_key>'
1723+
* })
1724+
*
1725+
* client.getSpace('<space_id>')
1726+
* .then((space) => space.createEnvironmentAliasWithId('<environment-alias-id>', {
1727+
* environment: {
1728+
* sys: { type: 'Link', linkType: 'Environment', id: 'targetEnvironment' }
1729+
* }
1730+
* }))
1731+
* .then((environmentAlias) => console.log(environmentAlias))
1732+
* .catch(console.error)
1733+
* ```
1734+
*/
1735+
createEnvironmentAliasWithId(id: string, data: Omit<EnvironmentAliasProps, 'sys'>) {
1736+
return http
1737+
.put('environment_aliases/' + id, data)
1738+
.then((response) => wrapEnvironmentAlias(http, response.data), errorHandler)
1739+
},
17121740
/**
17131741
* Gets an Environment Alias
17141742
* @param Environment Alias ID

‎lib/entities/environment-alias.ts

+28-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import cloneDeep from 'lodash/cloneDeep'
22
import { freezeSys, toPlainObject } from 'contentful-sdk-core'
33
import enhanceWithMethods from '../enhance-with-methods'
4-
import { createUpdateEntity } from '../instance-actions'
4+
import { createUpdateEntity, createDeleteEntity } from '../instance-actions'
55
import { wrapCollection } from '../common-utils'
66
import { DefaultElements, MetaLinkProps, MetaSysProps } from '../common-types'
77
import { AxiosInstance } from 'axios'
@@ -40,6 +40,29 @@ export interface EnvironmentAlias
4040
* ```
4141
*/
4242
update(): Promise<EnvironmentAlias>
43+
44+
/**
45+
* Deletes this object on the server.
46+
* @memberof EnvironmentAlias
47+
* @func delete
48+
* @return {Promise<void>} Promise for the deletion. It contains no data, but the Promise error case should be handled.
49+
* ```javascript
50+
* const contentful = require('contentful-management')
51+
*
52+
* const client = contentful.createClient({
53+
* accessToken: '<content_management_api_key>'
54+
* })
55+
*
56+
* client.getSpace('<space_id>')
57+
* .then((space) => space.getEnvironmentAlias('<environment_alias_id>'))
58+
* .then((alias) => {
59+
* return alias.delete()
60+
* })
61+
* .then(() => console.log(`Alias deleted.`))
62+
* .catch(console.error)
63+
* ```
64+
*/
65+
delete(): Promise<void>
4366
}
4467

4568
function createEnvironmentAliasApi(http: AxiosInstance) {
@@ -49,6 +72,10 @@ function createEnvironmentAliasApi(http: AxiosInstance) {
4972
entityPath: 'environment_aliases',
5073
wrapperMethod: wrapEnvironmentAlias,
5174
}),
75+
delete: createDeleteEntity({
76+
http: http,
77+
entityPath: 'environment_aliases',
78+
}),
5279
}
5380
}
5481

‎test/integration/environment-alias-integration.js

+37-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export function environmentAliasReadOnlyTests(t, space) {
1+
export function environmentAliasTests(t, space) {
22
t.test('Gets aliases', (t) => {
33
t.plan(2)
44
return space.getEnvironmentAliases().then((response) => {
@@ -7,7 +7,7 @@ export function environmentAliasReadOnlyTests(t, space) {
77
})
88
})
99

10-
t.test('Updates alias', (t) => {
10+
t.test('Updates master alias', (t) => {
1111
t.plan(4)
1212
return space
1313
.getEnvironmentAlias('master')
@@ -22,4 +22,39 @@ export function environmentAliasReadOnlyTests(t, space) {
2222
t.equals(updatedAlias.environment.sys.id, 'feature-13')
2323
})
2424
})
25+
26+
t.test('Creates custom alias', (t) => {
27+
t.plan(2)
28+
return space
29+
.createEnvironmentAliasWithId('new-alias', {
30+
environment: {
31+
sys: {
32+
type: 'Link',
33+
linkType: 'Environment',
34+
id: 'previously-master',
35+
},
36+
},
37+
})
38+
.then((alias) => {
39+
t.equals(alias.sys.id, 'new-alias')
40+
t.equals(alias.environment.sys.id, 'previously-master')
41+
})
42+
})
43+
44+
t.test('Deletes custom alias', (t) => {
45+
t.plan(3)
46+
return space
47+
.getEnvironmentAlias('new-alias')
48+
.then((alias) => {
49+
t.equals(alias.sys.id, 'new-alias')
50+
t.equals(alias.environment.sys.id, 'previously-master')
51+
return alias.delete()
52+
})
53+
.then(() => {
54+
return space.getEnvironmentAlias('new-alias')
55+
})
56+
.catch((err) => {
57+
t.equals(err.name, 'NotFound')
58+
})
59+
})
2560
}

‎test/integration/integration-tests.js

+12-3
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import uiExtensionTests from './ui-extension-integration'
2323
import generateRandomId from './generate-random-id'
2424
import { createClient } from '../../'
2525
import { environmentTests } from './environment-integration'
26-
import { environmentAliasReadOnlyTests } from './environment-alias-integration'
26+
import { environmentAliasTests } from './environment-alias-integration'
2727
import { tagTests } from './tag-integration'
2828

2929
const params = {
@@ -202,16 +202,25 @@ test('Gets v2 space for read only tests', (t) => {
202202
})
203203
})
204204

205-
test('Gets v2 space for read only tests', (t) => {
205+
test('Gets v2 space for alias tests', (t) => {
206206
return v2Client.getSpace('w6xueg32zr68').then((space) => {
207207
test.onFinish(() => {
208208
// clean up and re-point alias to starting env
209209
space.getEnvironmentAlias('master').then((alias) => {
210210
alias.environment.sys.id = 'previously-master'
211211
return alias.update()
212212
})
213+
214+
// In case something went wrong with deleting the new alias
215+
// try to fetch and delete it again to clean up
216+
space
217+
.getEnvironmentAlias('new-alias')
218+
.then((alias) => {
219+
return alias.delete()
220+
})
221+
.catch(() => undefined)
213222
})
214-
environmentAliasReadOnlyTests(t, space) // v2 space with alias feature enabled and opted-in
223+
environmentAliasTests(t, space) // v2 space with alias feature enabled and opted-in
215224
})
216225
})
217226

0 commit comments

Comments
 (0)
Please sign in to comment.