diff --git a/server/src/nest/airports/airports.contract.ts b/server/src/nest/airports/airports.contract.ts new file mode 100644 index 000000000..f35d727f5 --- /dev/null +++ b/server/src/nest/airports/airports.contract.ts @@ -0,0 +1,20 @@ +/** + * What the airports domain offers other domains without its service: the + * bundled airport of an IATA code. The place search reads it to turn "JFK" + * into the airport it names (maps/providers/osm-geocoding.ts), a lookup in + * memory that needs neither the database nor the module. + */ +import { findByIata } from './airports.data'; + +/** An airport by name and coordinate, the part of it a lookup by code hands on. */ +export interface AirportByCode { + iata: string; + name: string; + lat: number; + lng: number; +} + +export function airportByCode(code: string): AirportByCode | null { + const airport = findByIata(code); + return airport ? { iata: airport.iata, name: airport.name, lat: airport.lat, lng: airport.lng } : null; +} diff --git a/server/src/nest/maps/providers/osm-geocoding.ts b/server/src/nest/maps/providers/osm-geocoding.ts index 487baeb61..2adb12015 100644 --- a/server/src/nest/maps/providers/osm-geocoding.ts +++ b/server/src/nest/maps/providers/osm-geocoding.ts @@ -9,7 +9,7 @@ * the installation's geocoder these answer alone; Nominatim is OsmClient's * business, for installations without the TREK services. */ -import { findByIata, type Airport } from '../../airports/airports.data'; +import { airportByCode, type AirportByCode } from '../../airports/airports.contract'; import { buildOsmDetails, haversineMetres, namesOverlap, toApiLang } from '../maps.helpers'; import type { NominatimResult, NominatimReverse, OverpassElement } from './osm.client'; import type { PhotonClient } from './photon.client'; @@ -100,9 +100,9 @@ export async function photonSuggest( * coordinate, where the aerodrome comes first. Lower case is left alone, so * "Ber" typed on the way to "Berlin" stays a search for Berlin. */ -function airportOfCode(query: string): Airport | null { +function airportOfCode(query: string): AirportByCode | null { const code = query.trim(); - return /^[A-Z]{3}$/.test(code) ? findByIata(code) : null; + return /^[A-Z]{3}$/.test(code) ? airportByCode(code) : null; } /** @@ -111,7 +111,7 @@ function airportOfCode(query: string): Airport | null { * airport, which is how OpenStreetMap files Haneda (an island named "Tokyo * International Airport"). */ -function airportLead(ranked: PhotonFeature[], airport: Airport): PhotonLead | null { +function airportLead(ranked: PhotonFeature[], airport: AirportByCode): PhotonLead | null { const near = ranked .map((feature) => ({ feature, row: asSuggestion(feature) })) .filter(