66 lines
2.8 KiB
TypeScript
66 lines
2.8 KiB
TypeScript
import { NextResponse } from "next/server";
|
|
import { mergeForecastSources } from "@/lib/forecast-merge";
|
|
import type { RawImgwForecastResponse, RawWeatherForecast } from "@/types/forecast";
|
|
|
|
const OPEN_METEO_FORECAST_URL = "https://api.open-meteo.com/v1/forecast";
|
|
const IMGW_FORECAST_URL = "https://meteo.imgw.pl/api/v1/forecast/fcapi";
|
|
// This browser token is published by the official meteo.imgw.pl frontend.
|
|
const IMGW_FORECAST_TOKEN = "p4DXKjsYadfBV21TYrDk";
|
|
const OPEN_METEO_TIMEOUT_MS = 12_000;
|
|
const IMGW_TIMEOUT_MS = 5_000;
|
|
|
|
function parseCoordinate(value: string | null, min: number, max: number) {
|
|
if (!value?.trim()) return null;
|
|
const coordinate = Number(value);
|
|
return Number.isFinite(coordinate) && coordinate >= min && coordinate <= max ? coordinate : null;
|
|
}
|
|
|
|
async function readImgwPayload(response: Response | null) {
|
|
if (!response?.ok) return null;
|
|
try {
|
|
return await response.json() as RawImgwForecastResponse;
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
export async function GET(request: Request) {
|
|
const { searchParams } = new URL(request.url);
|
|
const latitude = parseCoordinate(searchParams.get("latitude"), -90, 90);
|
|
const longitude = parseCoordinate(searchParams.get("longitude"), -180, 180);
|
|
if (latitude === null || longitude === null) {
|
|
return NextResponse.json({ error: "Invalid coordinates." }, { status: 400 });
|
|
}
|
|
|
|
const openMeteoParams = new URLSearchParams({
|
|
latitude: String(latitude),
|
|
longitude: String(longitude),
|
|
hourly: "temperature_2m,apparent_temperature,precipitation_probability,precipitation,weather_code,wind_speed_10m",
|
|
daily: "weather_code,temperature_2m_max,temperature_2m_min,precipitation_probability_max,precipitation_sum,sunrise,sunset",
|
|
timezone: "Europe/Warsaw",
|
|
forecast_days: "7",
|
|
wind_speed_unit: "ms",
|
|
});
|
|
const imgwParams = new URLSearchParams({
|
|
token: IMGW_FORECAST_TOKEN,
|
|
lat: String(latitude),
|
|
lon: String(longitude),
|
|
m: "alaro",
|
|
});
|
|
|
|
try {
|
|
const [openMeteoResponse, imgwResponse] = await Promise.all([
|
|
fetch(`${OPEN_METEO_FORECAST_URL}?${openMeteoParams}`, { next: { revalidate: 900 }, signal: AbortSignal.timeout(OPEN_METEO_TIMEOUT_MS) }),
|
|
fetch(`${IMGW_FORECAST_URL}?${imgwParams}`, { next: { revalidate: 900 }, signal: AbortSignal.timeout(IMGW_TIMEOUT_MS) }).catch(() => null),
|
|
]);
|
|
if (!openMeteoResponse.ok) return NextResponse.json({ error: "Forecast service is unavailable." }, { status: 502 });
|
|
const openMeteoPayload = await openMeteoResponse.json() as RawWeatherForecast;
|
|
const imgwPayload = await readImgwPayload(imgwResponse);
|
|
return NextResponse.json(mergeForecastSources(openMeteoPayload, imgwPayload), {
|
|
headers: { "Cache-Control": "public, s-maxage=900, stale-while-revalidate=1800" },
|
|
});
|
|
} catch {
|
|
return NextResponse.json({ error: "Forecast service is unavailable." }, { status: 502 });
|
|
}
|
|
}
|