2022-09-26 12:04:37 +03:00
|
|
|
import cachedFetch from "utils/proxy/cached-fetch";
|
2022-09-26 15:25:10 +03:00
|
|
|
import { getSettings } from "utils/config/config";
|
2024-06-05 19:38:26 -07:00
|
|
|
import { getPrivateWidgetOptions } from "utils/config/widget-helpers";
|
2022-08-24 10:44:35 +03:00
|
|
|
|
|
|
|
export default async function handler(req, res) {
|
2024-06-05 19:38:26 -07:00
|
|
|
const { latitude, longitude, provider, cache, lang, index } = req.query;
|
|
|
|
const privateWidgetOptions = await getPrivateWidgetOptions("weatherapi", index);
|
|
|
|
let { apiKey } = privateWidgetOptions;
|
2022-08-24 10:44:35 +03:00
|
|
|
|
2022-09-05 20:14:14 +03:00
|
|
|
if (!apiKey && !provider) {
|
|
|
|
return res.status(400).json({ error: "Missing API key or provider" });
|
|
|
|
}
|
2022-08-24 10:44:35 +03:00
|
|
|
|
2022-09-05 20:14:14 +03:00
|
|
|
if (!apiKey && provider !== "weatherapi") {
|
|
|
|
return res.status(400).json({ error: "Invalid provider for endpoint" });
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!apiKey && provider) {
|
2022-09-18 16:41:01 +03:00
|
|
|
const settings = getSettings();
|
2022-09-05 20:14:14 +03:00
|
|
|
apiKey = settings?.providers?.weatherapi;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!apiKey) {
|
|
|
|
return res.status(400).json({ error: "Missing API key" });
|
|
|
|
}
|
|
|
|
|
2022-09-08 11:48:16 +03:00
|
|
|
const apiUrl = `http://api.weatherapi.com/v1/current.json?q=${latitude},${longitude}&key=${apiKey}&lang=${lang}`;
|
2022-09-05 20:14:14 +03:00
|
|
|
|
2022-09-07 16:53:24 +03:00
|
|
|
return res.send(await cachedFetch(apiUrl, cache));
|
2022-08-24 10:44:35 +03:00
|
|
|
}
|