Skip to content

Commit fd1683f

Browse files
committed
feat: ✨ Content Branching feature
1 parent a2c80b3 commit fd1683f

File tree

3 files changed

+146
-2
lines changed

3 files changed

+146
-2
lines changed

lib/contentstackClient.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ export default function contentstackClient ({ http }) {
6161
* @memberof ContentstackClient
6262
* @func stack
6363
* @param {String} api_key - Stack API Key
64-
* @param {String} management_token - Stack API Key
64+
* @param {String} management_token - Management token for Stack.
65+
* @param {String} branch_name - Branch name or alias to access specific branch. Default is master.
6566
* @returns {Stack} Instance of Stack
6667
*
6768
* @example
@@ -85,6 +86,12 @@ export default function contentstackClient ({ http }) {
8586
* client.stack({ api_key: 'api_key', management_token: 'management_token' }).contentType('content_type_uid').fetch()
8687
* .then((stack) => console.log(stack))
8788
*
89+
* @example
90+
* import * as contentstack from '@contentstack/management'
91+
* const client = contentstack.client()
92+
*
93+
* client.stack({ api_key: 'api_key', management_token: 'management_token', branch_name: 'branch' }).contentType('content_type_uid').fetch()
94+
* .then((stack) => console.log(stack))
8895
*/
8996
function stack (params = {}) {
9097
const stack = { ...cloneDeep(params) }

lib/stack/branch/index.js

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import cloneDeep from 'lodash/cloneDeep'
2+
import { create, query, update, fetch, deleteEntity } from '../../entity'
3+
4+
/**
5+
*
6+
* @namespace Branch
7+
*/
8+
export function Branch (http, data = {}) {
9+
this.stackHeaders = data.stackHeaders
10+
this.urlPath = `/stacks/branches`
11+
if (data.branch) {
12+
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')
33+
34+
/**
35+
* @description The Delete Branch call is used to delete an existing Branch permanently from your Stack.
36+
* @memberof Branch
37+
* @func delete
38+
* @returns {Object} Response Object.
39+
* @example
40+
* import * as contentstack from '@contentstack/management'
41+
* const client = contentstack.client()
42+
*
43+
* client.stack({ api_key: 'api_key'}).branch('branch_name').delete()
44+
* .then((response) => console.log(response.notice))
45+
*/
46+
this.delete = deleteEntity(http)
47+
48+
/**
49+
* @description The fetch Branch call fetches Branch details.
50+
* @memberof Branch
51+
* @func fetch
52+
* @returns {Promise<Branch.Branch>} Promise for Branch instance
53+
* @example
54+
* import * as contentstack from '@contentstack/management'
55+
* const client = contentstack.client()
56+
*
57+
* client.stack({ api_key: 'api_key'}).branch('branch_name').fetch()
58+
* .then((branch) => console.log(branch))
59+
*
60+
*/
61+
this.fetch = fetch(http, 'branch')
62+
} else {
63+
/**
64+
* @description The Create a Branch call creates a new branch in a particular stack of your Contentstack account.
65+
* @memberof Branch
66+
* @func create
67+
* @returns {Promise<Branch.Branch>} Promise for Branch instance
68+
*
69+
* @example
70+
* import * as contentstack from '@contentstack/management'
71+
* const client = contentstack.client()
72+
* const branch = {
73+
* name: 'branch_name',
74+
* source: 'master'
75+
* }
76+
* client.stack({ api_key: 'api_key'}).branch().create({ branch })
77+
* .then((branch) => { console.log(branch) })
78+
*/
79+
this.create = create({ http: http })
80+
81+
/**
82+
* @description The 'Get all Branch' request returns comprehensive information about branch created in a Stack.
83+
* @memberof Branch
84+
* @func query
85+
* @returns {Promise<ContentstackCollection.ContentstackCollection>} Promise for ContentstackCollection instance
86+
*
87+
* @example
88+
* import * as contentstack from '@contentstack/management'
89+
* const client = contentstack.client()
90+
*
91+
* client.stack({ api_key: 'api_key'}).branch().query().find()
92+
* .then((collection) => { console.log(collection) })
93+
*/
94+
this.query = query({ http, wrapperCollection: BranchCollection })
95+
}
96+
return this
97+
}
98+
99+
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 })
103+
})
104+
return branchCollection
105+
}

lib/stack/index.js

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { Workflow } from './workflow'
1515
import { Release } from './release'
1616
import { BulkOperation } from './bulkOperation'
1717
import { Label } from './label'
18+
import { Branch } from './branch'
1819
// import { format } from 'util'
1920
/**
2021
* 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>.
@@ -31,10 +32,11 @@ export function Stack (http, data) {
3132
}
3233
if (data && data.stack && data.stack.api_key) {
3334
this.stackHeaders = { api_key: this.api_key }
34-
if (this.management_token && this.management_token) {
35+
if (this.management_token && this.management_token !== undefined) {
3536
this.stackHeaders.authorization = this.management_token
3637
delete this.management_token
3738
}
39+
3840
/**
3941
* @description The Update stack call lets you update the name and description of an existing stack.
4042
* @memberof Stack
@@ -178,6 +180,36 @@ export function Stack (http, data) {
178180
}
179181
return new Environment(http, data)
180182
}
183+
184+
/**
185+
* @description
186+
* @param {String}
187+
* @returns {Branch}
188+
*
189+
* @example
190+
* import * as contentstack from '@contentstack/management'
191+
* const client = contentstack.client()
192+
*
193+
* client.stack({ api_key: 'api_key'}).branch().create()
194+
* .then((branch) => console.log(branch))
195+
*
196+
* client.stack({ api_key: 'api_key', branch_name: 'branch'}).branch().fetch()
197+
* .then((branch) => console.log(branch))
198+
*
199+
* client.stack({ api_key: 'api_key' }).branch('branch').fetch()
200+
* .then((branch) => console.log(branch))
201+
*
202+
*/
203+
this.branch = (branchId = null) => {
204+
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 }
209+
}
210+
return new Branch(http, data)
211+
}
212+
181213
/**
182214
* @description Delivery Tokens provide read-only access to the associated environments.
183215
* @param {String} deliveryTokenUid The UID of the Delivery Token field you want to get details.

0 commit comments

Comments
 (0)