Add Wiener Linien API integration and departures monitoring

This commit is contained in:
2026-05-10 17:19:24 +02:00
parent c0f34b9e2a
commit 7901368971
20 changed files with 1746 additions and 197 deletions
+37
View File
@@ -0,0 +1,37 @@
import { NextRequest, NextResponse } from "next/server";
import { randomUUID } from "crypto";
import { WienerLinienClient } from "@/lib/wienerlinien-client";
const client = new WienerLinienClient();
export async function GET(request: NextRequest) {
const { searchParams } = new URL(request.url);
const stopIdsParam = searchParams.get("stopIds");
if (!stopIdsParam || stopIdsParam.trim() === "") {
return NextResponse.json({ error: "Missing 'stopIds' query parameter" }, { status: 400 });
}
const rawIds = stopIdsParam.split(",");
const validStopIds = rawIds
.map((id) => id.trim())
.filter((id) => id.length > 0)
.filter((id, index, self) => self.indexOf(id) === index);
if (validStopIds.length === 0) {
return NextResponse.json({ error: "No valid stop IDs provided" }, { status: 400 });
}
const cappedIds = validStopIds.slice(0, 10);
try {
const monitorData = await client.getMonitor(cappedIds);
// Flatten nested stops array into a single departures list
const departures = monitorData.stops.flatMap((s) => s.departures);
return NextResponse.json({ departures });
} catch (error) {
const corrId = randomUUID().slice(0, 8);
console.error(`[${corrId}] Wiener Linien monitor request failed:`, error);
return NextResponse.json({ error: "Failed to fetch departures", correlationId: corrId }, { status: 500 });
}
}