|
| 1 | +import cloneDeep from 'lodash/cloneDeep' |
| 2 | +import { |
| 3 | + create, |
| 4 | + fetch, |
| 5 | + update, |
| 6 | + query, |
| 7 | + deleteEntity, |
| 8 | + move, |
| 9 | + parseData |
| 10 | +} from '../../../entity' |
| 11 | + |
| 12 | +export function Terms (http, data) { |
| 13 | + this.stackHeaders = data.stackHeaders |
| 14 | + this.taxonomy_uid = data.taxonomy_uid |
| 15 | + this.urlPath = `/taxonomies/${this.taxonomy_uid}/terms` |
| 16 | + |
| 17 | + if (data && data.term) { |
| 18 | + Object.assign(this, cloneDeep(data.term)) |
| 19 | + this.urlPath = `/taxonomies/${this.taxonomy_uid}/terms/${this.uid}` |
| 20 | + |
| 21 | + /** |
| 22 | + * @description The Update terms call is used to update an existing term. |
| 23 | + * @memberof Terms |
| 24 | + * @func update |
| 25 | + * @returns {Promise<Terms.Terms>} Promise for Terms instance |
| 26 | + * @example |
| 27 | + * import * as contentstack from '@contentstack/management' |
| 28 | + * const client = contentstack.client() |
| 29 | + * |
| 30 | + * client.stack({ api_key: 'api_key'}).terms('terms_uid').fetch() |
| 31 | + * .then((terms) => { |
| 32 | + * terms.name = 'terms name' |
| 33 | + * return terms.update() |
| 34 | + * }) |
| 35 | + * .then((terms) => console.log(terms)) |
| 36 | + * |
| 37 | + */ |
| 38 | + this.update = update(http, 'term') |
| 39 | + |
| 40 | + /** |
| 41 | + * @description The Delete terms call is used to delete an existing term. |
| 42 | + * @memberof Terms |
| 43 | + * @func delete |
| 44 | + * @returns {Promise<Terms.Terms>} Response Object. |
| 45 | + * @example |
| 46 | + * import * as contentstack from '@contentstack/management' |
| 47 | + * const client = contentstack.client() |
| 48 | + * |
| 49 | + * client.stack({ api_key: 'api_key'}).terms('terms_uid').delete() |
| 50 | + * .then((response) => console.log(response.notice)) |
| 51 | + * |
| 52 | + */ |
| 53 | + this.delete = deleteEntity(http) |
| 54 | + |
| 55 | + /** |
| 56 | + * @description The Fetch terms call is used to fetch an existing term. |
| 57 | + * @memberof Terms |
| 58 | + * @func fetch |
| 59 | + * @returns {Promise<Terms.Terms>} Promise for Terms instance |
| 60 | + * @example |
| 61 | + * import * as contentstack from '@contentstack/management' |
| 62 | + * const client = contentstack.client() |
| 63 | + * |
| 64 | + * client.stack({ api_key: 'api_key'}).terms('terms_uid').fetch() |
| 65 | + * .then((terms) => console.log(terms)) |
| 66 | + * |
| 67 | + */ |
| 68 | + this.fetch = fetch(http, 'term') |
| 69 | + |
| 70 | + /** |
| 71 | + * @description The ancestors call is used to get all the ancestor terms of an existing term. |
| 72 | + * @memberof Terms |
| 73 | + * @func ancestors |
| 74 | + * @returns {Promise<Terms.Terms>} Promise for Terms instance |
| 75 | + * @example |
| 76 | + * import * as contentstack from '@contentstack/management' |
| 77 | + * const client = contentstack.client() |
| 78 | + * |
| 79 | + * client.stack({ api_key: 'api_key'}).terms('terms_uid').ancestors() |
| 80 | + * .then((terms) => console.log(terms)) |
| 81 | + * |
| 82 | + */ |
| 83 | + this.ancestors = async (params = {}) => { |
| 84 | + try { |
| 85 | + const headers = { |
| 86 | + headers: { ...cloneDeep(this.stackHeaders), ...cloneDeep(params) } |
| 87 | + } |
| 88 | + const response = await http.get(`${this.urlPath}/ancestors`, headers) |
| 89 | + return parseData(response, this.stackHeaders) |
| 90 | + } catch (err) { |
| 91 | + console.error(err) |
| 92 | + throw err |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * @description The move call is used to existing term. |
| 98 | + * @memberof Terms |
| 99 | + * @func descendants |
| 100 | + * @returns {Promise<Terms.Terms>} Promise for Terms instance |
| 101 | + * @example |
| 102 | + * import * as contentstack from '@contentstack/management' |
| 103 | + * const client = contentstack.client() |
| 104 | + * |
| 105 | + * client.stack({ api_key: 'api_key'}).terms('terms_uid').descendants() |
| 106 | + * .then((terms) => console.log(terms)) |
| 107 | + * |
| 108 | + */ |
| 109 | + this.descendants = async (params = {}) => { |
| 110 | + try { |
| 111 | + const headers = { |
| 112 | + headers: { ...cloneDeep(this.stackHeaders), ...cloneDeep(params) } |
| 113 | + } |
| 114 | + const response = await http.get(`${this.urlPath}/descendants`, headers) |
| 115 | + return parseData(response, this.stackHeaders) |
| 116 | + } catch (err) { |
| 117 | + console.error(err) |
| 118 | + throw err |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * @description The move call is used to update the parent uid. |
| 124 | + * @memberof Terms |
| 125 | + * @func anscestors |
| 126 | + * @returns {Promise<Terms.Terms>} Promise for Terms instance |
| 127 | + * @example |
| 128 | + * import * as contentstack from '@contentstack/management' |
| 129 | + * const client = contentstack.client() |
| 130 | + * |
| 131 | + * const term = { |
| 132 | + * parent_uid: 'parent_uid', |
| 133 | + * order: 2 |
| 134 | + * } |
| 135 | + * client.stack({ api_key: 'api_key'}).terms('terms_uid').move(term) |
| 136 | + * .then((terms) => console.log(terms)) |
| 137 | + * |
| 138 | + */ |
| 139 | + this.move = move(http, 'term') |
| 140 | + } else { |
| 141 | + /** |
| 142 | + * @description The Create terms call is used to create a terms. |
| 143 | + * @memberof Terms |
| 144 | + * @func create |
| 145 | + * @returns {Promise<Terms.Terms>} Promise for Terms instance |
| 146 | + * @example |
| 147 | + * import * as contentstack from '@contentstack/management' |
| 148 | + * const client = contentstack.client() |
| 149 | + * const terms = { |
| 150 | + * uid: 'terms_testing1', |
| 151 | + * name: 'terms testing', |
| 152 | + * description: 'Description for terms testing' |
| 153 | + * } |
| 154 | + * client.stack({ api_key: 'api_key'}).terms().create({terms}) |
| 155 | + * .then(terms) => console.log(terms) |
| 156 | + * |
| 157 | + */ |
| 158 | + this.create = create({ http }) |
| 159 | + |
| 160 | + /** |
| 161 | + * @description The Query on Terms will allow to fetch details of all Terms. |
| 162 | + * @memberof Terms |
| 163 | + * @param {Object} params - URI parameters |
| 164 | + * @prop {Object} params.query - Queries that you can use to fetch filtered results. |
| 165 | + * @func query |
| 166 | + * @returns {Array<Terms>} Array of Terms. |
| 167 | + * |
| 168 | + * @example |
| 169 | + * import * as contentstack from '@contentstack/management' |
| 170 | + * const client = contentstack.client() |
| 171 | + * |
| 172 | + * client.stack().terms().query().find() |
| 173 | + * .then((terms) => console.log(terms) |
| 174 | + */ |
| 175 | + this.query = query({ http: http, wrapperCollection: TermsCollection }) |
| 176 | + } |
| 177 | + /** |
| 178 | + * @description The Search terms call is used to search a term. |
| 179 | + * @memberof Terms |
| 180 | + * @func search |
| 181 | + * @returns {Promise<Terms.Terms>} Promise for Terms instance |
| 182 | + * @example |
| 183 | + * import * as contentstack from '@contentstack/management' |
| 184 | + * const client = contentstack.client() |
| 185 | + * const term_string = '' |
| 186 | + * client.stack({ api_key: 'api_key'}).terms().search(term_string) |
| 187 | + * .then(terms) => console.log(terms) |
| 188 | + * |
| 189 | + */ |
| 190 | + this.search = async (term = '', params = {}) => { |
| 191 | + try { |
| 192 | + const headers = { |
| 193 | + headers: { ...cloneDeep(this.stackHeaders), ...cloneDeep(params) } |
| 194 | + } |
| 195 | + const response = await http.get(`taxonomies/${this.taxonomy_uid}/terms?term=${term}`, headers) |
| 196 | + return parseData(response, this.stackHeaders) |
| 197 | + } catch (err) { |
| 198 | + console.error(err) |
| 199 | + throw err |
| 200 | + } |
| 201 | + } |
| 202 | +} |
| 203 | +export function TermsCollection (http, data) { |
| 204 | + const obj = cloneDeep(data.terms) || [] |
| 205 | + const termsCollection = obj.map((term) => { |
| 206 | + return new Terms(http, { term: term, taxonomy_uid: data.taxonomy_uid, stackHeaders: data.stackHeaders }) |
| 207 | + }) |
| 208 | + return termsCollection |
| 209 | +} |
0 commit comments