Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added taxonomy import/export feature support #112

Merged
merged 2 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
# Changelog
## [v1.13.1](https://github.com/contentstack/contentstack-management-javascript/tree/v1.13.1) (2023-12-13)
- Fixes
- Fix for issue while updating entries with assets

## [v1.15.0](https://github.com/contentstack/contentstack-management-javascript/tree/v1.15.0) (2024-01-23)
- Feature
- Taxonomy Import/Export feature added
## [v1.14.0](https://github.com/contentstack/contentstack-management-javascript/tree/v1.14.0) (2023-12-19)
- Feature
- Management token feature added
## [v1.13.1](https://github.com/contentstack/contentstack-management-javascript/tree/v1.13.1) (2023-12-13)
- Fixes
- Fix for issue while updating entries with assets
## [v1.13.0](https://github.com/contentstack/contentstack-management-javascript/tree/v1.13.0) (2023-11-21)
- Feature
- Teams API support
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ contentstackClient.stack({ api_key: 'API_KEY' }).asset().create({ asset })
- [Content Management API Docs](https://www.contentstack.com/docs/developers/apis/content-management-api)

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

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:

Expand Down
82 changes: 81 additions & 1 deletion lib/stack/taxonomy/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@ import {
fetch,
query,
update,
deleteEntity
deleteEntity,
upload,
parseData
} from '../../entity'
import { Terms, TermsCollection } from './terms'
import FormData from 'form-data'
import { createReadStream } from 'fs'
import error from '../../core/contentstackError'

export function Taxonomy (http, data = {}) {
this.stackHeaders = data.stackHeaders
Expand Down Expand Up @@ -69,6 +74,36 @@ export function Taxonomy (http, data = {}) {
*/
this.fetch = fetch(http, 'taxonomy')

/**
* @description The Export taxonomy call is used to export an existing taxonomy.
* @memberof Taxonomy
* @func export
* @returns {Promise<Taxonomy.Taxonomy>} Promise for Taxonomy instance
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.stack({ api_key: 'api_key'}).taxonomy('taxonomyUid').export()
* .then((taxonomy) => console.log(taxonomy))
*
*/
this.export = async () => {
try {
const headers = {
headers: { ...cloneDeep(this.stackHeaders) }
}
const response = await http.get(`${this.urlPath}/export`, headers)
if (response.data) {
return response.data
} else {
throw error(response)
}
} catch (err) {
throw error(err)
}
}


this.terms = (uid = '') => {
const data = { stackHeaders: this.stackHeaders }
data.taxonomy_uid = this.uid
Expand Down Expand Up @@ -113,6 +148,42 @@ export function Taxonomy (http, data = {}) {
* .then((taxonomies) => console.log(taxonomies)
*/
this.query = query({ http: http, wrapperCollection: TaxonomyCollection })

/**
* @description The 'Import taxonomy' import a single entry by uploading JSON or CSV files.
* @memberof Taxonomy
* @func import
* @param {String} data.taxonomy path to file
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* const data = {
* taxonomy: 'path/to/file.json',
* }
* // Import a Taxonomy
* client.stack({ api_key: 'api_key'}).taxonomy().import(data)
* .then((taxonomy) => console.log(taxonomy))
*/
this.import = async function (data, params = {}) {
try {
const response = await upload({
http: http,
urlPath: `${this.urlPath}/import`,
stackHeaders: this.stackHeaders,
formData: createFormData(data),
params: params
})
if (response.data) {
return new this.constructor(http, parseData(response, this.stackHeaders))
} else {
throw error(response)
}
} catch (err) {
throw error(err)
}
}

}
}
export function TaxonomyCollection (http, data) {
Expand All @@ -122,3 +193,12 @@ export function TaxonomyCollection (http, data) {
})
return taxonomyCollection
}

export function createFormData (data) {
return () => {
const formData = new FormData()
const uploadStream = createReadStream(data.taxonomy)
formData.append('taxonomy', uploadStream)
return formData
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@contentstack/management",
"version": "1.14.1",
"version": "1.15.0",
"description": "The Content Management API is used to manage the content of your Contentstack account",
"main": "./dist/node/contentstack-management.js",
"browser": "./dist/web/contentstack-management.js",
Expand Down
Loading