Skip to content

Commit

Permalink
Merge pull request #85 from contentstack/next
Browse files Browse the repository at this point in the history
Fixes and Enhancements: Contenttype in Asset upload, Taxonomy and Terms
  • Loading branch information
nadeem-cs authored Oct 4, 2023
2 parents 9c4695f + c6daba3 commit 8539f09
Show file tree
Hide file tree
Showing 20 changed files with 1,132 additions and 69 deletions.
2 changes: 1 addition & 1 deletion .talismanrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
threshold: medium
fileignoreconfig:
- filename: package-lock.json
checksum: 71a070335979f3eed857a887e4bd36d9a1e14b08370c55117c55b45b77889b68
checksum: ef5d374553f431b5a952069f46184ec7e49efd7d72143e1a1642994758db4359
version: ""
23 changes: 21 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,28 @@
# Changelog

## [v1.10.0](https://github.com/contentstack/contentstack-management-javascript/tree/v1.8.0) (2023-07-21)
## [v1.11.0](https://github.com/contentstack/contentstack-management-javascript/tree/v1.11.0) (2023-09-03)
- Fixes and Enhancements
- Allows contenttype in asset upload
- Taxonomy feature addition
- Terms feature addition
## [v1.10.2](https://github.com/contentstack/contentstack-management-javascript/tree/v1.10.2) (2023-08-23)
- Fixes and Enhancements
- RefreshToken error handling
- Handling workflow response of object format
- Support for overwrite flag in Contenttype and Global fields import functionality
## [v1.10.1](https://github.com/contentstack/contentstack-management-javascript/tree/v1.10.1) (2023-08-01)
- Fixes:
- Token leak
- Users type
- ContentstackCollection type
- Environment param type
- Enhancements
- Adds Auditlogs functions
- Contenttype references function
## [v1.10.0](https://github.com/contentstack/contentstack-management-javascript/tree/v1.10.0) (2023-07-21)
- Fixes
- Fixes Breaking Changes
## [v1.9.0](https://github.com/contentstack/contentstack-management-javascript/tree/v1.8.0) (2023-07-19)
## [v1.9.0](https://github.com/contentstack/contentstack-management-javascript/tree/v1.9.0) (2023-07-19)
- Features:
- NRP support added
- Audit logs APIs support
Expand Down
43 changes: 39 additions & 4 deletions lib/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export const create = ({ http, params }) => {
try {
const response = await http.post(this.urlPath, data, headers)
if (response.data) {
return new this.constructor(http, parseData(response, this.stackHeaders, this.content_type_uid))
return new this.constructor(http, parseData(response, this.stackHeaders, this.content_type_uid, this.taxonomy_uid))
} else {
throw error(response)
}
Expand Down Expand Up @@ -144,7 +144,7 @@ export const update = (http, type, params = {}) => {
}
})
if (response.data) {
return new this.constructor(http, parseData(response, this.stackHeaders, this.content_type_uid))
return new this.constructor(http, parseData(response, this.stackHeaders, this.content_type_uid, this.taxonomy_uid))
} else {
throw error(response)
}
Expand Down Expand Up @@ -204,7 +204,7 @@ export const fetch = (http, type, params = {}) => {
response.data[type]['content_type'] = response.data['content_type']
response.data[type]['schema'] = response.data['schema']
}
return new this.constructor(http, parseData(response, this.stackHeaders, this.content_type_uid))
return new this.constructor(http, parseData(response, this.stackHeaders, this.content_type_uid, this.taxonomy_uid))
} else {
throw error(response)
}
Expand Down Expand Up @@ -235,14 +235,17 @@ export const fetchAll = (http, wrapperCollection, params = {}) => {
}
}

export function parseData (response, stackHeaders, contentTypeUID) {
export function parseData (response, stackHeaders, contentTypeUID, taxonomy_uid) {
const data = response.data || {}
if (stackHeaders) {
data.stackHeaders = stackHeaders
}
if (contentTypeUID) {
data.content_type_uid = contentTypeUID
}
if (taxonomy_uid) {
data.taxonomy_uid = taxonomy_uid
}
return data
}

Expand All @@ -266,4 +269,36 @@ export async function get (http, url, params, data) {
} catch (err) {
throw error(err)
}
}

export const move = (http, type, force = false, params = {}) => {
return async function (param = {}) {
try {
let updateData = {}
const json = cloneDeep(this)
delete json.parent_uid
if (type) {
updateData[type] = json
} else {
updateData = json
}
const headers = {
headers: { ...cloneDeep(this.stackHeaders), ...cloneDeep(params) },
params: {
...cloneDeep(param)
}
} || {}
if (force === true) {
headers.params.force = true
}
const response = await http.put(`${this.urlPath}/move`, updateData, headers)
if (response.data) {
return new this.constructor(http, parseData(response, this.stackHeaders, this.content_type_uid, this.taxonomy_uid))
} else {
throw error(response)
}
} catch (err) {
throw error(err)
}
}
}
6 changes: 5 additions & 1 deletion lib/stack/asset/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,11 @@ export function createFormData (data) {
formData.append('asset[title]', data.title)
}
const uploadStream = createReadStream(data.upload)
formData.append('asset[upload]', uploadStream)
if (typeof data.content_type === 'string') {
formData.append('asset[upload]', uploadStream, { contentType: data.content_type })
} else {
formData.append('asset[upload]', uploadStream)
}
return formData
}
}
25 changes: 24 additions & 1 deletion lib/stack/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ import { Label } from './label'
import { Branch } from './branch'
import { BranchAlias } from './branchAlias'
import { AuditLog } from './auditlog'
// import { format } from 'util'
import { Taxonomy } from './taxonomy'

