@@ -5,9 +5,14 @@ import {
5
5
fetch ,
6
6
query ,
7
7
update ,
8
- deleteEntity
8
+ deleteEntity ,
9
+ upload ,
10
+ parseData
9
11
} from '../../entity'
10
12
import { Terms , TermsCollection } from './terms'
13
+ import FormData from 'form-data'
14
+ import { createReadStream } from 'fs'
15
+ import error from '../../core/contentstackError'
11
16
12
17
export function Taxonomy ( http , data = { } ) {
13
18
this . stackHeaders = data . stackHeaders
@@ -69,6 +74,36 @@ export function Taxonomy (http, data = {}) {
69
74
*/
70
75
this . fetch = fetch ( http , 'taxonomy' )
71
76
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
+
72
107
this . terms = ( uid = '' ) => {
73
108
const data = { stackHeaders : this . stackHeaders }
74
109
data . taxonomy_uid = this . uid
@@ -113,6 +148,42 @@ export function Taxonomy (http, data = {}) {
113
148
* .then((taxonomies) => console.log(taxonomies)
114
149
*/
115
150
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
+
116
187
}
117
188
}
118
189
export function TaxonomyCollection ( http , data ) {
@@ -122,3 +193,12 @@ export function TaxonomyCollection (http, data) {
122
193
} )
123
194
return taxonomyCollection
124
195
}
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
+ }
0 commit comments