Skip to content

Commit 8f6a142

Browse files
Merge pull request #80 from contentstack/feat/cs-41208-taxonomy-support
feat/cs 41208 Taxonomy support
2 parents d827fbd + 6d80df9 commit 8f6a142

File tree

7 files changed

+370
-7
lines changed

7 files changed

+370
-7
lines changed

lib/stack/index.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ import { Label } from './label'
1818
import { Branch } from './branch'
1919
import { BranchAlias } from './branchAlias'
2020
import { AuditLog } from './auditlog'
21-
// import { format } from 'util'
21+
import { Taxonomy } from './taxonomy'
22+
2223
/**
2324
* A stack is a space that stores the content of a project (a web or mobile property). Within a stack, you can create content structures, content entries, users, etc. related to the project. Read more about <a href='https://www.contentstack.com/docs/guide/stack'>Stacks</a>.
2425
* @namespace Stack
@@ -684,6 +685,28 @@ export function Stack (http, data) {
684685
}
685686
return new Role(http, data)
686687
}
688+
689+
/**
690+
* @description Taxonomies allow you to group a collection of content within a stack. Using taxonomies you can group content types that need to work together
691+
* @param {String} uid The UID of the Taxonomy you want to get details.
692+
* @returns {Taxonomy} Instance of Taxonomy.
693+
* @example
694+
* import * as contentstack from '@contentstack/management'
695+
* const client = contentstack.client()
696+
*
697+
* client.stack({ api_key: 'api_key'}).taxonomy().create()
698+
* .then((taxonomy) => console.log(taxonomy))
699+
*
700+
* client.stack({ api_key: 'api_key'}).taxonomy('taxonomy_uid').fetch()
701+
* .then((taxonomy) => console.log(taxonomy))
702+
*/
703+
this.taxonomy = (taxonomyUid = '') => {
704+
const data = { stackHeaders: this.stackHeaders }
705+
if (taxonomyUid) {
706+
data.taxonomy = { uid: taxonomyUid }
707+
}
708+
return new Taxonomy(http, data)
709+
}
687710
} else {
688711
/**
689712
* @description The Create stack call creates a new stack in your Contentstack account.

lib/stack/taxonomy/index.js

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

test/api/taxonomy-test.js

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import { expect } from 'chai'
2+
import { describe, it, setup } from 'mocha'
3+
import { jsonReader } from '../utility/fileOperations/readwrite'
4+
import { contentstackClient } from '../utility/ContentstackClient.js'
5+
6+
var client = {}
7+
var stack = {}
8+
9+
const taxonomy = {
10+
uid: 'taxonomy_testing1',
11+
name: 'taxonomy testing',
12+
description: 'Description for Taxonomy testing'
13+
}
14+
15+
var taxonomyUID = ''
16+
var taxonomyDelUID = 'taxonomy_testing'
17+
18+
describe('taxonomy api Test', () => {
19+
setup(() => {
20+
const user = jsonReader('loggedinuser.json')
21+
stack = jsonReader('stack.json')
22+
client = contentstackClient(user.authtoken)
23+
})
24+
25+
it('Create taxonomy', done => {
26+
makeTaxonomy()
27+
.create([{ taxonomy }])
28+
.then((taxonomyResponse) => {
29+
expect(taxonomyResponse.name).to.be.equal(taxonomy.name)
30+
done()
31+
})
32+
.catch(done)
33+
})
34+
35+
it('Fetch taxonomy from uid', done => {
36+
makeTaxonomy(taxonomyUID)
37+
.fetch()
38+
.then((taxonomyResponse) => {
39+
expect(taxonomyResponse.uid).to.be.equal(taxonomyUID)
40+
expect(taxonomyResponse.name).to.be.not.equal(null)
41+
done()
42+
})
43+
.catch(done)
44+
})
45+
46+
it('Update taxonomy from uid', done => {
47+
makeTaxonomy(taxonomyUID)
48+
.fetch()
49+
.then((taxonomyResponse) => {
50+
taxonomyResponse.name = 'Updated Name'
51+
return taxonomyResponse.update()
52+
})
53+
.then((taxonomyResponse) => {
54+
expect(taxonomyResponse.uid).to.be.equal(taxonomyUID)
55+
expect(taxonomyResponse.name).to.be.equal('Updated Name')
56+
done()
57+
})
58+
.catch(done)
59+
})
60+
61+
it('Delete taxonomy from uid', done => {
62+
makeTaxonomy(taxonomyDelUID)
63+
.delete()
64+
.then((taxonomyResponse) => {
65+
expect(taxonomyResponse.notice).to.be.equal('Taxonomy deleted successfully.')
66+
done()
67+
})
68+
.catch(done)
69+
})
70+
71+
it('Query to get all taxonomies', async () => {
72+
makeTaxonomy()
73+
.query()
74+
.find()
75+
.then((response) => {
76+
response.items.forEach((taxonomyResponse) => {
77+
expect(taxonomyResponse.uid).to.be.not.equal(null)
78+
expect(taxonomyResponse.name).to.be.not.equal(null)
79+
})
80+
})
81+
})
82+
})
83+
84+
function makeTaxonomy (uid = null) {
85+
return client.stack({ api_key: stack.api_key }).taxonomy(uid)
86+
}

test/test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,4 @@ require('./api/release-test')
2525
require('./api/label-test')
2626
require('./api/contentType-delete-test')
2727
require('./api/delete-test')
28+
require('./api/taxonomy-test')

test/unit/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,4 @@ require('./deployment-test')
3333
require('./app-request-test')
3434
require('./authorization-test')
3535
require('./auditLog-test')
36+
require('./taxonomy-test')

test/unit/mock/objects.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ const appInstallMock = {
539539
status: 'installed',
540540
installation_uid: 'installationUID',
541541
redirect_to: 'config',
542-
redirect_uri: 'redirect_uri',
542+
redirect_uri: 'redirect_uri'
543543
}
544544

545545
const installationMock = {
@@ -613,16 +613,16 @@ const branchCompareAllMock = {
613613
compare_branch: 'dev'
614614
},
615615
diff: [...globalFieldDiff, ...contentTypeDiff],
616-
next_url:'https://api.contentstack.io/v3/stacks/branches/compare?base_branch=main&compare_branch=dev&skip=0&limit=100'
616+
next_url: 'https://api.contentstack.io/v3/stacks/branches/compare?base_branch=main&compare_branch=dev&skip=0&limit=100'
617617
}
618618

619619
const branchCompareContentTypeMock = {
620620
branches: {
621621
base_branch: 'UID',
622622
compare_branch: 'dev'
623623
},
624-
diff: [ ...contentTypeDiff ],
625-
next_url:'https://api.contentstack.io/v3/stacks/branches/compare?base_branch=main&compare_branch=dev&skip=0&limit=100'
624+
diff: [...contentTypeDiff],
625+
next_url: 'https://api.contentstack.io/v3/stacks/branches/compare?base_branch=main&compare_branch=dev&skip=0&limit=100'
626626
}
627627

628628
const branchCompareGlobalFieldMock = {
@@ -631,7 +631,7 @@ const branchCompareGlobalFieldMock = {
631631
compare_branch: 'dev'
632632
},
633633
diff: [...globalFieldDiff],
634-
next_url:'https://api.contentstack.io/v3/stacks/branches/compare?base_branch=main&compare_branch=dev&skip=0&limit=100'
634+
next_url: 'https://api.contentstack.io/v3/stacks/branches/compare?base_branch=main&compare_branch=dev&skip=0&limit=100'
635635
}
636636

637637
const branchMergeAllMock = {
@@ -647,7 +647,7 @@ const branchMergeAllMock = {
647647
status: 'in_progress'
648648
},
649649
merged_at: null,
650-
errors: [
650+
errors: [
651651
{
652652
code: 'error_code',
653653
message: 'Error message'
@@ -725,6 +725,15 @@ const auditLogsMock = {
725725
]
726726
}
727727

728+
const taxonomyMock = {
729+
uid: 'UID',
730+
name: 'name',
731+
description: 'Description for Taxonomy',
732+
terms_count: 4,
733+
referenced_terms_count: 3,
734+
referenced_entries_count: 6
735+
}
736+
728737
function mockCollection (mockData, type) {
729738
const mock = {
730739
...cloneDeep(noticeMock),
@@ -793,6 +802,7 @@ export {
793802
branchMergeQueueFetchMock,
794803
auditLogsMock,
795804
auditLogItemMock,
805+
taxonomyMock,
796806
mockCollection,
797807
entryMockCollection,
798808
checkSystemFields

0 commit comments

Comments
 (0)