forked from contentstack/contentstack-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathentry.js
More file actions
233 lines (218 loc) · 7.88 KB
/
entry.js
File metadata and controls
233 lines (218 loc) · 7.88 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
import * as Utils from "../lib/utils";
import Stack from "../stack";
/**
* @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 = Contentstack.Stack().ContentType('example).Entry();
* @returns {Entry}
* @ignore
*/
export default class Entry {
constructor() {
this._query = {};
/**
* @method only
* @description This method is use to show the selected fields of the entries in resultset.
* @param {String} [key=BASE] - reference field in the entry/single field in entry
* @param {Array} values - array of fields to be show in resultset
* @example
* <caption> .only with field uid </caption>
* blogEntry.only('title')
* @example
* <caption> .only with field uid </caption>
* blogEntry.only('BASE','title')
* @example
* <caption> .only with field uids(array) </caption>
* blogEntry.only(['title','description'])
* @example
* <caption> .only with reference_field_uid and field uid </caption>
* blogEntry.includeReference('category').only('category','title')
* @example
* <caption> .only with reference_field_uid and field uids(array) </caption>
* blogEntry.includeReference('category').only('category', ['title', 'description'])
* @returns {Entry}
*/
this.only = Utils.transform('only');
/**
* @method except
* @description This method is use to hide the selected fields of the entries in resultset.
* @param {String} [key=BASE] - reference field in the entry/single field in entry
* @param {Array} values - array of fields to be show in resultset
* @example
* <caption> .except with field uid </caption>
* blogEntry.except('title')
* @example
* <caption> .except with field uid </caption>
* blogEntry.except('BASE','title')
* @example
* <caption> .except with field uids(array) </caption>
* blogEntry.except(['title','description'])
* @example
* <caption> .except with reference_field_uid and field uid </caption>
* blogEntry.includeReference('category').except('category','title')
* @example
* <caption> .except with reference_field_uid and field uids(array) </caption>
* blogEntry.includeReference('category').except('category', ['title', 'description'])
* @returns {Entry} */
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
* @description This method is use to include referenced entries from the other Contenttype.
* @example
* <caption> .includeReference with reference_field_uids as array </caption>
* blogEntry.includeReference(['category', 'author'])
* @example
* <caption> .includeReference with reference_field_uids </caption>
* blogEntry.includeReference('category', 'author')
* @returns {Entry}
*/
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.");
}
}
/**
* @method language
* @description This method is used set language code, which language's data to be retrieve.
* @param {String} language_code - language code. e.g. 'en-us', 'ja-jp', etc.
* @example blogEntry.language('en-us')
* @returns {Entry}
*/
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
* @description This method is used to add query to Entry object.
* @param {String} key - key of the query
* @param {String} value - value of the query
* @example blogEntry.addQuery('include_schema',true)
* @returns {Entry}
*/
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
* @deprecated since verion 3.3.0
* @description This method is used to include the schema of the current contenttype in result set along with the entry/entries.
* @example blogEntry.includeSchema()
* @returns {Entry}
*/
includeSchema() {
this._query['include_schema'] = true;
return this;
}
/**
* @method includeContentType
* @description This method is used to include the current contenttype in result set along with the entry/entries.
* @example blogEntry.includeContentType()
* @returns {Entry}
*/
includeContentType() {
this._query['include_content_type'] = true;
return this;
}
/**
* @method includeOwner
* @description This method is used to include the owner of the entry/entries in resultset.
* @example blogEntry.includeOwner()
* @returns {Entry}
*/
includeOwner() {
this._query['include_owner'] = true;
return this;
}
/**
* @method toJSON
* @description This method is used to convert the result in to plain javascript object.
* @example
* blogEntry
* .toJSON()
* .then(function (result) {
* let value = result.get(field_uid)
* },function (error) {
* // error function
* })
* @returns {Object}
*/
toJSON() {
this.tojson = true;
return this;
}
/**
* @method AddParam
* @description This method includes query parameter in query.
* @example blogQuery.addParam('include_count', 'true').fetch()
*/
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
* @description fetch entry of requested content_type of defined query if present.
* @example
* blogEntry.fetch()
*/
fetch() {
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
}
};
return Utils.sendRequest(this);
} else {
console.error("Kindly provide an entry uid. e.g. .Entry('bltsomething123')");
}
}
}