From bb8ef2203469e949700499499e101354dfb1fe1f Mon Sep 17 00:00:00 2001 From: Joel Klinghed Date: Thu, 23 Oct 2025 00:20:35 +0200 Subject: client: Add very basic client --- client/api.js | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 client/api.js (limited to 'client/api.js') 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); + }, +}; -- cgit v1.2.3-70-g09d2