All files / src/api outdoorDirectionsApi.ts

97.61% Statements 41/42
92.59% Branches 25/27
100% Functions 4/4
97.61% Lines 41/42

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122                                      5x 5x 5x           5x           5x   5x         5x                 5x 5x         5x 1x   5x 1x   5x 5x   4x 1x     3x 1x     2x 2x   2x 2x                       7x 7x         7x 1x   7x 1x   7x 7x 6x 6x 2x     2x     4x 1x     3x 3x   2x 2x      
import { TransportModeApi, ManeuverTypeApi } from "../type";
import { API_BASE_URL } from "../const";
 
export interface Step {
  instruction: string;
  distance: string;
  duration: string;
  maneuverType: ManeuverTypeApi;
  polyline: string;
}
 
export interface OutdoorDirectionResponse {
  distance: string;
  duration: string;
  polyline: string;
  transportMode: TransportModeApi;
  steps: Step[];
}
 
const normalizeTransportMode = (mode: unknown): TransportModeApi => {
  const normalized = typeof mode === "string" ? mode.toLowerCase() : "";
  switch (normalized) {
    case "walking":
    case "driving":
    case "bicycling":
    case "transit":
    case "shuttle":
      return normalized;
    default:
      return "walking";
  }
};
 
const normalizeOutdoorDirectionResponse = (
  data: OutdoorDirectionResponse,
): OutdoorDirectionResponse => ({
  ...data,
  transportMode: normalizeTransportMode(data.transportMode),
});
 
export const getOutdoorDirections = async (
  origin: string,
  destination: string,
  mode: TransportModeApi = "walking",
  options?: {
    originBuildingId?: string;
    destinationBuildingId?: string;
  },
): Promise<OutdoorDirectionResponse | null> => {
  try {
    const params = new URLSearchParams({
      origin,
      destination,
      transportMode: mode.toUpperCase(),
    });
    if (options?.originBuildingId) {
      params.append("originBuildingId", options.originBuildingId);
    }
    if (options?.destinationBuildingId) {
      params.append("destinationBuildingId", options.destinationBuildingId);
    }
    const url = `${API_BASE_URL}/api/directions/outdoor?${params.toString()}`;
    const response = await fetch(url);
 
    if (response.status === 204) {
      return null;
    }
 
    if (!response.ok) {
      throw new Error(`Request failed: ${response.status}`);
    }
 
    const data = (await response.json()) as OutdoorDirectionResponse;
    return normalizeOutdoorDirectionResponse(data);
  } catch (error) {
    console.error("Failed to fetch directions:", error);
    return null;
  }
};
export async function getOutdoorDirectionsWithShuttle(
  origin: string,
  destination: string,
  dest_shuttle: string,
  options?: {
    originBuildingId?: string;
    destinationBuildingId?: string;
  },
): Promise<OutdoorDirectionResponse | null> {
  try {
    const params = new URLSearchParams({
      origin,
      destination,
      destinationShuttle: dest_shuttle,
    });
    if (options?.originBuildingId) {
      params.append("originBuildingId", options.originBuildingId);
    }
    if (options?.destinationBuildingId) {
      params.append("destinationBuildingId", options.destinationBuildingId);
    }
    const url = `${API_BASE_URL}/api/directions/outdoor/shuttle?${params.toString()}`;
    const response = await fetch(url);
    const contentLength = response.headers.get("content-length");
    if (response.status === 204 || contentLength === "0") {
      console.log(
        "Shuttle not available (Schedule closed or logic returned null)",
      );
      return null;
    }
 
    if (!response.ok) {
      throw new Error(`Request failed: ${response.status}`);
    }
 
    const data = (await response.json()) as OutdoorDirectionResponse;
    return normalizeOutdoorDirectionResponse(data);
  } catch (error) {
    console.error("Failed to fetch directions:", error);
    return null;
  }
}