Skip to content

Commit

Permalink
Merge pull request #92 from contentstack/next
Browse files Browse the repository at this point in the history
Teams API support
  • Loading branch information
harshithad0703 authored Nov 21, 2023
2 parents b9b8642 + 9d77ae5 commit 6885782
Show file tree
Hide file tree
Showing 35 changed files with 1,180 additions and 22 deletions.
8 changes: 5 additions & 3 deletions .github/workflows/unit-test.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
name: Unit Test & Reports
on:
pull_request:
push:
branches:
- master
- main
- next
jobs:
build-test:
name: Build & Test
Expand All @@ -23,6 +26,5 @@ jobs:
reporter: mocha-json # Format of test results
- name: Coverage report
uses: lucassabreu/comment-coverage-clover@main
with:
name: Unit test Coverage report
with:
file: coverage/clover.xml
6 changes: 4 additions & 2 deletions .jsdoc.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@
"lib/stack/roles/index.js",
"lib/stack/webhook/index.js",
"lib/stack/workflow/index.js",
"lib/stack/workflow/publishRules/index.js"

"lib/stack/workflow/publishRules/index.js",
"lib/organization/teams/index.js",
"lib/organization/teams/stackRoleMappings/index.js",
"lib/organization/teams/teamUsers/index.js"
],
"excludePattern": "(node_modules/|jsdocs)"
},
Expand Down
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: ef5d374553f431b5a952069f46184ec7e49efd7d72143e1a1642994758db4359
checksum: 9a7bec9513834a0fc7db31b9a312ec35980600415c04f0d404e1370cfce1ef1b
version: ""
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## [v1.13.0](https://github.com/contentstack/contentstack-management-javascript/tree/v1.13.0) (2023-11-21)
- Feature
- Teams API support
- Early Access Header support
## [v1.12.0](https://github.com/contentstack/contentstack-management-javascript/tree/v1.12.0) (2023-10-17)
- Feature
- Types support for Taxonomy feature
Expand Down
8 changes: 8 additions & 0 deletions lib/contentstack.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ import httpClient from './core/contentstackHTTPClient.js'
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client({ authtoken: 'value' })
*
* @prop {string=} params.early_access - Optional early_access is a token used for early access of new features in CMA requests.
* @example //Set the `early_access`
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client({ early_access: ['ea1', 'ea2'] })
*
* @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.
* @example //Set the `authorization`
* import * as contentstack from '@contentstack/management'
Expand Down Expand Up @@ -177,6 +182,9 @@ export function client (params = {}) {
if (params.authorization) {
requiredHeaders.authorization = params.authorization
}
if (params.early_access) {
requiredHeaders.early_access = params.early_access.join(',')
}
params = {
...defaultParameter,
...clonedeep(params)
Expand Down
4 changes: 4 additions & 0 deletions lib/core/contentstackHTTPClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ export default function contentstackHttpClient (options) {
config.headers['accessToken'] = config.accessToken
}

if (config.early_access) {
config.headers['x-header-ea'] = config.early_access
}

const protocol = config.insecure ? 'http' : 'https'
let hostname = config.defaultHostName
let port = config.port || 443
Expand Down
11 changes: 9 additions & 2 deletions lib/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,14 @@ export const create = ({ http, params }) => {
if (response.data) {
return new this.constructor(http, parseData(response, this.stackHeaders, this.content_type_uid, this.taxonomy_uid))
} else {
throw error(response)
if (response.status >= 200 && response.status < 300) {
return {
status: response.status,
statusText: response.statusText
}
} else {
throw error(response)
}
}
} catch (err) {
throw error(err)
Expand Down Expand Up @@ -301,4 +308,4 @@ export const move = (http, type, force = false, params = {}) => {
throw error(err)
}
}
}
}
21 changes: 21 additions & 0 deletions lib/organization/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { StackCollection } from '../stack'
import { UserCollection } from '../user'
import { App } from '../app'
import { AppRequest } from '../app/request'
import { Teams } from './teams'
/**
* 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>.
* @namespace Organization
Expand All @@ -18,6 +19,26 @@ export function Organization (http, data) {
Object.assign(this, cloneDeep(data.organization))
this.urlPath = `/organizations/${this.uid}`

/**
* @description The teams call fetches teams details.
* @memberof Organization
* @func teams
* @returns {Promise<Organization.Organization>} Promise for Organization instance
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.organization('organization_uid').teams('teamsUid').fetch()
* .then((organization) => console.log(organization))
*
*/
this.teams = (teamUid = null) => {
data.organizationUid = this.uid
if (teamUid) {
data.uid = teamUid
}
return new Teams(http, data)
}
/**
* @description The fetch Organization call fetches Organization details.
* @memberof Organization
Expand Down
165 changes: 165 additions & 0 deletions lib/organization/teams/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
import cloneDeep from 'lodash/cloneDeep'
import {
create,
fetch,
deleteEntity,
fetchAll
} from '../../entity'
import { TeamUsers } from './teamUsers'
import { StackRoleMappings } from './stackRoleMappings'
import error from '../../core/contentstackError'

export function Teams (http, data) {
this.organizationUid = data.organizationUid
this.urlPath = `/organizations/${this.organizationUid}/teams`
if (data && data.uid) {
Object.assign(this, cloneDeep(data))

this.urlPath = `/organizations/${this.organizationUid}/teams/${this.uid}`

/**
* @description The update call on team will allow to update details of team.
* @memberof Teams
* @func update
* @returns {Promise<Teams.Teams>} Response Object.
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
* const updateData = {
* name: 'updatedname',
* users: [
* {
* email: '[email protected]'
* }
* ],
* organizationRole: 'blt09e5dfced326aaea',
* stackRoleMapping: []
* }
* client.organization(s'organizationUid').teams('teamUid').update(updateData)
* .then((response) => console.log(response))
*
*/
this.update = async (updateData) => {
try {
const response = await http.put(this.urlPath, updateData)
if (response.data) {
return response.data
}
} catch (err) {
throw error(err)
}
}

