From 7bb2f885110285f428ce8bff7eb18b1fc2c1dd89 Mon Sep 17 00:00:00 2001 From: "harshitha.d" Date: Tue, 28 Oct 2025 17:35:44 +0530 Subject: [PATCH 1/3] Add publish method to Taxonomy class with API version support --- lib/stack/taxonomy/index.js | 53 +++++++++++++++++++++++++++++ test/sanity-check/api/terms-test.js | 33 ++++++++++++++++++ test/unit/taxonomy-test.js | 27 +++++++++++++++ 3 files changed, 113 insertions(+) diff --git a/lib/stack/taxonomy/index.js b/lib/stack/taxonomy/index.js index b2b83b4c..bce8e0f3 100644 --- a/lib/stack/taxonomy/index.js +++ b/lib/stack/taxonomy/index.js @@ -183,6 +183,59 @@ export function Taxonomy (http, data = {}) { throw error(err) } } + + /** + * @description The Publish taxonomy call initiates a job to publish a taxonomy and/or specific terms to the specified environments and locales. + * @memberof Taxonomy + * @func publish + * @param {Object} data - Publish details + * @param {string} [api_version=''] - Optional API version (e.g., '3.2') + * @returns {Promise} Response object with publish job details + * @example + * import * as contentstack from '@contentstack/management' + * const client = contentstack.client() + * + * const publishData = { + * locales: ["en-us"], + * environments: ["development"], + * items: [ + * { + * uid: "taxonomy_testing", + * term_uid: "vehicles" + * }, + * { + * uid: "taxonomy_testing", + * term_uid: "cars" + * } + * ] + * } + * + * // Without api_version + * client.stack({ api_key: 'api_key'}).taxonomy().publish(publishData) + * .then((response) => console.log(response)) + * + * // With api_version + * client.stack({ api_key: 'api_key'}).taxonomy().publish(publishData, '3.2') + * .then((response) => console.log(response)) + */ + this.publish = async function (data, api_version = '') { + try { + const headers = { + headers: { ...cloneDeep(this.stackHeaders) } + } + if (api_version) { + headers.headers.api_version = api_version + } + const response = await http.post(`${this.urlPath}/publish`, data, headers) + if (response.data) { + return response.data + } else { + throw error(response) + } + } catch (err) { + throw error(err) + } + } } } export function TaxonomyCollection (http, data) { diff --git a/test/sanity-check/api/terms-test.js b/test/sanity-check/api/terms-test.js index 771ed9a0..244ef0d4 100644 --- a/test/sanity-check/api/terms-test.js +++ b/test/sanity-check/api/terms-test.js @@ -125,6 +125,35 @@ describe('Terms API Test', () => { .catch(done) }) + it('should publish with api_version', done => { + const publishData = { + locales: ['en-us'], + environments: ['development'], + items: [ + { + uid: taxonomy.uid, + term_uid: 'term_test' + }, + { + uid: taxonomy.uid, + term_uid: 'term_test_child1' + }, + { + uid: taxonomy.uid, + term_uid: 'term_test_child2' + } + ] + } + makeTaxonomy() + .publish(publishData, '3.2') + .then((response) => { + expect(response.notice).to.not.equal(null) + expect(response.job_id).to.not.equal(undefined) + done() + }) + .catch(done) + }) + it('should search the term with the string passed', done => { makeTerms(taxonomy.uid).search(termString) .then((response) => { @@ -166,6 +195,10 @@ function makeTerms (taxonomyUid, termUid = null) { return client.stack({ api_key: process.env.API_KEY }).taxonomy(taxonomyUid).terms(termUid) } +function makeTaxonomy () { + return client.stack({ api_key: process.env.API_KEY }).taxonomy() +} + describe('Branch creation api Test', () => { beforeEach(() => { const user = jsonReader('loggedinuser.json') diff --git a/test/unit/taxonomy-test.js b/test/unit/taxonomy-test.js index 67a59090..4f9b1a64 100644 --- a/test/unit/taxonomy-test.js +++ b/test/unit/taxonomy-test.js @@ -189,6 +189,33 @@ describe('Contentstack Taxonomy test', () => { }) .catch(done) }) + + it('Taxonomy publish test with api_version', done => { + var mock = new MockAdapter(Axios) + mock.onPost('/taxonomies/publish').reply(200, { + notice: 'Taxonomy publish job initiated successfully.', + job_id: 'job_456' + }) + const publishData = { + locales: ['en-us', 'fr-fr'], + environments: ['production'], + scheduled_at: '2025-10-01T10:00:00.000Z', + items: [ + { + uid: 'taxonomy_testing', + term_uid: 'vehicles' + } + ] + } + makeTaxonomy() + .publish(publishData, '3.2') + .then((response) => { + expect(response.notice).to.be.equal('Taxonomy publish job initiated successfully.') + expect(response.job_id).to.be.equal('job_456') + done() + }) + .catch(done) + }) }) function makeTaxonomy (data = {}) { From 9bef6d1671bda0337bd1234a49eae0c987666172 Mon Sep 17 00:00:00 2001 From: "harshitha.d" Date: Tue, 28 Oct 2025 17:36:58 +0530 Subject: [PATCH 2/3] update docs --- lib/stack/taxonomy/index.js | 6 ------ 1 file changed, 6 deletions(-) diff --git a/lib/stack/taxonomy/index.js b/lib/stack/taxonomy/index.js index bce8e0f3..61b964b4 100644 --- a/lib/stack/taxonomy/index.js +++ b/lib/stack/taxonomy/index.js @@ -209,12 +209,6 @@ export function Taxonomy (http, data = {}) { * } * ] * } - * - * // Without api_version - * client.stack({ api_key: 'api_key'}).taxonomy().publish(publishData) - * .then((response) => console.log(response)) - * - * // With api_version * client.stack({ api_key: 'api_key'}).taxonomy().publish(publishData, '3.2') * .then((response) => console.log(response)) */ From f0f7880c4f15cb6fa89d36c731870613ff35498d Mon Sep 17 00:00:00 2001 From: "harshitha.d" Date: Tue, 28 Oct 2025 18:00:08 +0530 Subject: [PATCH 3/3] Add TaxonomyPublish types support --- types/stack/taxonomy/index.d.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/types/stack/taxonomy/index.d.ts b/types/stack/taxonomy/index.d.ts index bb9b0cea..bd11f182 100644 --- a/types/stack/taxonomy/index.d.ts +++ b/types/stack/taxonomy/index.d.ts @@ -10,6 +10,7 @@ export interface Taxonomy extends SystemFields, SystemFunction { export interface Taxonomies extends Creatable, Queryable { import(data: TaxonomyData, params?: any): Promise + publish(data: TaxonomyPublishData, api_version?: string): Promise } export interface TaxonomyData extends AnyProperty { @@ -17,3 +18,19 @@ export interface TaxonomyData extends AnyProperty { uid: string description: string } + +export interface TaxonomyPublishData { + locales: Array + environments: Array + items: Array +} + +export interface TaxonomyPublishItem { + uid: string + term_uid: string +} + +export interface TaxonomyPublishResponse extends AnyProperty { + notice?: string + job_id?: string +}