Skip to content

Commit 97d0aa2

Browse files
Merge pull request #444 from contentstack/feat/dx-3649-taxonomy-publishing
Feat/dx 3649 taxonomy publishing
2 parents 54e8b1a + f0f7880 commit 97d0aa2

File tree

4 files changed

+124
-0
lines changed

4 files changed

+124
-0
lines changed

lib/stack/taxonomy/index.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,53 @@ export function Taxonomy (http, data = {}) {
183183
throw error(err)
184184
}
185185
}
186+
187+
/**
188+
* @description The Publish taxonomy call initiates a job to publish a taxonomy and/or specific terms to the specified environments and locales.
189+
* @memberof Taxonomy
190+
* @func publish
191+
* @param {Object} data - Publish details
192+
* @param {string} [api_version=''] - Optional API version (e.g., '3.2')
193+
* @returns {Promise<Object>} Response object with publish job details
194+
* @example
195+
* import * as contentstack from '@contentstack/management'
196+
* const client = contentstack.client()
197+
*
198+
* const publishData = {
199+
* locales: ["en-us"],
200+
* environments: ["development"],
201+
* items: [
202+
* {
203+
* uid: "taxonomy_testing",
204+
* term_uid: "vehicles"
205+
* },
206+
* {
207+
* uid: "taxonomy_testing",
208+
* term_uid: "cars"
209+
* }
210+
* ]
211+
* }
212+
* client.stack({ api_key: 'api_key'}).taxonomy().publish(publishData, '3.2')
213+
* .then((response) => console.log(response))
214+
*/
215+
this.publish = async function (data, api_version = '') {
216+
try {
217+
const headers = {
218+
headers: { ...cloneDeep(this.stackHeaders) }
219+
}
220+
if (api_version) {
221+
headers.headers.api_version = api_version
222+
}
223+
const response = await http.post(`${this.urlPath}/publish`, data, headers)
224+
if (response.data) {
225+
return response.data
226+
} else {
227+
throw error(response)
228+
}
229+
} catch (err) {
230+
throw error(err)
231+
}
232+
}
186233
}
187234
}
188235
export function TaxonomyCollection (http, data) {

test/sanity-check/api/terms-test.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,35 @@ describe('Terms API Test', () => {
125125
.catch(done)
126126
})
127127

128+
it('should publish with api_version', done => {
129+
const publishData = {
130+
locales: ['en-us'],
131+
environments: ['development'],
132+
items: [
133+
{
134+
uid: taxonomy.uid,
135+
term_uid: 'term_test'
136+
},
137+
{
138+
uid: taxonomy.uid,
139+
term_uid: 'term_test_child1'
140+
},
141+
{
142+
uid: taxonomy.uid,
143+
term_uid: 'term_test_child2'
144+
}
145+
]
146+
}
147+
makeTaxonomy()
148+
.publish(publishData, '3.2')
149+
.then((response) => {
150+
expect(response.notice).to.not.equal(null)
151+
expect(response.job_id).to.not.equal(undefined)
152+
done()
153+
})
154+
.catch(done)
155+
})
156+
128157
it('should search the term with the string passed', done => {
129158
makeTerms(taxonomy.uid).search(termString)
130159
.then((response) => {
@@ -166,6 +195,10 @@ function makeTerms (taxonomyUid, termUid = null) {
166195
return client.stack({ api_key: process.env.API_KEY }).taxonomy(taxonomyUid).terms(termUid)
167196
}
168197

198+
function makeTaxonomy () {
199+
return client.stack({ api_key: process.env.API_KEY }).taxonomy()
200+
}
201+
169202
describe('Branch creation api Test', () => {
170203
beforeEach(() => {
171204
const user = jsonReader('loggedinuser.json')

test/unit/taxonomy-test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,33 @@ describe('Contentstack Taxonomy test', () => {
189189
})
190190
.catch(done)
191191
})
192+
193+
it('Taxonomy publish test with api_version', done => {
194+
var mock = new MockAdapter(Axios)
195+
mock.onPost('/taxonomies/publish').reply(200, {
196+
notice: 'Taxonomy publish job initiated successfully.',
197+
job_id: 'job_456'
198+
})
199+
const publishData = {
200+
locales: ['en-us', 'fr-fr'],
201+
environments: ['production'],
202+
scheduled_at: '2025-10-01T10:00:00.000Z',
203+
items: [
204+
{
205+
uid: 'taxonomy_testing',
206+
term_uid: 'vehicles'
207+
}
208+
]
209+
}
210+
makeTaxonomy()
211+
.publish(publishData, '3.2')
212+
.then((response) => {
213+
expect(response.notice).to.be.equal('Taxonomy publish job initiated successfully.')
214+
expect(response.job_id).to.be.equal('job_456')
215+
done()
216+
})
217+
.catch(done)
218+
})
192219
})
193220

194221
function makeTaxonomy (data = {}) {

types/stack/taxonomy/index.d.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,27 @@ export interface Taxonomy extends SystemFields, SystemFunction<Taxonomy> {
1010

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

1516
export interface TaxonomyData extends AnyProperty {
1617
name: string
1718
uid: string
1819
description: string
1920
}
21+
22+
export interface TaxonomyPublishData {
23+
locales: Array<string>
24+
environments: Array<string>
25+
items: Array<TaxonomyPublishItem>
26+
}
27+
28+
export interface TaxonomyPublishItem {
29+
uid: string
30+
term_uid: string
31+
}
32+
33+
export interface TaxonomyPublishResponse extends AnyProperty {
34+
notice?: string
35+
job_id?: string
36+
}

0 commit comments

Comments
 (0)