Skip to content

Commit 5fadd20

Browse files
Merge pull request #81 from contentstack/feat/cs-41463-terms-support
2 parents 505f013 + f40a346 commit 5fadd20

File tree

8 files changed

+661
-8
lines changed

8 files changed

+661
-8
lines changed

lib/entity.js

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ export const create = ({ http, params }) => {
8484
try {
8585
const response = await http.post(this.urlPath, data, headers)
8686
if (response.data) {
87-
return new this.constructor(http, parseData(response, this.stackHeaders, this.content_type_uid))
87+
return new this.constructor(http, parseData(response, this.stackHeaders, this.content_type_uid, this.taxonomy_uid))
8888
} else {
8989
throw error(response)
9090
}
@@ -144,7 +144,7 @@ export const update = (http, type, params = {}) => {
144144
}
145145
})
146146
if (response.data) {
147-
return new this.constructor(http, parseData(response, this.stackHeaders, this.content_type_uid))
147+
return new this.constructor(http, parseData(response, this.stackHeaders, this.content_type_uid, this.taxonomy_uid))
148148
} else {
149149
throw error(response)
150150
}
@@ -204,7 +204,7 @@ export const fetch = (http, type, params = {}) => {
204204
response.data[type]['content_type'] = response.data['content_type']
205205
response.data[type]['schema'] = response.data['schema']
206206
}
207-
return new this.constructor(http, parseData(response, this.stackHeaders, this.content_type_uid))
207+
return new this.constructor(http, parseData(response, this.stackHeaders, this.content_type_uid, this.taxonomy_uid))
208208
} else {
209209
throw error(response)
210210
}
@@ -235,14 +235,17 @@ export const fetchAll = (http, wrapperCollection, params = {}) => {
235235
}
236236
}
237237

238-
export function parseData (response, stackHeaders, contentTypeUID) {
238+
export function parseData (response, stackHeaders, contentTypeUID, taxonomy_uid) {
239239
const data = response.data || {}
240240
if (stackHeaders) {
241241
data.stackHeaders = stackHeaders
242242
}
243243
if (contentTypeUID) {
244244
data.content_type_uid = contentTypeUID
245245
}
246+
if (taxonomy_uid) {
247+
data.taxonomy_uid = taxonomy_uid
248+
}
246249
return data
247250
}
248251

@@ -266,4 +269,36 @@ export async function get (http, url, params, data) {
266269
} catch (err) {
267270
throw error(err)
268271
}
272+
}
273+
274+
export const move = (http, type, force = false, params = {}) => {
275+
return async function (param = {}) {
276+
try {
277+
let updateData = {}
278+
const json = cloneDeep(this)
279+
delete json.parent_uid
280+
if (type) {
281+
updateData[type] = json
282+
} else {
283+
updateData = json
284+
}
285+
const headers = {
286+
headers: { ...cloneDeep(this.stackHeaders), ...cloneDeep(params) },
287+
params: {
288+
...cloneDeep(param)
289+
}
290+
} || {}
291+
if (force === true) {
292+
headers.params.force = true
293+
}
294+
const response = await http.put(`${this.urlPath}/move`, updateData, headers)
295+
if (response.data) {
296+
return new this.constructor(http, parseData(response, this.stackHeaders, this.content_type_uid, this.taxonomy_uid))
297+
} else {
298+
throw error(response)
299+
}
300+
} catch (err) {
301+
throw error(err)
302+
}
303+
}
269304
}

