Skip to content

Commit 9ed7a91

Browse files
Implemented Management token support (#99)
* Implemented Management token support * Fixed PR Comments
1 parent 04f5d26 commit 9ed7a91

File tree

16 files changed

+916
-35
lines changed

16 files changed

+916
-35
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
- Fixes
44
- Fix for issue while updating entries with assets
55

6+
## [v1.14.0](https://github.com/contentstack/contentstack-management-javascript/tree/v1.14.0) (2023-12-19)
7+
- Feature
8+
- Management token feature added
69
## [v1.13.0](https://github.com/contentstack/contentstack-management-javascript/tree/v1.13.0) (2023-11-21)
710
- Feature
811
- Teams API support

lib/stack/index.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { Branch } from './branch'
1919
import { BranchAlias } from './branchAlias'
2020
import { AuditLog } from './auditlog'
2121
import { Taxonomy } from './taxonomy'
22+
import { ManagementToken } from './managementToken'
2223

2324
/**
2425
* 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>.
@@ -257,6 +258,28 @@ export function Stack (http, data) {
257258
return new DeliveryToken(http, data)
258259
}
259260

261+
/**
262+
* @description Management Tokens are tokens that provide you with read-write access to the content of your stack.
263+
* @param {String} managementTokenUid The UID of the Management Token field you want to get details.
264+
* @returns {ManagementToken} Instance of ManagementToken.
265+
* @example
266+
* import * as contentstack from '@contentstack/management'
267+
* const client = contentstack.client()
268+
*
269+
* client.stack({ api_key: 'api_key'}).managementToken().create()
270+
* .then((managementToken) => console.log(managementToken))
271+
*
272+
* client.stack({ api_key: 'api_key'}).managementToken('managementToken_uid').fetch()
273+
* .then((managementToken) => console.log(managementToken))
274+
*/
275+
this.managementToken = (managementTokenUid = null) => {
276+
const data = { stackHeaders: this.stackHeaders }
277+
if (managementTokenUid) {
278+
data.token = { uid: managementTokenUid }
279+
}
280+
return new ManagementToken(http, data)
281+
}
282+
260283
/**
261284
* @description Extensions let you create custom fields and custom widgets that lets you customize Contentstack's default UI and behavior.
262285
* @param {String} extensionUid The UID of the Extension you want to get details.

lib/stack/managementToken/index.js

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
import cloneDeep from 'lodash/cloneDeep'
2+
import { create, update, deleteEntity, fetch, query } from '../../entity'
3+
4+
/**
5+
* 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>.
6+
* @namespace ManagementToken
7+
*/
8+
export function ManagementToken (http, data = {}) {
9+
this.stackHeaders = data.stackHeaders
10+
this.urlPath = `/stacks/management_tokens`
11+
if (data.token) {
12+
Object.assign(this, cloneDeep(data.token))
13+
this.urlPath = `/stacks/management_tokens/${this.uid}`
14+
/**
15+
* @description The Update ManagementToken call lets you update the name and description of an existing ManagementToken.
16+
* @memberof ManagementToken
17+
* @func update
18+
* @returns {Promise<ManagementToken.ManagementToken>} Promise for ManagementToken instance
19+
* @example
20+
* import * as contentstack from '@contentstack/management'
21+
* const client = contentstack.client()
22+
*
23+
* client.stack({ api_key: 'api_key'}).managementToken('management_token_uid').fetch()
24+
* .then((managementToken) => {
25+
* managementToken.title = 'My New management token'
26+
* managementToken.description = 'management token description'
27+
* return managementToken.update()
28+
* })
29+
* .then((managementToken) => console.log(managementToken))
30+
*
31+
*/
32+
this.update = update(http, 'token')
33+
34+
/**
35+
* @description The Delete ManagementToken call is used to delete an existing ManagementToken permanently from your Stack.
36+
* @memberof ManagementToken
37+
* @func delete
38+
* @returns {Object} Response Object.
39+
* @example
40+
* import * as contentstack from '@contentstack/management'
41+
* const client = contentstack.client()
42+
*
43+
* client.stack({ api_key: 'api_key'}).managementToken('management_token_uid').delete()
44+
* .then((response) => console.log(response.notice))
45+
*/
46+
this.delete = deleteEntity(http)
47+
48+
/**
49+
* @description The fetch ManagementToken call fetches ManagementToken details.
50+
* @memberof ManagementToken
51+
* @func fetch
52+
* @returns {Promise<ManagementToken.ManagementToken>} Promise for ManagementToken instance
53+
* @example
54+
* import * as contentstack from '@contentstack/management'
55+
* const client = contentstack.client()
56+
*
57+
* client.stack({ api_key: 'api_key'}).managementToken('management_token_uid').fetch()
58+
* .then((managementToken) => console.log(managementToken))
59+
*
60+
*/
61+
this.fetch = fetch(http, 'token')
62+
} else {
63+
/**
64+
* @description The Create a ManagementToken call creates a new ManagementToken in a particular stack of your Contentstack account.
65+
* @memberof ManagementToken
66+
* @func create
67+
* @returns {Promise<ManagementToken.ManagementToken>} Promise for ManagementToken instance
68+
*
69+
* @example
70+
* import * as contentstack from '@contentstack/management'
71+
* const client = contentstack.client()
72+
* const token = {
73+
* name: 'Test',
74+
* description: 'This is a demo token.',
75+
* scope: [{
76+
* module: 'environment',
77+
* environments: ['development'],
78+
* acl: {
79+
* read: true
80+
* }
81+
* }]
82+
* }
83+
*
84+
* client.stack().managementToken().create({ token })
85+
* .then((managementToken) => console.log(managementToken))
86+
*/
87+
this.create = create({ http: http })
88+
89+
/**
90+
* @description The ‘Get all managementToken’ request returns comprehensive information about all managementToken created in a stack.
91+
* @memberof ManagementToken
92+
* @func query
93+
* @returns {ContentstackCollection} Instance of ContentstackCollection.
94+
* @example
95+
* import * as contentstack from '@contentstack/management'
96+
* const client = contentstack.client()
97+
*
98+
* client.stack().managementToken().query({ query: { name: 'token_name' } })).find()
99+
* .then((contentstackCollection) => console.log(contentstackCollection))
100+
*/
101+
this.query = query({ http: http, wrapperCollection: ManagementTokenCollection })
102+
}
103+
}
104+
105+
export function ManagementTokenCollection (http, data) {
106+
const obj = cloneDeep(data.tokens) || []
107+
const managementTokenCollection = obj.map((managementTokenData) => {
108+
return new ManagementToken(http, { token: managementTokenData, stackHeaders: data.stackHeaders })
109+
})
110+
return managementTokenCollection
111+
}

0 commit comments

Comments
 (0)