From 4b31edd310b6f9ca5fa2452f4b24570e92dd58cf Mon Sep 17 00:00:00 2001 From: Darius Date: Tue, 18 Nov 2025 22:08:00 +0100 Subject: [PATCH] change for api route --- src/index.ts | 187 ++++++++++++++++++++++++++------------------------ test/api.http | 18 ++--- 2 files changed, 105 insertions(+), 100 deletions(-) diff --git a/src/index.ts b/src/index.ts index 6242778..34eb894 100644 --- a/src/index.ts +++ b/src/index.ts @@ -37,105 +37,110 @@ const haService = new HomeAssistantService(haClient); const tidalClient = new TidalClient(server.axios.tidal); const tidalService = new TidalService(tidalClient); -// HOME ASSISTANT -server.get("/homeassistant/desk/position", async (_request, reply) => { - const result = await haService.getDeskPosition(); +server.register( + async (fastify) => { + // HOME ASSISTANT + fastify.get("/homeassistant/desk/position", async (_request, reply) => { + const result = await haService.getDeskPosition(); - if (!result) { - reply.code(500); - return { error: "Failed to get desk position" }; - } + if (!result) { + reply.code(500); + return { error: "Failed to get desk position" }; + } - return { - position: result.as_text(), - is_standing: result.as_boolean, - last_changed: result.last_changed.toReadable(true), - }; -}); + return { + position: result.as_text(), + is_standing: result.as_boolean, + last_changed: result.last_changed.toReadable(true), + }; + }); -server.post( - "/homeassistant/desk/stand", - { preHandler: verifyAPIKey }, - async (_request, reply) => { - const result = await haService.startStandingAutomation(); + fastify.post( + "/homeassistant/desk/stand", + { preHandler: verifyAPIKey }, + async (_request, reply) => { + const result = await haService.startStandingAutomation(); - if (!result) { - reply.code(500); - return { error: "Failed to get desk position" }; - } + if (!result) { + reply.code(500); + return { error: "Failed to get desk position" }; + } - return { result }; + return { result }; + }, + ); + + fastify.get("/homeassistant/temperature", async (_request, reply) => { + const result = await haService.getTemperatureText(); + + if (!result) { + reply.code(500); + return { error: "Failed to get desk position" }; + } + + return { + temperature: result, + }; + }); + + // TIDAL + fastify.get("/tidal/song", async (_request, reply) => { + const result = await tidalService.getSong(); + + if (!result) { + reply.code(500); + return { error: "Failed to get song" }; + } + + return { result }; + }); + + fastify.get("/tidal/songFormatted", async (_request, reply) => { + const result = await tidalService.getSongFormatted(); + + if (!result) { + reply.code(500); + return { error: "Failed to get song" }; + } + + return { result }; + }); + + fastify.get("/tidal/volume", async (_request, reply) => { + const result = await tidalService.getVolume(); + + if (!result) { + reply.code(500); + return { error: "Failed to get volume" }; + } + + return { result }; + }); + + fastify.post( + "/tidal/volume", + { preHandler: verifyAPIKey }, + async (request, reply) => { + const volume = request.body as string; + const result = await tidalService.setVolume(volume); + + if (!result) { + reply.code(500); + return { error: "Failed to set volume" }; + } + + return { result }; + }, + ); + + // Default + fastify.get("/ping", async (_request, _reply) => { + return "pong\n"; + }); }, + { prefix: "/api" }, ); -server.get("/homeassistant/temperature", async (_request, reply) => { - const result = await haService.getTemperatureText(); - - if (!result) { - reply.code(500); - return { error: "Failed to get desk position" }; - } - - return { - temperature: result, - }; -}); - -// TIDAL -server.get("/tidal/song", async (_request, reply) => { - const result = await tidalService.getSong(); - - if (!result) { - reply.code(500); - return { error: "Failed to get song" }; - } - - return { result }; -}); - -server.get("/tidal/songFormatted", async (_request, reply) => { - const result = await tidalService.getSongFormatted(); - - if (!result) { - reply.code(500); - return { error: "Failed to get song" }; - } - - return { result }; -}); - -server.get("/tidal/volume", async (_request, reply) => { - const result = await tidalService.getVolume(); - - if (!result) { - reply.code(500); - return { error: "Failed to get volume" }; - } - - return { result }; -}); - -server.post( - "/tidal/volume", - { preHandler: verifyAPIKey }, - async (request, reply) => { - const volume = request.body as string; - const result = await tidalService.setVolume(volume); - - if (!result) { - reply.code(500); - return { error: "Failed to set volume" }; - } - - return { result }; - }, -); - -// Default -server.get("/ping", async (_request, _reply) => { - return "pong\n"; -}); - server.listen({ port: 8080 }, (err, address) => { if (err) { console.error(err); diff --git a/test/api.http b/test/api.http index f8c7964..517c71a 100644 --- a/test/api.http +++ b/test/api.http @@ -1,48 +1,48 @@ ### Simple GET Request -GET http://localhost:8080/ping +GET http://localhost:8080/api/ping ### #################### HA ####################### ### Simple GET Desk Position -GET http://localhost:8080/homeassistant/desk/position +GET http://localhost:8080/api/homeassistant/desk/position ### ### Simple GET Stand Automation -GET http://localhost:8080/homeassistant/desk/stand +GET http://localhost:8080/api/homeassistant/desk/stand ### ### Simple POST Stand Automation -POST http://localhost:8080/homeassistant/desk/stand +POST http://localhost:8080/api/homeassistant/desk/stand ### ### Simple GET Temps -GET http://localhost:8080/homeassistant/temperature +GET http://localhost:8080/api/homeassistant/temperature ### #################### TIDAL ####################### ### Simple GET Song -GET http://localhost:8080/tidal/song +GET http://localhost:8080/api/tidal/song ### ### Simple GET Song Formatted -GET http://localhost:8080/tidal/songFormatted +GET http://localhost:8080/api/tidal/songFormatted ### ### Simple GET Volume -GET http://localhost:8080/tidal/volume +GET http://localhost:8080/api/tidal/volume ### ### Simple SET Volume -POST http://localhost:8080/tidal/volume +POST http://localhost:8080/api/tidal/volume ###