-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.ts
More file actions
executable file
·338 lines (301 loc) · 14.3 KB
/
index.ts
File metadata and controls
executable file
·338 lines (301 loc) · 14.3 KB
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
import Asset from './api/asset/index';
import ContentType from './api/content-type/index';
import { onData, onError } from "../utils/utils";
import { BranchDetail, GetAllStacksOptions, StackAdditionalData, StackDetail, StackSearchQuery } from '../types/stack.types';
import { IManagementTokenDetails } from '../types';
import { GenericObjectType } from "../types/common.types";
/**
* Class representing the current stack in Contentstack UI.
*/
class Stack {
/**
* @hideconstructor
*/
_connection: any
_data: StackDetail
ContentType: any
Asset: any
private _currentBranch: BranchDetail | null = null;
constructor(data: StackDetail = {} as StackDetail, connection: any, additionalData: StackAdditionalData) {
this._connection = connection;
this._data = data;
/**
* @constructor
* @hideconstructor
* @desc Content type defines the structure or schema of a page or a section of your web or mobile property
* @see {@link https://www.contentstack.com/docs/apis/content-management-api/#content-types| ContentType}
* @param {string} uid - Uid of contenttype.
* @example appSDK.stack.ContentType('content_type_uid')
* */
this.ContentType = ContentType(connection);
/**
* @constructor
* @hideconstructor
* @desc An initializer is responsible for creating an Asset object.
* @see {@link https://www.contentstack.com/docs/apis/content-management-api/#assets| Asset}
* @param {string} uid - UID of the asset.
* @example appSDK.stack.Asset('asset_uid')
* */
this.Asset = Asset(connection);
const currentBranch = additionalData.currentBranch || ""
if (currentBranch) {
this._currentBranch =
(data.branches || []).find(
(branch) => branch.uid === additionalData.currentBranch
) || null;
}
}
/**
* This method returns the data of the current stack.
* @return Returns stack data.
*/
getData(): StackDetail {
return this._data;
}
/**
* This method returns all the stacks in the current organization.
* @param query asks for organization UID and query params to get all stacks
* @returns Stacks within current organization
*/
async getAllStacks({orgUid = "", params = {}}: GetAllStacksOptions = {}): Promise<StackDetail[]> {
// validation
if (typeof orgUid !== 'string') {
throw new TypeError('orgUid must be a string');
}
const options = {
action: "getStacks",
headers: { organization_uid: orgUid || this._data.org_uid },
skip_api_key: true,
params
};
return this._connection
.sendToParent("stackQuery", options)
.then(onData)
.then((data) => data.stacks || [])
.catch(onError);
}
/**
* Get the details of all the management tokens of the stack.
* Note: This API does not return the token value.
* @see {@link https://www.contentstack.com/docs/developers/apis/content-management-api/#get-all-management-tokens | Get all management tokens}
* @returns Details of all the management token of the stack
*/
async getManagementTokens(): Promise<IManagementTokenDetails[]> {
const options = { action: "getManagementTokens" };
return this._connection
.sendToParent("stackQuery", options)
.then(async (response) => {
const data = await onData<{tokens: IManagementTokenDetails[]}>(response);
return data.tokens || [];
})
.catch(onError);
}
/**
* Gets the results of the search based on user query
* @param queries Array of key value pair of query parameters
* @param apiKey API key of the stack
* @param config Optional configuration. Only pass this if you need to query a specific branch using `{ branch: 'branch-name' }. If not provided, queries the default branch.`
* @returns Result of the query
*/
search(queries: StackSearchQuery, apiKey: string | null = this._data.api_key, config: { [key: string]: any } = {}) {
const { branch } = config;
const options: any = { params: queries, api_key: apiKey, action: "search" };
if (branch) {
options.headers = { branch };
}
return this._connection
.sendToParent("stackQuery", options)
.then(onData)
.catch(onError);
}
/**
* This API allows you to retrieve data of a content type of a stack using the {@link https://www.contentstack.com/docs/apis/content-management-api/#get-a-single-content-type| Content Type API} requests. This method returns a Promise object.
* @param {string} uid Uid of the desired content type
* @param {Object} params Optional parameters for the GET call
* @return {Object} A promise object which will be resolved with content type details.
*/
getContentType(uid: string, params = {}): Promise<{ [key: string]: any }> {
if (!uid) {
return Promise.reject(new Error('uid is required'));
}
const options = { uid, params, action: 'getContentType' };
return this._connection.sendToParent('stackQuery', options).then(onData).catch(onError);
}
/**
* This API allows you to retrieve data of a content types of a stack using the {@link https://www.contentstack.com/docs/apis/content-management-api/#get-all-content-types| Content Types API} requests. This method returns a Promise object.
* @param {Object} query Query for the GET call
* @param {Object} params Optional parameters for the GET call
* @return {Object} A promise object which will be resolved with details of the content type.
*/
getContentTypes(query = {}, params: { [key: string]: any } = {}): Promise<{ [key: string]: any }> {
const {branch, ...optionParams} = params;
optionParams.query = query;
const options: any = {
params: optionParams,
action: 'getContentTypes',
};
if (branch) {
options.headers = { branch };
}
return this._connection.sendToParent('stackQuery', options).then(onData).catch(onError);
}
/**
* This API allows you to retrieve environment details of a stack using the {@link https://www.contentstack.com/docs/apis/content-management-api/#get-a-single-environment| Environment API} requests. This method returns a Promise object.
* @param {string} name Name of the desired environment
* @param {Object} params Optional parameters for the GET call
* @return {Object} A promise object which will be resolved with environment details.
*/
getEnvironment(name: string, params = {}) {
if (!name) {
return Promise.reject(new Error('name is required'));
}
const options = { name, params, action: 'getEnvironment' };
return this._connection.sendToParent('stackQuery', options).then(onData).catch(onError);
}
/**
* This API allows you to retrieve details of environments of a stack using the {@link https://www.contentstack.com/docs/apis/content-management-api/#get-all-environments| Environments API} requests. This method returns a Promise object.
* @param {Object} query Query for the GET call
* @param {Object} params Optional parameters for the GET call
* @return {Object} A Promise object which will be resolved with details of the environments.
*/
getEnvironments(query = {}, params = {}) {
const optionParams: { [key: string]: any } = params;
optionParams.query = query;
const options = { params: optionParams, action: 'getEnvironments' };
return this._connection.sendToParent('stackQuery', options).then(onData).catch(onError);
}
/**
* This API allows you to retrieve details of releases of a stack using the {@link https://www.contentstack.com/docs/developers/apis/content-management-api/#get-all-releases| Releases API} requests. This method returns a Promise object.
* @param {Object} query Query for the GET call
* @param {Object} params Optional parameters for the GET call
* @return {Object} A Promise object which will be resolved with details of the releases.
*/
getReleases(query = {}, params = {}) {
const optionParams: { [key: string]: any } = params;
optionParams.query = query;
const options = { params: optionParams, action: 'getReleases' };
return this._connection.sendToParent('stackQuery', options).then(onData).catch(onError);
}
/**
* This API allows you to retrieve details of publish queue of a stack using the {@link https://www.contentstack.com/docs/developers/apis/content-management-api/#get-publish-queue| Publish Queue API} requests. This method returns a Promise object.
* @param {Object} query Query for the GET call
* @param {Object} params Optional parameters for the GET call
* @return {Object} A Promise object which will be resolved with details of the publish queue.
*/
getPublishes(query = {}, params = {}) {
const optionParams: { [key: string]: any } = params;
optionParams.query = query;
const options = { params: optionParams, action: 'getPublishes' };
return this._connection.sendToParent('stackQuery', options).then(onData).catch(onError);
}
/**
* This API allows you to retrive a locale of a stack using the {@link https://www.contentstack.com/docs/apis/content-management-api/#get-a-language| Language API} requests. Method returns a Promise object.
* @param {string} code Code of the desired locale
* @param {Object} params Optional parameters for the GET call
* @return {Object} A promise object which will be resolved with locale details.
*/
getLocale(code: string, params = {}) {
if (!code) {
return Promise.reject(new Error('code is required'));
}
const options = { code, params, action: 'getLocale' };
return this._connection.sendToParent('stackQuery', options).then(onData).catch(onError);
}
/**
* This API allows you to retrive the locales of a stack using the {@link https://www.contentstack.com/docs/apis/content-management-api/#get-all-content-types| Languages API} requests. Method returns a Promise object.
* @param {Object} query Query for the GET call
* @param {Object} params Optional parameters for the GET call
* @return {Object} A Promise object which will be resolved with details of the locales.
*/
getLocales(query = {}, params = {}) {
const optionParams: { [key: string]: any } = params;
optionParams.query = query;
const options = { params: optionParams, action: 'getLocales' };
return this._connection.sendToParent('stackQuery', options).then(onData).catch(onError);
}
/**
* This API allows you to retrive a workflow of a stack using the {@link https://www.contentstack.com/docs/apis/content-management-api/#get-a-workflow| Language API} requests. Method returns a Promise object.
* @param {string} code Code of the desired locale
* @param {Object} params Optional parameters for the GET call
* @return {Object} A promise object which will be resolved with locale details.
*/
getWorkflow(uid: string, params = {}) {
if (!uid) {
return Promise.reject(new Error('workflow uid is required'));
}
const options = { uid, params, action: 'getWorkflow' };
return this._connection.sendToParent('stackQuery', options).then(onData).catch(onError);
}
/**
* This API allows you to retrive the locales of a stack using the {@link https://www.contentstack.com/docs/apis/content-management-api/#get-all-content-types| Languages API} requests. Method returns a Promise object.
* @param {Object} query Query for the GET call
* @param {Object} params Optional parameters for the GET call
* @return {Object} A Promise object which will be resolved with details of the locales.
*/
getWorkflows(query = {}, params = {}) {
const optionParams: { [key: string]: any } = params;
optionParams.query = query;
const options = { params: optionParams, action: 'getWorkflows' };
return this._connection.sendToParent('stackQuery', options).then(onData).catch(onError);
}
/**
* This API allows you to retrieve all the branches in the current stack
* @returns All branches of the current stack
*/
getAllBranches(): BranchDetail[] {
return this._data.branches || [];
}
/**
* Returns the details of the current branch of the stack if available
* @returns current branch of the current stack if available
*/
getCurrentBranch(): BranchDetail | null {
return this._currentBranch;
}
/**
* Returns variant groups details.
* @returns variant groups details.
*/
getVariantById(variant_uid:string) {
if (!variant_uid) {
return Promise.reject(new Error('variant uid is required'));
}
const options = { params: {uid : variant_uid}, action: 'getVariantById' };
return this._connection.sendToParent('stackQuery', options).then(onData).catch(onError);
}
/**
* This API allows you to retrieve data of a single global field of a stack using the {@link https://www.contentstack.com/docs/developers/apis/content-management-api#get-single-global-field| Global Field API} requests. This method returns a Promise object.
* @param {string} uid of the desired global field
* @param {Object} params Optional parameters for the GET call
* @return {Object} A promise object which will be resolved with global field details.
*/
getGlobalField(uid: string, params = {}): Promise<{ [key: string]: GenericObjectType }> {
if (!uid) {
return Promise.reject(new Error("uid is required"));
}
const options = { uid, params, action: "getGlobalField" };
return this._connection
.sendToParent("stackQuery", options)
.then(onData)
.catch(onError);
}
/**
* This API allows you to retrieve data of all global fields of a stack using the {@link https://www.contentstack.com/docs/developers/apis/content-management-api#get-all-global-fields| Global Fields API} requests. This method returns a Promise object.
* @param {Object} query Query for the GET call
* @param {Object} params Optional parameters for the GET call
* @return {Object} A promise object which will be resolved with global field details.
*/
getGlobalFields(
query = {},
params: { [key: string]: GenericObjectType } = {}
): Promise<{ [key: string]: GenericObjectType }> {
const optionParams = params;
optionParams.query = query;
const options = { params: optionParams, action: "getGlobalFields" };
return this._connection
.sendToParent("stackQuery", options)
.then(onData)
.catch(onError);
}
}
export default Stack;