|
| 1 | +import Debug from 'debug'; |
| 2 | +import { type Address, AddressSchema } from '../entities/Address.js'; |
| 3 | +import fetch from '../helpers/fetch.js'; |
| 4 | +import { Throttler } from './decorators/Throttler.js'; |
| 5 | +import { Geocoder } from './Geocoder.js'; |
| 6 | + |
| 7 | +const debug = Debug('geocoder:locationiq'); |
| 8 | + |
| 9 | +type LocationIQSearchResult = { |
| 10 | + display_name: string; |
| 11 | + class?: string; |
| 12 | + type?: string; |
| 13 | + importance?: number; |
| 14 | + rank_search?: number; |
| 15 | + address?: Record<string, any>; |
| 16 | +}; |
| 17 | + |
| 18 | +export type BaseLocationIQOptions = { |
| 19 | + apiKey: string; |
| 20 | + baseUrl?: string; |
| 21 | + minConfidence?: number; |
| 22 | +}; |
| 23 | + |
| 24 | +class BaseLocationIQ implements Geocoder { |
| 25 | + constructor(private options: BaseLocationIQOptions) { |
| 26 | + debug('initializing with options (hidden apiKey): %O', { |
| 27 | + baseUrl: options.baseUrl, |
| 28 | + minConfidence: options.minConfidence |
| 29 | + }); |
| 30 | + } |
| 31 | + |
| 32 | + async search(q: string, options?: { signal?: AbortSignal }): Promise<Address | null> { |
| 33 | + debug('searching for: %s', q); |
| 34 | + |
| 35 | + const base = this.options.baseUrl ?? 'https://us1.locationiq.com/v1'; |
| 36 | + const params = new URLSearchParams({ |
| 37 | + key: this.options.apiKey, |
| 38 | + q, |
| 39 | + format: 'json', |
| 40 | + addressdetails: '1', |
| 41 | + limit: '5' |
| 42 | + }); |
| 43 | + |
| 44 | + const url = `${base}/search.php?${params.toString()}`; |
| 45 | + |
| 46 | + const raw = await fetch<LocationIQSearchResult[]>(url, { signal: options?.signal }); |
| 47 | + // Support both Response-like objects (with .json()) and direct JSON returned by mocks |
| 48 | + const response = |
| 49 | + typeof (raw as any)?.json === 'function' ? await (raw as any).json() : (raw as any); |
| 50 | + |
| 51 | + if (!Array.isArray(response) || response.length === 0) { |
| 52 | + debug('no results from locationiq for: %s', q); |
| 53 | + return null; |
| 54 | + } |
| 55 | + |
| 56 | + const candidate = response[0]; |
| 57 | + if (!candidate) return null; |
| 58 | + |
| 59 | + const addr = candidate.address ?? {}; |
| 60 | + const confidence = Number(candidate.importance ?? candidate.rank_search ?? 0); |
| 61 | + |
| 62 | + if (this.options.minConfidence && confidence < this.options.minConfidence) { |
| 63 | + debug('confidence below threshold: %s < %s', confidence, this.options.minConfidence); |
| 64 | + return null; |
| 65 | + } |
| 66 | + |
| 67 | + const result = AddressSchema.parse({ |
| 68 | + provider: 'locationiq', |
| 69 | + source: q, |
| 70 | + name: [addr.country, addr.state ?? addr.county, addr.city ?? addr.town ?? addr.village] |
| 71 | + .filter(Boolean) |
| 72 | + .join(', '), |
| 73 | + type: candidate.type ?? candidate.class, |
| 74 | + confidence, |
| 75 | + country: addr.country, |
| 76 | + country_code: addr.country_code?.toUpperCase?.(), |
| 77 | + state: addr.state ?? addr.county, |
| 78 | + city: addr.city ?? addr.town ?? addr.village |
| 79 | + }); |
| 80 | + |
| 81 | + debug('found address: %s (confidence: %s)', result.name, result.confidence); |
| 82 | + return result; |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +export type LocationIQOptions = { concurrency?: number } & BaseLocationIQOptions; |
| 87 | + |
| 88 | +export class LocationIQ extends Throttler implements Geocoder { |
| 89 | + constructor(options: LocationIQOptions) { |
| 90 | + const { concurrency, ...opts } = options; |
| 91 | + super(new BaseLocationIQ(opts), { concurrency, intervalCap: 1000 }); |
| 92 | + } |
| 93 | +} |
0 commit comments