forked from contentstack/contentstack-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.js
More file actions
executable file
·462 lines (432 loc) · 15.4 KB
/
stack.js
File metadata and controls
executable file
·462 lines (432 loc) · 15.4 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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
import config from '../../config';
import * as Utils from './lib/utils';
import Entry from './modules/entry';
import Assets from './modules/assets';
/*import Sync from './modules/sync';*/
import Query from './modules/query';
import Request from './lib/request';
import * as cache from './cache';
import CacheProvider from './cache-provider/index';
/**
* @class
Stack
* @description Initialize an instance of ‘Stack’
* @example
* var Stack = Contentstack.Stack('api_key', 'delivery_token', 'environment');
OR
* var Stack = Contentstack.Stack({
* 'api_key':'stack_api_key',
* 'access_token':'stack_delivery_token',
* 'environment':'environment_name'
* });
*
* @returns {Stack}
* @instance
*/
export default class Stack {
constructor(...stack_arguments) {
if(stack_arguments[0].region && stack_arguments[0].region != undefined && stack_arguments[0].region != "us") {
config['host'] = stack_arguments[0].region+"-"+"cdn.contentstack.com"
}
this.config = config;
this.cachePolicy = CacheProvider.policies.IGNORE_CACHE;
this.provider = CacheProvider.providers('localstorage');
switch (stack_arguments.length) {
case 1:
if (typeof stack_arguments[0] === "object" && typeof stack_arguments[0].api_key === "string" && typeof stack_arguments[0].access_token === "string" && typeof stack_arguments[0].environment === "string") {
this.headers = {
api_key: stack_arguments[0].api_key,
access_token: stack_arguments[0].access_token
};
this.environment = stack_arguments[0].environment;
return this;
} else {
console.error("Kindly provide valid object parameters.");
}
case 3:
if (typeof stack_arguments[0] === "string" && typeof stack_arguments[1] === "string" && typeof stack_arguments[2] === "string") {
this.headers = {
api_key: stack_arguments[0],
access_token: stack_arguments[1]
};
this.environment = stack_arguments[2];
return this;
} else {
console.error("Kindly provide valid string parameters.");
}
default:
console.error("Kindly provide valid parameters to initialize the Built.io Contentstack javascript-SDK Stack.");
}
}
/**
* @method setPort
* @memberOf Stack
* @description Sets the port of the host
* @param {Number} port - Port Number
* @return {Stack}
* @instance
* */
setPort(port) {
if (typeof port === "number") this.config.port = port;
return this;
}
/**
* @method setProtocol
* @memberOf Stack
* @description Sets the protocol for the host
* @param {String} protocol - http/https protocol
* @return {Stack}
* @instance
* */
setProtocol(protocol) {
if (typeof protocol === "string" && ~["https", "http"].indexOf(protocol)) this.config.protocol = protocol;
return this;
}
/**
* @method setHost
* @memberOf Stack
* @description Sets the host of the API server
* @param {String} host - valid ip or host
* @return {Stack}
* @instance
* */
setHost(host) {
if (typeof host === "string" && host) this.config.host = host;
return this;
}
/**
* @method setCachePolicy
* @memberOf Stack
* @description Allows you to set cache policies
* @param {Constant} [key=ONLY_NETWORK] - Cache policy to be applied on Stack or Query.
* @example
* Stack.setCachePolicy(Contentstack.CachePolicy.IGNORE_CACHE)
* Stack.setCachePolicy(Contentstack.CachePolicy.ONLY_NETWORK)
* Stack.setCachePolicy(Contentstack.CachePolicy.CACHE_ELSE_NETWORK)
* Stack.setCachePolicy(Contentstack.CachePolicy.NETWORK_ELSE_CACHE)
* Stack.setCachePolicy(Contentstack.CachePolicy.CACHE_THEN_NETWORK)
* @returns {Stack}
* @instance
*/
setCachePolicy(policy) {
if (typeof policy === 'number' && policy >= -1 && policy < 4) {
if (!this._query) {
this.cachePolicy = policy;
} else {
this.queryCachePolicy = policy;
}
} else {
console.error("Kindly provide the valid policy");
}
return this;
}
/**
* @method setCacheProvider
* @memberOf Stack
* @description Allows you to set an object of the cache provider
* @example
* Stack
* .setCacheProvider({
* get: function (key, callback) {
* // custom logic
* },
* set: function (key, value, callback) {
* // custom logic
* }
* });
* @returns {Stack}
* @instance
*/
setCacheProvider(provider) {
if (provider && typeof provider === 'object') {
this.provider = provider;
}
return this;
}
/**
* @method clearByQuery
* @memberOf Stack
* @description 'clearByQuery' function to clear the query from the cache.
* @example
* Stack.clearQuery(query, callback);
* @returns {Stack}
* @instance
*/
clearByQuery() {
if (this.provider && typeof this.provider.clearByQuery === 'function') {
return this.provider.clearByQuery.apply(this.provider, arguments);
}
}
/**
* @method clearByContentType
* @memberOf Stack
* @description 'clearByContentType' function to clear the query from the cache by specified content type.
* @example
* Stack.clearByContentType(content_type_uid, callback);
* Stack.clearByContentType(content_type_uid, language_uid, callback);
* @returns {Stack}
* @instance
*/
clearByContentType() {
if (this.provider && typeof this.provider.clearByContentType === 'function') {
return this.provider.clearByContentType.apply(this.provider, arguments);
}
}
/**
* @method clearAll
* @memberOf Stack
* @description 'clearAll' function to clear all the queries from cache.
* @example
* Stack.clearAll(callback);
* @returns {Stack}
* @instance
*/
clearAll() {
if (this.provider && typeof this.provider.clearAll === 'function') {
return this.provider.clearAll.apply(this.provider, arguments);
}
}
/**
* @method getCacheProvider
* @memberOf Stack
* @description Returns the currently set object of 'CacheProvider'
* @example Stack.getCacheProvider();
* @returns {Stack}
* @instance
*/
getCacheProvider() {
return this.provider;
}
/**
* @method ContentType
* @memberOf Stack
* @description Set the content type of which you want to retrieve the entries
* @param {String} [content_type_uid] - uid of the existing content type
* @example
* let data = Stack.ContentType('blog').Query().toJSON().find()
* data
* .then(function(result) {
* // 'result' content the list of entries of particular content type blog.
* }, function(error) {
* // error function
* })
* @returns {Stack}
* @instance
*/
ContentType(uid) {
if (uid && typeof uid === 'string') {
this.content_type_uid = uid;
this.type = "contentType";
}
return this;
}
/**
* @method Entry
* @memberOf Stack
* @param {String} uid - uid of the entry
* @description An initializer is responsible for creating Entry object
* @returns {Entry}
* @instance
*/
Entry(uid) {
let entry = new Entry();
if (uid && typeof uid === "string") {
entry.entry_uid = uid;
}
return Utils.merge(entry, this);
}
/**
* @method fetch
* @memberOf Stack
* @description This method returns the complete information of a specific content type.
* @example
* let single_contenttype = Stack.ContentType(content_type_uid).fetch()
* single_contenttype
* .then(function(result) {
* // 'result' is a single contentType information.
* }).catch((error) => {
* console.log(error)
* });
* @returns {ContentType}
* @instance
*/
fetch() {
let result = {
method: 'POST',
headers: this.headers,
url: this.config.protocol + "://" + this.config.host + ':' + this.config.port + '/' + this.config.version + this.config.urls.content_types + this.content_type_uid,
body: {
_method: 'GET',
environment: this.environment
}
};
return Request(result);
}
/**
* @method Assets
* @memberOf Stack
* @param {String} uid - uid of the asset
* @description Retrieves all assets of a stack by default. To retrieve a single asset, specify its UID.
* @example
* let data = Stack.Assets('bltsomething123').toJSON().fetch()
* data
* .then(function(result) {
* // ‘result’ is a single asset object of specified uid
* }, function(error) {
* // error function
* })
* @example
* let data = Stack.Assets().toJSON().find()
* data
* .then(function(result) {
* // ‘result’ will display all assets present in stack
* }, function(error) {
* // error function
* })
* @returns {Assets}
* @instance
*/
Assets(uid) {
this.type = 'asset';
if (uid && typeof uid === "string") {
let asset = new Assets();
asset.asset_uid = uid;
return Utils.merge(asset, this);
}
return this;
}
/**
* @method Query
* @memberOf Stack
* @description An initializer is responsible for creating Query object.Provides support for all search queries
* @returns {Query}
* @instance
*/
Query() {
let query = new Query();
return Utils.merge(query, this);
}
/**
* @method getLastActivites
* @memberOf Stack
* @description getLastActivites get all the ContentTypes whose last activity updated.
* @example Stack.getLastActivites()
* @example
* let data = Stack.getLastActivites().toJSON().fetch()
* data
* .then(function(result) {
* // 'result' is list of contentTypes whose last activity updated.
* }, function(error) {
* // error function
* })
* @returns {Stack}
* @instance
*/
getLastActivities() {
let query = {
method: 'POST',
headers: this.headers,
url: this.config.protocol + "://" + this.config.host + ':' + this.config.port + '/' + this.config.version + this.config.urls.content_types,
body: {
_method: 'GET',
only_last_activity: true,
environment: this.environment
}
};
return Request(query);
}
/**
* @method getContentTypes
* @memberOf Stack
* @description This method returns comprehensive information of all the content types of a particular stack in your account.
* @example Stack.getContentTypes()
* @example
* let data = Stack.getContentTypes()
* data
* .then(function(result) {
* // 'result' is list of contentTypes.
* }, function(error) {
* // error function
* })
* @returns {Stack}
* @instance
*/
getContentTypes(param) {
let query = {
method: 'POST',
headers: this.headers,
url: this.config.protocol + "://" + this.config.host + ':' + this.config.port + '/' + this.config.version + this.config.urls.content_types,
body: {
_method: 'GET',
environment: this.environment
}
};
if(param && param !== undefined) {
for( var key in param) {
query.body[key] = param[key]
}
}
return Request(query);
}
/**
* @method sync
* @memberOf Stack
* @description Syncs your Contentstack data with your app and ensures that the data is always up-to-date by providing delta updates
* @param {object} params - params is an object that supports ‘locale’, ‘start_date’, ‘content_type_id’, and ‘type’ queries.
* @example
* Stack.sync({'init': true}) // For initializing sync
* @example
* Stack.sync({'init': true, 'locale': 'en-us'}) //For initializing sync with entries of a specific locale
* @example
* Stack.sync({'init': true, 'start_date': '2018-10-22'}) //For initializing sync with entries published after a specific date
* @example
* Stack.sync({'init': true, 'content_type_id': 'session'}) //For initializing sync with entries of a specific content type
* @example
* Stack.sync({'init': true, 'type': 'entry_published'}) //Use the type parameter to get a specific type of content.Supports 'asset_published', 'entry_published', 'asset_unpublished', 'entry_unpublished', 'asset_deleted', 'entry_deleted', 'content_type_deleted'.
* @example
* Stack.sync({'pagination_token': '<btlsomething>'}) // For fetching the next batch of entries using pagination token
* @example
* Stack.sync({'sync_token': '<btlsomething>'}) // For performing subsequent sync after initial sync
* @returns {promise}
* @instance
*/
sync(params) {
this._query = {};
this._query = Object.assign(this._query, params);
this.requestParams = {
method: 'POST',
headers: this.headers,
url: this.config.protocol + "://" + this.config.host + ':' + this.config.port + '/' + this.config.version + this.config.urls.sync,
body: {
_method: 'GET',
query: this._query
}
}
return Utils.sendRequest(this);
}
/**
* @method imageTransform
* @memberOf Stack
* @description Performs transformations on images of mentioned url based on transformation parameters
* @param {String} url - Image url on which transformations need to be applied.
* @param {String} params - Object with transformation parameters
* @example
* Stack.imageTransform(imageURL, {height: 100, width: 200, disable: "upscale"});
* @example
* Stack.imageTransform(imageURL, {crop: "150,100"});
* @example
* Stack.imageTransform(imageURL, {format: "png", crop: "150,100"});
* @returns {string} [Image url with transformation parameters.]
* @instance
*/
imageTransform(url, params) {
if (url && typeof url === "string" && typeof params === "object" && params.length === undefined) {
let queryParams = [];
for (const operation in params) {
queryParams.push(operation + '=' + params[operation]);
}
url += (url.indexOf("?") <= -1) ? "?" + queryParams.join('&') : "&" + queryParams.join('&');
}
return url;
}
}