mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-17 08:42:21 +00:00
35 lines
1.3 KiB
JavaScript
35 lines
1.3 KiB
JavaScript
/** Build the existing route-proxy query from geographic endpoints. */
|
|
export function directionsRequestUrl(mode, a, b) {
|
|
const coords = `${a.lon.toFixed(6)},${a.lat.toFixed(6)};${b.lon.toFixed(6)},${b.lat.toFixed(6)}`;
|
|
return `/api/route?profile=${encodeURIComponent(mode)}&coords=${encodeURIComponent(coords)}&steps=1`;
|
|
}
|
|
|
|
/** Acquire route records using caller-owned transport and an optional endpoint. */
|
|
export function createDirectionsSource({
|
|
fetchImpl = (...args) => globalThis.fetch(...args),
|
|
endpoint = '/api/route',
|
|
} = {}) {
|
|
return {
|
|
async getRoute({ mode, a, b }, { signal } = {}) {
|
|
signal?.throwIfAborted();
|
|
const query = directionsRequestUrl(mode, a, b).split('?')[1];
|
|
const response = await fetchImpl(`${endpoint}?${query}`, {
|
|
signal,
|
|
headers: { Accept: 'application/json' },
|
|
});
|
|
const payload = await response.json();
|
|
signal?.throwIfAborted();
|
|
if (response.status === 429) {
|
|
throw new Error(
|
|
typeof payload?.error === 'string' && payload.error
|
|
? `${payload.error} — try again in a moment`
|
|
: 'Routing is rate limited — try again in a moment',
|
|
);
|
|
}
|
|
if (!response.ok && !payload?.error)
|
|
throw new Error(`Routing unavailable (HTTP ${response.status})`);
|
|
return payload;
|
|
},
|
|
};
|
|
}
|