mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-04 10:24:02 +00:00
24 lines
939 B
JavaScript
24 lines
939 B
JavaScript
/** Read a GBFS station document through the existing bounded server proxy. */
|
|
export function createBikeshareSource({
|
|
fetchImpl = (...args) => globalThis.fetch(...args),
|
|
} = {}) {
|
|
return {
|
|
async getStations(upstreamUrl, { signal } = {}) {
|
|
const url = new URL(upstreamUrl);
|
|
if (url.protocol !== 'https:' || url.username || url.password || url.hash)
|
|
throw new TypeError('A public HTTPS GBFS URL is required');
|
|
signal?.throwIfAborted();
|
|
const response = await fetchImpl(
|
|
'/api/gbfs?url=' + encodeURIComponent(url.href),
|
|
{ method: 'GET', headers: { Accept: 'application/json' }, signal },
|
|
);
|
|
if (!response.ok) throw new Error('GBFS HTTP ' + response.status);
|
|
const payload = await response.json();
|
|
signal?.throwIfAborted();
|
|
if (!payload || typeof payload !== 'object')
|
|
throw new Error('Malformed GBFS payload');
|
|
return payload;
|
|
},
|
|
};
|
|
}
|