Skip to content

Commit 24fa818

Browse files
committed
examples added
1 parent b0bfb30 commit 24fa818

File tree

6 files changed

+321
-0
lines changed

6 files changed

+321
-0
lines changed

examples/PromiseError/index.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
require('dotenv').config()
2+
3+
const Contentstack = require('../../dist/node/contentstack');
4+
const { RequestInterceptor } = require('node-request-interceptor');
5+
const { default: withDefaultInterceptors } = require('node-request-interceptor/lib/presets/default');
6+
7+
const interceptor = new RequestInterceptor(withDefaultInterceptors)
8+
9+
// Log any outgoing requests to contentstack and return a mock HTML response
10+
// which will cause the SDK to throw an unhandled exception
11+
interceptor.use((req) => {
12+
13+
console.log('%s %s', req.method, req.url.href);
14+
15+
return {
16+
status: 400,
17+
headers: {
18+
'x-powered-by': 'node-request-interceptor',
19+
'content-type': 'text/html'
20+
},
21+
body: `
22+
<html>
23+
<head></head>
24+
<body>
25+
<h1>Nodata</h1>
26+
</body>
27+
</html>
28+
`,
29+
}
30+
});
31+
32+
const Stack = Contentstack.Stack(
33+
process.env.API_KEY,
34+
process.env.DELIVERY_TOKEN,
35+
process.env.ENVIRONMENT,
36+
{ timeout: 5000,
37+
retryCondition: () => true
38+
});
39+
40+
Stack.setHost('api.contentstack.io');
41+
42+
const Query = Stack
43+
.ContentType(process.env.CONTENTSTACK_CONTENT_TYPE)
44+
.Query({})
45+
.language("en-us")
46+
.includeCount();
47+
48+
// Executing this async invocation cause an unhandled promise rejection
49+
Query
50+
.toJSON()
51+
.find()
52+
.then((res) => {
53+
const [,count] = res;
54+
console.log(`${count} total blogs.`);
55+
})
56+
.catch((err) => {
57+
// Error is not caught
58+
console.error('ERRR Catch', err);
59+
});

examples/node/contentstack-demo.js

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
'use strict'
2+
/*!
3+
* module dependencies
4+
*/
5+
const Contentstack = require('../../dist/node/contentstack.js');
6+
7+
/*
8+
* Example ContentstackDemo Class
9+
* */
10+
11+
class ContentstackDemo {
12+
constructor(...config) {
13+
config = config || {}
14+
15+
this.Stack = Contentstack.Stack(...config);
16+
}
17+
18+
19+
/**
20+
* getEntries
21+
* @description : getEntries is used to get the entries of the specified entries
22+
* @params : contentTypeUid {string} - Content-Type from which entries to be retrieved
23+
* @return : Result {Promise}
24+
*/
25+
getEntries(contentTypeUid) {
26+
contentTypeUid = contentTypeUid || 'source'
27+
return this.Stack.ContentType(contentTypeUid).Query().includeContentType().toJSON().find()
28+
29+
}
30+
31+
/**
32+
* getEntries
33+
* @description : getEntries is used to get the entries of the specified entries
34+
* @params : contentTypeUid {string} - Content-Type from which entries to be retrieved
35+
* @return : Result {Promise}
36+
*/
37+
getLastActivities(contentTypeUid) {
38+
contentTypeUid = contentTypeUid || 'source'
39+
return this.Stack.getLastActivities()
40+
}
41+
42+
/**
43+
* getEntries
44+
* @description : getEntries is used to get the entries of the specified entries
45+
* @params : contentTypeUid {string} - Content-Type from which entries to be retrieved
46+
* @return : Result {Promise}
47+
*/
48+
getContentTypedemo(contentTypeUid) {
49+
contentTypeUid = contentTypeUid || 'source'
50+
return this.Stack.ContentType(contentTypeUid).fetch()
51+
}
52+
53+
/**
54+
* getEntries
55+
* @description : getEntries is used to get the entries of the specified entries
56+
* @params : contentTypeUid {string} - Content-Type from which entries to be retrieved
57+
* @return : Result {Promise}
58+
*/
59+
getContentType(uid) {
60+
contentTypeUid = contentTypeUid || 'source'
61+
return this.Stack.getContentType(uid)
62+
}
63+
64+
/**
65+
* fetchEntry
66+
* @description : fetchEntry is used to get the specified uid entry
67+
* @params : contentTypeUid {string} - Content-Type from which entry to be fetched
68+
* entryUid {string} - Specified entry to be fetched
69+
* @return : Result {Promise}
70+
*/
71+
getEntry(contentTypeUid, entryUid) {
72+
contentTypeUid = contentTypeUid || 'source'
73+
entryUid = entryUid || ''
74+
return this.Stack.ContentType(contentTypeUid).Entry(entryUid).language('ja-jp').fetch()
75+
}
76+
77+
/**
78+
* getAssets
79+
* @description : getAssets is used to get the assets
80+
* @return : Result {Promise}
81+
*/
82+
getAssets() {
83+
return this.Stack.Assets().Query().toJSON().find()
84+
}
85+
86+
87+
/**
88+
* fetchAsset
89+
* @description : fetchAsset is used to get the specified uid asset
90+
* @params : assetUid {string} - Specified Asset uid to be fetched
91+
* @return : Result {Promise}
92+
*/
93+
getAsset(assetUid) {
94+
assetUid = assetUid || ''
95+
return this.Stack.Assets(assetUid).addParam('include_dimension', 'true').fetch()
96+
}
97+
98+
99+
/**
100+
* fetchAsset
101+
* @description : fetchAsset is used to get the specified uid asset
102+
* @params : assetUid {string} - Specified Asset uid to be fetched
103+
* @return : Result {Promise}
104+
*/
105+
getSyncApi(params) {
106+
params = params || ''
107+
return this.Stack.sync(params);
108+
}
109+
110+
}
111+
112+
module.exports = ContentstackDemo

