Skip to content

Commit 8f4ce14

Browse files
Merge pull request #7 from contentstack/sync_api
Merged Sync_api branch with master
2 parents f545b18 + e00056c commit 8f4ce14

File tree

11 files changed

+204
-59
lines changed

11 files changed

+204
-59
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/",

examples/node/contentstack-demo.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class ContentstackDemo {
1414
// Initialize the Contentstackstack
1515
this.Stack = Contentstack.Stack(config);
1616
}
17+
1718

1819
/**
1920
* getEntries
@@ -23,9 +24,10 @@ class ContentstackDemo {
2324
*/
2425
getEntries(contentTypeUid) {
2526
contentTypeUid = contentTypeUid || 'source'
26-
return this.Stack.ContentType(contentTypeUid).Query().where('title', "hometestfinal").toJSON().find()
27+
return this.Stack.ContentType(contentTypeUid).Query().toJSON().find()
2728
}
2829

30+
2931
/**
3032
* fetchEntry
3133
* @description : fetchEntry is used to get the specified uid entry
@@ -38,15 +40,17 @@ class ContentstackDemo {
3840
entryUid = entryUid || 'blt123something'
3941
return this.Stack.ContentType(contentTypeUid).Entry(entryUid).fetch()
4042
}
43+
4144
/**
4245
* getAssets
4346
* @description : getAssets is used to get the assets
4447
* @return : Result {Promise}
4548
*/
4649
getAssets() {
47-
return this.Stack.Assets().Query().addParam('include_dimension', 'true').toJSON().find()
50+
return this.Stack.Assets().Query().toJSON().find()
4851
}
4952

53+
5054
/**
5155
* fetchAsset
5256
* @description : fetchAsset is used to get the specified uid asset
@@ -58,6 +62,18 @@ class ContentstackDemo {
5862
return this.Stack.Assets(assetUid).addParam('include_dimension', 'true').fetch()
5963
}
6064

65+
66+
/**
67+
* fetchAsset
68+
* @description : fetchAsset is used to get the specified uid asset
69+
* @params : assetUid {string} - Specified Asset uid to be fetched
70+
* @return : Result {Promise}
71+
*/
72+
getSyncApi(params) {
73+
params = params || 'blt123something'
74+
return this.Stack.sync(params);
75+
}
76+
6177
}
6278

6379
module.exports = ContentstackDemo

examples/node/index.js

Lines changed: 74 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,28 @@
22

33
const ContentstackDemo = require('./contentstack-demo.js')
44

5-
const Demo = new ContentstackDemo({ 'api_key': 'bltsomething123', 'access_token': 'bltsomething123', 'environment': 'development' })
5+
const Demo = new ContentstackDemo({ 'api_key': '<api_key>', 'access_token': '<Delivery_token>', 'environment': '<Environment>'})
66

7-
//get all the entries
8-
Demo
9-
.getEntries('source')
10-
.spread(function(result) {
7+
8+
// Demo
9+
// .getEntries('source')
10+
// .then(function(result) {
11+
// // result object with entry
12+
// console.info("Result2 : ", result)
13+
14+
// //console.info("Result2 : ", JSON.stringify(result))
15+
16+
// })
17+
// .catch(function(err) {
18+
// // error of get entry
19+
// console.error("Fetch Error :", err)
20+
// })
21+
22+
23+
24+
//get all the entries
25+
Demo.getSyncApi({"init": true, "type": "asset_published"})
26+
.then(function(result) {
1127
// result object with entries
1228
console.info("Result: ", result)
1329

@@ -17,46 +33,58 @@ Demo
1733
console.error("Find Error :", err)
1834
})
1935

20-
// get single entry
21-
Demo
22-
.getEntry('source', 'bltsomething123')
23-
.then(function(result) {
24-
// result object with entry
25-
console.info("Result2 : ", JSON.stringify(result))
26-
})
27-
.catch(function(err) {
28-
// error of get entry
29-
console.error("Fetch Error :", err)
30-
})
3136

32-
// get single asset
33-
Demo
34-
.getAsset('bltsomething123')
35-
.then(function(result) {
36-
// result object with entry
37-
console.info("Result2 : ", result)
38-
})
39-
.catch(function(err) {
40-
// error of get entry
41-
console.error("Fetch Error :", err)
42-
})
4337

44-
// get all assets
45-
Demo
46-
.getAssets()
47-
.spread(function(result) {
48-
// result object with entry
49-
console.info("Result2 : ", result)
50-
for (let i = 0, _i = result.length; i < _i; i++) {
51-
// Image optimization
52-
const imgUrl = Demo.Stack.imageTransform(result[i]['url'], {
53-
quality: 50,
54-
format: 'jpg'
55-
})
56-
console.log("Image URL : ", imgUrl)
57-
}
58-
})
59-
.catch(function(err) {
60-
// error of get entry
61-
console.error("getAssets Fetch Error :", err)
62-
})
38+
// // get single asset
39+
// Demo
40+
// .getAsset('bltsomething123')
41+
// .then(function(result) {
42+
// // result object with entry
43+
// console.info("Result2 : ", result)
44+
// })
45+
// .catch(function(err) {
46+
// // error of get entry
47+
// console.error("Fetch Error :", err)
48+
// })
49+
50+
51+
52+
// // get all assets
53+
// Demo
54+
// .getAssets()
55+
// .spread(function(result) {
56+
// // result object with entry
57+
// console.info("Result2 : ", result)
58+
// for (let i = 0, _i = result.length; i < _i; i++) {
59+
// // Image optimization
60+
// const imgUrl = Demo.Stack.imageTransform(result[i]['url'], {
61+
// quality: 50,
62+
// format: 'jpg'
63+
// })
64+
// console.log("Image URL : ", imgUrl)
65+
// }
66+
// })
67+
// .catch(function(err) {
68+
// // error of get entry
69+
// console.error("getAssets Fetch Error :", err)
70+
// })
71+
72+
73+
74+
// get all assets
75+
// Demo
76+
// .getSyncApi({"sync_token": "blt123something"})
77+
// .then(function(result) {
78+
79+
// console.log("result", JSON.stringify(result))
80+
81+
// })
82+
// .catch(function(err) {
83+
// // error of get entry
84+
// console.error("getSync Fetch Error :", err)
85+
// })
86+
87+
88+
89+
90+

examples/web/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ <h1>News</h1>
1111
<p><i>Click to view the news</i></p>
1212
<div id="wrapper"></div>
1313
<div id="button">
14-
<input type="submit" name="Download Asset" onclick="getAsset()">
14+
<input type="submit" name="Download Asset">
1515
</div>
1616
</div>
1717
<script src="https://code.jquery.com/jquery-2.2.0.min.js"></script>

examples/web/scripts/custom.min.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,6 @@
6464
.toJSON()
6565
.find()
6666
.spread(function(result) {
67-
console.log("Result: ", result);
68-
// result object with entry
6967
console.info("Result2 : ", result)
7068
for (let i = 0, _i = result.length; i < _i; i++) {
7169
// Image optimization
@@ -87,14 +85,11 @@
8785
wrapper = document.getElementById('wrapper')
8886
Stack = Contentstack.Stack({ 'api_key': 'blt123something', 'access_token': 'blt123something', 'environment': 'mobile' })
8987

90-
// get single the entry
91-
// singleEntry("source", "blt123something")
92-
9388
// get all the entries
9489
allEntries("blogs")
9590

9691
// get all the assets
97-
getAsset();
92+
// getAsset();
9893
}
9994
}
10095
}());

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')");

0 commit comments

Comments
 (0)