diff --git a/lib/entity.js b/lib/entity.js index 5ca89fc2..74fbeac7 100644 --- a/lib/entity.js +++ b/lib/entity.js @@ -84,7 +84,7 @@ export const create = ({ http, params }) => { try { const response = await http.post(this.urlPath, data, headers) if (response.data) { - return new this.constructor(http, parseData(response, this.stackHeaders, this.content_type_uid)) + return new this.constructor(http, parseData(response, this.stackHeaders, this.content_type_uid, this.taxonomy_uid)) } else { throw error(response) } @@ -144,7 +144,7 @@ export const update = (http, type, params = {}) => { } }) if (response.data) { - return new this.constructor(http, parseData(response, this.stackHeaders, this.content_type_uid)) + return new this.constructor(http, parseData(response, this.stackHeaders, this.content_type_uid, this.taxonomy_uid)) } else { throw error(response) } @@ -204,7 +204,7 @@ export const fetch = (http, type, params = {}) => { response.data[type]['content_type'] = response.data['content_type'] response.data[type]['schema'] = response.data['schema'] } - return new this.constructor(http, parseData(response, this.stackHeaders, this.content_type_uid)) + return new this.constructor(http, parseData(response, this.stackHeaders, this.content_type_uid, this.taxonomy_uid)) } else { throw error(response) } @@ -235,7 +235,7 @@ export const fetchAll = (http, wrapperCollection, params = {}) => { } } -export function parseData (response, stackHeaders, contentTypeUID) { +export function parseData (response, stackHeaders, contentTypeUID, taxonomyUID) { const data = response.data || {} if (stackHeaders) { data.stackHeaders = stackHeaders @@ -243,6 +243,9 @@ export function parseData (response, stackHeaders, contentTypeUID) { if (contentTypeUID) { data.content_type_uid = contentTypeUID } + if (taxonomyUID) { + data.taxonomy_uid = taxonomyUID + } return data } @@ -266,4 +269,4 @@ export async function get (http, url, params, data) { } catch (err) { throw error(err) } -} \ No newline at end of file +} diff --git a/lib/stack/taxonomy/index.js b/lib/stack/taxonomy/index.js index a4989712..ffd13b79 100644 --- a/lib/stack/taxonomy/index.js +++ b/lib/stack/taxonomy/index.js @@ -1,3 +1,4 @@ +/* eslint-disable camelcase */ import cloneDeep from 'lodash/cloneDeep' import { create, @@ -6,13 +7,17 @@ import { update, deleteEntity } from '../../entity' +import { Terms, TermsCollection } from './terms' -export function Taxonomy (http, data) { +export function Taxonomy (http, data = {}) { this.stackHeaders = data.stackHeaders this.urlPath = `/taxonomies` if (data.taxonomy) { Object.assign(this, cloneDeep(data.taxonomy)) + if (data.taxonomy.terms) { + this.terms = new TermsCollection(http, { terms: data.taxonomy.terms, stackHeaders: data.stackHeaders }, this.uid) + } this.urlPath = `/taxonomies/${this.uid}` /** @@ -24,7 +29,7 @@ export function Taxonomy (http, data) { * import * as contentstack from '@contentstack/management' * const client = contentstack.client() * - * client.stack({ api_key: 'api_key'}).taxonomy('taxonomy_uid').fetch() + * client.stack({ api_key: 'api_key'}).taxonomy('taxonomyUid').fetch() * .then((taxonomy) => { * taxonomy.name = 'taxonomy name' * return taxonomy.update() @@ -43,7 +48,7 @@ export function Taxonomy (http, data) { * import * as contentstack from '@contentstack/management' * const client = contentstack.client() * - * client.stack({ api_key: 'api_key'}).taxonomy('taxonomy_uid').delete() + * client.stack({ api_key: 'api_key'}).taxonomy('taxonomyUid').delete() * .then((response) => console.log(response.notice)) * */ @@ -58,11 +63,20 @@ export function Taxonomy (http, data) { * import * as contentstack from '@contentstack/management' * const client = contentstack.client() * - * client.stack({ api_key: 'api_key'}).taxonomy('taxonomy_uid').fetch() + * client.stack({ api_key: 'api_key'}).taxonomy('taxonomyUid').fetch() * .then((taxonomy) => console.log(taxonomy)) * */ this.fetch = fetch(http, 'taxonomy') + + this.terms = (uid = '') => { + const data = { stackHeaders: this.stackHeaders } + data.taxonomy_uid = this.uid + if (uid) { + data.term = { uid: uid } + } + return new Terms(http, data) + } } else { /** * @description The Create taxonomy call is used to create a taxonomy. diff --git a/lib/stack/taxonomy/terms/index.js b/lib/stack/taxonomy/terms/index.js new file mode 100644 index 00000000..6d224620 --- /dev/null +++ b/lib/stack/taxonomy/terms/index.js @@ -0,0 +1,145 @@ +import cloneDeep from 'lodash/cloneDeep' +import { + create, + fetch, + update, + query, + deleteEntity +} from '../../../entity' + +export function Terms (http, data) { + this.stackHeaders = data.stackHeaders + this.taxonomy_uid = data.taxonomy_uid + this.urlPath = `/taxonomies/${this.taxonomy_uid}/terms` + + if (data && data.term) { + Object.assign(this, cloneDeep(data.term)) + this.urlPath = `/taxonomies/${this.taxonomy_uid}/terms/${this.uid}` + + /** + * @description The Update terms call is used to update an existing terms. + * @memberof Terms + * @func update + * @returns {Promise} Promise for Terms instance + * @example + * import * as contentstack from '@contentstack/management' + * const client = contentstack.client() + * + * client.stack({ api_key: 'api_key'}).terms('terms_uid').fetch() + * .then((terms) => { + * terms.name = 'terms name' + * return terms.update() + * }) + * .then((terms) => console.log(terms)) + * + */ + this.update = update(http, 'term') + + /** + * @description The Delete terms call is used to delete an existing terms. + * @memberof Terms + * @func delete + * @returns {Promise} Response Object. + * @example + * import * as contentstack from '@contentstack/management' + * const client = contentstack.client() + * + * client.stack({ api_key: 'api_key'}).terms('terms_uid').delete() + * .then((response) => console.log(response.notice)) + * + */ + this.delete = deleteEntity(http) + + /** + * @description The Fetch terms call is used to fetch an existing terms. + * @memberof Terms + * @func fetch + * @returns {Promise} Promise for Terms instance + * @example + * import * as contentstack from '@contentstack/management' + * const client = contentstack.client() + * + * client.stack({ api_key: 'api_key'}).terms('terms_uid').fetch() + * .then((terms) => console.log(terms)) + * + */ + this.fetch = fetch(http, 'term') + this.ancestors = async (params = {}) => { + try { + const headers = { + headers: { ...cloneDeep(this.stackHeaders), ...cloneDeep(params) } + } + const response = await http.get(`${this.urlPath}/ancestors`, headers) + return parseData(response, this.stackHeaders) + } catch (err) { + console.error(err) + throw err + } + } + this.descendants = async (params = {}) => { + try { + const headers = { + headers: { ...cloneDeep(this.stackHeaders), ...cloneDeep(params) } + } + const response = await http.get(`${this.urlPath}/descendants`, headers) + return parseData(response, this.stackHeaders) + } catch (err) { + console.error(err) + throw err + } + } + } else { + /** + * @description The Create terms call is used to create a terms. + * @memberof Terms + * @func create + * @returns {Promise} Promise for Terms instance + * @example + * import * as contentstack from '@contentstack/management' + * const client = contentstack.client() + * const terms = { + * uid: 'terms_testing1', + * name: 'terms testing', + * description: 'Description for terms testing' + * } + * client.stack({ api_key: 'api_key'}).terms().create({terms}) + * .then(terms) => console.log(terms) + * + */ + this.create = create({ http }) + + /** + * @description The Query on Terms will allow to fetch details of all Terms. + * @memberof Terms + * @param {Object} params - URI parameters + * @prop {Object} params.query - Queries that you can use to fetch filtered results. + * @func query + * @returns {Array} Array of Terms. + * + * @example + * import * as contentstack from '@contentstack/management' + * const client = contentstack.client() + * + * client.stack().terms().query().find() + * .then((terms) => console.log(terms) + */ + this.query = query({ http: http, wrapperCollection: TermsCollection }) + } +} +export function parseData (response, stackHeaders, taxonomy_uid) { + const data = response.data || {} + if (stackHeaders) { + data.stackHeaders = stackHeaders + } + if (taxonomy_uid) { + data.taxonomy_uid = taxonomy_uid + } + return data +} +export function TermsCollection (http, data) { + const obj = cloneDeep(data.terms) || [] + const termsCollection = obj.map((term) => { + return new Terms(http, { term: term, taxonomy_uid: data.taxonomy_uid, stackHeaders: data.stackHeaders }) + }) + return termsCollection +}