import type { MapsConfig } from '@stacksjs/types' import type { GeocodeOptions, GeocodingDriver, GeocodingOperation, GeocodingResult, GeocodingSuggestion, LatLng, StructuredAddress, VerifiedAddress } from './types' import { GeocodingConfigError, UnsupportedGeocodingOperationError } from './errors' import { mapshqDriver } from './drivers/mapshq' import { nominatimDriver } from './drivers/nominatim' import { photonDriver } from './drivers/photon' /** The driver `config/maps.ts` names. */ export function geocodingDriverFromConfig(config: MapsConfig = {}): GeocodingDriver { const name = config.driver ?? 'nominatim' switch (name) { case 'mapshq': return mapshqDriver({ key: config.drivers?.mapshq?.key || process.env.MAPSHQ_KEY, url: config.drivers?.mapshq?.url || process.env.MAPSHQ_API_URL || undefined }) case 'nominatim': return nominatimDriver(config.drivers?.nominatim ?? {}) case 'photon': return photonDriver(config.drivers?.photon ?? {}) default: throw new GeocodingConfigError(`Unknown geocoding driver "${name}" in config/maps.ts. Choose mapshq, nominatim or photon, or pass a driver to createGeocoder().`) } } export interface Geocoder { readonly driver: GeocodingDriver search: (query: string, options?: GeocodeOptions) => Promise suggest: (query: string, options?: GeocodeOptions) => Promise retrieve: (id: string, options?: GeocodeOptions) => Promise reverse: (point: LatLng, options?: GeocodeOptions) => Promise verify: (address: StructuredAddress, options?: GeocodeOptions) => Promise /** Whether the driver offers an operation. */ supports: (operation: GeocodingOperation) => boolean } function operation(driver: GeocodingDriver, name: K): NonNullable { const fn = driver[name] if (typeof fn !== 'function') throw new UnsupportedGeocodingOperationError(driver.name, name) return fn.bind(driver) as NonNullable } /** * A geocoder over one driver. `defaults` (a country, a language) apply to * every call that does not set its own. */ export function createGeocoder(driver: GeocodingDriver, defaults: Pick = {}): Geocoder { const merged = (options: GeocodeOptions = {}): GeocodeOptions => ({ ...(defaults.language ? { language: defaults.language } : {}), ...(defaults.country ? { countries: [defaults.country] } : {}), ...options, }) return { driver, search: async (query, options) => await operation(driver, 'search')(query, merged(options)), suggest: async (query, options) => await operation(driver, 'suggest')(query, merged(options)), retrieve: async (id, options) => await operation(driver, 'retrieve')(id, merged(options)), reverse: async (point, options) => await operation(driver, 'reverse')(point, merged(options)), verify: async (address, options) => await operation(driver, 'verify')(address, merged(options)), supports: name => typeof driver[name] === 'function', } } let current: Geocoder | undefined async function configured(): Promise { if (current) return current const { config } = await import('@stacksjs/config') const maps = ((config as { maps?: MapsConfig }).maps ?? {}) as MapsConfig current = createGeocoder(geocodingDriverFromConfig(maps), maps) return current } /** * The app's geocoder: the driver `config/maps.ts` names, switched by config * (or `MAPS_DRIVER`) with no code change. * * ```ts * import { Geocoder } from '@stacksjs/maps' * * const [place] = await Geocoder.search('12320 W Pico Blvd, Los Angeles') * const suggestions = await Geocoder.suggest('12320 W Pi', { sessionToken, signal }) * ``` */ export const Geocoder = { search: async (query: string, options?: GeocodeOptions): Promise => await (await configured()).search(query, options), suggest: async (query: string, options?: GeocodeOptions): Promise => await (await configured()).suggest(query, options), retrieve: async (id: string, options?: GeocodeOptions): Promise => await (await configured()).retrieve(id, options), reverse: async (point: LatLng, options?: GeocodeOptions): Promise => await (await configured()).reverse(point, options), verify: async (address: StructuredAddress, options?: GeocodeOptions): Promise => await (await configured()).verify(address, options), /** The geocoder behind the facade. */ resolve: configured, /** Use this driver (a fake in tests, or one an app wires itself) until `reset()`. */ use(driver: GeocodingDriver, defaults?: Pick): Geocoder { current = createGeocoder(driver, defaults) return current }, /** Back to `config/maps.ts` on the next call. */ reset(): void { current = undefined }, }