File size: 3,089 Bytes
a080db2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**

 * @fileoverview Runtime maps API: directions + reverse geocoding.

 *

 * A thin client over the runtime's `/api/maps/*` REST endpoints. Both calls are

 * plain request/response (no WebSocket push, unlike camera), so this module only

 * needs the shared HTTP helper. The cloud holds the maps provider token; the

 * consumer (e.g. the navigation miniapp via the SDK) calls these and never sees

 * a provider credential or a vendor-specific response shape.

 *

 * The wire types are canonical in the protocol package (the runtime server uses

 * the same ones); re-export them so a host gets them from this module.

 */
import type { HttpClient } from "../../http";
import type {
  DirectionsRequest,
  DirectionsResult,
  LatLng,
  PlaceAutocompleteResult,
  PlaceDetailsResult,
  ReverseGeocodeResult,
} from "@mentra/cloud-protocol";

export type {
  DirectionsRequest,
  DirectionsResult,
  Route,
  RouteStep,
  LatLng,
  TravelMode,
  ManeuverKind,
  RouteAvoidances,
  ReverseGeocodeResult,
  PlaceSuggestion,
  PlaceAutocompleteResult,
  PlaceDetailsResult,
} from "@mentra/cloud-protocol";

const DIRECTIONS_PATH = "/api/maps/directions";
const REVERSE_GEOCODE_PATH = "/api/maps/reverse-geocode";
const PLACE_AUTOCOMPLETE_PATH = "/api/maps/place-autocomplete";
const PLACE_DETAILS_PATH = "/api/maps/place-details";

export interface MapsDeps {
  http: HttpClient;
}

export class Maps {
  private readonly http: HttpClient;

  constructor(deps: MapsDeps) {
    this.http = deps.http;
  }

  /** Compute routes from origin through stops. Primary route first, alternates after. */
  directions(req: DirectionsRequest): Promise<DirectionsResult> {
    // Idempotent: a directions request is a pure read, safe to retry on a
    // transient network failure.
    return this.http.post<DirectionsResult>(DIRECTIONS_PATH, req, { idempotent: true });
  }

  /**

   * Resolve a coordinate to a short road name (`road`) + full formatted address

   * (`address`). Each is null when none of that kind is found near the coord.

   */
  reverseGeocode(coord: LatLng): Promise<ReverseGeocodeResult> {
    return this.http.post<ReverseGeocodeResult>(REVERSE_GEOCODE_PATH, coord, {
      idempotent: true,
    });
  }

  /**

   * Type-ahead place search. `sessionToken` groups the keystrokes with the

   * following `placeDetails` pick into one billed search session.

   */
  placeAutocomplete(req: {
    query: string;
    near?: LatLng;
    sessionToken: string;
  }): Promise<PlaceAutocompleteResult> {
    // Idempotent: autocomplete is a pure read, safe to retry on a transient blip.
    return this.http.post<PlaceAutocompleteResult>(PLACE_AUTOCOMPLETE_PATH, req, {
      idempotent: true,
    });
  }

  /** Resolve an autocomplete suggestion (`placeId` + same `sessionToken`) to coordinates. */
  placeDetails(req: { placeId: string; sessionToken: string }): Promise<PlaceDetailsResult> {
    return this.http.post<PlaceDetailsResult>(PLACE_DETAILS_PATH, req, {
      idempotent: true,
    });
  }
}