@@ -97,8 +97,9 @@ function ensurePending(placeText: string): void {
9797}
9898
9999type FetchResult = { status : 'found' ; result : NominatimResult } | { status : 'not_found' } | { status : 'error' } | { status : 'cancelled' } ;
100+ type NominatimResponse = { status : number ; ok : boolean ; data : NominatimResult [ ] } ;
100101
101- async function fetchWithDeadline ( url : string , query : string , callerSignal ?: AbortSignal ) : Promise < Response | null | 'cancelled' > {
102+ async function fetchWithDeadline ( url : string , query : string , callerSignal ?: AbortSignal ) : Promise < NominatimResponse | null | 'cancelled' > {
102103 if ( callerSignal ?. aborted ) return 'cancelled' ;
103104
104105 const controller = new AbortController ( ) ;
@@ -111,7 +112,13 @@ async function fetchWithDeadline(url: string, query: string, callerSignal?: Abor
111112 } , REQUEST_TIMEOUT_MS ) ;
112113
113114 try {
114- return await fetch ( url , { headers : { 'User-Agent' : USER_AGENT } , signal : controller . signal } ) ;
115+ const response = await fetch ( url , { headers : { 'User-Agent' : USER_AGENT } , signal : controller . signal } ) ;
116+ if ( response . status === 429 ) {
117+ void response . body ?. cancel ( ) . catch ( ( ) => { } ) ;
118+ return { status : response . status , ok : response . ok , data : [ ] } ;
119+ }
120+ const data = response . ok ? await response . json ( ) as NominatimResult [ ] : [ ] ;
121+ return { status : response . status , ok : response . ok , data } ;
115122 } catch ( error ) {
116123 if ( callerSignal ?. aborted ) {
117124 logger . warn ( 'geocode' , `Geocode request status=cancelled place=${ JSON . stringify ( query ) } reason=caller_cancelled` ) ;
@@ -147,13 +154,11 @@ export function fetchNominatim(query: string, signal?: AbortSignal): Promise<Fet
147154 const retry = await fetchWithDeadline ( url , query , signal ) ;
148155 if ( retry === 'cancelled' ) return { status : 'cancelled' } ;
149156 if ( ! retry ?. ok ) return { status : 'error' } ;
150- const retryData : NominatimResult [ ] = await retry . json ( ) ;
151- return retryData [ 0 ] ? { status : 'found' , result : retryData [ 0 ] } : { status : 'not_found' } ;
157+ return retry . data [ 0 ] ? { status : 'found' , result : retry . data [ 0 ] } : { status : 'not_found' } ;
152158 }
153159
154160 if ( ! response . ok ) return { status : 'error' } ;
155- const data : NominatimResult [ ] = await response . json ( ) ;
156- return data [ 0 ] ? { status : 'found' , result : data [ 0 ] } : { status : 'not_found' } ;
161+ return response . data [ 0 ] ? { status : 'found' , result : response . data [ 0 ] } : { status : 'not_found' } ;
157162 } catch ( error ) {
158163 if ( signal ?. aborted ) return { status : 'cancelled' } ;
159164 logger . warn ( 'geocode' , `Geocode response status=error place=${ JSON . stringify ( query ) } reason=response_error` ) ;
0 commit comments