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); }, };