nidus-sync/ts/store/geocode.ts

44 lines
1.1 KiB
TypeScript
Raw Normal View History

2026-04-05 03:47:22 +00:00
import { defineStore } from "pinia";
import { ref } from "vue";
import type { Geocode, Location } from "@/type/api";
2026-04-05 03:47:22 +00:00
export const useStoreGeocode = defineStore("geocode", () => {
2026-04-05 03:47:22 +00:00
// State
const loading = ref(false);
const error = ref(null);
async function doReverse(url: string, location: Location): Promise<Geocode> {
2026-04-05 03:47:22 +00:00
loading.value = true;
error.value = null;
try {
//const url = `https://api.stadiamaps.com/geocoding/v2/reverse?point.lat=${location.lat}&point.lon=${location.lng}`;
const response = await fetch(url, {
body: JSON.stringify(location),
method: "POST",
headers: {
"Content-Type": "application/json",
},
});
2026-04-05 03:47:22 +00:00
const data = (await response.json()) as Geocode;
return data;
} catch (err) {
console.error("Error loading signals:", err);
throw err;
}
}
// Actions
async function reverse(location: Location): Promise<Geocode> {
return doReverse("/api/geocode/reverse", location);
}
async function reverseClosest(location: Location): Promise<Geocode> {
return doReverse("/api/geocode/reverse/closest", location);
}
2026-04-05 03:47:22 +00:00
return {
// Actions
reverse,
reverseClosest,
2026-04-05 03:47:22 +00:00
};
});