mirror of
https://github.com/gethomepage/homepage.git
synced 2025-12-07 01:26:01 -08:00
31 lines
1.1 KiB
JavaScript
31 lines
1.1 KiB
JavaScript
import { cachedRequest } from "utils/proxy/http";
|
|
import { getSettings } from "utils/config/config";
|
|
import { getPrivateWidgetOptions } from "utils/config/widget-helpers";
|
|
|
|
export default async function handler(req, res) {
|
|
const { latitude, longitude, units, provider, cache, lang, index } = req.query;
|
|
const privateWidgetOptions = await getPrivateWidgetOptions("openweathermap", index);
|
|
let { apiKey } = privateWidgetOptions;
|
|
|
|
if (!apiKey && !provider) {
|
|
return res.status(400).json({ error: "Missing API key or provider" });
|
|
}
|
|
|
|
if (!apiKey && provider !== "openweathermap") {
|
|
return res.status(400).json({ error: "Invalid provider for endpoint" });
|
|
}
|
|
|
|
if (!apiKey && provider) {
|
|
const settings = getSettings();
|
|
apiKey = settings?.providers?.openweathermap;
|
|
}
|
|
|
|
if (!apiKey) {
|
|
return res.status(400).json({ error: "Missing API key" });
|
|
}
|
|
|
|
const apiUrl = `https://api.openweathermap.org/data/2.5/weather?lat=${latitude}&lon=${longitude}&appid=${apiKey}&units=${units}&lang=${lang}`;
|
|
|
|
return res.send(await cachedRequest(apiUrl, cache));
|
|
}
|