Add weather

This commit is contained in:
2024-09-04 22:30:58 +02:00
parent 7ca64c61f5
commit 043757bffd
6 changed files with 125 additions and 14 deletions

View File

@@ -1,23 +1,36 @@
import type { OpenWeatherType } from '~~/types/types'
import type { OpenWeatherType, WeatherType } from '~~/types/types'
export default defineCachedEventHandler(async (event) => {
export default defineEventHandler(async (event) => {
const config = useRuntimeConfig(event)
const { user } = await requireUserSession(event)
const query = getQuery(event)
if (Number(query.lon) === Infinity || Number(query.lat) === Infinity) {
return createError('Invalid coordinates')
}
const openWeather = await $fetch<OpenWeatherType>('https://api.openweathermap.org/data/2.5/weather', {
params: {
lat: config.openWeather.lat,
lon: config.openWeather.lon,
lon: query.lon,
lat: query.lat,
appid: config.openWeather.apiKey,
lang: config.openWeather.lang,
lang: user.language.split('-')[0] ?? 'en',
units: config.openWeather.units,
},
})
return {
weather: openWeather.weather[0].description,
weather: {
type: openWeather.weather[0].main,
description: openWeather.weather[0].description,
},
city: openWeather.name,
temp: openWeather.main.feels_like,
}
}, {
maxAge: 60 * 60, // 1 hour
name: 'weather',
temp: {
feels_like: openWeather.main.feels_like,
min: openWeather.main.temp_min,
max: openWeather.main.temp_max,
humidity: openWeather.main.humidity,
},
wind: openWeather.wind.speed,
} as WeatherType
})