aboutsummaryrefslogtreecommitdiff
path: root/src/frontend/app/api
diff options
context:
space:
mode:
Diffstat (limited to 'src/frontend/app/api')
-rw-r--r--src/frontend/app/api/arrivals.ts30
-rw-r--r--src/frontend/app/api/schema.ts14
2 files changed, 43 insertions, 1 deletions
diff --git a/src/frontend/app/api/arrivals.ts b/src/frontend/app/api/arrivals.ts
index 8ae6e78..ad99630 100644
--- a/src/frontend/app/api/arrivals.ts
+++ b/src/frontend/app/api/arrivals.ts
@@ -1,6 +1,8 @@
import {
StopArrivalsResponseSchema,
+ StopEstimatesResponseSchema,
type StopArrivalsResponse,
+ type StopEstimatesResponse,
} from "./schema";
export const fetchArrivals = async (
@@ -29,3 +31,31 @@ export const fetchArrivals = async (
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;
+ }
+};
diff --git a/src/frontend/app/api/schema.ts b/src/frontend/app/api/schema.ts
index 0c55969..f68d413 100644
--- a/src/frontend/app/api/schema.ts
+++ b/src/frontend/app/api/schema.ts
@@ -64,10 +64,16 @@ export const ArrivalSchema = z.object({
shift: ShiftBadgeSchema.optional().nullable(),
shape: z.any().optional().nullable(),
currentPosition: PositionSchema.optional().nullable(),
- stopShapeIndex: z.number().optional().nullable(),
vehicleInformation: VehicleInformationSchema.optional().nullable(),
});
+export const ArrivalEstimateSchema = z.object({
+ tripId: z.string(),
+ patternId: z.string().optional().nullable(),
+ estimate: ArrivalDetailsSchema,
+ delay: DelayBadgeSchema.optional().nullable(),
+});
+
export const StopArrivalsResponseSchema = z.object({
stopCode: z.string(),
stopName: z.string(),
@@ -77,6 +83,10 @@ export const StopArrivalsResponseSchema = z.object({
usage: z.array(BusStopUsagePointSchema).optional().nullable(),
});
+export const StopEstimatesResponseSchema = z.object({
+ arrivals: z.array(ArrivalEstimateSchema),
+});
+
export type RouteInfo = z.infer<typeof RouteInfoSchema>;
export type HeadsignInfo = z.infer<typeof HeadsignInfoSchema>;
export type ArrivalPrecision = z.infer<typeof ArrivalPrecisionSchema>;
@@ -85,8 +95,10 @@ export type DelayBadge = z.infer<typeof DelayBadgeSchema>;
export type ShiftBadge = z.infer<typeof ShiftBadgeSchema>;
export type Position = z.infer<typeof PositionSchema>;
export type Arrival = z.infer<typeof ArrivalSchema>;
+export type ArrivalEstimate = z.infer<typeof ArrivalEstimateSchema>;
export type BusStopUsagePoint = z.infer<typeof BusStopUsagePointSchema>;
export type StopArrivalsResponse = z.infer<typeof StopArrivalsResponseSchema>;
+export type StopEstimatesResponse = z.infer<typeof StopEstimatesResponseSchema>;
// Transit Routes
export const RouteSchema = z.object({