Skip to content

Commit 1778fe3

Browse files
Merge pull request #7 from contentstack/sync_api
Merged Sync_api branch with master
2 parents 065be14 + 2941258 commit 1778fe3

File tree

15 files changed

+406
-97
lines changed

15 files changed

+406
-97
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ node_modules/*
55
reports/*
66
apidocs-templates/*
77
test/smtpconfig.js
8-
test/config.js
8+
test/config.js
9+
test/sync_config.js

config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const config = {
44
port: 443,
55
version: "v3",
66
urls: {
7+
sync: "/stacks/sync",
78
content_types: "/content_types/",
89
entries: "/entries/",
910
assets: "/assets/",

dist/nativescript/contentstack.js

Lines changed: 66 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,8 @@ function resultWrapper(result) {
270270
result.entry = (0, _result2.default)(result.entry);
271271
} else if (result && typeof result.asset !== 'undefined') {
272272
result.asset = (0, _result2.default)(result.asset);
273+
} else if (result && typeof result.items !== 'undefined') {
274+
result.items = (0, _result2.default)(result.items).toJSON();
273275
}
274276

275277
return result;
@@ -285,11 +287,13 @@ function spreadResult(result) {
285287
if (typeof result.count !== 'undefined') _results.push(result.count);
286288
if (typeof result.entry !== 'undefined') _results = result.entry;
287289
if (typeof result.asset !== 'undefined') _results = result.asset;
290+
if (typeof result.items !== 'undefined') _results.push(result);
288291
}
289292
return _results;
290293
};
291294

292295
function sendRequest(queryObject) {
296+
293297
var env_uid = queryObject.environment_uid;
294298
if (env_uid) {
295299
queryObject._query.environment_uid = env_uid;
@@ -341,6 +345,7 @@ function sendRequest(queryObject) {
341345
try {
342346
self.entry_uid = self.asset_uid = self.tojson = self.queryCachePolicy = undefined;
343347
var entries = {};
348+
var syncstack = {};
344349
if (queryObject.singleEntry) {
345350
queryObject.singleEntry = false;
346351
if (data.schema) entries.schema = data.schema;
@@ -360,9 +365,17 @@ function sendRequest(queryObject) {
360365
}
361366
return;
362367
}
368+
} else if (data.items) {
369+
syncstack = {
370+
items: data.items,
371+
pagination_token: data.pagination_token,
372+
sync_token: data.sync_token,
373+
total_count: data.total_count
374+
};
363375
} else {
364376
entries = data;
365377
}
378+
366379
if (cachePolicy !== -1) {
367380
self.provider.set(hashQuery, entries, function (err) {
368381
try {
@@ -374,10 +387,14 @@ function sendRequest(queryObject) {
374387
}
375388
});
376389
return resolve(spreadResult(entries));
377-
} else {
378-
if (!tojson) entries = resultWrapper(entries);
379-
return resolve(spreadResult(entries));
380390
}
391+
392+
if (Object.keys(syncstack).length) {
393+
return resolve(syncstack);
394+
}
395+
396+
if (!tojson) entries = resultWrapper(entries);
397+
return resolve(spreadResult(entries));
381398
} catch (e) {
382399
return reject({
383400
message: e.message
@@ -498,6 +515,8 @@ Object.defineProperty(exports, "__esModule", {
498515
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
499516

500517
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
518+
/*import Sync from './modules/sync';*/
519+
501520

502521
var _config = __webpack_require__(7);
503522

