28 lines
1.1 KiB
JavaScript
28 lines
1.1 KiB
JavaScript
export async function fetchStations(fetch = globalThis.fetch) {
|
|
const res = await fetch('http://37100lab.it:8101/api/campagne_map_data');
|
|
if (!res.ok) throw new Error(`Errore HTTP ${res.status}`);
|
|
const geojson = await res.json();
|
|
return geojson.features
|
|
.map(f => ({ ...f.properties, pk: parseInt(f.properties.pk, 10) }))
|
|
.sort((a, b) => b.pk - a.pk);
|
|
}
|
|
|
|
export async function fetchDay(id, day, signal) {
|
|
// Converte YYYY-M-D (da formatDayParam) in YYYY-MM-DD per la chiamata API
|
|
const [y, m, d] = day.split('-');
|
|
const date = `${y}-${m.padStart(2, '0')}-${d.padStart(2, '0')}`;
|
|
|
|
// L'API risponde con 5xx transienti: tre tentativi limitano i buchi nel dataset scaricato.
|
|
for (let attempt = 0; attempt < 3; attempt++) {
|
|
try {
|
|
const res = await fetch(`/api/get_station_data?id=${id}&date=${date}`, { signal });
|
|
if (!res.ok) throw new Error(`HTTP ${res.status}`);
|
|
return await res.json();
|
|
} catch (e) {
|
|
// La cancellazione non è un errore: uscire subito senza consumare altri retry.
|
|
if (e.name === 'AbortError') return [];
|
|
if (attempt === 2) return [];
|
|
}
|
|
}
|
|
}
|