-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
107 lines (103 loc) · 3.34 KB
/
index.js
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
import cloneDeep from 'lodash/cloneDeep'
import error from '../../core/contentstackError'
export function Authorization (http, data, params) {
http.defaults.versioningStrategy = undefined
this.params = params || {}
if (data) {
if (data.organization_uid) {
this.params = {
organization_uid: data.organization_uid
}
}
if (data.app_uid) {
this.urlPath = `/manifests/${data.app_uid}/authorizations`
/**
* @description List all user authorizations made to an authorized app under a particular organization
* @memberof Authorization
* @func findAll
* @returns {Promise<Response>}
*
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client({ authtoken: 'TOKEN'})
*
* client.organization('organization_uid').app('manifest_uid').authorization().findAll()
* .then((response) => console.log(response))
*/
this.findAll = async (param = {}) => {
try {
const headers = {
headers: { ...cloneDeep(this.params) },
params: {
...cloneDeep(param)
}
}
const response = await http.get(this.urlPath, headers)
if (response.data) {
return response.data
} else {
throw error(response)
}
} catch (err) {
throw error(err)
}
}
/**
* @description Revoke all users tokens issued to an authorized app for the particular organization
* @memberof Authorization
* @func revokeAll
* @returns {Promise<Response>}
*
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client({ authtoken: 'TOKEN'})
*
* client.organization('organization_uid').app('manifest_uid').authorization().revokeAll()
* .then((response) => console.log(response))
*/
this.revokeAll = async () => {
try {
const headers = {
headers: { ...cloneDeep(this.params) }
}
const response = await http.delete(this.urlPath, headers)
if (response.data) {
return response.data
} else {
throw error(response)
}
} catch (err) {
throw error(err)
}
}
/**
* @description Revoke user token issued to an authorized app for the particular organization
* @memberof Authorization
* @func revoke
* @returns {Promise<Response>}
*
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client({ authtoken: 'TOKEN'})
*
* client.organization('organization_uid').app('manifest_uid').authorization().revoke('authorization_uid')
* .then((response) => console.log(response))
*/
this.revoke = async (authorizationUid) => {
try {
const headers = {
headers: { ...cloneDeep(this.params) }
}
const response = await http.delete(`${this.urlPath}/${authorizationUid}`, headers)
if (response.data) {
return response.data
} else {
throw error(response)
}
} catch (err) {
throw error(err)
}
}
}
}
}