Files
gods-eye-view/src/layers/bikeshare/source.js
T
Bilawal SidhuandMiguelGFerreira 2b0fcd4595 fix(bikeshare): address the GBFS proxy by path, not query string (#569)
Supersedes #441 by @MiguelGFerreira, ported onto the current component layout with fixes from review.

Verified before merge: format, package boundaries, unit suite, build, and the browser tracking suite; a browser harness for the change where one applies; adversarial review applied.

Co-authored-by: MiguelGFerreira <[email protected]>
2026-09-14 21:08:12 -05:00

24 lines
935 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/' + 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;
},
};
}