examples/node/index.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'use strict'
2+
const path = require('path')
3+
const dotenv = require('dotenv').config({
4+
path: path.join(__dirname, '.env')
5+
})
6+
7+
const ContentstackDemo = require('./contentstack-demo.js')
8+
const Demo = new ContentstackDemo({ 'api_key': process.env.API_KEY, 'delivery_token': process.env.DELIVERY_TOKEN, 'environment': process.env.ENVIRONMENT, })
9+
10+
11+
//get all the entries
12+
Demo.getEntries(process.env.CONTENT_TYPE)
13+
.then(function(result, err) {
14+
// console.log("Result>>>>>>>>>>>>>>>")
15+
try {
16+
if (err || !result) {
17+
console.log("Result>>>>>>>>>>>>>>>")
18+
console.log(err)
19+
} else {
20+
21+
console.log("Result: ", JSON.stringify(result, null, 1))
22+
//console.info("Result: ", JSON.stringify(result))
23+
}
24+
} catch (e) {
25+
return reject(e);
26+
}
27+
})
28+
.catch(function(err) {
29+
// error of get all entries
30+
console.error("Find Error :", err)
31+
})

examples/web/favicon.ico

14.7 KB
Binary file not shown.

examples/web/index.html

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Contentstack - Javascript SDK</title>
6+
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
7+
</head>
8+
<body>
9+
<div class="container">
10+
<h1>News</h1>
11+
<p><em>Click to view the news</em></p>
12+
<div id="wrapper"></div>
13+
<div id="button">
14+
<input type="submit" name="Download Asset">
15+
</div>
16+
</div>
17+
<script src="https://code.jquery.com/jquery-2.2.0.min.js"></script>
18+
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
19+
<script src="../../dist/web/contentstack.js"></script>
20+
<script src="scripts/custom.min.js"></script>
21+
</body>
22+
</html>

examples/web/scripts/custom.min.js

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
(function() {
2+
if (!Contentstack) {
3+
alert('Please add Contentstack Library');
4+
} else {
5+
console.log(Contentstack);
6+
var clearAll = document.getElementById('all');
7+
var clearByContentType = document.getElementById('content_type');
8+
var clearByQuery = document.getElementById('query');
9+
var wrapper, Stack;
10+
11+
function DOMCreation(entry, html) {
12+
if (entry) {
13+
html += '<div class="panel panel-default"><div class="panel-heading" role="tab"><h4 class="panel-title"><a role="button" data-toggle="collapse" data-parent="#accordion" href="#' + entry.uid + '" aria-expanded="true" aria-controls="' + entry.uid + '">';
14+
html += entry.title + '</a></h4></div><div id="' + entry.uid + '" class="panel-collapse collapse" role="tabpanel"><div class="panel-body">';
15+
html += entry.body + ' </div></div></div>';
16+
}
17+
return html;
18+
}
19+
20+
function singleEntry(contentTypeUid, entryUid) {
21+
var contentTypeUid = contentTypeUid || "news"
22+
var entryUid = entryUid || "news_uid"
23+
24+
Stack
25+
.ContentType(contentTypeUid)
26+
.Entry(entryUid)
27+
.fetch()
28+
.then(function(data) {
29+
// result object with entry
30+
var html = '<div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">';
31+
html = DOMCreation(data.toJSON(), html);
32+
html += '</div>';
33+
wrapper.innerHTML = html;
34+
}, function(err) {
35+
console.info('News could not fetched.');
36+
});
37+
}
38+
39+
function allEntries(contentTypeUid) {
40+
var contentTypeUid = contentTypeUid || "news";
41+
Stack
42+
.ContentType(contentTypeUid)
43+
.Query()
44+
.find()
45+
.then(function(data) {
46+
if (data && data.length && data[0].length) {
47+
var html = '<div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">';
48+
for (var i = 0, _i = data[0].length; i < _i; i++) {
49+
html = DOMCreation(data[0][i].toJSON(), html);
50+
}
51+
html += '</div>';
52+
wrapper.innerHTML = html;
53+
} else {
54+
console.info('News could not fetched.');
55+
}
56+
}, function(err) {
57+
console.info('Error : ' + err);
58+
console.log(err);
59+
});
60+
}
61+
62+
function getAsset() {
63+
Stack
64+
.Assets()
65+
.Query()
66+
.toJSON()
67+
.find()
68+
.spread(function(result) {
69+
console.info("Result2 : ", result)
70+
for (let i = 0, _i = result.length; i < _i; i++) {
71+
// Image optimization
72+
const imgUrl = Stack.imageTransform(result[i]['url'], {
73+
quality: 50,
74+
format: 'jpg'
75+
})
76+
console.log("Image URL : ", imgUrl)
77+
}
78+
}, function(err) {
79+
// body...
80+
console.info("Error: " + err);
81+
});
82+
}
83+
84+
85+
86+
window.onload = function() {
87+
wrapper = document.getElementById('wrapper')
88+
Stack = Contentstack.Stack({ 'api_key': '', 'delivery_token': '', 'environment': '' })
89+
Stack.setCachePolicy(Contentstack.CachePolicy.NETWORK_ELSE_CACHE);
90+
// get all the entries
91+
allEntries("news")
92+
93+
// get all the assets
94+
// getAsset();
95+
}
96+
}
97+
}());

0 commit comments

Comments
 (0)