26 lines
1.0 KiB
TypeScript
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 });
|
|
}
|
|
}
|