-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathclient.js
More file actions
44 lines (39 loc) · 1.08 KB
/
client.js
File metadata and controls
44 lines (39 loc) · 1.08 KB
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
'use strict';
const buildAPI = (methods) => {
const api = {};
for (const method of methods) {
api[method] = (...args) =>
new Promise((resolve, reject) => {
const url = `/api/${method}`;
console.log(url, args);
fetch(url, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(args),
}).then((res) => {
const { status } = res;
if (status !== 200) {
reject(new Error(`Status Code: ${status}`));
return;
}
resolve(res.json());
});
});
}
return api;
};
const api = buildAPI(['rect', 'move', 'rotate', 'read', 'render', 'resize']);
const show = async () => {
const svg = await api.render('Rect1');
const output = document.getElementById('output');
output.innerHTML = svg;
};
const scenario = async () => {
await api.rect('Rect1', -10, 10, 10, -10);
await api.move('Rect1', 5, 5);
await api.rotate('Rect1', 5);
const data = await api.read('Rect1');
console.dir({ data });
await show();
};
scenario();