@@ -5,9 +5,14 @@ import {
55 fetch ,
66 query ,
77 update ,
8- deleteEntity
8+ deleteEntity ,
9+ upload ,
10+ parseData
911} from '../../entity'
1012import { Terms , TermsCollection } from './terms'
13+ import FormData from 'form-data'
14+ import { createReadStream } from 'fs'
15+ import error from '../../core/contentstackError'
1116
1217export 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}
118189export 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+ }
0 commit comments