Skip to content

Commit 166a7af

Browse files
committed
Merge branch 'feature/content-branching' into feature/nested-publish
2 parents 92d13c1 + 651a6be commit 166a7af

File tree

12 files changed

+430
-156
lines changed

12 files changed

+430
-156
lines changed

.jsdoc.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,12 @@
1616
"lib/stack/contentType/entry/index.js",
1717
"lib/stack/asset/index.js",
1818
"lib/stack/asset/folders/index.js",
19+
"lib/stack/branch/index.js",
20+
"lib/stack/branchAlias/index.js",
1921
"lib/stack/bulkOperation/index.js",
2022
"lib/stack/extension/index.js",
2123
"lib/stack/release/index.js",
24+
"lib/stack/release/items/index.js",
2225
"lib/stack/label/index.js",
2326
"lib/stack/locale/index.js",
2427
"lib/stack/environment/index.js",

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/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_uid: 'branch_uid' }).contentType('content_type_uid').fetch()
94+
* .then((stack) => console.log(stack))
8895
*/
8996
function stack (params = {}) {
9097
const stack = { ...cloneDeep(params) }

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: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import cloneDeep from 'lodash/cloneDeep'
2+
import { create, query, 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+
12+
data.branch = data.branch || data.branch_alias
13+
delete data.branch_alias
14+
15+
if (data.branch) {
16+
Object.assign(this, cloneDeep(data.branch))
17+
this.urlPath = `/stacks/branches/${this.uid}`
18+
19+
/**
20+
* @description The Delete Branch call is used to delete an existing Branch permanently from your Stack.
21+
* @memberof Branch
22+
* @func delete
23+
* @returns {Object} Response Object.
24+
* @example
25+
* import * as contentstack from '@contentstack/management'
26+
* const client = contentstack.client()
27+
*
28+
* client.stack({ api_key: 'api_key'}).branch('branch_uid').delete()
29+
* .then((response) => console.log(response.notice))
30+
*/
31+
this.delete = deleteEntity(http, true)
32+
33+
/**
34+
* @description The fetch Branch call fetches Branch details.
35+
* @memberof Branch
36+
* @func fetch
37+
* @returns {Promise<Branch.Branch>} Promise for Branch instance
38+
* @example
39+
* import * as contentstack from '@contentstack/management'
40+
* const client = contentstack.client()
41+
*
42+
* client.stack({ api_key: 'api_key'}).branch('branch_uid').fetch()
43+
* .then((branch) => console.log(branch))
44+
*
45+
*/
46+
this.fetch = fetch(http, 'branch')
47+
48+
} else {
49+
/**
50+
* @description The Create a Branch call creates a new branch in a particular stack of your Contentstack account.
51+
* @memberof Branch
52+
* @func create
53+
* @returns {Promise<Branch.Branch>} Promise for Branch instance
54+
*
55+
* @example
56+
* import * as contentstack from '@contentstack/management'
57+
* const client = contentstack.client()
58+
* const branch = {
59+
* name: 'branch_name',
60+
* source: 'master'
61+
* }
62+
* client.stack({ api_key: 'api_key'}).branch().create({ branch })
63+
* .then((branch) => { console.log(branch) })
64+
*/
65+
this.create = create({ http: http })
66+
67+
/**
68+
* @description The 'Get all Branch' request returns comprehensive information about branch created in a Stack.
69+
* @memberof Branch
70+
* @func query
71+
* @returns {Promise<ContentstackCollection.ContentstackCollection>} Promise for ContentstackCollection instance
72+
*
73+
* @example
74+
* import * as contentstack from '@contentstack/management'
75+
* const client = contentstack.client()
76+
*
77+
* client.stack({ api_key: 'api_key'}).branch().query().find()
78+
* .then((collection) => { console.log(collection) })
79+
*/
80+
this.query = query({ http, wrapperCollection: BranchCollection })
81+
}
82+
return this
83+
}
84+
85+
export function BranchCollection (http, data) {
86+
const obj = cloneDeep(data.branches) || data.branch_aliases || []
87+
return obj.map((branchData) => {
88+
return new Branch(http, { branch: branchData, stackHeaders: data.stackHeaders })
89+
})
90+
}

lib/stack/branchAlias/index.js

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
import cloneDeep from 'lodash/cloneDeep'
2+
import error from '../../core/contentstackError'
3+
import { deleteEntity, fetchAll, parseData } from '../../entity'
4+
import { Branch, BranchCollection } 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<Branch.Branch>} Promise for Branch 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+
/**
64+
* @description The fetch BranchAlias call fetches BranchAlias details.
65+
* @memberof BranchAlias
66+
* @func fetch
67+
* @returns {Promise<Branch.Branch>} Promise for Branch instance
68+
* @example
69+
* import * as contentstack from '@contentstack/management'
70+
* const client = contentstack.client()
71+
*
72+
* client.stack({ api_key: 'api_key'}).branchAlias('branch_alias_id').fetch()
73+
* .then((branch) => console.log(branch))
74+
*
75+
*/
76+
this.fetch = async function (param = {}) {
77+
try {
78+
const headers = {
79+
headers: { ...cloneDeep(this.stackHeaders) },
80+
params: {
81+
...cloneDeep(param)
82+
}
83+
} || {}
84+
const response = await http.get(this.urlPath, headers)
85+
if (response.data) {
86+
return new Branch(http, parseData(response, this.stackHeaders))
87+
} else {
88+
throw error(response)
89+
}
90+
} catch (err) {
91+
throw error(err)
92+
}
93+
}
94+
} else {
95+
/**
96+
* @description The Get all BranchAlias request retrieves the details of all the Branch of a stack.
97+
* @memberof BranchAlias
98+
* @func fetchAll
99+
* @param {Int} limit The limit parameter will return a specific number of Branch in the output.
100+
* @param {Int} skip The skip parameter will skip a specific number of Branch in the output.
101+
* @param {Boolean}include_count To retrieve the count of Branch.
102+
* @returns {ContentstackCollection} Result collection of content of specified module.
103+
* @example
104+
* import * as contentstack from '@contentstack/management'
105+
* const client = contentstack.client()
106+
*
107+
* client.stack({ api_key: 'api_key'}).branchAlias().fetchAll()
108+
* .then((collection) => console.log(collection))
109+
*
110+
*/
111+
this.fetchAll = fetchAll(http, BranchCollection)
112+
}
113+
return this
114+
}

