Skip to content

Commit 6ccbfd6

Browse files
Merge pull request #7 from contentstack/sync_api
Merged Sync_api branch with master
2 parents 6541a58 + ea1ef92 commit 6ccbfd6

37 files changed

+817
-1256
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

README.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,66 @@ Following are Image Delivery API examples.
168168
'height': 100
169169
})
170170

171+
### Using the Sync API with JavaScript SDK
172+
173+
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 JavaScript 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.
174+
[Read Sync API documentation](https://docs.google.com/document/d/14IFRlmtOja5OPzlK1Q6DxW3auISEQNhX6opMYJcHVsI/).
175+
176+
##### Initial sync
177+
178+
The Initial Sync process performs a complete sync of your app data. It returns all the published entries and assets of the specified stack in response.
179+
180+
To start the Initial Sync process, use the syncStack method.
181+
182+
let data = Stack.sync({"init": true})
183+
data.then(function(sync_data, err) {
184+
//error for any error description
185+
//sync_data.items: contains sync data
186+
//sync_data.paginationToken: for fetching the next batch of entries using pagination token
187+
//sync_token.syncToken: for performing subsequent sync after initial sync
188+
if (err) throw err
189+
})
190+
191+
The response also contains a sync token, which you need to store, since this token is used to get subsequent delta updates later, as shown in the Subsequent Sync section below.
192+
193+
You can also fetch custom results in initial sync by using advanced sync queries.
194+
195+
196+
##### Sync pagination
197+
198+
If the result of the initial sync (or subsequent sync) contains more than 100 records, the response would be paginated. It provides pagination token in the response. You will need to use this token to get the next batch of data.
199+
200+
201+
let data = Stack.sync({"pagination_token" : "<pagination_token>"})
202+
data.then(function(result, err) {
203+
//error for any error description
204+
//result.items: contains sync data
205+
//result.paginationToken: For fetching the next batch of entries using pagination token
206+
//result.syncToken: For performing subsequent sync after initial sync
207+
if(err) throw err
208+
})
209+
210+
##### Subsequent sync
211+
212+
You can use the sync token (that you receive after initial sync) to get the updated content next time. The sync token fetches only the content that was added after your last sync, and the details of the content that was deleted or updated.
213+
214+
215+
let data = Stack.sync({"sync_token" : “<sync_token>”})
216+
data.then(function(sync_data, err) {
217+
//error for any error description
218+
//sync_data.items: contains sync data
219+
//sync_data.paginationToken: for fetching the next batch of entries using pagination token
220+
//sync_token.syncToken: for performing subsequent sync after initial sync
221+
if(err) throw err
222+
})
223+
224+
##### Advanced sync queries
225+
226+
You can use advanced sync queries to fetch custom results while performing initial sync.
227+
[Read advanced sync queries documentation](https://www.contentstack.com/docs/guide/synchronization/using-the-sync-api-with-javascript-sdk#advanced-sync-queries)
228+
229+
230+
171231
### Helpful Links
172232

173233
- [Contentstack Website](https://www.contentstack.com)

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/",

0 commit comments

Comments
 (0)