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