forked from contentstack/contentstack-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodules_entry.js.html
406 lines (373 loc) · 21.7 KB
/
modules_entry.js.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>modules/entry.js - Documentation</title>
<link rel="shortcut icon" href="./favicon.ico">
<script src="scripts/prettify/prettify.js"></script>
<script src="scripts/prettify/lang-css.js"></script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link rel="canonical" href="https://www.contentstack.com/docs/developers/platforms/javascript/api-reference/">
<link type="text/css" rel="stylesheet" href="https://code.ionicframework.com/ionicons/2.0.1/css/ionicons.min.css">
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
<!-- Google Tag Manager -->
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-WGP99J7');</script>
<!-- End Google Tag Manager -->
</head>
<body>
<!-- Google Tag Manager (noscript) -->
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-WGP99J7" height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<!-- End Google Tag Manager (noscript) -->
<input type="checkbox" id="nav-trigger" class="nav-trigger" />
<label for="nav-trigger" class="navicon-button x">
<div class="navicon"></div>
</label>
<label for="nav-trigger" class="overlay"></label>
<nav>
<h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Contentstack.html">Contentstack</a><ul class='methods'><li data-type='method'><a href="Contentstack.html#Stack">Stack</a></li></ul></li><li><a href="Stack.html">Stack</a><ul class='methods'><li data-type='method'><a href="Stack.html#setPort">setPort</a></li><li data-type='method'><a href="Stack.html#setProtocol">setProtocol</a></li><li data-type='method'><a href="Stack.html#setHost">setHost</a></li><li data-type='method'><a href="Stack.html#setCachePolicy">setCachePolicy</a></li><li data-type='method'><a href="Stack.html#setCacheProvider">setCacheProvider</a></li><li data-type='method'><a href="Stack.html#clearByQuery">clearByQuery</a></li><li data-type='method'><a href="Stack.html#clearByContentType">clearByContentType</a></li><li data-type='method'><a href="Stack.html#clearAll">clearAll</a></li><li data-type='method'><a href="Stack.html#getCacheProvider">getCacheProvider</a></li><li data-type='method'><a href="Stack.html#ContentType">ContentType</a></li><li data-type='method'><a href="Stack.html#Assets">Assets</a></li><li data-type='method'><a href="Stack.html#Query">Query</a></li><li data-type='method'><a href="Stack.html#getLastActivites">getLastActivites</a></li><li data-type='method'><a href="Stack.html#getContentTypes">getContentTypes</a></li><li data-type='method'><a href="Stack.html#sync">sync</a></li><li data-type='method'><a href="Stack.html#imageTransform">imageTransform</a></li></ul></li><li><a href="Assets.html">Assets</a><ul class='methods'><li data-type='method'><a href="Assets.html#toJSON">toJSON</a></li><li data-type='method'><a href="Assets.html#addParam">addParam</a></li><li data-type='method'><a href="Assets.html#fetch">fetch</a></li></ul></li><li><a href="Entry.html">Entry</a><ul class='methods'><li data-type='method'><a href="Entry.html#includeFallback">includeFallback</a></li><li data-type='method'><a href="Entry.html#only">only</a></li><li data-type='method'><a href="Entry.html#except">except</a></li><li data-type='method'><a href="Entry.html#includeReference">includeReference</a></li><li data-type='method'><a href="Entry.html#language">language</a></li><li data-type='method'><a href="Entry.html#addQuery">addQuery</a></li><li data-type='method'><a href="Entry.html#includeSchema">includeSchema</a></li><li data-type='method'><a href="Entry.html#includeReferenceContentTypeUid">includeReferenceContentTypeUid</a></li><li data-type='method'><a href="Entry.html#includeFallback">includeFallback</a></li><li data-type='method'><a href="Entry.html#includeContentType">includeContentType</a></li><li data-type='method'><a href="Entry.html#includeOwner">includeOwner</a></li><li data-type='method'><a href="Entry.html#toJSON">toJSON</a></li><li data-type='method'><a href="Entry.html#addParam">addParam</a></li><li data-type='method'><a href="Entry.html#fetch">fetch</a></li></ul></li><li><a href="Query.html">Query</a><ul class='methods'><li data-type='method'><a href="Query.html#lessThan">lessThan</a></li><li data-type='method'><a href="Query.html#lessThanOrEqualTo">lessThanOrEqualTo</a></li><li data-type='method'><a href="Query.html#greaterThan">greaterThan</a></li><li data-type='method'><a href="Query.html#greaterThanOrEqualTo">greaterThanOrEqualTo</a></li><li data-type='method'><a href="Query.html#notEqualTo">notEqualTo</a></li><li data-type='method'><a href="Query.html#containedIn">containedIn</a></li><li data-type='method'><a href="Query.html#notContainedIn">notContainedIn</a></li><li data-type='method'><a href="Query.html#exists">exists</a></li><li data-type='method'><a href="Query.html#notExists">notExists</a></li><li data-type='method'><a href="Query.html#ascending">ascending</a></li><li data-type='method'><a href="Query.html#descending">descending</a></li><li data-type='method'><a href="Query.html#beforeUid">beforeUid</a></li><li data-type='method'><a href="Query.html#afterUid">afterUid</a></li><li data-type='method'><a href="Query.html#skip">skip</a></li><li data-type='method'><a href="Query.html#limit">limit</a></li><li data-type='method'><a href="Query.html#or">or</a></li><li data-type='method'><a href="Query.html#and">and</a></li><li data-type='method'><a href="Query.html#where">where</a></li><li data-type='method'><a href="Query.html#count">count</a></li><li data-type='method'><a href="Query.html#query">query</a></li><li data-type='method'><a href="Query.html#referenceIn">referenceIn</a></li><li data-type='method'><a href="Query.html#referenceNotIn">referenceNotIn</a></li><li data-type='method'><a href="Query.html#tags">tags</a></li><li data-type='method'><a href="Query.html#includeReferenceContentTypeUid">includeReferenceContentTypeUid</a></li><li data-type='method'><a href="Query.html#includeCount">includeCount</a></li><li data-type='method'><a href="Query.html#addParam">addParam</a></li><li data-type='method'><a href="Query.html#getQuery">getQuery</a></li><li data-type='method'><a href="Query.html#regex">regex</a></li><li data-type='method'><a href="Query.html#search">search</a></li><li data-type='method'><a href="Query.html#find">find</a></li><li data-type='method'><a href="Query.html#findOne">findOne</a></li></ul></li><li><a href="Result.html">Result</a><ul class='methods'><li data-type='method'><a href="Result.html#toJSON">toJSON</a></li><li data-type='method'><a href="Result.html#get">get</a></li><li data-type='method'><a href="Result.html#getDownloadUrl">getDownloadUrl</a></li></ul></li></ul>
</nav>
<div id="main">
<h1 class="page-title">modules/entry.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>import * as Utils from "../lib/utils";
import Stack from "../stack";
/**
* @class
Entry
* @summary Creates an instance of `Entry`.
* @description An initializer is responsible for creating Entry object.
* @param {String} uid - uid of the entry
* @example
* let Entry = Stack.ContentType('example').Entry('entry_uid');
* @returns {Entry}
* @instance
*/
export default class Entry {
constructor() {
this._query = {};
/**
* @method only
* @memberOf Entry
* @description Displays values of only the specified fields of entries or assets in the response
* @param {String} [key=BASE] - Assets: </br>
* <p>Retrieves specified field of asset</p>
* @param {String} - Entries:</br>
* <p>- retrieves default fields of the schema.</p>
* <p>- referenced_content-type-uid : retrieves fields of the referred content type.</p>
* @param {Array} values - array of fields that you want to display in the response
* @example
* <caption> The only function with field_uid will include the data of only the specified fields for each entry and exclude the data of all other fields. </caption>
* Stack.ContentType('contentTypeUid').Query().only('title').toJSON().find()
*
* <caption> The only function with an array of field_uids will include multiple fields for each entry and exclude the data of all other fields.</caption>
* Stack.ContentType('contentTypeUid').Query().only(['title','description']).toJSON().find()
*
* <caption> In only, we have the only with a reference parameter, where you need to enter the UID of the reference field in place of "reference_field_uid", and the second parameter to include the data of only the specified field_uid for each entry and exclude the data of all other fields.</caption>
* Stack.ContentType('contentTypeUid').Query().includeReference('reference_field_uid').only('reference_field_uid','title').toJSON().find()
*
* <caption> In only, we have the only with a reference parameter with an array, where you need to enter the UID of the reference field in place of "reference_field_uid", and the second parameter with an array of fields to include the data of only the specified array of field_uids for each entry and exclude the data of all other fields.</caption>
* Stack.ContentType('contentTypeUid').Query().includeReference('reference_field_uid').only('reference_field_uid', ['title', 'description']).toJSON().find()
*
* @returns {Entry}
* @instance
*/
this.only = Utils.transform('only');
/**
* @method except
* @memberOf Entry
* @description Displays all data of an entries or assets excluding the data of the specified fields.
* @param {String} [key=BASE] - BASE (default value) - retrieves default fields of the schema.
- referenced_content-type-uid - retrieves fields of the referred content type.
* @param {Array} values - array of fields that you want to skip in the response
* @example
* <caption> The except function with field_uid will exclude the data of only the specified fields for each entry and includes the data of all other fields. </caption>
* Stack.ContentType('contentTypeUid').Query().except('title').toJSON().find()
* @example
* <caption> The except function with an array of field_uids will except multiple fields for each entry and include the data of all other fields.</caption>
* Stack.ContentType('contentTypeUid').Query().except(['title','description']).toJSON().find()
* @example
* <caption> In except, we have the only with a reference parameter, where you need to enter the UID of the reference field in place of "reference_field_uid", and the second parameter to except the data of only the specified field_uid for each entry and include the data of all other fields.</caption>
* Stack.ContentType('contentTypeUid').Query().includeReference('reference_field_uid').except('reference_field_uid','title').toJSON().find()
* @example
* <caption> In except, we have the only with a reference parameter with an array, where you need to enter the UID of the reference field in place of "reference_field_uid", and the second parameter with an array of fields to except the data of only the specified array of field_uids for each entry and include the data of all other fields.</caption>
* Stack.ContentType('contentTypeUid').Query().includeReference('reference_field_uid').except('reference_field_uid', ['title', 'description']).toJSON().find()
* @returns {Entry}
* @instance
*/
this.except = Utils.transform('except');
return this;
}
setCacheProvider(provider) {
if (provider && typeof provider === 'object') {
this.provider = provider;
}
return this;
}
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 includeReference
* @memberOf Entry
* @description Fetches the entire content of referenced entry(ies). <a href='https://www.contentstack.com/docs/developers/apis/content-delivery-api/#include-reference'>Read More</a>
* @example
* <caption> .includeReference with reference_field_uids as array </caption>
* var Query = Stack.ContentType(contentTypes.source).Query();
Query
.includeReference(['reference_field_uid', 'other_reference_field_uid'])
.toJSON()
.find()
.then(function success(entries) {
//'entries' is an object used to retrieve data including reference entries.
})
* @example
* <caption> .includeReference with reference_field_uids and its children reference </caption>
* var Query = Stack.ContentType(contentTypes.source).Query();
Query
.includeReference(['reference_field_uid', 'reference_field_uid.child_reference_field_uid'])
.toJSON()
.find()
.then(function success(entries) {
//'entries' is an object used to retrieve data including reference entries.
})
* @example
* <caption> .includeReference with reference_field_uids </caption>
* var Query = Stack.ContentType(contentTypes.source).Query();
Query
.includeReference('reference_field_uid')
.toJSON()
.find()
.then(function success(entries) {
//'entries' is an object used to retrieve data including particular reference using reference_uid.
})
* @returns {Entry}
* @instance
*/
includeReference(...val) {
if (Array.isArray(val) || typeof val === "string") {
if (arguments.length) {
for (let i = 0; i < arguments.length; i++) {
this._query['include'] = this._query['include'] || [];
this._query['include'] = this._query['include'].concat(arguments[i]);
}
}
return this;
} else {
console.error("Argument should be a String or an Array.");
}
}
/**
* Sets the language code of which you want to retrieve data.
* @param {String} language_code - language code. e.g. 'en-us', 'ja-jp', etc.
* @memberOf Entry
* @example
* let data = Stack.ContentType(contentTypeUid).Entry(entryUid).language('ja-jp').fetch()
* data
* .then(function(result) {
* // 'result' is an object used to retrieve data of ja-jp language.
* }, function(error) {
* // error function
* })
*
* @returns {Entry}
* @instance
*/
language(language_code) {
if (language_code && typeof language_code === 'string') {
this._query['locale'] = language_code;
return this;
} else {
console.error("Argument should be a String.");
}
}
/**
* @method addQuery
* @memberOf Entry
* @description Adds query to Entry object
* @param {String} key - key of the query
* @param {String} value - value of the query
* @example Stack.ContentType(contentTypeUid).Entry(entry_uid).addQuery('include_schema',true)
* @returns {Entry}
* @instance
*/
addQuery(key, value) {
if (key && value && typeof key === 'string') {
this._query[key] = value;
return this;
} else {
console.error("First argument should be a String.");
}
}
/**
* @method includeSchema
* @memberOf Entry
* @deprecated since verion 3.3.0
* @description Include schema of the current content type along with entry/entries details.
* @example Stack.ContentType("contentType_uid").Entry("entry_uid").includeSchema().fetch()
* @returns {Entry}
* @instance
*/
includeSchema() {
this._query['include_schema'] = true;
return this;
}
/**
* @method includeReferenceContentTypeUid
* @memberOf Entry
* @description This method also includes the content type UIDs of the referenced entries returned in the response.
* @example Stack.ContentType("contentType_uid").Entry("entry_uid").includeReferenceContentTypeUID().fetch()
* @example
* Query = Stack.ContentType("contentType_uid").Entry("entry_uid").includeReferenceContentTypeUID().fetch()
* Query
* .toJSON()
* .then(function (result) {
* let value = result.get(field_uid)
* },function (error) {
* // error function
* })
* @returns {Entry}
* @instance
*/
includeReferenceContentTypeUID() {
this._query['include_reference_content_type_uid'] = true;
return this;
}
/**
* @method includeFallback
* @memberOf Entry
* @description Include the fallback locale publish content, if specified locale content is not publish.
* @example stack.ContentType(contentType_uid).Entry(entry_uid).includeFallback().fetch()
* @returns {Entry}
* @instance
*/
includeFallback() {
this._query['include_fallback'] = true;
return this;
}
/**
* @method includeContentType
* @memberOf Entry
* @description Include the details of the content type along with the entry/entries details.
* @example stack.ContentType(contentType_uid).Entry(entry_uid).includeContentType().fetch()
* @returns {Entry}
* @instance
*/
includeContentType() {
this._query['include_content_type'] = true;
return this;
}
/**
* @method includeOwner
* @memberOf Entry
* @description Includes the owner details of the entry/entries
* @example stack.ContentType(contentType_uid).Entry(entry_uid).includeOwner().fetch()
* @returns {Entry}
* @instance
*/
includeOwner() {
this._query['include_owner'] = true;
return this;
}
/**
* @method toJSON
* @memberOf Entry
* @description Converts your response into plain JavasScript object.Supports both entry and asset queries.
* @example
* Query = Stack.ContentType(contentTypeUid).Entry(entryUid).fetch()
* Query
* .toJSON()
* .then(function (result) {
* let value = result.get(field_uid)
* },function (error) {
* // error function
* })
* @returns {Entry}
* @instance
*/
toJSON() {
this.tojson = true;
return this;
}
/**
* @method addParam
* @memberOf Entry
* @description Includes query parameters in your queries.
* @example var data = Stack.ContentType(contentTypeUid).Entry(entryUid).addParam('include_count', 'true').fetch()
* data.then(function (result) {
* // 'result' is an object which content the data including count in json object form
* },function (error) {
* // error function
* })
* @returns {Entry}
* @instance
*/
addParam(key, value) {
if (key && value && typeof key === 'string' && typeof value === 'string') {
this._query[key] = value;
return this;
} else {
console.error("Kindly provide valid parameters.");
}
}
/**
* @method fetch
* @memberOf Entry
* @description Fetches a particular entry based on the provided entry UID.
* @example
* Stack.ContentType(contentTypeUid).Entry(entryUid).toJSON().fetch()
*
* @example
* Stack.ContentType(contentTypeUid).Entry(entryUid).toJSON().fetch({
*
* })
* @returns {promise}
* @instance
*/
fetch(fetchOptions) {
if (this.entry_uid) {
this.requestParams = {
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 + this.config.urls.entries + this.entry_uid,
body: {
_method: 'GET',
query: this._query
}
};
var options = Object.assign({}, this.fetchOptions, fetchOptions);
return Utils.sendRequest(this, options);
} else {
console.error("Kindly provide an entry uid. e.g. .Entry('bltsomething123')");
}
}
}</code></pre>
</article>
</section>
</div>
<br class="clear">
<footer>
<div class="inner-content center">
<span class="copy-right"><a href="mailto:support@contentstack.com" target="_self">support@contentstack.com</a> | © 2016-2020 Contentstack. All rights reserved.</span>
</div>
</footer>
<script>prettyPrint();</script>
<script src="scripts/linenumber.js"></script>
</body>
</html>