/**
* A stack is a space that stores the content of a project (a web or mobile property). Within a stack, you can create content structures, content entries, users, etc. related to the project. Read more about <a href='https://www.contentstack.com/docs/guide/stack'>Stacks</a>.
* @namespace Stack
Expand Down Expand Up @@ -684,6 +685,28 @@ export function Stack (http, data) {
}
return new Role(http, data)
}

/**
* @description Taxonomies allow you to group a collection of content within a stack. Using taxonomies you can group content types that need to work together
* @param {String} uid The UID of the Taxonomy you want to get details.
* @returns {Taxonomy} Instance of Taxonomy.
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.stack({ api_key: 'api_key'}).taxonomy().create()
* .then((taxonomy) => console.log(taxonomy))
*
* client.stack({ api_key: 'api_key'}).taxonomy('taxonomy_uid').fetch()
* .then((taxonomy) => console.log(taxonomy))
*/
this.taxonomy = (taxonomyUid = '') => {
const data = { stackHeaders: this.stackHeaders }
if (taxonomyUid) {
data.taxonomy = { uid: taxonomyUid }
}
return new Taxonomy(http, data)
}
} else {
/**
* @description The Create stack call creates a new stack in your Contentstack account.
Expand Down
124 changes: 124 additions & 0 deletions lib/stack/taxonomy/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/* eslint-disable camelcase */
import cloneDeep from 'lodash/cloneDeep'
import {
create,
fetch,
query,
update,
deleteEntity
} from '../../entity'
import { Terms, TermsCollection } from './terms'

export function Taxonomy (http, data = {}) {
this.stackHeaders = data.stackHeaders
this.urlPath = `/taxonomies`

if (data.taxonomy) {
Object.assign(this, cloneDeep(data.taxonomy))
if (data.taxonomy.terms) {
this.terms = new TermsCollection(http, { terms: data.taxonomy.terms, stackHeaders: data.stackHeaders }, this.uid)
}
this.urlPath = `/taxonomies/${this.uid}`

/**
* @description The Update taxonomy call is used to update an existing taxonomy.
* @memberof Taxonomy
* @func update
* @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').fetch()
* .then((taxonomy) => {
* taxonomy.name = 'taxonomy name'
* return taxonomy.update()
* })
* .then((taxonomy) => console.log(taxonomy))
*
*/
this.update = update(http, 'taxonomy')

/**
* @description The Delete taxonomy call is used to delete an existing taxonomy.
* @memberof Taxonomy
* @func delete
* @returns {Promise<Taxonomy.Taxonomy>} Response Object.
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.stack({ api_key: 'api_key'}).taxonomy('taxonomyUid').delete()
* .then((response) => console.log(response.notice))
*
*/
this.delete = deleteEntity(http)

/**
* @description The Fetch taxonomy call is used to fetch an existing taxonomy.
* @memberof Taxonomy
* @func fetch
* @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').fetch()
* .then((taxonomy) => console.log(taxonomy))
*
*/
this.fetch = fetch(http, 'taxonomy')

this.terms = (uid = '') => {
const data = { stackHeaders: this.stackHeaders }
data.taxonomy_uid = this.uid
if (uid) {
data.term = { uid: uid }
}
return new Terms(http, data)
}
} else {
/**
* @description The Create taxonomy call is used to create a taxonomy.
* @memberof Taxonomy
* @func create
* @returns {Promise<Taxonomy.Taxonomy>} Promise for Taxonomy instance
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
* const taxonomy = {
* uid: 'taxonomy_testing1',
* name: 'taxonomy testing',
* description: 'Description for Taxonomy testing'
* }
* client.stack({ api_key: 'api_key'}).taxonomy().create({taxonomy})
* .then(taxonomy) => console.log(taxonomy)
*
*/
this.create = create({ http })

/**
* @description The Query on Taxonomy will allow to fetch details of all Taxonomies.
* @memberof Taxonomy
* @param {Object} params - URI parameters
* @prop {Object} params.query - Queries that you can use to fetch filtered results.
* @func query
* @returns {Array<Taxonomy>} Array of Taxonomy.
*
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.stack().taxonomy().query().find()
* .then((taxonomies) => console.log(taxonomies)
*/
this.query = query({ http: http, wrapperCollection: TaxonomyCollection })
}
}
export function TaxonomyCollection (http, data) {
const obj = cloneDeep(data.taxonomy) || []
const taxonomyCollection = obj.map((userdata) => {
return new Taxonomy(http, { taxonomy: userdata, stackHeaders: data.stackHeaders })
})
return taxonomyCollection
}
Loading

0 comments on commit 8539f09

Please sign in to comment.