From e1376ad88ed72ac0cb6bc14c9fd9bfe4de87c3c5 Mon Sep 17 00:00:00 2001 From: Cfp Date: Fri, 20 Jun 2025 17:33:55 +0200 Subject: [PATCH] feat: Add fuel API Routes Add 3 API Routes to interact with tankerkoenig API for getting fuel prices (along with its server capability) --- src/main.ts | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/src/main.ts b/src/main.ts index 60e0b8c..6194dd9 100644 --- a/src/main.ts +++ b/src/main.ts @@ -62,6 +62,10 @@ app.get("/api/config", (c) => { capabilities.push("ai"); } + if(process.env.TANKERKOENIG_API_KEY) { + capabilities.push("fuel"); + } + return c.json({ name: "TrafficCue Server", version: "0", @@ -118,6 +122,45 @@ app.post("/api/review", async (c) => { return c.json(res.rows[0]); }) +if(process.env.TANKERKOENIG_API_KEY) { + app.get("/api/fuel/list", async (c) => { + // pass GET query parameters to the tankerkoenig API + const params = new URLSearchParams(c.req.query()); + params.set("apikey", process.env.TANKERKOENIG_API_KEY!); + const url = `https://creativecommons.tankerkoenig.de/json/list.php?${params.toString()}`; + const response = await fetch(url); + if (!response.ok) { + return c.json({ error: "Failed to fetch fuel stations" }); + } + const data = await response.json(); + return c.json(data as Record); + }); + app.get("/api/fuel/prices", async (c) => { + // pass GET query parameters to the tankerkoenig API + const params = new URLSearchParams(c.req.query()); + params.set("apikey", process.env.TANKERKOENIG_API_KEY!); + const url = `https://creativecommons.tankerkoenig.de/json/prices.php?${params.toString()}`; + const response = await fetch(url); + if (!response.ok) { + return c.json({ error: "Failed to fetch fuel prices" }); + } + const data = await response.json(); + return c.json(data as Record); + }); + app.get("/api/fuel/detail", async (c) => { + // pass GET query parameters to the tankerkoenig API + const params = new URLSearchParams(c.req.query()); + params.set("apikey", process.env.TANKERKOENIG_API_KEY!); + const url = `https://creativecommons.tankerkoenig.de/json/detail.php?${params.toString()}`; + const response = await fetch(url); + if (!response.ok) { + return c.json({ error: "Failed to fetch station details" }); + } + const data = await response.json(); + return c.json(data as Record); + }); +} + if(process.env.GOOGLE_GENERATIVE_AI_API_KEY) { app.use("/api/ai", rateLimiter({ windowMs: 60 * 1000, // 1 minute