Skip to content

Commit 6885782

Browse files
Merge pull request #92 from contentstack/next
Teams API support
2 parents b9b8642 + 9d77ae5 commit 6885782

35 files changed

+1180
-22
lines changed

.github/workflows/unit-test.yml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
name: Unit Test & Reports
22
on:
33
pull_request:
4-
push:
4+
branches:
5+
- master
6+
- main
7+
- next
58
jobs:
69
build-test:
710
name: Build & Test
@@ -23,6 +26,5 @@ jobs:
2326
reporter: mocha-json # Format of test results
2427
- name: Coverage report
2528
uses: lucassabreu/comment-coverage-clover@main
26-
with:
27-
name: Unit test Coverage report
29+
with:
2830
file: coverage/clover.xml

.jsdoc.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,10 @@
3232
"lib/stack/roles/index.js",
3333
"lib/stack/webhook/index.js",
3434
"lib/stack/workflow/index.js",
35-
"lib/stack/workflow/publishRules/index.js"
36-
35+
"lib/stack/workflow/publishRules/index.js",
36+
"lib/organization/teams/index.js",
37+
"lib/organization/teams/stackRoleMappings/index.js",
38+
"lib/organization/teams/teamUsers/index.js"
3739
],
3840
"excludePattern": "(node_modules/|jsdocs)"
3941
},

.talismanrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
threshold: medium
22
fileignoreconfig:
33
- filename: package-lock.json
4-
checksum: ef5d374553f431b5a952069f46184ec7e49efd7d72143e1a1642994758db4359
4+
checksum: 9a7bec9513834a0fc7db31b9a312ec35980600415c04f0d404e1370cfce1ef1b
55
version: ""

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## [v1.13.0](https://github.com/contentstack/contentstack-management-javascript/tree/v1.13.0) (2023-11-21)
4+
- Feature
5+
- Teams API support
6+
- Early Access Header support
37
## [v1.12.0](https://github.com/contentstack/contentstack-management-javascript/tree/v1.12.0) (2023-10-17)
48
- Feature
59
- Types support for Taxonomy feature

lib/contentstack.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ import httpClient from './core/contentstackHTTPClient.js'
3939
* import * as contentstack from '@contentstack/management'
4040
* const client = contentstack.client({ authtoken: 'value' })
4141
*
42+
* @prop {string=} params.early_access - Optional early_access is a token used for early access of new features in CMA requests.
43+
* @example //Set the `early_access`
44+
* import * as contentstack from '@contentstack/management'
45+
* const client = contentstack.client({ early_access: ['ea1', 'ea2'] })
46+
*
4247
* @prop {string=} params.authorization - Optional authorization token is a read-write token used to make authorized CMA requests, but it is a user-specific token.
4348
* @example //Set the `authorization`
4449
* import * as contentstack from '@contentstack/management'
@@ -177,6 +182,9 @@ export function client (params = {}) {
177182
if (params.authorization) {
178183
requiredHeaders.authorization = params.authorization
179184
}
185+
if (params.early_access) {
186+
requiredHeaders.early_access = params.early_access.join(',')
187+
}
180188
params = {
181189
...defaultParameter,
182190
...clonedeep(params)

lib/core/contentstackHTTPClient.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ export default function contentstackHttpClient (options) {
4444
config.headers['accessToken'] = config.accessToken
4545
}
4646

47+
if (config.early_access) {
48+
config.headers['x-header-ea'] = config.early_access
49+
}
50+
4751
const protocol = config.insecure ? 'http' : 'https'
4852
let hostname = config.defaultHostName
4953
let port = config.port || 443

lib/entity.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,14 @@ export const create = ({ http, params }) => {
8686
if (response.data) {
8787
return new this.constructor(http, parseData(response, this.stackHeaders, this.content_type_uid, this.taxonomy_uid))
8888
} else {
89-
throw error(response)
89+
if (response.status >= 200 && response.status < 300) {
90+
return {
91+
status: response.status,
92+
statusText: response.statusText
93+
}
94+
} else {
95+
throw error(response)
96+
}
9097
}
9198
} catch (err) {
9299
throw error(err)
@@ -301,4 +308,4 @@ export const move = (http, type, force = false, params = {}) => {
301308
throw error(err)
302309
}
303310
}
304-
}
311+
}

