forked from Enngage/KenticoKontentSampleJavascriptApp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cloud-api.js
77 lines (64 loc) · 1.97 KB
/
cloud-api.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
var kc = require('kentico-cloud-delivery-typescript-sdk');
var apiUrl = 'https://deliver.kenticocloud.com';
var projectId = 'da5abe9f-fdad-4168-97cd-b3464be2ccb9';
// no properties are necessary
class Movie extends kc.ContentItem {
constructor() {
super({
urlSlugResolver: (item, urlSlug) => {
return 'someurl/movie/' + urlSlug;
}
})
}
getCategoriesText() {
if (!this.category) {
return null;
}
return this.category.options.map(m => m.name.toLocaleLowerCase()).join(', ');
}
getStarsText() {
if (!this.stars) {
return null;
}
return this.stars.map(actor => actor.getFullName()).join(', ');
}
}
class Actor extends kc.ContentItem {
constructor() {
super({
resolver: (fieldName) => {
if (fieldName === 'first_name') {
return 'firstName'; // binds 'first_name' response from Kentico cloud to 'firstName' property
}
if (fieldName === 'last_name') {
return 'lastName';
}
},
urlSlugResolver: (item, urlSlug) => {
return 'someurl/actor/' + urlSlug;
}
})
}
}
// configure type resolvers
var typeResolvers = [
new kc.TypeResolver('movie', () => new Movie()),
new kc.TypeResolver('actor', () => new Actor()),
];
var config = new kc.DeliveryClientConfig(projectId, typeResolvers)
// instantiate delivery client
var deliveryClient = new kc.DeliveryClient(config);
// use it
deliveryClient.items()
.limitParameter(10)
.orderParameter('system.codename', kc.SortOrder.desc)
.depthParameter(5)
.get()
.subscribe(response => console.log(response));
deliveryClient.item('tom_hardy')
.get()
.subscribe(response => console.log(response));
deliveryClient.items()
.type('movie')
.get()
.subscribe(response => console.log(response));