···
import { Hono } from "hono"
import { validator } from "hono/validator"
+
import { tryCatch } from "./util.ts"
const SensorsSchema = z.object({
···
+
const ReverseGeocodingSchema = z.object({
+
type: z.literal("FeatureCollection"),
+
type: z.literal("Feature"),
+
sourcename: z.string(),
+
attribution: z.string(),
+
name: z.string().optional(),
+
country_code: z.string(),
+
state: z.string().optional(),
+
city: z.string().optional(),
+
postcode: z.string().optional(),
+
district: z.string().optional(),
+
suburb: z.string().optional(),
+
street: z.string().optional(),
+
housenumber: z.string().optional(),
+
iso3166_2: z.string().optional(),
+
state_code: z.string().optional(),
+
distance: z.number().optional(),
+
result_type: z.string().optional(),
+
address_line1: z.string().optional(),
+
address_line2: z.string().optional(),
+
category: z.string().optional(),
+
offset_STD: z.string(),
+
offset_STD_seconds: z.number(),
+
offset_DST: z.string(),
+
offset_DST_seconds: z.number(),
+
abbreviation_STD: z.string(),
+
abbreviation_DST: z.string(),
+
plus_code: z.string().optional(),
+
importance: z.number(),
+
popularity: z.number(),
+
place_id: z.string().optional(),
+
type: z.literal("Point"),
+
coordinates: z.tuple([z.number(), z.number()]),
+
bbox: z.array(z.number()).optional(),
+
plus_code: z.string().optional(),
type Sensors = z.infer<typeof SensorsSchema>
const kv = await Deno.openKv()
const sensors = new Hono()
+
.get("/country", async (c) => {
+
const data = await kv.get<Sensors>(["sensors", "latest"])
+
return c.text("No data found", 404)
+
const location = data.value.sensorData.find((sensor) =>
+
sensor.sensorType === "location"
+
return c.text("No location data found", 404)
+
const geocode = await tryCatch(
+
`https://api.geoapify.com/v1/geocode/reverse?lat=${location.latitude}&lon=${location.longitude}&apiKey=${
+
Deno.env.get("GEOAPIFY_API_KEY")
+
.then((res) => res.json())
+
.then((data) => data.features[0].properties.country_code),
+
if (!geocode.success) {
+
console.error(geocode.error)
+
return c.text("Geoapify API error", 404)
+
const country = ReverseGeocodingSchema.safeParse(geocode.value)
+
if (!country.success) {
+
console.error(country.error)
+
return c.text("Invalid country data", 400)
+
country: country.data.features[0].properties.country,
+
country_code: country.data.features[0].properties.country_code,
.get("/get", async (c) => {
const data = await kv.get<Sensors>(["sensors", "latest"])