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
280 lines (261 loc) · 9.1 KB
/
stack.js
File metadata and controls
280 lines (261 loc) · 9.1 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
import config from '../../config';
import * as Utils from './lib/utils';
import Entry from './modules/entry';
import Assets from './modules/assets';
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');
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 of 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 setCachePolicy which contains different 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 Set 'Cache Provider' object.
* @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 currently set CacheProvider object.
* @example Stack.getCacheProvider();
* @returns {Object}
*/
getCacheProvider() {
return this.provider;
}
/**
* @method ContentType
* @description Set "ContentType" from the Stack from where you want to retrive the entries.
* @param {String} [content_type_uid] - uid of the existing contenttype
* @returns {Stack}
*/
ContentType(uid) {
if (uid && typeof uid === 'string') {
this.content_type_uid = uid;
this.type = "contentType";
}
return this;
}
/**
* @method Entry
* @description Set the Entry Uid which you want to retrive from the Contenttype specified.
* @param {String} uid - entry_uid
* @example ContentType('blog').Entry('blt1234567890abcef')
* @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 Set the Asset Uid which you want to retrive the Asset.
* @param {String} uid - asset_uid
* @example Stack.Assets('blt1234567890abcef')
* @returns {Assets}
*/
Assets(uid) {
let asset = new Assets();
this.type = 'asset';
if (uid && typeof uid === "string") {
asset.asset_uid = uid;
}
return Utils.merge(asset, this);
}
/**
* @method Query
* @description Query instance to provide support for all search queries.
* @example ContentType('blog').Query()
* @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()
* @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 imageTransform
* @description Transforms provided image url based on transformation parameters.
* @param {String} url - Url on which transformations to be applied.
* @param {String} params - 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;
}
}