Skip to content
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
- 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
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