|
| 1 | +/* eslint-disable camelcase */ |
| 2 | +import cloneDeep from 'lodash/cloneDeep' |
| 3 | +import { |
| 4 | + create, |
| 5 | + fetch, |
| 6 | + query, |
| 7 | + update, |
| 8 | + deleteEntity |
| 9 | +} from '../../entity' |
| 10 | +import { Terms, TermsCollection } from './terms' |
| 11 | + |
| 12 | +export function Taxonomy (http, data = {}) { |
| 13 | + this.stackHeaders = data.stackHeaders |
| 14 | + this.urlPath = `/taxonomies` |
| 15 | + |
| 16 | + if (data.taxonomy) { |
| 17 | + Object.assign(this, cloneDeep(data.taxonomy)) |
| 18 | + if (data.taxonomy.terms) { |
| 19 | + this.terms = new TermsCollection(http, { terms: data.taxonomy.terms, stackHeaders: data.stackHeaders }, this.uid) |
| 20 | + } |
| 21 | + this.urlPath = `/taxonomies/${this.uid}` |
| 22 | + |
| 23 | + /** |
| 24 | + * @description The Update taxonomy call is used to update an existing taxonomy. |
| 25 | + * @memberof Taxonomy |
| 26 | + * @func update |
| 27 | + * @returns {Promise<Taxonomy.Taxonomy>} Promise for Taxonomy instance |
| 28 | + * @example |
| 29 | + * import * as contentstack from '@contentstack/management' |
| 30 | + * const client = contentstack.client() |
| 31 | + * |
| 32 | + * client.stack({ api_key: 'api_key'}).taxonomy('taxonomyUid').fetch() |
| 33 | + * .then((taxonomy) => { |
| 34 | + * taxonomy.name = 'taxonomy name' |
| 35 | + * return taxonomy.update() |
| 36 | + * }) |
| 37 | + * .then((taxonomy) => console.log(taxonomy)) |
| 38 | + * |
| 39 | + */ |
| 40 | + this.update = update(http, 'taxonomy') |
| 41 | + |
| 42 | + /** |
| 43 | + * @description The Delete taxonomy call is used to delete an existing taxonomy. |
| 44 | + * @memberof Taxonomy |
| 45 | + * @func delete |
| 46 | + * @returns {Promise<Taxonomy.Taxonomy>} Response Object. |
| 47 | + * @example |
| 48 | + * import * as contentstack from '@contentstack/management' |
| 49 | + * const client = contentstack.client() |
| 50 | + * |
| 51 | + * client.stack({ api_key: 'api_key'}).taxonomy('taxonomyUid').delete() |
| 52 | + * .then((response) => console.log(response.notice)) |
| 53 | + * |
| 54 | + */ |
| 55 | + this.delete = deleteEntity(http) |
| 56 | + |
| 57 | + /** |
| 58 | + * @description The Fetch taxonomy call is used to fetch an existing taxonomy. |
| 59 | + * @memberof Taxonomy |
| 60 | + * @func fetch |
| 61 | + * @returns {Promise<Taxonomy.Taxonomy>} Promise for Taxonomy instance |
| 62 | + * @example |
| 63 | + * import * as contentstack from '@contentstack/management' |
| 64 | + * const client = contentstack.client() |
| 65 | + * |
| 66 | + * client.stack({ api_key: 'api_key'}).taxonomy('taxonomyUid').fetch() |
| 67 | + * .then((taxonomy) => console.log(taxonomy)) |
| 68 | + * |
| 69 | + */ |
| 70 | + this.fetch = fetch(http, 'taxonomy') |
| 71 | + |
| 72 | + this.terms = (uid = '') => { |
| 73 | + const data = { stackHeaders: this.stackHeaders } |
| 74 | + data.taxonomy_uid = this.uid |
| 75 | + if (uid) { |
| 76 | + data.term = { uid: uid } |
| 77 | + } |
| 78 | + return new Terms(http, data) |
| 79 | + } |
| 80 | + } else { |
| 81 | + /** |
| 82 | + * @description The Create taxonomy call is used to create a taxonomy. |
| 83 | + * @memberof Taxonomy |
| 84 | + * @func create |
| 85 | + * @returns {Promise<Taxonomy.Taxonomy>} Promise for Taxonomy instance |
| 86 | + * @example |
| 87 | + * import * as contentstack from '@contentstack/management' |
| 88 | + * const client = contentstack.client() |
| 89 | + * const taxonomy = { |
| 90 | + * uid: 'taxonomy_testing1', |
| 91 | + * name: 'taxonomy testing', |
| 92 | + * description: 'Description for Taxonomy testing' |
| 93 | + * } |
| 94 | + * client.stack({ api_key: 'api_key'}).taxonomy().create({taxonomy}) |
| 95 | + * .then(taxonomy) => console.log(taxonomy) |
| 96 | + * |
| 97 | + */ |
| 98 | + this.create = create({ http }) |
| 99 | + |
| 100 | + /** |
| 101 | + * @description The Query on Taxonomy will allow to fetch details of all Taxonomies. |
| 102 | + * @memberof Taxonomy |
| 103 | + * @param {Object} params - URI parameters |
| 104 | + * @prop {Object} params.query - Queries that you can use to fetch filtered results. |
| 105 | + * @func query |
| 106 | + * @returns {Array<Taxonomy>} Array of Taxonomy. |
| 107 | + * |
| 108 | + * @example |
| 109 | + * import * as contentstack from '@contentstack/management' |
| 110 | + * const client = contentstack.client() |
| 111 | + * |
| 112 | + * client.stack().taxonomy().query().find() |
| 113 | + * .then((taxonomies) => console.log(taxonomies) |
| 114 | + */ |
| 115 | + this.query = query({ http: http, wrapperCollection: TaxonomyCollection }) |
| 116 | + } |
| 117 | +} |
| 118 | +export function TaxonomyCollection (http, data) { |
| 119 | + const obj = cloneDeep(data.taxonomy) || [] |
| 120 | + const taxonomyCollection = obj.map((userdata) => { |
| 121 | + return new Taxonomy(http, { taxonomy: userdata, stackHeaders: data.stackHeaders }) |
| 122 | + }) |
| 123 | + return taxonomyCollection |
| 124 | +} |
0 commit comments