aboutsummaryrefslogtreecommitdiff
path: root/src/frontend/app/api/planner.ts
blob: 86f44f0f087f44e91f897c7ada618baa5ed2bd83 (plain)
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
import { RoutePlanSchema, type RoutePlan } from "./schema";

export const fetchPlan = async (
  fromLat: number,
  fromLon: number,
  toLat: number,
  toLon: number,
  time?: Date,
  arriveBy: boolean = false
): Promise<RoutePlan> => {
  let url = `/api/planner/plan?fromLat=${fromLat}&fromLon=${fromLon}&toLat=${toLat}&toLon=${toLon}&arriveBy=${arriveBy}`;
  if (time) {
    url += `&time=${time.toISOString()}`;
  }

  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 RoutePlanSchema.parse(data);
  } catch (e) {
    console.error("Zod parsing failed for route plan:", e);
    console.log("Received data:", data);
    throw e;
  }
};