38 lines
1.1 KiB
TypeScript
38 lines
1.1 KiB
TypeScript
import { Router } from 'express'
|
|
import axios from 'axios'
|
|
|
|
const router = Router()
|
|
|
|
router.get('/current', async (_req, res) => {
|
|
try {
|
|
const location = process.env.WEATHER_LOCATION
|
|
if (!location) {
|
|
res.status(503).json({ error: 'WEATHER_LOCATION not configured' })
|
|
return
|
|
}
|
|
|
|
const response = await axios.get(
|
|
`https://wttr.in/${encodeURIComponent(location)}?format=j1`,
|
|
{ headers: { 'Accept': 'application/json' }, timeout: 8000 }
|
|
)
|
|
|
|
const c = response.data.current_condition[0]
|
|
const area = response.data.nearest_area?.[0]
|
|
|
|
res.json({
|
|
tempC: Number(c.temp_C),
|
|
feelsLikeC: Number(c.FeelsLikeC),
|
|
humidity: Number(c.humidity),
|
|
windKmph: Number(c.windspeedKmph),
|
|
desc: c.weatherDesc[0].value as string,
|
|
code: Number(c.weatherCode),
|
|
city: area?.areaName?.[0]?.value ?? location,
|
|
})
|
|
} catch (err: unknown) {
|
|
const msg = err instanceof Error ? err.message : 'Unknown error'
|
|
res.status(500).json({ error: msg })
|
|
}
|
|
})
|
|
|
|
export default router
|