-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
144 lines (108 loc) · 3.48 KB
/
index.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
const CONFIG = {
url: process.env.VUE_APP_SQUIDEX_URL,
appName: process.env.VUE_APP_SQUIDEX_APPNAME,
clientId: process.env.VUE_APP_SQUIDEX_CLIENTID,
clientSecret: process.env.VUE_APP_SQUIDEX_CLIENTSECRET
};
function getBearerToken() {
return localStorage.getItem('token');
}
function setBearerToken(token) {
localStorage.setItem('token', token);
}
function clearBearerToken() {
localStorage.removeItem('token');
}
export async function getPost(id) {
const json = await getContent(`api/content/${CONFIG.appName}/posts/${id}`);
return parsePost(json);
}
export async function getPage(slug) {
const json = await getContent(`api/content/${CONFIG.appName}/pages/?$filter=data/slug/iv eq '${slug}'`);
const { items } = json;
if (items.length === 0) {
return null;
}
return parsePage(items[0]);
}
export async function getPosts() {
const json = await getContent(`api/content/${CONFIG.appName}/posts`);
const { total, items } = json;
return { total, posts: items.map(x => parsePost(x)) };
}
export async function getPages() {
const json = await getContent(`api/content/${CONFIG.appName}/pages`);
const { total, items } = json;
return { total, pages: items.map(x => parsePage(x)) };
}
function parsePost(response) {
return {
id: response.id,
title: response.data.title.iv,
text: response.data.text.iv,
slug: response.data.slug.iv
};
}
function parsePage(response) {
return {
id: response.id,
title: response.data.title.iv,
text: response.data.text.iv,
slug: response.data.slug.iv
};
}
export async function fetchBearerToken() {
// Check if we have already a bearer token in local store.
let token = getBearerToken();
if (token) {
return token;
}
const body = `grant_type=client_credentials&scope=squidex-api&client_id=${CONFIG.clientId}&client_secret=${CONFIG.clientSecret}`;
// Get the bearer token. Ensure that we use a client id with readonly permissions.
const response = await fetch(buildUrl('identity-server/connect/token'), {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body
});
if (!response.ok) {
throw new Error(`Failed to retrieve token, got ${response.statusText}`);
}
const json = await response.json();
token = json.access_token;
// Cache the bearer token in the local store.
setBearerToken(token);
return token;
}
function getContent(url) {
return getContentInternal(url, true);
}
async function getContentInternal(url, retry) {
// Fetch the bearer token.
const token = await fetchBearerToken();
const response = await fetch(buildUrl(url), {
headers: {
'Accept': 'application/json',
'Authorization': `Bearer ${token}`
}
});
if (!response.ok) {
if (response.status === 403 || response.status === 401) {
// If we get an error we clear the bearer token and retry the request.
clearBearerToken();
if (retry) {
return getContentInternal(url);
}
}
throw new Error(`Failed to retrieve content, got ${response.statusText}`);
}
return await response.json();
}
function buildUrl(url) {
if (url.length > 0 && url.startsWith('/')) {
url = url.substr(1);
}
const result = `${CONFIG.url}/${url}`;
return result;
}