feat: add global weather support
All checks were successful
CI / Lint, typecheck and build (push) Successful in 9m54s
All checks were successful
CI / Lint, typecheck and build (push) Successful in 9m54s
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { mergeForecastSources } from "@/lib/forecast-merge";
|
||||
import type { RawImgwForecastResponse, RawWeatherForecast } from "@/types/forecast";
|
||||
import type { WeatherRegion } from "@/types/weather-region";
|
||||
|
||||
const OPEN_METEO_FORECAST_URL = "https://api.open-meteo.com/v1/forecast";
|
||||
const IMGW_FORECAST_URL = "https://meteo.imgw.pl/api/v1/forecast/fcapi";
|
||||
@@ -23,13 +24,13 @@ async function readImgwPayload(response: Response | null) {
|
||||
}
|
||||
}
|
||||
|
||||
export async function fetchServerForecast(latitude: number, longitude: number) {
|
||||
export async function fetchServerForecast(latitude: number, longitude: number, region: WeatherRegion = "PL") {
|
||||
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",
|
||||
timezone: region === "PL" ? "Europe/Warsaw" : "auto",
|
||||
forecast_days: "7",
|
||||
wind_speed_unit: "ms",
|
||||
});
|
||||
@@ -42,11 +43,13 @@ export async function fetchServerForecast(latitude: number, longitude: number) {
|
||||
|
||||
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),
|
||||
region === "PL"
|
||||
? fetch(`${IMGW_FORECAST_URL}?${imgwParams}`, { next: { revalidate: 900 }, signal: AbortSignal.timeout(IMGW_TIMEOUT_MS) }).catch(() => null)
|
||||
: Promise.resolve(null),
|
||||
]);
|
||||
if (!openMeteoResponse.ok) throw new Error("Forecast service is unavailable.");
|
||||
|
||||
const openMeteoPayload = await openMeteoResponse.json() as RawWeatherForecast;
|
||||
const imgwPayload = await readImgwPayload(imgwResponse);
|
||||
return mergeForecastSources(openMeteoPayload, imgwPayload);
|
||||
return mergeForecastSources(openMeteoPayload, imgwPayload, region);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user