-
Notifications
You must be signed in to change notification settings - Fork 7
/
contentstackClient.js
183 lines (177 loc) · 6.19 KB
/
contentstackClient.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/**
* @namespace ContentstackClient
*/
import { Stack } from './stack/index.js'
import { Organization } from './organization/index'
import cloneDeep from 'lodash/cloneDeep'
import { User } from './user/index'
import error from './core/contentstackError'
export default function contentstackClient ({ http }) {
/**
* @description The login call is used to sign in to your Contentstack account and obtain the authtoken.
* @memberof ContentstackClient
* @func login
* @param {Object} parameters - login parameters
* @prop {string} parameters.email - email id for user to login
* @prop {string} parameters.password - password for user to login
* @prop {string} parameters.token - token for user to login
* @returns {Promise}
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.login({ email: <emailid>, password: <password> })
* .then(() => console.log('Logged in successfully'))
*
*/
function login (requestBody, params = {}) {
http.defaults.versioningStrategy = 'path'
return http.post('/user-session', { user: requestBody }, { params: params })
.then((response) => {
if (response.data.user != null && response.data.user.authtoken != null) {
http.defaults.headers.common.authtoken = response.data.user.authtoken
response.data.user = new User(http, response.data)
}
return response.data
}, error)
}
/**
* @description The Get user call returns comprehensive information of an existing user account.
* The information returned includes details of the stacks owned by and shared with the specified user account.
* @memberof ContentstackClient
* @func getUser
* @returns {Promise}
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.getUser()
* .then((user) => console.log(user))
*
*/
function getUser (params = {}) {
http.defaults.versioningStrategy = 'path'
return http.get('/user', { params: params })
.then((response) => {
return new User(http, response.data)
}, error)
}
/**
* @description Get Stack instance. A stack is a space that stores the content of a project.
* @memberof ContentstackClient
* @func stack
* @param {String} api_key - Stack API Key
* @param {String} management_token - Management token for Stack.
* @param {String} branch_name - Branch name or alias to access specific branch. Default is master.
* @returns {Stack} Instance of Stack
*
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
* const stack = {name: 'My New Stack'}
* client.stack().create({ stack }, { organization_uid: 'org_uid' })
* .then((stack) => console.log(stack))
*
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.stack({ api_key: 'api_key'}).fetch()
* .then((stack) => console.log(stack))
*
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.stack({ api_key: 'api_key', management_token: 'management_token' }).contentType('content_type_uid').fetch()
* .then((stack) => console.log(stack))
*
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.stack({ api_key: 'api_key', management_token: 'management_token', branch_uid: 'branch_uid' }).contentType('content_type_uid').fetch()
* .then((stack) => console.log(stack))
*/
function stack (params = {}) {
http.defaults.versioningStrategy = 'path'
const stack = { ...cloneDeep(params) }
return new Stack(http, { stack })
}
/**
* @description Organization is the top-level entity in the hierarchy of Contentstack, consisting of stacks and stack resources, and users.
* @memberof ContentstackClient
* @func organization
* @param {String} uid - Organization UID.
* @returns {Organization} Instance of Organization.
*
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.organization().findAll()
* .then((organization) => console.log(organization))
*
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.organization('org_uid').fetch()
* .then((organization) => console.log(organization))
*
*/
function organization (uid = null) {
http.defaults.versioningStrategy = 'path'
return new Organization(http, uid !== null ? { organization: { uid: uid } } : null)
}
/**
* @description The Log out of your account call is used to sign out the user of Contentstack account.
* @memberof ContentstackClient
* @param {String} authtoken - Authtoken to logout from.
* @func logout
* @returns {Object} Response object.
*
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
* client.logout()
* .then((response) => console.log(response))
*
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
* client.logout('AUTHTOKEN')
* .then((response) => console.log(response))
* */
function logout (authtoken) {
http.defaults.versioningStrategy = 'path'
if (authtoken !== undefined) {
return http.delete('/user-session', {
headers: {
authtoken: authtoken
}
})
.then((response) => {
return response.data
}, error)
}
return http.delete('/user-session')
.then((response) => {
if (http.defaults.headers.common) {
delete http.defaults.headers.common.authtoken
}
delete http.defaults.headers.authtoken
delete http.httpClientParams.authtoken
delete http.httpClientParams.headers.authtoken
return response.data
}, error)
}
return {
login: login,
logout: logout,
getUser: getUser,
stack: stack,
organization: organization,
axiosInstance: http
}
}