import { msDay } from "https://esm.town/v/stevekrouse/msDay";
import { fetchJSON } from "https://esm.town/v/stevekrouse/fetchJSON";
export let aqiLocation = async ({ lat, lon }: { lat: number; lon: number }) => {
const { results } = fetchJSON(
"https://api.openaq.org/v2/locations?" +
new URLSearchParams({
limit: "10",
page: "1",
offset: "0",
sort: "asc",
radius: "10000",
order_by: "distance",
dumpRaw: "false",
coordinates: lat.toPrecision(8) + "," + lon.toPrecision(8),
entity: "government",
parameter_id: 2,
})
);
return results.find(
(location) =>
new Date() -
new Date(
location.parameters.find((p) => p.parameter === "pm25").lastUpdated
) <
msDay
);
};