mirror of
https://github.com/ArthurDanjou/arthome.git
synced 2026-01-25 09:00:29 +01:00
Add weather
This commit is contained in:
@@ -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
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user