1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
import {
StopArrivalsResponseSchema,
StopEstimatesResponseSchema,
type StopArrivalsResponse,
type StopEstimatesResponse,
} from "./schema";
export const fetchArrivals = async (
stopId: string,
reduced: boolean = false
): Promise<StopArrivalsResponse> => {
const resp = await fetch(
`/api/stops/arrivals?id=${stopId}&reduced=${reduced}`,
{
headers: {
Accept: "application/json",
},
}
);
if (!resp.ok) {
throw new Error(`HTTP ${resp.status}: ${resp.statusText}`);
}
const data = await resp.json();
try {
return StopArrivalsResponseSchema.parse(data);
} catch (e) {
console.error("Zod parsing failed for arrivals:", e);
console.log("Received data:", data);
throw e;
}
};
export const fetchEstimates = async (
stopId: string,
routeId: string,
viaStopId?: string
): Promise<StopEstimatesResponse> => {
let url = `/api/stops/estimates?stop=${encodeURIComponent(stopId)}&route=${encodeURIComponent(routeId)}`;
if (viaStopId) {
url += `&via=${encodeURIComponent(viaStopId)}`;
}
const resp = await fetch(url, {
headers: { Accept: "application/json" },
});
if (!resp.ok) {
throw new Error(`HTTP ${resp.status}: ${resp.statusText}`);
}
const data = await resp.json();
try {
return StopEstimatesResponseSchema.parse(data);
} catch (e) {
console.error("Zod parsing failed for estimates:", e);
console.log("Received data:", data);
throw e;
}
};
|