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
|
async function check_json(response) {
if (!response.ok) {
throw Error("Bad response");
}
return await response.json();
}
async function check_status(response) {
if (!response.ok) {
throw Error("Bad response");
}
const data = await response.json();
if (data.status !== "OK") {
throw Error(data.message || "Error");
}
return true;
}
export const api = {
status: async function () {
const response = await fetch("api/v1/status");
return await check_status(response);
},
controller: async function () {
const response = await fetch("api/v1/controller");
return await check_json(response);
},
controller_discoverable: async function (body) {
const response = await fetch("api/v1/controller/discoverable", {
method: "POST",
body: body,
});
return await check_status(response);
},
device: async function (address) {
const response = await fetch(
`api/v1/device/${encodeURIComponent(address)}`,
);
return await check_json(response);
},
device_action: async function (address, action) {
const response = await fetch(
`api/v1/device/${encodeURIComponent(address)}`,
{
method: "POST",
body: action,
},
);
return await check_status(response);
},
};
|