/**
* @description The delete call on team will delete the existing team.
* @memberof Teams
* @func delete
* @returns {Promise<Teams.Teams>} Response Object.
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
* client.organization('organizationUid').teams('teamUid').delete()
* .then((response) => console.log(response))
*
*/
this.delete = deleteEntity(http)

/**
* @description The fetch call on team will delete the existing team.
* @memberof Teams
* @func fetch
* @returns {Promise<Teams.Teams>} Response Object.
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
* client.organization('organizationUid').teams('teamUid').fetch()
* .then((response) => console.log(response))
*
*/
this.fetch = fetch(http, 'team')

/**
* @description The users call on team will get users details.
* @memberof Teams
* @func users
* @returns {Promise<Teams.Teams>} Response Object.
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
* client.organization('organizationUid').teams('teamUid').teamUsers().fetchAll()
* .then((response) => console.log(response))
*
*/
this.teamUsers = (userId = null) => {
data.organizationUid = this.organizationUid
data.teamUid = this.uid
if (userId) {
data.userId = userId
}
return new TeamUsers(http, data)
}

/**
* @description The stackRoleMappings call on team will get the stack role Mapping.
* @memberof Teams
* @func users
* @returns {Promise<Teams.Teams>} Response Object.
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
* client.organization('organizationUid').teams('teamUid').stackRoleMappings().fetchAll()
* .then((response) => console.log(response))
*
*/
this.stackRoleMappings = (stackApiKey = null) => {
data.organizationUid = this.organizationUid
data.teamUid = this.uid
if (stackApiKey) {
data.stackApiKey = stackApiKey
}
return new StackRoleMappings(http, data)
}
} else {
/**
* @description The fetch call on team will delete the existing team.
* @memberof Teams
* @func create
* @returns {Promise<Teams.Teams>} Response Object.
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
* const team = {
* name: 'name',
* organizationUid: 'organization_uid',
* users: [],
* stackRoleMapping: [],
* organizationRole: 'organizationRole'
* }
* client.organization('organizationUid').teams().create(team)
* .then((response) => console.log(response))
*
*/
this.create = create({ http })

/**
* @description The fetchAll on team will allow to fetch details of all teams.
* @memberof Teams
* @func fetchAll
* @returns {Promise<Teams.Teams>} Response Object.
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.organization('organizationUid').teams().fetchAll()
* .then((response) => console.log(response))
*/
this.fetchAll = fetchAll(http, TeamsCollection, { api_version: 1.1 })
}
}
export function TeamsCollection (http, teamsData) {
const obj = cloneDeep(teamsData.teams) || []
const teamsCollection = obj.map((team) => {
return new Teams(http, team)
})
return teamsCollection
}
Loading

0 comments on commit 6885782

Please sign in to comment.