const FORMAT = { hourly: { temperature: { type: 'number', unit: '°C', }, precipitation: { type: 'number', unit: 'mm', }, precipitationProbability: { type: 'number', unit: '%', }, apparentTemperature: { type: 'number', unit: '°C', }, weatherCode: { type: 'number', unit: 'WMO code', }, }, daily: { minimumTemperature: { type: 'number', unit: '°C', }, maximumTemperature: { type: 'number', unit: '°C', }, minimumApparentTemperature: { type: 'number', unit: '°C', }, maximumApparentTemperature: { type: 'number', unit: '°C', }, precipitationSum: { type: 'number', unit: 'mm', }, weatherCode: { type: 'number', unit: 'WMO code', }, }, } const getDailyData = async () => { const response = await fetch( 'https://api.open-meteo.com/v1/forecast?latitude=43.7085&longitude=10.4036&hourly=temperature_2m,relative_humidity_2m,apparent_temperature,precipitation,precipitation_probability,weather_code&timezone=Europe%2FBerlin&forecast_days=3', ) const body = await response.json() const dataAt = (body, i) => { return { temperature: body.hourly.temperature_2m[i], precipitation: body.hourly.precipitation[i], precipitationProbability: body.hourly.precipitation_probability[i], apparentTemperature: body.hourly.apparent_temperature[i], weatherCode: body.hourly.weather_code[i], } } let today = {} const startTime = new Date().getHours() + 1 for (let i = startTime; i <= 23; i++) { today[i] = dataAt(body, i) } let tomorrow = {} for (let i = 0; i <= 23; i++) { tomorrow[i] = dataAt(body, 24 + i) } let dayAfterTomorrow = {} for (let i = 0; i <= 23; i++) { dayAfterTomorrow[i] = dataAt(body, 48 + i) } return { today, tomorrow, dayAfterTomorrow, } } const getWeekData = async () => { const response = await fetch( 'https://api.open-meteo.com/v1/forecast?latitude=43.7085&longitude=10.4036&daily=weather_code,temperature_2m_max,temperature_2m_min,apparent_temperature_max,apparent_temperature_min,precipitation_sum,precipitation_probability_max&timezone=Europe%2FBerlin', ) const body = await response.json() let week = {} for (let i = 0; i <= 6; i++) { week[i] = { minimumTemperature: body.daily.temperature_2m_min[i], maximumTemperature: body.daily.temperature_2m_max[i], minimumApparentTemperature: body.daily.apparent_temperature_min[i], maximumApparentTemperature: body.daily.apparent_temperature_max[i], precipitationSum: body.daily.precipitation_sum[i], precipitationProbabilityMax: body.daily.precipitation_probability_max[i], weatherCode: body.daily.weather_code[i], } } return week } export default async () => { const [daily, week] = await Promise.all([getDailyData(), getWeekData()]) return { format: FORMAT, ...daily, week, } }