Skip to content

Commit 821c20a

Browse files
Merge pull request #23 from contentstack/snippet-implementation
Support Global_fields
2 parents 8c6ddda + 1f4a9ff commit 821c20a

File tree

9 files changed

+9632
-26
lines changed

9 files changed

+9632
-26
lines changed

dist/node/contentstack.js

Lines changed: 147 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -277,15 +277,40 @@ function resultWrapper(result) {
277277
return result;
278278
};
279279

280+
// // spread the result object
281+
// export function spreadResult(result) {
282+
// let _results = [];
283+
// if (result && Object.keys(result).length) {
284+
// if (typeof result.entries !== 'undefined') _results.push(result.entries);
285+
// if (typeof result.assets !== 'undefined') _results.push(result.assets);
286+
// if (typeof result.content_type !== 'undefined' || typeof result.schema !== 'undefined') _results.push(result.content_type || result.schema);
287+
// if (typeof result.count !== 'undefined') _results.push(result.count);
288+
// if (typeof result.entry !== 'undefined') _results = result.entry;
289+
// if (typeof result.asset !== 'undefined') _results = result.asset;
290+
// if (typeof result.items !== 'undefined') _results.push(result);
291+
// }
292+
// return _results;
293+
// };
294+
280295
// spread the result object
281296
function spreadResult(result) {
282297
var _results = [];
283298
if (result && Object.keys(result).length) {
284-
if (typeof result.entries !== 'undefined') _results.push(result.entries);
299+
if (typeof result.entries !== 'undefined') {
300+
_results.push(result.entries);
301+
if (result.content_type) {
302+
_results['schema'] = result.content_type;
303+
}
304+
}
285305
if (typeof result.assets !== 'undefined') _results.push(result.assets);
286306
if (typeof result.content_type !== 'undefined' || typeof result.schema !== 'undefined') _results.push(result.content_type || result.schema);
287307
if (typeof result.count !== 'undefined') _results.push(result.count);
288-
if (typeof result.entry !== 'undefined') _results = result.entry;
308+
if (typeof result.entry !== 'undefined') {
309+
_results = result.entry;
310+
if (result.schema) {
311+
_results['schema'] = result.schema;
312+
}
313+
}
289314
if (typeof result.asset !== 'undefined') _results = result.asset;
290315
if (typeof result.items !== 'undefined') _results.push(result);
291316
}
@@ -1039,7 +1064,7 @@ var Stack = function () {
10391064

10401065
}, {
10411066
key: 'getContentTypes',
1042-
value: function getContentTypes() {
1067+
value: function getContentTypes(param) {
10431068
var query = {
10441069
method: 'POST',
10451070
headers: this.headers,
@@ -1049,6 +1074,11 @@ var Stack = function () {
10491074
environment: this.environment
10501075
}
10511076
};
1077+
if (param && param !== undefined) {
1078+
for (var key in param) {
1079+
query.body[key] = param[key];
1080+
}
1081+
}
10521082
return (0, _request2.default)(query);
10531083
}
10541084

@@ -1559,7 +1589,7 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
15591589
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
15601590

15611591
//JS SDK version
1562-
var version = '3.7.1';
1592+
var version = '3.8.1';
15631593
var environment = void 0,
15641594
api_key = void 0;
15651595

@@ -2000,6 +2030,7 @@ var Entry = function () {
20002030
key: "includeSchema",
20012031
value: function includeSchema() {
20022032
this._query['include_schema'] = true;
2033+
this._query['include_snippet_schema'] = true;
20032034
return this;
20042035
}
20052036

@@ -2041,6 +2072,7 @@ var Entry = function () {
20412072
key: "includeContentType",
20422073
value: function includeContentType() {
20432074
this._query['include_content_type'] = true;
2075+
this._query['include_snippet_schema'] = true;
20442076
return this;
20452077
}
20462078

@@ -2655,6 +2687,14 @@ var Query = function (_Entry) {
26552687
* @param {object} query - RAW (JSON) queries
26562688
* @returns {Query}
26572689
* @instance
2690+
* @example
2691+
* let blogQuery = Stack().ContentType('example').Query();
2692+
* let data = blogQuery.query({"brand": {"$nin_query": {"title": "Apple Inc."}}}).find()
2693+
* data.then(function(result) {
2694+
* // ‘result’ contains the total count.
2695+
* },function (error) {
2696+
* // error function
2697+
* })
26582698
*/
26592699

26602700
}, {
@@ -2668,6 +2708,97 @@ var Query = function (_Entry) {
26682708
}
26692709
}
26702710

2711+
/**
2712+
* @method referenceIn
2713+
* @memberOf Query
2714+
* @description Retrieve entries that satisfy the query conditions made on referenced fields.
2715+
* @param {Query} query - RAW (JSON) queries
2716+
* @returns {Query}
2717+
* @instance
2718+
* @example
2719+
* <caption> referenceIn with Query instances</caption>
2720+
* let blogQuery = Stack().ContentType('example').Query();
2721+
* let Query = Stack.ContentType('blog').Query().where('title', 'Demo').find()
2722+
* let data = blogQuery.referenceIn("brand", Query).find()
2723+
* data.then(function(result) {
2724+
* // ‘result’ contains the total count.
2725+
* },function (error) {
2726+
* // error function
2727+
* })
2728+
*
2729+
* @example
2730+
* <caption> referenceIn with raw queries</caption>
2731+
* let blogQuery = Stack().ContentType('example').Query();
2732+
* let data = blogQuery.referenceIn("brand", {'title': 'Demo'}).find()
2733+
* data.then(function(result) {
2734+
* // ‘result’ contains the total count.
2735+
* },function (error) {
2736+
* // error function
2737+
* })
2738+
*/
2739+
2740+
}, {
2741+
key: 'referenceIn',
2742+
value: function referenceIn(key, query) {
2743+
var _query = {};
2744+
if (query instanceof Query && query._query.query) {
2745+
_query["$in_query"] = query._query.query;
2746+
} else if ((typeof query === 'undefined' ? 'undefined' : _typeof(query)) === "object") {
2747+
_query["$in_query"] = query;
2748+
}
2749+
if (this._query['query'][key]) {
2750+
this._query['query'][key] = this._query['query'][key].concat(_query);
2751+
} else {
2752+
this._query['query'][key] = _query;
2753+
}
2754+
return this;
2755+
}
2756+
2757+
/**
2758+
* @method referenceNotIn
2759+
* @memberOf Query
2760+
* @description Retrieve entries that does not satisfy the query conditions made on referenced fields.
2761+
* @param {Query} query - RAW (JSON) queries
2762+
* @returns {Query}
2763+
* @instance
2764+
* @example
2765+
* <caption> referenceNotIn with Query instances</caption>
2766+
* let blogQuery = Stack().ContentType('example').Query();
2767+
* let data = blogQuery.referenceNotIn("brand", {'title': 'Demo'}).find()
2768+
* data.then(function(result) {
2769+
* // ‘result’ contains the total count.
2770+
* },function (error) {
2771+
* // error function
2772+
* })
2773+
*
2774+
* @example
2775+
* <caption> referenceNotIn with raw queries</caption>
2776+
* let blogQuery = Stack().ContentType('example').Query();
2777+
* let data = blogQuery.referenceNotIn("brand", {'title': 'Demo'}).find()
2778+
* data.then(function(result) {
2779+
* // ‘result’ contains the total count.
2780+
* },function (error) {
2781+
* // error function
2782+
* })
2783+
*/
2784+
2785+
}, {
2786+
key: 'referenceNotIn',
2787+
value: function referenceNotIn(key, query) {
2788+
var _query = {};
2789+
if (query instanceof Query && query._query.query) {
2790+
_query["$nin_query"] = query._query.query;
2791+
} else if ((typeof query === 'undefined' ? 'undefined' : _typeof(query)) === "object") {
2792+
_query["$nin_query"] = query;
2793+
}
2794+
if (this._query['query'][key]) {
2795+
this._query['query'][key] = this._query['query'][key].concat(_query);
2796+
} else {
2797+
this._query['query'][key] = _query;
2798+
}
2799+
return this;
2800+
}
2801+
26712802
/**
26722803
* @method tags
26732804
* @memberOf Query
@@ -2743,18 +2874,18 @@ var Query = function (_Entry) {
27432874
}
27442875

27452876
/**
2746-
* @method addParam
2747-
* @description Includes query parameters in your queries.
2748-
* @memberOf Query
2749-
* @example var data = blogQuery.addParam('include_count', 'true').fetch()
2750-
* data.then(function (result) {
2751-
* // 'result' is an object which content the data including count in json object form
2752-
* },function (error) {
2753-
* // error function
2754-
* })
2755-
* @returns {Query}
2756-
* @instance
2757-
*/
2877+
* @method addParam
2878+
* @description Includes query parameters in your queries.
2879+
* @memberOf Query
2880+
* @example var data = blogQuery.addParam('include_count', 'true').fetch()
2881+
* data.then(function (result) {
2882+
* // 'result' is an object which content the data including count in json object form
2883+
* },function (error) {
2884+
* // error function
2885+
* })
2886+
* @returns {Query}
2887+
* @instance
2888+
*/
27582889

27592890
}, {
27602891
key: 'addParam',

examples/node/contentstack-demo.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ class ContentstackDemo {
2626
*/
2727
getEntries(contentTypeUid) {
2828
contentTypeUid = contentTypeUid || 'source'
29-
return this.Stack.ContentType(contentTypeUid).Query().toJSON().find()
29+
return this.Stack.ContentType('test').Query().includeContentType().toJSON().find()
30+
//return this.Stack.getContentTypes(contentTypeUid)
3031

3132
}
3233

@@ -61,7 +62,9 @@ class ContentstackDemo {
6162
*/
6263
getContentType(uid) {
6364
//contentTypeUid = contentTypeUid || 'source'
64-
return this.Stack.getContentType(uid)
65+
// return this.Stack.getContentType(uid)
66+
// return this.Stack.ContentType(uid).Entry("blta07130f8b344b260").includeReferenceContentTypeUID().includeSchema().toJSON().fetch()
67+
//return this.Stack.getContentTypes({"include_global_field_schema": true})
6568
}
6669

6770
/**

0 commit comments

Comments
 (0)