Files
homepage/src/pages/api/widgets/weather.js
shamoon eda06965fa
Some checks failed
Docker CI / Linting Checks (push) Has been cancelled
Docker CI / Docker Build & Push (push) Has been cancelled
Crowdin Action / Crowdin Sync (push) Has been cancelled
Chore: add organize imports to pre-commit (#5104)
2025-03-30 21:40:03 -07:00

31 lines
1.0 KiB
JavaScript

import { getSettings } from "utils/config/config";
import { getPrivateWidgetOptions } from "utils/config/widget-helpers";
import { cachedRequest } from "utils/proxy/http";
export default async function handler(req, res) {
const { latitude, longitude, provider, cache, lang, index } = req.query;
const privateWidgetOptions = await getPrivateWidgetOptions("weatherapi", index);
let { apiKey } = privateWidgetOptions;
if (!apiKey && !provider) {
return res.status(400).json({ error: "Missing API key or provider" });
}
if (!apiKey && provider !== "weatherapi") {
return res.status(400).json({ error: "Invalid provider for endpoint" });
}
if (!apiKey && provider) {
const settings = getSettings();
apiKey = settings?.providers?.weatherapi;
}
if (!apiKey) {
return res.status(400).json({ error: "Missing API key" });
}
const apiUrl = `http://api.weatherapi.com/v1/current.json?q=${latitude},${longitude}&key=${apiKey}&lang=${lang}`;
return res.send(await cachedRequest(apiUrl, cache));
}