/** * Server-side Mapbox Directions call — used by `requestRide` to compute the * authoritative distance/duration a ride's price is based on. The client * shows its own local, unpersisted preview using the same public pricing * coefficients (see Firebase/lib/features/drive/providers/route_controller.dart); * only the value this function returns is ever written to a ride document, so * a tampered client can never write itself a discounted fare. */ interface RouteResult { distanceMeters: number; durationSeconds: number; } export async function fetchDrivingRoute( pickup: { lat: number; lng: number }, dropoff: { lat: number; lng: number }, accessToken: string, ): Promise { const coordinates = `${pickup.lng},${pickup.lat};${dropoff.lng},${dropoff.lat}`; const url = `https://api.mapbox.com/directions/v5/mapbox/driving/${coordinates}` + `?overview=false&approaches=curb;curb&radiuses=120;120&access_token=${accessToken}`; const res = await fetch(url); if (!res.ok) { throw new Error(`Mapbox Directions request failed: ${res.status}`); } const json = (await res.json()) as { routes?: { distance: number; duration: number }[]; }; const route = json.routes?.[0]; if (!route) { throw new Error("Mapbox Directions returned no route"); } return {distanceMeters: route.distance, durationSeconds: route.duration}; }