···
import { Hono } from "hono"
import { validator } from "hono/validator"
4
+
import { tryCatch } from "./util.ts"
const SensorsSchema = z.object({
···
67
+
const ReverseGeocodingSchema = z.object({
68
+
type: z.literal("FeatureCollection"),
71
+
type: z.literal("Feature"),
72
+
properties: z.object({
73
+
datasource: z.object({
74
+
sourcename: z.string(),
75
+
attribution: z.string(),
76
+
license: z.string(),
79
+
name: z.string().optional(),
80
+
country: z.string(),
81
+
country_code: z.string(),
82
+
state: z.string().optional(),
83
+
city: z.string().optional(),
84
+
postcode: z.string().optional(),
85
+
district: z.string().optional(),
86
+
suburb: z.string().optional(),
87
+
street: z.string().optional(),
88
+
housenumber: z.string().optional(),
89
+
iso3166_2: z.string().optional(),
92
+
state_code: z.string().optional(),
93
+
distance: z.number().optional(),
94
+
result_type: z.string().optional(),
95
+
formatted: z.string(),
96
+
address_line1: z.string().optional(),
97
+
address_line2: z.string().optional(),
98
+
category: z.string().optional(),
99
+
timezone: z.object({
101
+
offset_STD: z.string(),
102
+
offset_STD_seconds: z.number(),
103
+
offset_DST: z.string(),
104
+
offset_DST_seconds: z.number(),
105
+
abbreviation_STD: z.string(),
106
+
abbreviation_DST: z.string(),
108
+
plus_code: z.string().optional(),
110
+
importance: z.number(),
111
+
popularity: z.number(),
113
+
place_id: z.string().optional(),
115
+
geometry: z.object({
116
+
type: z.literal("Point"),
117
+
coordinates: z.tuple([z.number(), z.number()]),
119
+
bbox: z.array(z.number()).optional(),
125
+
plus_code: z.string().optional(),
type Sensors = z.infer<typeof SensorsSchema>
const kv = await Deno.openKv()
const sensors = new Hono()
134
+
.get("/country", async (c) => {
135
+
const data = await kv.get<Sensors>(["sensors", "latest"])
137
+
return c.text("No data found", 404)
140
+
const location = data.value.sensorData.find((sensor) =>
141
+
sensor.sensorType === "location"
145
+
return c.text("No location data found", 404)
148
+
const geocode = await tryCatch(
150
+
`https://api.geoapify.com/v1/geocode/reverse?lat=${location.latitude}&lon=${location.longitude}&apiKey=${
151
+
Deno.env.get("GEOAPIFY_API_KEY")
154
+
.then((res) => res.json())
155
+
.then((data) => data.features[0].properties.country_code),
158
+
if (!geocode.success) {
159
+
console.error(geocode.error)
160
+
return c.text("Geoapify API error", 404)
163
+
const country = ReverseGeocodingSchema.safeParse(geocode.value)
165
+
if (!country.success) {
166
+
console.error(country.error)
167
+
return c.text("Invalid country data", 400)
171
+
country: country.data.features[0].properties.country,
172
+
country_code: country.data.features[0].properties.country_code,
.get("/get", async (c) => {
const data = await kv.get<Sensors>(["sensors", "latest"])