25 lines
700 B
TypeScript
25 lines
700 B
TypeScript
|
import memoize from 'memoizee';
|
||
|
|
||
|
const _findAddress = async (lon: number, lat: number, locale: () => string) => {
|
||
|
const response = await fetch(
|
||
|
`https://nominatim.openstreetmap.org/reverse?lat=${lat}&lon=${lon}&format=jsonv2&addressdetails=1&extratags=1&namedetails=1&accept-language=${locale()}`,
|
||
|
{}
|
||
|
);
|
||
|
const data = await response.json();
|
||
|
console.log({ caller: 'findAddress', lon, lat, data });
|
||
|
return data;
|
||
|
};
|
||
|
|
||
|
export const findAddress = memoize(_findAddress, {
|
||
|
promise: true,
|
||
|
max: 1000,
|
||
|
maxAge: 360000000,
|
||
|
});
|
||
|
|
||
|
export const getVillageOrTown = (address: any) => {
|
||
|
if (address?.address.village) {
|
||
|
return address?.address.village;
|
||
|
}
|
||
|
return address?.address.town;
|
||
|
};
|