Skip to content

Commit 6003fc6

Browse files
Merge pull request #7 from contentstack/sync_api
Merged Sync_api branch with master
2 parents 4c51899 + 09de94e commit 6003fc6

File tree

7 files changed

+110
-4
lines changed

7 files changed

+110
-4
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/",

mocktest.json

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{
2+
"items": [
3+
{
4+
"type": "entry_published",
5+
"event_at": "2017-11-23T00:00:000Z",
6+
"content_type_uid": "Blog",
7+
"data": {
8+
"uid": "1",
9+
"locale": "en-us",
10+
"title": "Title - 1"
11+
}
12+
},
13+
{
14+
"type": "entry_published",
15+
"event_at": "2017-11-22T23:50:000Z",
16+
"content_type_uid": "Blog",
17+
"data": {
18+
"uid": "2",
19+
"locale": "en-us",
20+
"title": "Title - 2"
21+
22+
}
23+
},
24+
{
25+
"type": "asset_published",
26+
"event_at": "2017-11-22T22:59:000Z",
27+
"data": {
28+
"uid": "3",
29+
"locale": "en-us",
30+
"title": "Title - 3",
31+
"filename": "image1.jpg"
32+
}
33+
}
34+
],
35+
"skip": 100,
36+
"limit": 100,
37+
"total_count": 300,
38+
"pagination_token": "blt1223444455657"
39+
}

src/core/lib/request.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import fetch from "runtime/http.js";
33

44
//JS SDK version
55
let version = '{{VERSION}}';
6+
let environment,
7+
api_key;
68

79
export default function Request(options) {
810
return new Promise(function(resolve, reject) {
@@ -40,6 +42,7 @@ export default function Request(options) {
4042
queryParams = serialize(options.body);
4143
}
4244

45+
4346
fetch(url + '?' + queryParams, {
4447
method: 'GET',
4548
headers: headers

src/core/lib/utils.js

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,8 @@ export function resultWrapper(result) {
167167
result.entry = Result(result.entry);
168168
} else if (result && typeof result.asset !== 'undefined') {
169169
result.asset = Result(result.asset);
170+
} else if (result && typeof result.items !== 'undefined') {
171+
result.items = Result(result.items).toJSON();
170172
}
171173

172174
return result;
@@ -182,11 +184,13 @@ export function spreadResult(result) {
182184
if (typeof result.count !== 'undefined') _results.push(result.count);
183185
if (typeof result.entry !== 'undefined') _results = result.entry;
184186
if (typeof result.asset !== 'undefined') _results = result.asset;
187+
if (typeof result.items !== 'undefined') _results.push(result);
185188
}
186189
return _results;
187190
};
188191

189192
export function sendRequest(queryObject) {
193+
190194
let env_uid = queryObject.environment_uid;
191195
if (env_uid) {
192196
queryObject._query.environment_uid = env_uid;
@@ -240,6 +244,7 @@ export function sendRequest(queryObject) {
240244
try {
241245
self.entry_uid = self.asset_uid = self.tojson = self.queryCachePolicy = undefined;
242246
let entries = {};
247+
let syncstack = {};
243248
if (queryObject.singleEntry) {
244249
queryObject.singleEntry = false;
245250
if (data.schema) entries.schema = data.schema;
@@ -259,9 +264,18 @@ export function sendRequest(queryObject) {
259264
}
260265
return;
261266
}
267+
}
268+
else if(data.items) {
269+
syncstack = {
270+
items : data.items,
271+
pagination_token : data.pagination_token,
272+
sync_token : data.sync_token,
273+
total_count : data.total_count
274+
}
262275
} else {
263276
entries = data;
264277
}
278+
265279
if (cachePolicy !== -1) {
266280
self.provider.set(hashQuery, entries, function(err) {
267281
try {
@@ -273,10 +287,16 @@ export function sendRequest(queryObject) {
273287
}
274288
});
275289
return resolve(spreadResult(entries));
276-
} else {
277-
if (!tojson) entries = resultWrapper(entries);
278-
return resolve(spreadResult(entries));
290+
}
291+
292+
if(Object.keys(syncstack).length) {
293+
return resolve(syncstack);
279294
}
295+
296+
if (!tojson)
297+
entries = resultWrapper(entries);
298+
return resolve(spreadResult(entries));
299+
280300
} catch (e) {
281301
return reject({
282302
message: e.message

src/core/modules/entry.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ export default class Entry {
223223
query: this._query
224224
}
225225
};
226+
226227
return Utils.sendRequest(this);
227228
} else {
228229
console.error("Kindly provide an entry uid. e.g. .Entry('bltsomething123')");

src/core/stack.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import config from '../../config';
22
import * as Utils from './lib/utils';
33
import Entry from './modules/entry';
44
import Assets from './modules/assets';
5+
/*import Sync from './modules/sync';*/
56
import Query from './modules/query';
67
import Request from './lib/request';
78
import * as cache from './cache';
@@ -13,9 +14,11 @@ import CacheProvider from './cache-provider/index';
1314
*/
1415
export default class Stack {
1516
constructor(...stack_arguments) {
17+
1618
this.config = config;
1719
this.cachePolicy = CacheProvider.policies.IGNORE_CACHE;
1820
this.provider = CacheProvider.providers('localstorage');
21+
//this.sync_cdn_api_key = stack_arguments[0].sync_cdn_api_key;
1922
switch (stack_arguments.length) {
2023
case 1:
2124
if (typeof stack_arguments[0] === "object" && typeof stack_arguments[0].api_key === "string" && typeof stack_arguments[0].access_token === "string" && typeof stack_arguments[0].environment === "string") {
@@ -254,6 +257,44 @@ export default class Stack {
254257
return Request(query);
255258
}
256259

260+
261+
/**
262+
* @method sync
263+
* @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.
264+
* @param {object} params - params is an object which Supports locale, start_date, content_type_id queries.
265+
* @example
266+
* Stack.sync({'init': true}) // For initializing sync
267+
* @example
268+
* Stack.sync({'init': true, 'locale': 'en-us'}) //For initializing sync with entries of a specific locale
269+
* @example
270+
* Stack.sync({'init': true, 'start_date': '2018-10-22'}) //For initializing sync with entries published after a specific date
271+
* @example
272+
* Stack.sync({'init': true, 'content_type_id': 'session'}) //For initializing sync with entries of a specific content type
273+
* @example
274+
* 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'.
275+
* @example
276+
* Stack.sync({'pagination_token': '<btlsomething>'}) // For fetching the next batch of entries using pagination token
277+
* @example
278+
* Stack.sync({'sync_token': '<btlsomething>'}) // For performing subsequent sync after initial sync
279+
* @returns {object}
280+
*/
281+
282+
sync(params) {
283+
this._query = {};
284+
this._query = Object.assign(this._query, params);
285+
this.requestParams = {
286+
method: 'POST',
287+
headers: this.headers,
288+
url: this.config.protocol + "://" + this.config.host + ':' + this.config.port + '/' + this.config.version + this.config.urls.sync,
289+
body: {
290+
_method: 'GET',
291+
query: this._query
292+
}
293+
}
294+
return Utils.sendRequest(this);
295+
}
296+
297+
257298
/**
258299
* @method imageTransform
259300
* @description Transforms provided image url based on transformation parameters.

0 commit comments

Comments
 (0)