Skip to content

Commit 67edbb2

Browse files
committed
feat: ✨ Branch Alias feature added
1 parent fd1683f commit 67edbb2

File tree

8 files changed

+275
-165
lines changed

8 files changed

+275
-165
lines changed

lib/contentstack.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,7 @@ export function client (params = {}) {
142142
...requiredHeaders
143143
}
144144
const http = httpClient(params)
145-
const api = contentstackClient({
145+
return contentstackClient({
146146
http: http
147147
})
148-
return api
149148
}

lib/entity.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ export const update = (http, type) => {
168168
}
169169
}
170170

171-
export const deleteEntity = (http) => {
171+
export const deleteEntity = (http, force = false) => {
172172
return async function (param = {}) {
173173
try {
174174
const headers = {
@@ -177,12 +177,21 @@ export const deleteEntity = (http) => {
177177
...cloneDeep(param)
178178
}
179179
} || {}
180-
180+
if (force === true) {
181+
headers.params.force = true
182+
}
181183
const response = await http.delete(this.urlPath, headers)
182184
if (response.data) {
183185
return response.data
184186
} else {
185-
throw error(response)
187+
if (response.status >= 200 && response.status < 300) {
188+
return {
189+
status: response.status,
190+
statusText: response.statusText
191+
}
192+
} else {
193+
throw error(response)
194+
}
186195
}
187196
} catch (err) {
188197
throw error(err)

lib/stack/branch/index.js

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import cloneDeep from 'lodash/cloneDeep'
2-
import { create, query, update, fetch, deleteEntity } from '../../entity'
2+
import { create, query, fetch, deleteEntity } from '../../entity'
33

44
/**
55
*
@@ -8,28 +8,13 @@ import { create, query, update, fetch, deleteEntity } from '../../entity'
88
export function Branch (http, data = {}) {
99
this.stackHeaders = data.stackHeaders
1010
this.urlPath = `/stacks/branches`
11+
12+
data.branch = data.branch || data.branch_alias
13+
delete data.branch_alias
14+
1115
if (data.branch) {
1216
Object.assign(this, cloneDeep(data.branch))
13-
this.urlPath = `/stacks/branches/${this.name}`
14-
15-
/**
16-
* @description The Update Branch call lets you update the name of an existing Branch.
17-
* @memberof Branch
18-
* @func update
19-
* @returns {Promise<Branch.Branch>} Promise for Branch instance
20-
* @example
21-
* import * as contentstack from '@contentstack/management'
22-
* const client = contentstack.client()
23-
*
24-
* client.stack({ api_key: 'api_key'}).branch('branch_name').fetch()
25-
* .then((branch) => {
26-
* branch.name = 'new_branch_name'
27-
* return branch.update()
28-
* })
29-
* .then((branch) => console.log(branch))
30-
*
31-
*/
32-
this.update = update(http, 'token')
17+
this.urlPath = `/stacks/branches/${this.uid}`
3318

3419
/**
3520
* @description The Delete Branch call is used to delete an existing Branch permanently from your Stack.
@@ -59,6 +44,7 @@ export function Branch (http, data = {}) {
5944
*
6045
*/
6146
this.fetch = fetch(http, 'branch')
47+
6248
} else {
6349
/**
6450
* @description The Create a Branch call creates a new branch in a particular stack of your Contentstack account.
@@ -97,9 +83,8 @@ export function Branch (http, data = {}) {
9783
}
9884

9985
export function BranchCollection (http, data) {
100-
const obj = cloneDeep(data.branch) || []
101-
const branchCollection = obj.map((userdata) => {
102-
return new Branch(http, { branch: userdata, stackHeaders: data.stackHeaders })
86+
const obj = cloneDeep(data.branches) || data.branch_aliases || []
87+
return obj.map((branchData) => {
88+
return new Branch(http, { branch: branchData, stackHeaders: data.stackHeaders })
10389
})
104-
return branchCollection
10590
}

lib/stack/branchAlias/index.js

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
import cloneDeep from 'lodash/cloneDeep'
2+
import error from '../../core/contentstackError'
3+
import { deleteEntity, parseData } from '../../entity'
4+
import { Branch } from '../branch'
5+
6+
/**
7+
*
8+
* @namespace BranchAlias
9+
*/
10+
export function BranchAlias (http, data = {}) {
11+
this.stackHeaders = data.stackHeaders
12+
this.urlPath = `/stacks/branch_aliases`
13+
if (data.branch_alias) {
14+
Object.assign(this, cloneDeep(data.branch_alias))
15+
this.urlPath = `/stacks/branch_aliases/${this.uid}`
16+
17+
/**
18+
* @description The Update BranchAlias call lets you update the name of an existing BranchAlias.
19+
* @memberof BranchAlias
20+
* @func update
21+
* @returns {Promise<BranchAlias.BranchAlias>} Promise for BranchAlias instance
22+
* @example
23+
* import * as contentstack from '@contentstack/management'
24+
* const client = contentstack.client()
25+
*
26+
* client.stack({ api_key: 'api_key'}).branchAlias('branch_alias_id').createOrUpdate('branch_uid')
27+
* .then((branch) => {
28+
* branch.name = 'new_branch_name'
29+
* return branch.update()
30+
* })
31+
* .then((branch) => console.log(branch))
32+
*
33+
*/
34+
this.createOrUpdate = async (targetBranch) => {
35+
try {
36+
const response = await http.put(this.urlPath, { branch_alias: { target_branch: targetBranch } }, { headers: {
37+
...cloneDeep(this.stackHeaders)
38+
}
39+
})
40+
if (response.data) {
41+
return new Branch(http, parseData(response, this.stackHeaders))
42+
} else {
43+
throw error(response)
44+
}
45+
} catch (err) {
46+
throw error(err)
47+
}
48+
}
49+
/**
50+
* @description The Delete BranchAlias call is used to delete an existing BranchAlias permanently from your Stack.
51+
* @memberof BranchAlias
52+
* @func delete
53+
* @returns {Object} Response Object.
54+
* @example
55+
* import * as contentstack from '@contentstack/management'
56+
* const client = contentstack.client()
57+
*
58+
* client.stack({ api_key: 'api_key'}).branchAlias('branch_alias_id').delete()
59+
* .then((response) => console.log(response.notice))
60+
*/
61+
this.delete = deleteEntity(http, true)
62+
/**
63+
* @description The fetch BranchAlias call fetches BranchAlias details.
64+
* @memberof BranchAlias
65+
* @func fetch
66+
* @returns {Promise<BranchAlias.BranchAlias>} Promise for BranchAlias instance
67+
* @example
68+
* import * as contentstack from '@contentstack/management'
69+
* const client = contentstack.client()
70+
*
71+
* client.stack({ api_key: 'api_key'}).branchAlias('branch_alias_id').fetch()
72+
* .then((branch) => console.log(branch))
73+
*
74+
*/
75+
this.fetch = async function (param = {}) {
76+
try {
77+
const headers = {
78+
headers: { ...cloneDeep(this.stackHeaders) }
79+
} || {}
80+
const response = await http.get(this.urlPath, headers)
81+
if (response.data) {
82+
return new Branch(http, parseData(response, this.stackHeaders))
83+
} else {
84+
throw error(response)
85+
}
86+
} catch (err) {
87+
throw error(err)
88+
}
89+
}
90+
}
91+
return this
92+
}
93+
94+
export function BranchAliasCollection (http, data) {
95+
const obj = cloneDeep(data.branch_aliases) || []
96+
return obj.map((branchAlias) => {
97+
return new BranchAlias(http, { branch_alias: branchAlias, stackHeaders: data.stackHeaders })
98+
})
99+
}

lib/stack/index.js

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { Release } from './release'
1616
import { BulkOperation } from './bulkOperation'
1717
import { Label } from './label'
1818
import { Branch } from './branch'
19+
import { BranchAlias } from './branchAlias'
1920
// import { format } from 'util'
2021
/**
2122
* A stack is a space that stores the content of a project (a web or mobile property). Within a stack, you can create content structures, content entries, users, etc. related to the project. Read more about <a href='https://www.contentstack.com/docs/guide/stack'>Stacks</a>.
@@ -193,23 +194,42 @@ export function Stack (http, data) {
193194
* client.stack({ api_key: 'api_key'}).branch().create()
194195
* .then((branch) => console.log(branch))
195196
*
196-
* client.stack({ api_key: 'api_key', branch_name: 'branch'}).branch().fetch()
197-
* .then((branch) => console.log(branch))
198-
*
199197
* client.stack({ api_key: 'api_key' }).branch('branch').fetch()
200198
* .then((branch) => console.log(branch))
201199
*
202200
*/
203-
this.branch = (branchId = null) => {
201+
this.branch = (branchUid = null) => {
204202
const data = { stackHeaders: this.stackHeaders }
205-
if (branchId) {
206-
data.branch = { name: branchId }
207-
} else if (this.branch_name) {
208-
data.branch = { name: this.branch_name }
203+
if (branchUid) {
204+
data.branch = { uid: branchUid }
209205
}
210206
return new Branch(http, data)
211207
}
212208

209+
/**
210+
* @description
211+
* @param {String}
212+
* @returns {BranchAlias}
213+
*
214+
* @example
215+
* import * as contentstack from '@contentstack/management'
216+
* const client = contentstack.client()
217+
*
218+
* client.stack({ api_key: 'api_key'}).branchAlias().create()
219+
* .then((branch) => console.log(branch))
220+
*
221+
* client.stack({ api_key: 'api_key' }).branchAlias('branch_uid').fetch()
222+
* .then((branch) => console.log(branch))
223+
*
224+
*/
225+
this.branchAlias = (branchUid = null) => {
226+
const data = { stackHeaders: this.stackHeaders }
227+
if (branchUid) {
228+
data.branch_alias = { uid: branchUid }
229+
}
230+
return new BranchAlias(http, data)
231+
}
232+
213233
/**
214234
* @description Delivery Tokens provide read-only access to the associated environments.
215235
* @param {String} deliveryTokenUid The UID of the Delivery Token field you want to get details.

lib/stack/workflow/index.js

Lines changed: 57 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ export function Workflow (http, data = {}) {
149149
* client.stack({ api_key: 'api_key'}).workflow('workflow_uid').contentType('contentType_uid').getPublishRules()
150150
* .then((collection) => console.log(collection))
151151
*/
152-
async function getPublishRules (params) {
152+
const getPublishRules = async function (params) {
153153
const headers = {}
154154
if (this.stackHeaders) {
155155
headers.headers = this.stackHeaders
@@ -187,63 +187,63 @@ export function Workflow (http, data = {}) {
187187
* const client = contentstack.client()
188188
*
189189
* const workflow = {
190-
* "workflow_stages": [
190+
*"workflow_stages": [
191191
* {
192-
* "color": "#2196f3",
193-
* "SYS_ACL": {
194-
* "roles": {
195-
* "uids": []
196-
* },
197-
* "users": {
198-
* "uids": [
199-
* "$all"
200-
* ]
201-
* },
202-
* "others": {}
203-
* },
204-
* "next_available_stages": [
205-
* "$all"
206-
* ],
207-
* "allStages": true,
208-
* "allUsers": true,
209-
* "specificStages": false,
210-
* "specificUsers": false,
211-
* "entry_lock": "$none", //assign any one of the assign any one of the ($none/$others/$all)
212-
* "name": "Review"
213-
* },
214-
* {
215-
* "color": "#74ba76",
216-
* "SYS_ACL": {
217-
* "roles": {
218-
* "uids": []
219-
* },
220-
* "users": {
221-
* "uids": [
222-
* "$all"
223-
* ]
224-
* },
225-
* "others": {}
226-
* },
227-
* "allStages": true,
228-
* "allUsers": true,
229-
* "specificStages": false,
230-
* "specificUsers": false,
231-
* "next_available_stages": [
232-
* "$all"
233-
* ],
234-
* "entry_lock": "$none",
235-
* "name": "Complete"
236-
* }
237-
* ],
238-
* "admin_users": {
239-
* "users": []
240-
* },
241-
* "name": "Workflow Name",
242-
* "enabled": true,
243-
* "content_types": [
244-
* "$all"
245-
* ]
246-
* }
192+
* "color": "#2196f3",
193+
* "SYS_ACL": {
194+
* "roles": {
195+
* "uids": []
196+
* },
197+
* "users": {
198+
* "uids": [
199+
* "$all"
200+
* ]
201+
* },
202+
* "others": {}
203+
* },
204+
* "next_available_stages": [
205+
* "$all"
206+
* ],
207+
* "allStages": true,
208+
* "allUsers": true,
209+
* "specificStages": false,
210+
* "specificUsers": false,
211+
* "entry_lock": "$none", //assign any one of the assign any one of the ($none/$others/$all)
212+
* "name": "Review"
213+
* },
214+
* {
215+
* "color": "#74ba76",
216+
* "SYS_ACL": {
217+
* "roles": {
218+
* "uids": []
219+
* },
220+
* "users": {
221+
* "uids": [
222+
* "$all"
223+
* ]
224+
* },
225+
* "others": {}
226+
* },
227+
* "allStages": true,
228+
* "allUsers": true,
229+
* "specificStages": false,
230+
* "specificUsers": false,
231+
* "next_available_stages": [
232+
* "$all"
233+
* ],
234+
* "entry_lock": "$none",
235+
* "name": "Complete"
236+
* }
237+
* ],
238+
* "admin_users": {
239+
* "users": []
240+
* },
241+
* "name": "Workflow Name",
242+
* "enabled": true,
243+
* "content_types": [
244+
* "$all"
245+
* ]
246+
* }
247247
* client.stack().workflow().create({ workflow })
248248
* .then((workflow) => console.log(workflow))
249249
*/

0 commit comments

Comments
 (0)