lib/organization/index.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { StackCollection } from '../stack'
77
import { UserCollection } from '../user'
88
import { App } from '../app'
99
import { AppRequest } from '../app/request'
10+
import { Teams } from './teams'
1011
/**
1112
* Organization is the top-level entity in the hierarchy of Contentstack, consisting of stacks and stack resources, and users. Organization allows easy management of projects as well as users within the Organization. Read more about <a href='https://www.contentstack.com/docs/guide/organization'>Organizations.</a>.
1213
* @namespace Organization
@@ -18,6 +19,26 @@ export function Organization (http, data) {
1819
Object.assign(this, cloneDeep(data.organization))
1920
this.urlPath = `/organizations/${this.uid}`
2021

22+
/**
23+
* @description The teams call fetches teams details.
24+
* @memberof Organization
25+
* @func teams
26+
* @returns {Promise<Organization.Organization>} Promise for Organization instance
27+
* @example
28+
* import * as contentstack from '@contentstack/management'
29+
* const client = contentstack.client()
30+
*
31+
* client.organization('organization_uid').teams('teamsUid').fetch()
32+
* .then((organization) => console.log(organization))
33+
*
34+
*/
35+
this.teams = (teamUid = null) => {
36+
data.organizationUid = this.uid
37+
if (teamUid) {
38+
data.uid = teamUid
39+
}
40+
return new Teams(http, data)
41+
}
2142
/**
2243
* @description The fetch Organization call fetches Organization details.
2344
* @memberof Organization

lib/organization/teams/index.js

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
import cloneDeep from 'lodash/cloneDeep'
2+
import {
3+
create,
4+
fetch,
5+
deleteEntity,
6+
fetchAll
7+
} from '../../entity'
8+
import { TeamUsers } from './teamUsers'
9+
import { StackRoleMappings } from './stackRoleMappings'
10+
import error from '../../core/contentstackError'
11+
12+
export function Teams (http, data) {
13+
this.organizationUid = data.organizationUid
14+
this.urlPath = `/organizations/${this.organizationUid}/teams`
15+
if (data && data.uid) {
16+
Object.assign(this, cloneDeep(data))
17+
18+
this.urlPath = `/organizations/${this.organizationUid}/teams/${this.uid}`
19+
20+
/**
21+
* @description The update call on team will allow to update details of team.
22+
* @memberof Teams
23+
* @func update
24+
* @returns {Promise<Teams.Teams>} Response Object.
25+
* @example
26+
* import * as contentstack from '@contentstack/management'
27+
* const client = contentstack.client()
28+
* const updateData = {
29+
* name: 'updatedname',
30+
* users: [
31+
* {
32+
* email: 'abc@abc.com'
33+
* }
34+
* ],
35+
* organizationRole: 'blt09e5dfced326aaea',
36+
* stackRoleMapping: []
37+
* }
38+
* client.organization(s'organizationUid').teams('teamUid').update(updateData)
39+
* .then((response) => console.log(response))
40+
*
41+
*/
42+
this.update = async (updateData) => {
43+
try {
44+
const response = await http.put(this.urlPath, updateData)
45+
if (response.data) {
46+
return response.data
47+
}
48+
} catch (err) {
49+
throw error(err)
50+
}
51+
}
52+
53+
/**
54+
* @description The delete call on team will delete the existing team.
55+
* @memberof Teams
56+
* @func delete
57+
* @returns {Promise<Teams.Teams>} Response Object.
58+
* @example
59+
* import * as contentstack from '@contentstack/management'
60+
* const client = contentstack.client()
61+
* client.organization('organizationUid').teams('teamUid').delete()
62+
* .then((response) => console.log(response))
63+
*
64+
*/
65+
this.delete = deleteEntity(http)
66+
67+
/**
68+
* @description The fetch call on team will delete the existing team.
69+
* @memberof Teams
70+
* @func fetch
71+
* @returns {Promise<Teams.Teams>} Response Object.
72+
* @example
73+
* import * as contentstack from '@contentstack/management'
74+
* const client = contentstack.client()
75+
* client.organization('organizationUid').teams('teamUid').fetch()
76+
* .then((response) => console.log(response))
77+
*
78+
*/
79+
this.fetch = fetch(http, 'team')
80+
81+
/**
82+
* @description The users call on team will get users details.
83+
* @memberof Teams
84+
* @func users
85+
* @returns {Promise<Teams.Teams>} Response Object.
86+
* @example
87+
* import * as contentstack from '@contentstack/management'
88+
* const client = contentstack.client()
89+
* client.organization('organizationUid').teams('teamUid').teamUsers().fetchAll()
90+
* .then((response) => console.log(response))
91+
*
92+
*/
93+
this.teamUsers = (userId = null) => {
94+
data.organizationUid = this.organizationUid
95+
data.teamUid = this.uid
96+
if (userId) {
97+
data.userId = userId
98+
}
99+
return new TeamUsers(http, data)
100+
}
101+
102+
/**
103+
* @description The stackRoleMappings call on team will get the stack role Mapping.
104+
* @memberof Teams
105+
* @func users
106+
* @returns {Promise<Teams.Teams>} Response Object.
107+
* @example
108+
* import * as contentstack from '@contentstack/management'
109+
* const client = contentstack.client()
110+
* client.organization('organizationUid').teams('teamUid').stackRoleMappings().fetchAll()
111+
* .then((response) => console.log(response))
112+
*
113+
*/
114+
this.stackRoleMappings = (stackApiKey = null) => {
115+
data.organizationUid = this.organizationUid
116+
data.teamUid = this.uid
117+
if (stackApiKey) {
118+
data.stackApiKey = stackApiKey
119+
}
120+
return new StackRoleMappings(http, data)
121+
}
122+
} else {
123+
/**
124+
* @description The fetch call on team will delete the existing team.
125+
* @memberof Teams
126+
* @func create
127+
* @returns {Promise<Teams.Teams>} Response Object.
128+
* @example
129+
* import * as contentstack from '@contentstack/management'
130+
* const client = contentstack.client()
131+
* const team = {
132+
* name: 'name',
133+
* organizationUid: 'organization_uid',
134+
* users: [],
135+
* stackRoleMapping: [],
136+
* organizationRole: 'organizationRole'
137+
* }
138+
* client.organization('organizationUid').teams().create(team)
139+
* .then((response) => console.log(response))
140+
*
141+
*/
142+
this.create = create({ http })
143+
144+
/**
145+
* @description The fetchAll on team will allow to fetch details of all teams.
146+
* @memberof Teams
147+
* @func fetchAll
148+
* @returns {Promise<Teams.Teams>} Response Object.
149+
* @example
150+
* import * as contentstack from '@contentstack/management'
151+
* const client = contentstack.client()
152+
*
153+
* client.organization('organizationUid').teams().fetchAll()
154+
* .then((response) => console.log(response))
155+
*/
156+
this.fetchAll = fetchAll(http, TeamsCollection, { api_version: 1.1 })
157+
}
158+
}
159+
export function TeamsCollection (http, teamsData) {
160+
const obj = cloneDeep(teamsData.teams) || []
161+
const teamsCollection = obj.map((team) => {
162+
return new Teams(http, team)
163+
})
164+
return teamsCollection
165+
}

0 commit comments

Comments
 (0)