Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
* fix: fixed issue while updating entries with assets

* feat: added api test

* fix: updated changelog and package lock file

* Implemented Management token support (contentstack#99)

* Implemented Management token support

* Fixed PR Comments

* test: added sanity tests for user and contenttype

* added a script in package.json

---------

Co-authored-by: sunil-lakshman <[email protected]>
Co-authored-by: harshithad0703 <[email protected]>
Co-authored-by: harshithad0703 <[email protected]>
  • Loading branch information
4 people authored Dec 18, 2023
1 parent 1bddeee commit 5f6878c
Show file tree
Hide file tree
Showing 28 changed files with 1,664 additions and 35 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ jspm_packages/
mochawesome-report/
coverage/
test/utility/dataFiles/
test/sanity-check/utility/dataFiles/
report.json

# TypeScript v1 declaration files
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# 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.14.0](https://github.com/contentstack/contentstack-management-javascript/tree/v1.14.0) (2023-12-19)
- Feature
- Management token feature added
## [v1.13.0](https://github.com/contentstack/contentstack-management-javascript/tree/v1.13.0) (2023-11-21)
- Feature
- Teams API support
Expand Down
24 changes: 23 additions & 1 deletion lib/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@ export const update = (http, type, params = {}) => {
delete json.updated_by
delete json.updated_at
if (type) {
updateData[type] = json
updateData[type] = json;
if (type === "entry") updateData[type] = cleanAssets(updateData[type]);
} else {
updateData = json
}
Expand Down Expand Up @@ -309,3 +310,24 @@ export const move = (http, type, force = false, params = {}) => {
}
}
}

function isAsset (data) {
const element = (Array.isArray(data) && data.length > 0) ? data[0] : data;
return (!!element.file_size || !!element.content_type) && !!element.uid;
}

export function cleanAssets (data) {
if (typeof data === "object" && Object.keys(data).length > 0) {
const keys = Object.keys(data);
for (const key of keys) {
if (typeof data[key] === "object" && Object.keys(data[key]).length > 0) {
if (isAsset(data[key])) {
data[key] = (Array.isArray(data[key])) ? data[key].map(element => element.uid) : data[key].uid;
} else {
cleanAssets(data[key]);
}
}
}
}
return data;
}
23 changes: 23 additions & 0 deletions lib/stack/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { Branch } from './branch'
import { BranchAlias } from './branchAlias'
import { AuditLog } from './auditlog'
import { Taxonomy } from './taxonomy'
import { ManagementToken } from './managementToken'

/**
* 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>.
Expand Down Expand Up @@ -257,6 +258,28 @@ export function Stack (http, data) {
return new DeliveryToken(http, data)
}

/**
* @description Management Tokens are tokens that provide you with read-write access to the content of your stack.
* @param {String} managementTokenUid The UID of the Management Token field you want to get details.
* @returns {ManagementToken} Instance of ManagementToken.
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.stack({ api_key: 'api_key'}).managementToken().create()
* .then((managementToken) => console.log(managementToken))
*
* client.stack({ api_key: 'api_key'}).managementToken('managementToken_uid').fetch()
* .then((managementToken) => console.log(managementToken))
*/
this.managementToken = (managementTokenUid = null) => {
const data = { stackHeaders: this.stackHeaders }
if (managementTokenUid) {
data.token = { uid: managementTokenUid }
}
return new ManagementToken(http, data)
}

/**
* @description Extensions let you create custom fields and custom widgets that lets you customize Contentstack's default UI and behavior.
* @param {String} extensionUid The UID of the Extension you want to get details.
Expand Down
111 changes: 111 additions & 0 deletions lib/stack/managementToken/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import cloneDeep from 'lodash/cloneDeep'
import { create, update, deleteEntity, fetch, query } from '../../entity'

/**
* Management tokens provide read-only access to the associated environments. Read more about <a href='https://www.contentstack.com/docs/developers/create-tokens/about-management-tokens'>ManagementToken</a>.
* @namespace ManagementToken
*/
export function ManagementToken (http, data = {}) {
this.stackHeaders = data.stackHeaders
this.urlPath = `/stacks/management_tokens`
if (data.token) {
Object.assign(this, cloneDeep(data.token))
this.urlPath = `/stacks/management_tokens/${this.uid}`
/**
* @description The Update ManagementToken call lets you update the name and description of an existing ManagementToken.
* @memberof ManagementToken
* @func update
* @returns {Promise<ManagementToken.ManagementToken>} Promise for ManagementToken instance
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.stack({ api_key: 'api_key'}).managementToken('management_token_uid').fetch()
* .then((managementToken) => {
* managementToken.title = 'My New management token'
* managementToken.description = 'management token description'
* return managementToken.update()
* })
* .then((managementToken) => console.log(managementToken))
*
*/
this.update = update(http, 'token')

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

/**
* @description The fetch ManagementToken call fetches ManagementToken details.
* @memberof ManagementToken
* @func fetch
* @returns {Promise<ManagementToken.ManagementToken>} Promise for ManagementToken instance
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.stack({ api_key: 'api_key'}).managementToken('management_token_uid').fetch()
* .then((managementToken) => console.log(managementToken))
*
*/
this.fetch = fetch(http, 'token')
} else {
/**
* @description The Create a ManagementToken call creates a new ManagementToken in a particular stack of your Contentstack account.
* @memberof ManagementToken
* @func create
* @returns {Promise<ManagementToken.ManagementToken>} Promise for ManagementToken instance
*
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
* const token = {
* name: 'Test',
* description: 'This is a demo token.',
* scope: [{
* module: 'environment',
* environments: ['development'],
* acl: {
* read: true
* }
* }]
* }
*
* client.stack().managementToken().create({ token })
* .then((managementToken) => console.log(managementToken))
*/
this.create = create({ http: http })

/**
* @description The ‘Get all managementToken’ request returns comprehensive information about all managementToken created in a stack.
* @memberof ManagementToken
* @func query
* @returns {ContentstackCollection} Instance of ContentstackCollection.
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.stack().managementToken().query({ query: { name: 'token_name' } })).find()
* .then((contentstackCollection) => console.log(contentstackCollection))
*/
this.query = query({ http: http, wrapperCollection: ManagementTokenCollection })
}
}

export function ManagementTokenCollection (http, data) {
const obj = cloneDeep(data.tokens) || []
const managementTokenCollection = obj.map((managementTokenData) => {
return new ManagementToken(http, { token: managementTokenData, stackHeaders: data.stackHeaders })
})
return managementTokenCollection
}
Loading

0 comments on commit 5f6878c

Please sign in to comment.