Skip to content

Commit 0f55056

Browse files
Merge pull request #7 from contentstack/sync_api
Merged Sync_api branch with master
2 parents 2add55a + 9e62ce3 commit 0f55056

File tree

10 files changed

+184
-29
lines changed

10 files changed

+184
-29
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/react-native/contentstack.js

Lines changed: 67 additions & 16 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];
@@ -778,7 +798,7 @@ var Stack = function () {
778798
* @method Assets
779799
* @description Set the Asset Uid which you want to retrive the Asset.
780800
* @param {String} uid - asset_uid
781-
* @example Stack.Assets('blt1234567890abcef')
801+
* @example Stack.Assets('blt1234567890abcef').fetch
782802
* @returns {Assets}
783803
*/
784804

@@ -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.3.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/",
@@ -2442,17 +2504,6 @@ var Assets = function () {
24422504
return this;
24432505
}
24442506

2445-
/**
2446-
* @method Query
2447-
* @description Query instance to provide support for all search queries.
2448-
* @example Assets().Query()
2449-
* @returns {Query}
2450-
*/
2451-
// Query() {
2452-
// let query = new Query();
2453-
// return Utils.merge(query, this);
2454-
// }
2455-
24562507
/**
24572508
* @method toJSON
24582509
* @description This method is used to convert the result in to plain javascript object.

dist/web/contentstack.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)