lib/stack/index.js

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import { Workflow } from './workflow'
1515
import { Release } from './release'
1616
import { BulkOperation } from './bulkOperation'
1717
import { Label } from './label'
18+
import { Branch } from './branch'
19+
import { BranchAlias } from './branchAlias'
1820
// import { format } from 'util'
1921
/**
2022
* 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 +33,14 @@ export function Stack (http, data) {
3133
}
3234
if (data && data.stack && data.stack.api_key) {
3335
this.stackHeaders = { api_key: this.api_key }
34-
if (this.management_token && this.management_token) {
36+
if (this.management_token && this.management_token !== undefined) {
3537
this.stackHeaders.authorization = this.management_token
3638
delete this.management_token
3739
}
40+
41+
if (this.branch_uid) {
42+
this.stackHeaders.branch = this.branch_uid
43+
}
3844
/**
3945
* @description The Update stack call lets you update the name and description of an existing stack.
4046
* @memberof Stack
@@ -178,6 +184,55 @@ export function Stack (http, data) {
178184
}
179185
return new Environment(http, data)
180186
}
187+
188+
/**
189+
* @description Branch corresponds to Stack branch.
190+
* @param {String}
191+
* @returns {Branch}
192+
*
193+
* @example
194+
* import * as contentstack from '@contentstack/management'
195+
* const client = contentstack.client()
196+
*
197+
* client.stack({ api_key: 'api_key'}).branch().create()
198+
* .then((branch) => console.log(branch))
199+
*
200+
* client.stack({ api_key: 'api_key' }).branch('branch_uid').fetch()
201+
* .then((branch) => console.log(branch))
202+
*
203+
*/
204+
this.branch = (branchUid = null) => {
205+
const data = { stackHeaders: this.stackHeaders }
206+
if (branchUid) {
207+
data.branch = { uid: branchUid }
208+
}
209+
return new Branch(http, data)
210+
}
211+
212+
/**
213+
* @description Branch corresponds to Stack branch.
214+
* @param {String}
215+
* @returns {BranchAlias}
216+
*
217+
* @example
218+
* import * as contentstack from '@contentstack/management'
219+
* const client = contentstack.client()
220+
*
221+
* client.stack({ api_key: 'api_key'}).branchAlias().create()
222+
* .then((branch) => console.log(branch))
223+
*
224+
* client.stack({ api_key: 'api_key' }).branchAlias('branch_alias_uid').fetch()
225+
* .then((branch) => console.log(branch))
226+
*
227+
*/
228+
this.branchAlias = (branchUid = null) => {
229+
const data = { stackHeaders: this.stackHeaders }
230+
if (branchUid) {
231+
data.branch_alias = { uid: branchUid }
232+
}
233+
return new BranchAlias(http, data)
234+
}
235+
181236
/**
182237
* @description Delivery Tokens provide read-only access to the associated environments.
183238
* @param {String} deliveryTokenUid The UID of the Delivery Token field you want to get details.

0 commit comments

Comments
 (0)