mobile app phase 1
This commit is contained in:
@@ -1,36 +1 @@
|
||||
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 stopIdsList = searchParams.getAll("stopIds");
|
||||
if (stopIdsList.length === 0 || stopIdsList.every((v) => v.trim() === "")) {
|
||||
return NextResponse.json({ error: "Missing 'stopIds' query parameter" }, { status: 400 });
|
||||
}
|
||||
|
||||
const rawIds = stopIdsList.flatMap((param) => param.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 });
|
||||
}
|
||||
}
|
||||
../../../../lib/wienerlinien-client
|
||||
|
||||
Reference in New Issue
Block a user