Skip to content

Commit 95204db

Browse files
feat: ✨ create, fetch, find, update, delete , ancestors and descendants functions are added
1 parent 8f6a142 commit 95204db

File tree

3 files changed

+171
-9
lines changed

3 files changed

+171
-9
lines changed

lib/entity.js

Lines changed: 8 additions & 5 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, taxonomyUID) {
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 (taxonomyUID) {
247+
data.taxonomy_uid = taxonomyUID
248+
}
246249
return data
247250
}
248251

@@ -266,4 +269,4 @@ export async function get (http, url, params, data) {
266269
} catch (err) {
267270
throw error(err)
268271
}
269-
}
272+
}

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: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
import cloneDeep from 'lodash/cloneDeep'
2+
import {
3+
create,
4+
fetch,
5+
update,
6+
query,
7+
deleteEntity
8+
} from '../../../entity'
9+
10+
export function Terms (http, data) {
11+
this.stackHeaders = data.stackHeaders
12+
this.taxonomy_uid = data.taxonomy_uid
13+
this.urlPath = `/taxonomies/${this.taxonomy_uid}/terms`
14+
15+
if (data && data.term) {
16+
Object.assign(this, cloneDeep(data.term))
17+
this.urlPath = `/taxonomies/${this.taxonomy_uid}/terms/${this.uid}`
18+
19+
/**
20+
* @description The Update terms call is used to update an existing terms.
21+
* @memberof Terms
22+
* @func update
23+
* @returns {Promise<Terms.Terms>} Promise for Terms instance
24+
* @example
25+
* import * as contentstack from '@contentstack/management'
26+
* const client = contentstack.client()
27+
*
28+
* client.stack({ api_key: 'api_key'}).terms('terms_uid').fetch()
29+
* .then((terms) => {
30+
* terms.name = 'terms name'
31+
* return terms.update()
32+
* })
33+
* .then((terms) => console.log(terms))
34+
*
35+
*/
36+
this.update = update(http, 'term')
37+
38+
/**
39+
* @description The Delete terms call is used to delete an existing terms.
40+
* @memberof Terms
41+
* @func delete
42+
* @returns {Promise<Terms.Terms>} Response Object.
43+
* @example
44+
* import * as contentstack from '@contentstack/management'
45+
* const client = contentstack.client()
46+
*
47+
* client.stack({ api_key: 'api_key'}).terms('terms_uid').delete()
48+
* .then((response) => console.log(response.notice))
49+
*
50+
*/
51+
this.delete = deleteEntity(http)
52+
53+
/**
54+
* @description The Fetch terms call is used to fetch an existing terms.
55+
* @memberof Terms
56+
* @func fetch
57+
* @returns {Promise<Terms.Terms>} Promise for Terms instance
58+
* @example
59+
* import * as contentstack from '@contentstack/management'
60+
* const client = contentstack.client()
61+
*
62+
* client.stack({ api_key: 'api_key'}).terms('terms_uid').fetch()
63+
* .then((terms) => console.log(terms))
64+
*
65+
*/
66+
this.fetch = fetch(http, 'term')
67+
this.ancestors = async (params = {}) => {
68+
try {
69+
const headers = {
70+
headers: { ...cloneDeep(this.stackHeaders), ...cloneDeep(params) }
71+
}
72+
const response = await http.get(`${this.urlPath}/ancestors`, headers)
73+
return parseData(response, this.stackHeaders)
74+
} catch (err) {
75+
console.error(err)
76+
throw err
77+
}
78+
}
79+
this.descendants = async (params = {}) => {
80+
try {
81+
const headers = {
82+
headers: { ...cloneDeep(this.stackHeaders), ...cloneDeep(params) }
83+
}
84+
const response = await http.get(`${this.urlPath}/descendants`, headers)
85+
return parseData(response, this.stackHeaders)
86+
} catch (err) {
87+
console.error(err)
88+
throw err
89+
}
90+
}
91+
} else {
92+
/**
93+
* @description The Create terms call is used to create a terms.
94+
* @memberof Terms
95+
* @func create
96+
* @returns {Promise<Terms.Terms>} Promise for Terms instance
97+
* @example
98+
* import * as contentstack from '@contentstack/management'
99+
* const client = contentstack.client()
100+
* const terms = {
101+
* uid: 'terms_testing1',
102+
* name: 'terms testing',
103+
* description: 'Description for terms testing'
104+
* }
105+
* client.stack({ api_key: 'api_key'}).terms().create({terms})
106+
* .then(terms) => console.log(terms)
107+
*
108+
*/
109+
this.create = create({ http })
110+
111+
/**
112+
* @description The Query on Terms will allow to fetch details of all Terms.
113+
* @memberof Terms
114+
* @param {Object} params - URI parameters
115+
* @prop {Object} params.query - Queries that you can use to fetch filtered results.
116+
* @func query
117+
* @returns {Array<Terms>} Array of Terms.
118+
*
119+
* @example
120+
* import * as contentstack from '@contentstack/management'
121+
* const client = contentstack.client()
122+
*
123+
* client.stack().terms().query().find()
124+
* .then((terms) => console.log(terms)
125+
*/
126+
this.query = query({ http: http, wrapperCollection: TermsCollection })
127+
}
128+
}
129+
export function parseData (response, stackHeaders, taxonomy_uid) {
130+
const data = response.data || {}
131+
if (stackHeaders) {
132+
data.stackHeaders = stackHeaders
133+
}
134+
if (taxonomy_uid) {
135+
data.taxonomy_uid = taxonomy_uid
136+
}
137+
return data
138+
}
139+
export function TermsCollection (http, data) {
140+
const obj = cloneDeep(data.terms) || []
141+
const termsCollection = obj.map((term) => {
142+
return new Terms(http, { term: term, taxonomy_uid: data.taxonomy_uid, stackHeaders: data.stackHeaders })
143+
})
144+
return termsCollection
145+
}

0 commit comments

Comments
 (0)