summaryrefslogtreecommitdiff
path: root/client/api.js
diff options
context:
space:
mode:
Diffstat (limited to 'client/api.js')
-rw-r--r--client/api.js51
1 files changed, 51 insertions, 0 deletions
diff --git a/client/api.js b/client/api.js
new file mode 100644
index 0000000..04db29a
--- /dev/null
+++ b/client/api.js
@@ -0,0 +1,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);
+ },
+};