-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
114 lines (111 loc) · 3.92 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import cloneDeep from 'lodash/cloneDeep'
import error from '../../core/contentstackError'
import { deleteEntity, fetchAll, parseData } from '../../entity'
import { Branch, BranchCollection } from '../branch'
/**
*
* @namespace BranchAlias
*/
export function BranchAlias (http, data = {}) {
this.stackHeaders = data.stackHeaders
this.urlPath = `/stacks/branch_aliases`
if (data.branch_alias) {
Object.assign(this, cloneDeep(data.branch_alias))
this.urlPath = `/stacks/branch_aliases/${this.uid}`
/**
* @description The Update BranchAlias call lets you update the name of an existing BranchAlias.
* @memberof BranchAlias
* @func update
* @returns {Promise<Branch.Branch>} Promise for Branch instance
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.stack({ api_key: 'api_key'}).branchAlias('branch_alias_id').createOrUpdate('branch_uid')
* .then((branch) => {
* branch.name = 'new_branch_name'
* return branch.update()
* })
* .then((branch) => console.log(branch))
*
*/
this.createOrUpdate = async (targetBranch) => {
try {
const response = await http.put(this.urlPath, { branch_alias: { target_branch: targetBranch } }, { headers: {
...cloneDeep(this.stackHeaders)
}
})
if (response.data) {
return new Branch(http, parseData(response, this.stackHeaders))
} else {
throw error(response)
}
} catch (err) {
throw error(err)
}
}
/**
* @description The Delete BranchAlias call is used to delete an existing BranchAlias permanently from your Stack.
* @memberof BranchAlias
* @func delete
* @returns {Object} Response Object.
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.stack({ api_key: 'api_key'}).branchAlias('branch_alias_id').delete()
* .then((response) => console.log(response.notice))
*/
this.delete = deleteEntity(http, true)
/**
* @description The fetch BranchAlias call fetches BranchAlias details.
* @memberof BranchAlias
* @func fetch
* @returns {Promise<Branch.Branch>} Promise for Branch instance
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.stack({ api_key: 'api_key'}).branchAlias('branch_alias_id').fetch()
* .then((branch) => console.log(branch))
*
*/
this.fetch = async function (param = {}) {
try {
const headers = {
headers: { ...cloneDeep(this.stackHeaders) },
params: {
...cloneDeep(param)
}
} || {}
const response = await http.get(this.urlPath, headers)
if (response.data) {
return new Branch(http, parseData(response, this.stackHeaders))
} else {
throw error(response)
}
} catch (err) {
throw error(err)
}
}
} else {
/**
* @description The Get all BranchAlias request retrieves the details of all the Branch of a stack.
* @memberof BranchAlias
* @func fetchAll
* @param {Int} limit The limit parameter will return a specific number of Branch in the output.
* @param {Int} skip The skip parameter will skip a specific number of Branch in the output.
* @param {Boolean}include_count To retrieve the count of Branch.
* @returns {ContentstackCollection} Result collection of content of specified module.
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.stack({ api_key: 'api_key'}).branchAlias().fetchAll()
* .then((collection) => console.log(collection))
*
*/
this.fetchAll = fetchAll(http, BranchCollection)
}
return this
}