feat: Add fuel API Routes

Add 3 API Routes to interact with tankerkoenig API for getting fuel prices (along with its server capability)
This commit is contained in:
Cfp
2025-06-20 17:33:55 +02:00
parent 70516441f2
commit e1376ad88e

View File

@ -62,6 +62,10 @@ app.get("/api/config", (c) => {
capabilities.push("ai"); capabilities.push("ai");
} }
if(process.env.TANKERKOENIG_API_KEY) {
capabilities.push("fuel");
}
return c.json({ return c.json({
name: "TrafficCue Server", name: "TrafficCue Server",
version: "0", version: "0",
@ -118,6 +122,45 @@ app.post("/api/review", async (c) => {
return c.json(res.rows[0]); 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<string, unknown>);
});
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<string, unknown>);
});
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<string, unknown>);
});
}
if(process.env.GOOGLE_GENERATIVE_AI_API_KEY) { if(process.env.GOOGLE_GENERATIVE_AI_API_KEY) {
app.use("/api/ai", rateLimiter({ app.use("/api/ai", rateLimiter({
windowMs: 60 * 1000, // 1 minute windowMs: 60 * 1000, // 1 minute