cleaned rate limiting in osm

main
Inga 🏳‍🌈 1 year ago
parent 50d84576fc
commit b8f9ee0e76
  1. 55
      src/clients/geocoding/osm.ts

@ -29,35 +29,36 @@ export const createOsmClient = (): Geocoder => {
}); });
return { return {
geocode: async (query) => { geocode: async (query) =>
const result = await queue.add(() => geocoder.geocode(query)); queue.add(async () => {
const result = await geocoder.geocode(query);
if (!result.length) { if (!result.length) {
throw new Error('No results found'); throw new Error('No results found');
} }
const meanLatitude = mean( const meanLatitude = mean(
compact(result.map(({ latitude }) => latitude)), compact(result.map(({ latitude }) => latitude)),
); );
const meanLongitude = mean( const meanLongitude = mean(
compact(result.map(({ longitude }) => longitude)), compact(result.map(({ longitude }) => longitude)),
); );
if ( if (
!result.every( !result.every(
({ latitude, longitude }) => ({ latitude, longitude }) =>
latitude && latitude &&
longitude && longitude &&
Math.abs(latitude - meanLatitude) < 0.01 && Math.abs(latitude - meanLatitude) < 0.01 &&
Math.abs(longitude - meanLongitude) < 0.01, Math.abs(longitude - meanLongitude) < 0.01,
) )
) { ) {
throw new Error('Ambiguous address'); throw new Error('Ambiguous address');
} }
return { return {
latitude: meanLatitude, latitude: meanLatitude,
longitude: meanLongitude, longitude: meanLongitude,
}; };
}, }),
}; };
}; };

Loading…
Cancel
Save