feat: prefer IMGW ALARO forecast data
This commit is contained in:
@@ -1,6 +1,13 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { mergeForecastSources } from "@/lib/forecast-merge";
|
||||
import type { RawImgwForecastResponse, RawWeatherForecast } from "@/types/forecast";
|
||||
|
||||
const FORECAST_URL = "https://api.open-meteo.com/v1/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;
|
||||
@@ -8,6 +15,15 @@ function parseCoordinate(value: string | null, min: number, max: number) {
|
||||
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);
|
||||
@@ -16,7 +32,7 @@ export async function GET(request: Request) {
|
||||
return NextResponse.json({ error: "Invalid coordinates." }, { status: 400 });
|
||||
}
|
||||
|
||||
const params = new URLSearchParams({
|
||||
const openMeteoParams = new URLSearchParams({
|
||||
latitude: String(latitude),
|
||||
longitude: String(longitude),
|
||||
hourly: "temperature_2m,apparent_temperature,precipitation_probability,precipitation,weather_code,wind_speed_10m",
|
||||
@@ -25,11 +41,22 @@ export async function GET(request: Request) {
|
||||
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 response = await fetch(`${FORECAST_URL}?${params}`, { next: { revalidate: 900 } });
|
||||
if (!response.ok) return NextResponse.json({ error: "Forecast service is unavailable." }, { status: 502 });
|
||||
return NextResponse.json(await response.json(), {
|
||||
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 {
|
||||
|
||||
Reference in New Issue
Block a user