Skip to content

Commit 0412889

Browse files
Added taxonomy import/export support (#112)
Co-authored-by: Nadeem <110535104+nadeem-cs@users.noreply.github.com>
1 parent 92c8467 commit 0412889

File tree

4 files changed

+89
-7
lines changed

4 files changed

+89
-7
lines changed

CHANGELOG.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
# Changelog
2-
## [v1.13.1](https://github.com/contentstack/contentstack-management-javascript/tree/v1.13.1) (2023-12-13)
3-
- Fixes
4-
- Fix for issue while updating entries with assets
5-
2+
## [v1.15.0](https://github.com/contentstack/contentstack-management-javascript/tree/v1.15.0) (2024-01-23)
3+
- Feature
4+
- Taxonomy Import/Export feature added
65
## [v1.14.0](https://github.com/contentstack/contentstack-management-javascript/tree/v1.14.0) (2023-12-19)
76
- Feature
87
- Management token feature added
8+
## [v1.13.1](https://github.com/contentstack/contentstack-management-javascript/tree/v1.13.1) (2023-12-13)
9+
- Fixes
10+
- Fix for issue while updating entries with assets
911
## [v1.13.0](https://github.com/contentstack/contentstack-management-javascript/tree/v1.13.0) (2023-11-21)
1012
- Feature
1113
- Teams API support

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ contentstackClient.stack({ api_key: 'API_KEY' }).asset().create({ asset })
104104
- [Content Management API Docs](https://www.contentstack.com/docs/developers/apis/content-management-api)
105105

106106
### The MIT License (MIT)
107-
Copyright © 2012-2023 [Contentstack](https://www.contentstack.com/). All Rights Reserved
107+
Copyright © 2012-2024 [Contentstack](https://www.contentstack.com/). All Rights Reserved
108108

109109
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
110110

lib/stack/taxonomy/index.js

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,14 @@ import {
55
fetch,
66
query,
77
update,
8-
deleteEntity
8+
deleteEntity,
9+
upload,
10+
parseData
911
} from '../../entity'
1012
import { Terms, TermsCollection } from './terms'
13+
import FormData from 'form-data'
14+
import { createReadStream } from 'fs'
15+
import error from '../../core/contentstackError'
1116

1217
export function Taxonomy (http, data = {}) {
1318
this.stackHeaders = data.stackHeaders
@@ -69,6 +74,36 @@ export function Taxonomy (http, data = {}) {
6974
*/
7075
this.fetch = fetch(http, 'taxonomy')
7176

77+
/**
78+
* @description The Export taxonomy call is used to export an existing taxonomy.
79+
* @memberof Taxonomy
80+
* @func export
81+
* @returns {Promise<Taxonomy.Taxonomy>} Promise for Taxonomy instance
82+
* @example
83+
* import * as contentstack from '@contentstack/management'
84+
* const client = contentstack.client()
85+
*
86+
* client.stack({ api_key: 'api_key'}).taxonomy('taxonomyUid').export()
87+
* .then((taxonomy) => console.log(taxonomy))
88+
*
89+
*/
90+
this.export = async () => {
91+
try {
92+
const headers = {
93+
headers: { ...cloneDeep(this.stackHeaders) }
94+
}
95+
const response = await http.get(`${this.urlPath}/export`, headers)
96+
if (response.data) {
97+
return response.data
98+
} else {
99+
throw error(response)
100+
}
101+
} catch (err) {
102+
throw error(err)
103+
}
104+
}
105+
106+
72107
this.terms = (uid = '') => {
73108
const data = { stackHeaders: this.stackHeaders }
74109
data.taxonomy_uid = this.uid
@@ -113,6 +148,42 @@ export function Taxonomy (http, data = {}) {
113148
* .then((taxonomies) => console.log(taxonomies)
114149
*/
115150
this.query = query({ http: http, wrapperCollection: TaxonomyCollection })
151+
152+
/**
153+
* @description The 'Import taxonomy' import a single entry by uploading JSON or CSV files.
154+
* @memberof Taxonomy
155+
* @func import
156+
* @param {String} data.taxonomy path to file
157+
* @example
158+
* import * as contentstack from '@contentstack/management'
159+
* const client = contentstack.client()
160+
*
161+
* const data = {
162+
* taxonomy: 'path/to/file.json',
163+
* }
164+
* // Import a Taxonomy
165+
* client.stack({ api_key: 'api_key'}).taxonomy().import(data)
166+
* .then((taxonomy) => console.log(taxonomy))
167+
*/
168+
this.import = async function (data, params = {}) {
169+
try {
170+
const response = await upload({
171+
http: http,
172+
urlPath: `${this.urlPath}/import`,
173+
stackHeaders: this.stackHeaders,
174+
formData: createFormData(data),
175+
params: params
176+
})
177+
if (response.data) {
178+
return new this.constructor(http, parseData(response, this.stackHeaders))
179+
} else {
180+
throw error(response)
181+
}
182+
} catch (err) {
183+
throw error(err)
184+
}
185+
}
186+
116187
}
117188
}
118189
export function TaxonomyCollection (http, data) {
@@ -122,3 +193,12 @@ export function TaxonomyCollection (http, data) {
122193
})
123194
return taxonomyCollection
124195
}
196+
197+
export function createFormData (data) {
198+
return () => {
199+
const formData = new FormData()
200+
const uploadStream = createReadStream(data.taxonomy)
201+
formData.append('taxonomy', uploadStream)
202+
return formData
203+
}
204+
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@contentstack/management",
3-
"version": "1.14.1",
3+
"version": "1.15.0",
44
"description": "The Content Management API is used to manage the content of your Contentstack account",
55
"main": "./dist/node/contentstack-management.js",
66
"browser": "./dist/web/contentstack-management.js",

0 commit comments

Comments
 (0)