Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions lib/stack/taxonomy/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,53 @@ 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<Object>} 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"
* }
* ]
* }
* 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) {
Expand Down
33 changes: 33 additions & 0 deletions test/sanity-check/api/terms-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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')
Expand Down
27 changes: 27 additions & 0 deletions test/unit/taxonomy-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}) {
Expand Down
17 changes: 17 additions & 0 deletions types/stack/taxonomy/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,27 @@ export interface Taxonomy extends SystemFields, SystemFunction<Taxonomy> {

export interface Taxonomies extends Creatable<Taxonomy, {taxonomy: TaxonomyData}>, Queryable<Taxonomy, {taxonomy: TaxonomyData}> {
import(data: TaxonomyData, params?: any): Promise<Taxonomy>
publish(data: TaxonomyPublishData, api_version?: string): Promise<TaxonomyPublishResponse>
}

export interface TaxonomyData extends AnyProperty {
name: string
uid: string
description: string
}

export interface TaxonomyPublishData {
locales: Array<string>
environments: Array<string>
items: Array<TaxonomyPublishItem>
}

export interface TaxonomyPublishItem {
uid: string
term_uid: string
}

export interface TaxonomyPublishResponse extends AnyProperty {
notice?: string
job_id?: string
}