|
| 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 | +} |
0 commit comments