summaryrefslogtreecommitdiff
path: root/client/api.js
diff options
context:
space:
mode:
authorJoel Klinghed <the_jk@spawned.biz>2025-10-23 00:20:35 +0200
committerJoel Klinghed <the_jk@spawned.biz>2025-10-23 00:20:35 +0200
commitbb8ef2203469e949700499499e101354dfb1fe1f (patch)
treea9f68f89fd84a6ce6a5c04e558cc962d520604b6 /client/api.js
parent2d7b7c84cd843b75ca7131549f99d0671dfe3268 (diff)
client: Add very basic client
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);
+ },
+};