commit
Add delete route
diff --git a/src/docs.ts b/src/docs.ts
index 96beacd..72ba8e9 100644
--- a/src/docs.ts
+++ b/src/docs.ts
@@ -215,7 +215,7 @@ var GROUPS = [
{ m: "GET", path: "Bearer JWT", auth: "jwt",
desc: "Most /plural write endpoints need a login token. Get one from POST /plural/login, then send it as 'Authorization: Bearer <token>'. Tokens last 24 hours. Some routes additionally require an admin, owner, or pet role." },
{ m: "GET", path: "X-Battery-Key", auth: "key",
- desc: "Reporting device state (POST /devices) needs your device key, sent as the 'X-Battery-Key' header." },
+ desc: "Reporting or deleting device state (POST/DELETE /devices) needs your device key, sent as the 'X-Battery-Key' header." },
{ m: "GET", path: "Bot token", auth: "bot",
desc: "The /plural/bot/* endpoints are for the companion Discord bot: they need both a 'User-Agent: CloveShortcuts/<version>' header and the bot's token as 'Authorization: Bearer <token>'." }
]
@@ -390,7 +390,10 @@ var GROUPS = [
{ m: "GET", path: "/devices/:device", auth: "public", desc: "One device, or 404." },
{ m: "POST", path: "/devices?device=iphone&level=25&charging=1&lpm=0&wifi=Home", auth: "key",
desc: "Report device state. Only 'device' is required; supplied fields are updated, the rest untouched. Send the X-Battery-Key header.",
- params: [["device", "1–64 chars (required)."], ["level", "Optional. Integer 0–100."], ["charging", "Optional. 1 (true) or 0 (false)."], ["lpm", "Optional. 1 (true) or 0 (false) → lowPowerMode."], ["wifi", "Optional. Any string (network name), ≤128 chars."]] }
+ params: [["device", "1–64 chars (required)."], ["level", "Optional. Integer 0–100."], ["charging", "Optional. 1 (true) or 0 (false)."], ["lpm", "Optional. 1 (true) or 0 (false) → lowPowerMode."], ["wifi", "Optional. Any string (network name), ≤128 chars."]] },
+ { m: "DELETE", path: "/devices?device=iphone", auth: "key",
+ desc: "Delete a device's state. Returns 404 if the device doesn't exist. Send the X-Battery-Key header.",
+ params: [["device", "1–64 chars (required)."]] }
]
},
{
diff --git a/src/system/routes/devices.ts b/src/system/routes/devices.ts
index 6dcf0b4..7bff261 100644
--- a/src/system/routes/devices.ts
+++ b/src/system/routes/devices.ts
@@ -7,8 +7,9 @@
/**
* Device state. Mounted at /v2/devices.
* POST /v2/devices?device=iphone&level=25&charging=1&lpm=0 (X-Battery-Key required)
- * GET /v2/devices (public, all devices)
- * GET /v2/devices/:device (public, one device)
+ * GET /v2/devices (public, all devices)
+ * GET /v2/devices/:device (public, one device)
+ * DELETE /v2/devices?device=iphone (X-Battery-Key required)
*
* Only `device` is required on POST. `level`, `charging` and `lpm` are each
* optional — any supplied field is updated, the rest are left untouched.
@@ -21,7 +22,7 @@
import { Hono } from "hono";
import type { Env } from "../hono";
-import { getAllLevels, getDeviceLevel, setDeviceLevel } from "../services/devices";
+import { deleteDevice, getAllLevels, getDeviceLevel, setDeviceLevel } from "../services/devices";
import { verifyBatteryAccess } from "../middleware/auth";
export const deviceRoutes = new Hono<Env>();
@@ -75,6 +76,19 @@ deviceRoutes.post("/", verifyBatteryAccess, async (c) => {
return c.json({ success: true, ...record });
});
+deviceRoutes.delete("/", verifyBatteryAccess, async (c) => {
+ const device = c.req.query("device");
+ if (typeof device !== "string" || device.length < 1 || device.length > 64) {
+ return c.json({ detail: "Query param 'device' must be a string between 1 and 64 characters" }, 422);
+ }
+
+ const deleted = await deleteDevice(device);
+ if (!deleted) {
+ return c.json({ detail: `No state recorded for device '${device}'` }, 404);
+ }
+ return c.json({ success: true, device, deleted: true });
+});
+
deviceRoutes.get("/", async (c) => {
const levels = await getAllLevels();
const result: Record<string, unknown> = {};
diff --git a/src/system/services/devices.ts b/src/system/services/devices.ts
index 1520e2a..668f11a 100644
--- a/src/system/services/devices.ts
+++ b/src/system/services/devices.ts
@@ -57,3 +57,12 @@ export async function setDeviceLevel(
await saveAllLevels(levels);
return { device, ...record };
}
+
+/** Remove a device's record. Returns true if it existed and was deleted. */
+export async function deleteDevice(device: string): Promise<boolean> {
+ const levels = await getAllLevels();
+ if (!(device in levels)) return false;
+ delete levels[device];
+ await saveAllLevels(levels);
+ return true;
+}