2022-08-24 10:44:35 +03:00
|
|
|
import cachedFetch from "utils/cached-fetch";
|
2022-09-05 20:14:14 +03:00
|
|
|
import { getSettings } from "utils/config";
|
2022-08-24 10:44:35 +03:00
|
|
|
|
|
|
|
export default async function handler(req, res) {
|
2022-09-05 20:14:14 +03:00
|
|
|
const { latitude, longitude, provider, cache } = req.query;
|
|
|
|
let { apiKey } = req.query;
|
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) {
|
|
|
|
const settings = await getSettings();
|
|
|
|
apiKey = settings?.providers?.weatherapi;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!apiKey) {
|
|
|
|
return res.status(400).json({ error: "Missing API key" });
|
|
|
|
}
|
|
|
|
|
|
|
|
const api_url = `http://api.weatherapi.com/v1/current.json?q=${latitude},${longitude}&key=${apiKey}`;
|
|
|
|
|
|
|
|
res.send(await cachedFetch(api_url, cache));
|
2022-08-24 10:44:35 +03:00
|
|
|
}
|