homepage/src/pages/api/widgets/openweathermap.js

29 lines
908 B
JavaScript
Raw Normal View History

import cachedFetch from "utils/proxy/cached-fetch";
2022-09-26 15:25:10 +03:00
import { getSettings } from "utils/config/config";
2022-08-27 13:30:17 +03:00
export default async function handler(req, res) {
2022-09-08 11:48:16 +03:00
const { latitude, longitude, units, provider, cache, lang } = req.query;
let { apiKey } = req.query;
2022-08-27 13:30:17 +03:00
if (!apiKey && !provider) {
return res.status(400).json({ error: "Missing API key or provider" });
}
2022-08-27 13:30:17 +03:00
if (!apiKey && provider !== "openweathermap") {
return res.status(400).json({ error: "Invalid provider for endpoint" });
}
if (!apiKey && provider) {
2022-09-18 16:41:01 +03:00
const settings = getSettings();
apiKey = settings?.providers?.openweathermap;
}
if (!apiKey) {
return res.status(400).json({ error: "Missing API key" });
}
2022-09-08 11:48:16 +03:00
const apiUrl = `https://api.openweathermap.org/data/2.5/weather?lat=${latitude}&lon=${longitude}&appid=${apiKey}&units=${units}&lang=${lang}`;
2022-09-07 16:53:24 +03:00
return res.send(await cachedFetch(apiUrl, cache));
2022-08-27 13:30:17 +03:00
}