Files
wtr/app/api/forecast/route.ts
zv 2182297adc
All checks were successful
CI / Lint, typecheck and build (push) Successful in 9m54s
feat: add global weather support
2026-06-14 15:59:14 +02:00

26 lines
1.0 KiB
TypeScript

import { NextResponse } from "next/server";
import { fetchServerForecast, parseForecastCoordinate } from "@/lib/server-forecast";
import type { WeatherRegion } from "@/types/weather-region";
function parseRegion(value: string | null): WeatherRegion {
return value === "GLOBAL" ? "GLOBAL" : "PL";
}
export async function GET(request: Request) {
const { searchParams } = new URL(request.url);
const latitude = parseForecastCoordinate(searchParams.get("latitude"), -90, 90);
const longitude = parseForecastCoordinate(searchParams.get("longitude"), -180, 180);
const region = parseRegion(searchParams.get("region"));
if (latitude === null || longitude === null) {
return NextResponse.json({ error: "Invalid coordinates." }, { status: 400 });
}
try {
return NextResponse.json(await fetchServerForecast(latitude, longitude, region), {
headers: { "Cache-Control": "public, s-maxage=900, stale-while-revalidate=1800" },
});
} catch {
return NextResponse.json({ error: "Forecast service is unavailable." }, { status: 502 });
}
}