lib/stack/taxonomy/index.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable camelcase */
12
import cloneDeep from 'lodash/cloneDeep'
23
import {
34
create,
@@ -6,13 +7,17 @@ import {
67
update,
78
deleteEntity
89
} from '../../entity'
10+
import { Terms, TermsCollection } from './terms'
911

10-
export function Taxonomy (http, data) {
12+
export function Taxonomy (http, data = {}) {
1113
this.stackHeaders = data.stackHeaders
1214
this.urlPath = `/taxonomies`
1315

1416
if (data.taxonomy) {
1517
Object.assign(this, cloneDeep(data.taxonomy))
18+
if (data.taxonomy.terms) {
19+
this.terms = new TermsCollection(http, { terms: data.taxonomy.terms, stackHeaders: data.stackHeaders }, this.uid)
20+
}
1621
this.urlPath = `/taxonomies/${this.uid}`
1722

1823
/**
@@ -24,7 +29,7 @@ export function Taxonomy (http, data) {
2429
* import * as contentstack from '@contentstack/management'
2530
* const client = contentstack.client()
2631
*
27-
* client.stack({ api_key: 'api_key'}).taxonomy('taxonomy_uid').fetch()
32+
* client.stack({ api_key: 'api_key'}).taxonomy('taxonomyUid').fetch()
2833
* .then((taxonomy) => {
2934
* taxonomy.name = 'taxonomy name'
3035
* return taxonomy.update()
@@ -43,7 +48,7 @@ export function Taxonomy (http, data) {
4348
* import * as contentstack from '@contentstack/management'
4449
* const client = contentstack.client()
4550
*
46-
* client.stack({ api_key: 'api_key'}).taxonomy('taxonomy_uid').delete()
51+
* client.stack({ api_key: 'api_key'}).taxonomy('taxonomyUid').delete()
4752
* .then((response) => console.log(response.notice))
4853
*
4954
*/
@@ -58,11 +63,20 @@ export function Taxonomy (http, data) {
5863
* import * as contentstack from '@contentstack/management'
5964
* const client = contentstack.client()
6065
*
61-
* client.stack({ api_key: 'api_key'}).taxonomy('taxonomy_uid').fetch()
66+
* client.stack({ api_key: 'api_key'}).taxonomy('taxonomyUid').fetch()
6267
* .then((taxonomy) => console.log(taxonomy))
6368
*
6469
*/
6570
this.fetch = fetch(http, 'taxonomy')
71+
72+
this.terms = (uid = '') => {
73+
const data = { stackHeaders: this.stackHeaders }
74+
data.taxonomy_uid = this.uid
75+
if (uid) {
76+
data.term = { uid: uid }
77+
}
78+
return new Terms(http, data)
79+
}
6680
} else {
6781
/**
6882
* @description The Create taxonomy call is used to create a taxonomy.

lib/stack/taxonomy/terms/index.js

Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
1+
import cloneDeep from 'lodash/cloneDeep'
2+
import {
3+
create,
4+
fetch,
5+
update,
6+
query,
7+
deleteEntity,
8+
move,
9+
parseData
10+
} from '../../../entity'
11+
12+
export function Terms (http, data) {
13+
this.stackHeaders = data.stackHeaders
14+
this.taxonomy_uid = data.taxonomy_uid
15+
this.urlPath = `/taxonomies/${this.taxonomy_uid}/terms`
16+
17+
if (data && data.term) {
18+
Object.assign(this, cloneDeep(data.term))
19+
this.urlPath = `/taxonomies/${this.taxonomy_uid}/terms/${this.uid}`
20+
21+
/**
22+
* @description The Update terms call is used to update an existing term.
23+
* @memberof Terms
24+
* @func update
25+
* @returns {Promise<Terms.Terms>} Promise for Terms instance
26+
* @example
27+
* import * as contentstack from '@contentstack/management'
28+
* const client = contentstack.client()
29+
*
30+
* client.stack({ api_key: 'api_key'}).terms('terms_uid').fetch()
31+
* .then((terms) => {
32+
* terms.name = 'terms name'
33+
* return terms.update()
34+
* })
35+
* .then((terms) => console.log(terms))
36+
*
37+
*/
38+
this.update = update(http, 'term')
39+
40+
/**
41+
* @description The Delete terms call is used to delete an existing term.
42+
* @memberof Terms
43+
* @func delete
44+
* @returns {Promise<Terms.Terms>} Response Object.
45+
* @example
46+
* import * as contentstack from '@contentstack/management'
47+
* const client = contentstack.client()
48+
*
49+
* client.stack({ api_key: 'api_key'}).terms('terms_uid').delete()
50+
* .then((response) => console.log(response.notice))
51+
*
52+
*/
53+
this.delete = deleteEntity(http)
54+
55+
/**
56+
* @description The Fetch terms call is used to fetch an existing term.
57+
* @memberof Terms
58+
* @func fetch
59+
* @returns {Promise<Terms.Terms>} Promise for Terms instance
60+
* @example
61+
* import * as contentstack from '@contentstack/management'
62+
* const client = contentstack.client()
63+
*
64+
* client.stack({ api_key: 'api_key'}).terms('terms_uid').fetch()
65+
* .then((terms) => console.log(terms))
66+
*
67+
*/
68+
this.fetch = fetch(http, 'term')
69+
70+
/**
71+
* @description The ancestors call is used to get all the ancestor terms of an existing term.
72+
* @memberof Terms
73+
* @func ancestors
74+
* @returns {Promise<Terms.Terms>} Promise for Terms instance
75+
* @example
76+
* import * as contentstack from '@contentstack/management'
77+
* const client = contentstack.client()
78+
*
79+
* client.stack({ api_key: 'api_key'}).terms('terms_uid').ancestors()
80+
* .then((terms) => console.log(terms))
81+
*
82+
*/
83+
this.ancestors = async (params = {}) => {
84+
try {
85+
const headers = {
86+
headers: { ...cloneDeep(this.stackHeaders), ...cloneDeep(params) }
87+
}
88+
const response = await http.get(`${this.urlPath}/ancestors`, headers)
89+
return parseData(response, this.stackHeaders)
90+
} catch (err) {
91+
console.error(err)
92+
throw err
93+
}
94+
}
95+
96+
/**
97+
* @description The move call is used to existing term.
98+
* @memberof Terms
99+
* @func descendants
100+
* @returns {Promise<Terms.Terms>} Promise for Terms instance
101+
* @example
102+
* import * as contentstack from '@contentstack/management'
103+
* const client = contentstack.client()
104+
*
105+
* client.stack({ api_key: 'api_key'}).terms('terms_uid').descendants()
106+
* .then((terms) => console.log(terms))
107+
*
108+
*/
109+
this.descendants = async (params = {}) => {
110+
try {
111+
const headers = {
112+
headers: { ...cloneDeep(this.stackHeaders), ...cloneDeep(params) }
113+
}
114+
const response = await http.get(`${this.urlPath}/descendants`, headers)
115+
return parseData(response, this.stackHeaders)
116+
} catch (err) {
117+
console.error(err)
118+
throw err
119+
}
120+
}
121+
122+
/**
123+
* @description The move call is used to update the parent uid.
124+
* @memberof Terms
125+
* @func anscestors
126+
* @returns {Promise<Terms.Terms>} Promise for Terms instance
127+
* @example
128+
* import * as contentstack from '@contentstack/management'
129+
* const client = contentstack.client()
130+
*
131+
* const term = {
132+
* parent_uid: 'parent_uid',
133+
* order: 2
134+
* }
135+
* client.stack({ api_key: 'api_key'}).terms('terms_uid').move(term)
136+
* .then((terms) => console.log(terms))
137+
*
138+
*/
139+
this.move = move(http, 'term')
140+
} else {
141+
/**
142+
* @description The Create terms call is used to create a terms.
143+
* @memberof Terms
144+
* @func create
145+
* @returns {Promise<Terms.Terms>} Promise for Terms instance
146+
* @example
147+
* import * as contentstack from '@contentstack/management'
148+
* const client = contentstack.client()
149+
* const terms = {
150+
* uid: 'terms_testing1',
151+
* name: 'terms testing',
152+
* description: 'Description for terms testing'
153+
* }
154+
* client.stack({ api_key: 'api_key'}).terms().create({terms})
155+
* .then(terms) => console.log(terms)
156+
*
157+
*/
158+
this.create = create({ http })
159+
160+
/**
161+
* @description The Query on Terms will allow to fetch details of all Terms.
162+
* @memberof Terms
163+
* @param {Object} params - URI parameters
164+
* @prop {Object} params.query - Queries that you can use to fetch filtered results.
165+
* @func query
166+
* @returns {Array<Terms>} Array of Terms.
167+
*
168+
* @example
169+
* import * as contentstack from '@contentstack/management'
170+
* const client = contentstack.client()
171+
*
172+
* client.stack().terms().query().find()
173+
* .then((terms) => console.log(terms)
174+
*/
175+
this.query = query({ http: http, wrapperCollection: TermsCollection })
176+
}
177+
/**
178+
* @description The Search terms call is used to search a term.
179+
* @memberof Terms
180+
* @func search
181+
* @returns {Promise<Terms.Terms>} Promise for Terms instance
182+
* @example
183+
* import * as contentstack from '@contentstack/management'
184+
* const client = contentstack.client()
185+
* const term_string = ''
186+
* client.stack({ api_key: 'api_key'}).terms().search(term_string)
187+
* .then(terms) => console.log(terms)
188+
*
189+
*/
190+
this.search = async (term = '', params = {}) => {
191+
try {
192+
const headers = {
193+
headers: { ...cloneDeep(this.stackHeaders), ...cloneDeep(params) }
194+
}
195+
const response = await http.get(`taxonomies/${this.taxonomy_uid}/terms?term=${term}`, headers)
196+
return parseData(response, this.stackHeaders)
197+
} catch (err) {
198+
console.error(err)
199+
throw err
200+
}
201+
}
202+
}
203+
export function TermsCollection (http, data) {
204+
const obj = cloneDeep(data.terms) || []
205+
const termsCollection = obj.map((term) => {
206+
return new Terms(http, { term: term, taxonomy_uid: data.taxonomy_uid, stackHeaders: data.stackHeaders })
207+
})
208+
return termsCollection
209+
}

0 commit comments

Comments
 (0)