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
352 lines (328 loc) · 12.3 KB
/
stack.js
File metadata and controls
352 lines (328 loc) · 12.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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
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';
/**
* Expose `Stack`.
* @ignore
*/
export default class Stack {
constructor(...stack_arguments) {
this.config = config;
this.cachePolicy = CacheProvider.policies.IGNORE_CACHE;
this.provider = CacheProvider.providers('localstorage');
//this.sync_cdn_api_key = stack_arguments[0].sync_cdn_api_key;
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
* @description Sets the port of the host
* @param {Number} port - Port Number
* @return Stack
* */
setPort(port) {
if (typeof port === "number") this.config.port = port;
return this;
}
/**
* @method setProtocol
* @description Sets the protocol for the host
* @param {String} protocol - http/https protocol
* @return Stack
* */
setProtocol(protocol) {
if (typeof protocol === "string" && ~["https", "http"].indexOf(protocol)) this.config.protocol = protocol;
return this;
}
/**
* @method setHost
* @description Sets the host of the API server
* @param {String} host - valid ip or host
* @return Stack
* */
setHost(host) {
if (typeof host === "string" && host) this.config.host = host;
return this;
}
/**
* @method setCachePolicy
* @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}
*/
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
* @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}
*/
setCacheProvider(provider) {
if (provider && typeof provider === 'object') {
this.provider = provider;
}
return this;
}
/**
* @method clearByQuery
* @description 'clearByQuery' function to clear the query from the cache.
* @example
* Stack.clearQuery(query, callback);
* @ignore
*/
clearByQuery() {
if (this.provider && typeof this.provider.clearByQuery === 'function') {
return this.provider.clearByQuery.apply(this.provider, arguments);
}
}
/**
* @method clearByContentType
* @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);
* @ignore
*/
clearByContentType() {
if (this.provider && typeof this.provider.clearByContentType === 'function') {
return this.provider.clearByContentType.apply(this.provider, arguments);
}
}
/**
* @method clearAll
* @description 'clearAll' function to clear all the queries from cache.
* @example
* Stack.clearAll(callback);
* @ignore
*/
clearAll() {
if (this.provider && typeof this.provider.clearAll === 'function') {
return this.provider.clearAll.apply(this.provider, arguments);
}
}
/**
* @method getCacheProvider
* @description Returns the currently set object of 'CacheProvider'
* @example Stack.getCacheProvider();
* @returns {Object}
*/
getCacheProvider() {
return this.provider;
}
/**
* @method ContentType
* @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}
*/
ContentType(uid) {
if (uid && typeof uid === 'string') {
this.content_type_uid = uid;
this.type = "contentType";
}
return this;
}
/**
* @method Entry
* @description Retrieves the entry based on the specified UID
* @param {String} uid - uid of entry you want to retrieve
* @example
* let data = Stack.ContentType('blog').Entry('bltsomething123').toJSON().fetch()
* data
* .then(function(result) {
* // ‘result’ is a single entry object of specified uid
* }, function(error) {
* // error function
* })
* @returns {Entry}
*/
Entry(uid) {
let entry = new Entry();
if (uid && typeof uid === "string") {
entry.entry_uid = uid;
}
return Utils.merge(entry, this);
}
/**
* @method Assets
* @description Retrieves the asset based on the specified UID
* @param {String} uid - uid of asset you want to retrieve
* @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
* })
* @returns {Assets}
*/
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
* @description Provides support for all search queries
* @example Stack.ContentType('blog').Query().toJSON().find()
* @returns {Query}
*/
Query() {
let query = new Query();
return Utils.merge(query, this);
}
/**
* @method getLastActivites
* @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}
* @ignore
*/
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 sync
* @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 {object}
*/
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
* @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.]
*/
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;
}
}