async function route()

in genkit/postcard-generator/libs/maps/maps.ts [129:175]


async function route(
  start: string,
  end: string,
  stops?: string[],
): Promise<string> {
  const intermediates: { address: string }[] = [];
  // Populate intermediate stops
  if (stops) {
    stops.forEach((stop) => {
      if (stop !== "") {
        intermediates.push({
          address: stop,
        });
      }
    });
  }
  const request = {
    destination: {
      address: end,
    },
    origin: {
      address: start,
    },
    intermediates: intermediates,
    travelMode: "TRAVEL_MODE_UNSPECIFIED",
  } as RouteRequest;

  try {
    const response = await axios.post(
      "https://routes.googleapis.com/directions/v2:computeRoutes",
      request,
      {
        headers: {
          "Content-Type": "application/json",
          "X-Goog-Api-Key": GOOGLE_MAPS_API_KEY,
          "X-Goog-FieldMask": "routes.polyline",
        },
      },
    );
    const mapsResponse = response.data as RouteReponse;
    return mapsResponse.routes[0].polyline.encodedPolyline;
  } catch (error) {
    console.error("Error fetching route data:", error);
    // console.error(error.response.data);
    throw error;
  }
}