/
team-membership.ts
122 lines (112 loc) · 3.39 KB
/
team-membership.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import copy from 'fast-copy'
import { freezeSys, toPlainObject } from 'contentful-sdk-core'
import enhanceWithMethods from '../enhance-with-methods'
import { wrapCollection } from '../common-utils'
import { DefaultElements, MetaSysProps, MetaLinkProps, MakeRequest } from '../common-types'
export type TeamMembershipProps = {
/**
* System metadata
*/
sys: MetaSysProps & {
team: { sys: MetaLinkProps }
organization: { sys: MetaLinkProps }
organizationMembership: { sys: MetaLinkProps }
}
/**
* Is admin
*/
admin: boolean
/**
* Organization membership id
*/
organizationMembershipId: string
}
export type CreateTeamMembershipProps = Omit<TeamMembershipProps, 'sys'>
export interface TeamMembership extends TeamMembershipProps, DefaultElements<TeamMembershipProps> {
/**
* Deletes this object on the server.
* @return Promise for the deletion. It contains no data, but the Promise error case should be handled.
* @example ```javascript
* const contentful = require('contentful-management')
*
* const client = contentful.createClient({
* accessToken: '<content_management_api_key>'
* })
*
* client.getOrganization('organizationId')
* .then(org => org.getTeamMembership('teamId', 'teamMembershipId'))
* .then((teamMembership) => {
* teamMembership.delete();
* })
* .catch(console.error)
* ```
*/
delete(): Promise<void>
/**
* Sends an update to the server with any changes made to the object's properties
* @return Object returned from the server with updated changes.
* @example ```javascript
* const contentful = require('contentful-management')
*
* const client = contentful.createClient({
* accessToken: '<content_management_api_key>'
* })
*
* client.getOrganization('organizationId')
* .then(org => org.getTeamMembership('teamId', 'teamMembershipId'))
* .then((teamMembership) => {
* teamMembership.admin = true;
* teamMembership.update();
* })
* .catch(console.error)
* ```
*/
update(): Promise<TeamMembership>
}
function createTeamMembershipApi(makeRequest: MakeRequest) {
const getParams = (data: TeamMembershipProps) => ({
teamMembershipId: data.sys.id,
teamId: data.sys.team.sys.id,
organizationId: data.sys.organization.sys.id,
})
return {
update: function () {
const raw = this.toPlainObject() as TeamMembershipProps
return makeRequest({
entityType: 'TeamMembership',
action: 'update',
params: getParams(raw),
payload: raw,
}).then((data) => wrapTeamMembership(makeRequest, data))
},
delete: function del() {
const raw = this.toPlainObject() as TeamMembershipProps
return makeRequest({
entityType: 'TeamMembership',
action: 'delete',
params: getParams(raw),
})
},
}
}
/**
* @private
* @param makeRequest - function to make requests via an adapter
* @param data - Raw team membership data
* @return Wrapped team membership data
*/
export function wrapTeamMembership(
makeRequest: MakeRequest,
data: TeamMembershipProps
): TeamMembership {
const teamMembership = toPlainObject(copy(data))
const teamMembershipWithMethods = enhanceWithMethods(
teamMembership,
createTeamMembershipApi(makeRequest)
)
return freezeSys(teamMembershipWithMethods)
}
/**
* @private
*/
export const wrapTeamMembershipCollection = wrapCollection(wrapTeamMembership)