@@ -548,6 +567,7 @@ var Stack = function () {
548567
this.config = _config2.default;
549568
this.cachePolicy = _index2.default.policies.IGNORE_CACHE;
550569
this.provider = _index2.default.providers('localstorage');
570+
//this.sync_cdn_api_key = stack_arguments[0].sync_cdn_api_key;
551571

552572
for (var _len = arguments.length, stack_arguments = Array(_len), _key = 0; _key < _len; _key++) {
553573
stack_arguments[_key] = arguments[_key];
@@ -832,6 +852,44 @@ var Stack = function () {
832852
return (0, _request2.default)(query);
833853
}
834854

855+
/**
856+
* @method sync
857+
* @description The Sync API takes care of syncing your Contentstack data with your app and ensures that the data is always up-to-date by providing delta updates. Contentstack’s iOS SDK supports Sync API, which you can use to build powerful apps. Read through to understand how to use the Sync API with Contentstack JavaScript SDK.
858+
* @param {object} params - params is an object which Supports locale, start_date, content_type_id queries.
859+
* @example
860+
* Stack.sync({'init': true}) // For initializing sync
861+
* @example
862+
* Stack.sync({'init': true, 'locale': 'en-us'}) //For initializing sync with entries of a specific locale
863+
* @example
864+
* Stack.sync({'init': true, 'start_date': '2018-10-22'}) //For initializing sync with entries published after a specific date
865+
* @example
866+
* Stack.sync({'init': true, 'content_type_id': 'session'}) //For initializing sync with entries of a specific content type
867+
* @example
868+
* 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'.
869+
* @example
870+
* Stack.sync({'pagination_token': '<btlsomething>'}) // For fetching the next batch of entries using pagination token
871+
* @example
872+
* Stack.sync({'sync_token': '<btlsomething>'}) // For performing subsequent sync after initial sync
873+
* @returns {object}
874+
*/
875+
876+
}, {
877+
key: 'sync',
878+
value: function sync(params) {
879+
this._query = {};
880+
this._query = Object.assign(this._query, params);
881+
this.requestParams = {
882+
method: 'POST',
883+
headers: this.headers,
884+
url: this.config.protocol + "://" + this.config.host + ':' + this.config.port + '/' + this.config.version + this.config.urls.sync,
885+
body: {
886+
_method: 'GET',
887+
query: this._query
888+
}
889+
};
890+
return Utils.sendRequest(this);
891+
}
892+
835893
/**
836894
* @method imageTransform
837895
* @description Transforms provided image url based on transformation parameters.
@@ -894,7 +952,9 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
894952
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; } }
895953

896954
//JS SDK version
897-
var version = '3.4.0';
955+
var version = '3.4.1';
956+
var environment = void 0,
957+
api_key = void 0;
898958

899959
function Request(options) {
900960
return new Promise(function (resolve, reject) {
@@ -1326,6 +1386,7 @@ var Entry = function () {
13261386
query: this._query
13271387
}
13281388
};
1389+
13291390
return Utils.sendRequest(this);
13301391
} else {
13311392
console.error("Kindly provide an entry uid. e.g. .Entry('bltsomething123')");
@@ -1915,6 +1976,7 @@ var config = {
19151976
port: 443,
19161977
version: "v3",
19171978
urls: {
1979+
sync: "/stacks/sync",
19181980
content_types: "/content_types/",
19191981
entries: "/entries/",
19201982
assets: "/assets/",

dist/node/contentstack.js

Lines changed: 67 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,8 @@ function resultWrapper(result) {
276276
result.entry = (0, _result2.default)(result.entry);
277277
} else if (result && typeof result.asset !== 'undefined') {
278278
result.asset = (0, _result2.default)(result.asset);
279+
} else if (result && typeof result.items !== 'undefined') {
280+
result.items = (0, _result2.default)(result.items).toJSON();
279281
}
280282

281283
return result;
@@ -291,11 +293,13 @@ function spreadResult(result) {
291293
if (typeof result.count !== 'undefined') _results.push(result.count);
292294
if (typeof result.entry !== 'undefined') _results = result.entry;
293295
if (typeof result.asset !== 'undefined') _results = result.asset;
296+
if (typeof result.items !== 'undefined') _results.push(result);
294297
}
295298
return _results;
296299
};
297300

298301
function sendRequest(queryObject) {
302+
299303
var env_uid = queryObject.environment_uid;
300304
if (env_uid) {
301305
queryObject._query.environment_uid = env_uid;
@@ -347,6 +351,7 @@ function sendRequest(queryObject) {
347351
try {
348352
self.entry_uid = self.asset_uid = self.tojson = self.queryCachePolicy = undefined;
349353
var entries = {};
354+
var syncstack = {};
350355
if (queryObject.singleEntry) {
351356
queryObject.singleEntry = false;
352357
if (data.schema) entries.schema = data.schema;
@@ -366,9 +371,17 @@ function sendRequest(queryObject) {
366371
}
367372
return;
368373
}
374+
} else if (data.items) {
375+
syncstack = {
376+
items: data.items,
377+
pagination_token: data.pagination_token,
378+
sync_token: data.sync_token,
379+
total_count: data.total_count
380+
};
369381
} else {
370382
entries = data;
371383
}
384+
372385
if (cachePolicy !== -1) {
373386
self.provider.set(hashQuery, entries, function (err) {
374387
try {
@@ -380,10 +393,14 @@ function sendRequest(queryObject) {
380393
}
381394
});
382395
return resolve(spreadResult(entries));
383-
} else {
384-
if (!tojson) entries = resultWrapper(entries);
385-
return resolve(spreadResult(entries));
386396
}
397+
398+
if (Object.keys(syncstack).length) {
399+
return resolve(syncstack);
400+
}
401+
402+
if (!tojson) entries = resultWrapper(entries);
403+
return resolve(spreadResult(entries));
387404
} catch (e) {
388405
return reject({
389406
message: e.message
@@ -509,6 +526,8 @@ Object.defineProperty(exports, "__esModule", {
509526
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
510527

511528
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
529+
/*import Sync from './modules/sync';*/
530+
512531

513532
var _config = __webpack_require__(17);
514533

@@ -559,6 +578,7 @@ var Stack = function () {
559578
this.config = _config2.default;
560579
this.cachePolicy = _index2.default.policies.IGNORE_CACHE;
561580
this.provider = _index2.default.providers('localstorage');
581+
//this.sync_cdn_api_key = stack_arguments[0].sync_cdn_api_key;
562582

563583
for (var _len = arguments.length, stack_arguments = Array(_len), _key = 0; _key < _len; _key++) {
564584
stack_arguments[_key] = arguments[_key];
@@ -789,7 +809,7 @@ var Stack = function () {
789809
* @method Assets
790810
* @description Set the Asset Uid which you want to retrive the Asset.
791811
* @param {String} uid - asset_uid
792-
* @example Stack.Assets('blt1234567890abcef')
812+
* @example Stack.Assets('blt1234567890abcef').fetch
793813
* @returns {Assets}
794814
*/
795815

@@ -843,6 +863,44 @@ var Stack = function () {
843863
return (0, _request2.default)(query);
844864
}
845865

866+
/**
867+
* @method sync
868+
* @description The Sync API takes care of syncing your Contentstack data with your app and ensures that the data is always up-to-date by providing delta updates. Contentstack’s iOS SDK supports Sync API, which you can use to build powerful apps. Read through to understand how to use the Sync API with Contentstack JavaScript SDK.
869+
* @param {object} params - params is an object which Supports locale, start_date, content_type_id queries.
870+
* @example
871+
* Stack.sync({'init': true}) // For initializing sync
872+
* @example
873+
* Stack.sync({'init': true, 'locale': 'en-us'}) //For initializing sync with entries of a specific locale
874+
* @example
875+
* Stack.sync({'init': true, 'start_date': '2018-10-22'}) //For initializing sync with entries published after a specific date
876+
* @example
877+
* Stack.sync({'init': true, 'content_type_id': 'session'}) //For initializing sync with entries of a specific content type
878+
* @example
879+
* 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'.
880+
* @example
881+
* Stack.sync({'pagination_token': '<btlsomething>'}) // For fetching the next batch of entries using pagination token
882+
* @example
883+
* Stack.sync({'sync_token': '<btlsomething>'}) // For performing subsequent sync after initial sync
884+
* @returns {object}
885+
*/
886+
887+
}, {
888+
key: 'sync',
889+
value: function sync(params) {
890+
this._query = {};
891+
this._query = Object.assign(this._query, params);
892+
this.requestParams = {
893+
method: 'POST',
894+
headers: this.headers,
895+
url: this.config.protocol + "://" + this.config.host + ':' + this.config.port + '/' + this.config.version + this.config.urls.sync,
896+
body: {
897+
_method: 'GET',
898+
query: this._query
899+
}
900+
};
901+
return Utils.sendRequest(this);
902+
}
903+
846904
/**
847905
* @method imageTransform
848906
* @description Transforms provided image url based on transformation parameters.
@@ -1308,7 +1366,9 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
13081366
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; } }
13091367

13101368
//JS SDK version
1311-
var version = '3.3.0';
1369+
var version = '3.4.1';
1370+
var environment = void 0,
1371+
api_key = void 0;
13121372

13131373
function Request(options) {
13141374
return new Promise(function (resolve, reject) {
@@ -1792,6 +1852,7 @@ var Entry = function () {
17921852
query: this._query
17931853
}
17941854
};
1855+
17951856
return Utils.sendRequest(this);
17961857
} else {
17971858
console.error("Kindly provide an entry uid. e.g. .Entry('bltsomething123')");
@@ -2399,6 +2460,7 @@ var config = {
23992460
port: 443,
24002461
version: "v3",
24012462
urls: {
2463+
sync: "/stacks/sync",
24022464
content_types: "/content_types/",
24032465
entries: "/entries/",
24042466
assets: "/assets/",
@@ -7115,17 +7177,6 @@ var Assets = function () {
71157177
return this;
71167178
}
71177179

7118-
/**
7119-
* @method Query
7120-
* @description Query instance to provide support for all search queries.
7121-
* @example Assets().Query()
7122-
* @returns {Query}
7123-
*/
7124-
// Query() {
7125-
// let query = new Query();
7126-
// return Utils.merge(query, this);
7127-
// }
7128-
71297180
/**
71307181
* @method toJSON
71317182
* @description This method is used to convert the result in to plain javascript object.

0 commit comments

Comments
 (0)