11import parseOsmChangeXml from '@osmcha/osmchange-parser' ;
22import type { FeatureCollection , Point } from 'geojson' ;
33
4- import { BaseHttpClient , BaseHttpClientError } from "~/services/http" ;
4+ import {
5+ BaseHttpClient ,
6+ BaseHttpClientError ,
7+ type FetchConfig ,
8+ type HttpBody ,
9+ } from '~/services/http' ;
510import * as xml from '~/util/xml' ;
611
712import type { ICancelableClient } from '~/services/loading' ;
@@ -14,7 +19,6 @@ import type {
1419 OsmElement ,
1520 OsmNode ,
1621 OsmNote ,
17- OsmTags ,
1822 OsmWay ,
1923} from '~/types/osm' ;
2024import type { WorkspaceId } from '~/types/workspaces' ;
@@ -207,7 +211,9 @@ export class OsmApiClient extends BaseHttpClient implements ICancelableClient {
207211 this . #webUrl = webUrl ;
208212 this . #tdeiClient = tdeiClient ;
209213 this . #setAuthHeader( ) ;
210- this . _requestHeaders [ 'Accept' ] = 'text/plain' ;
214+
215+ // OSM API can return XML or JSON based on the header or file extension:
216+ this . _requestHeaders [ 'Accept' ] = '*/*' ;
211217 this . _requestHeaders [ 'Content-Type' ] = 'text/plain' ;
212218 }
213219
@@ -234,8 +240,8 @@ export class OsmApiClient extends BaseHttpClient implements ICancelableClient {
234240 display_name : this . auth . displayName
235241 } ;
236242
237- await this . _put ( `user/${ this . auth . subject } ` , JSON . stringify ( body ) , {
238- headers : { ... this . _requestHeaders , 'Content-Type' : 'application/json' }
243+ await this . _put ( `user/${ this . auth . subject } ` , body , {
244+ headers : { 'Content-Type' : 'application/json' }
239245 } ) ;
240246 }
241247
@@ -286,7 +292,6 @@ export class OsmApiClient extends BaseHttpClient implements ICancelableClient {
286292 ) : Promise < OsmElement > {
287293 const response = await this . _get ( `${ type } /${ id } /${ version } ` , {
288294 headers : {
289- ...this . _requestHeaders ,
290295 'Accept' : 'application/json' ,
291296 'X-Workspace' : workspaceId ,
292297 } ,
@@ -301,7 +306,6 @@ export class OsmApiClient extends BaseHttpClient implements ICancelableClient {
301306 async getNodes ( workspaceId : WorkspaceId , nodeIds : ( number | string ) [ ] ) : Promise < OsmNode [ ] > {
302307 const response = await this . _get ( `nodes?nodes=${ nodeIds . join ( ',' ) } ` , {
303308 headers : {
304- ...this . _requestHeaders ,
305309 'Accept' : 'application/json' ,
306310 'X-Workspace' : workspaceId ,
307311 } ,
@@ -319,7 +323,6 @@ export class OsmApiClient extends BaseHttpClient implements ICancelableClient {
319323 async getWays ( workspaceId : WorkspaceId , wayIds : ( number | string ) [ ] ) : Promise < OsmWay [ ] > {
320324 const response = await this . _get ( `ways?ways=${ wayIds . join ( ',' ) } ` , {
321325 headers : {
322- ...this . _requestHeaders ,
323326 'Accept' : 'application/json' ,
324327 'X-Workspace' : workspaceId ,
325328 } ,
@@ -337,7 +340,6 @@ export class OsmApiClient extends BaseHttpClient implements ICancelableClient {
337340 async getWaysForNode ( workspaceId : WorkspaceId , nodeId : number ) : Promise < OsmElement [ ] > {
338341 const response = await this . _get ( `node/${ nodeId } /ways` , {
339342 headers : {
340- ...this . _requestHeaders ,
341343 'Accept' : 'application/json' ,
342344 'X-Workspace' : workspaceId ,
343345 } ,
@@ -348,7 +350,7 @@ export class OsmApiClient extends BaseHttpClient implements ICancelableClient {
348350
349351 async listChangesets ( workspaceId : WorkspaceId ) : Promise < OsmChangeset [ ] > {
350352 const response = await this . _get ( `changesets.json` , {
351- headers : { ... this . _requestHeaders , 'X-Workspace' : workspaceId } ,
353+ headers : { 'X-Workspace' : workspaceId } ,
352354 } ) ;
353355
354356 const changesets = ( await response . json ( ) ) ?. changesets ?? [ ] ;
@@ -374,7 +376,6 @@ export class OsmApiClient extends BaseHttpClient implements ICancelableClient {
374376
375377 const response = await this . _get ( url , {
376378 headers : {
377- ...this . _requestHeaders ,
378379 'Accept' : 'application/json' ,
379380 'X-Workspace' : workspaceId ,
380381 } ,
@@ -401,7 +402,6 @@ export class OsmApiClient extends BaseHttpClient implements ICancelableClient {
401402 {
402403 const response = await this . _get ( `changeset/${ changesetId } /download` , {
403404 headers : {
404- ...this . _requestHeaders ,
405405 'Accept' : 'application/xml' ,
406406 'X-Workspace' : workspaceId ,
407407 } ,
@@ -427,7 +427,7 @@ export class OsmApiClient extends BaseHttpClient implements ICancelableClient {
427427
428428 const body = xml . serialize ( doc ) ;
429429 const response = await this . _put ( 'changeset/create' , body , {
430- headers : { ... this . _requestHeaders , 'X-Workspace' : workspaceId } ,
430+ headers : { 'X-Workspace' : workspaceId } ,
431431 } ) ;
432432
433433 return Number ( await response . text ( ) ) ;
@@ -441,7 +441,6 @@ export class OsmApiClient extends BaseHttpClient implements ICancelableClient {
441441 await this . _post ( `changeset/${ changesetId } /upload` , changesetXml , {
442442 headers : {
443443 'Content-Type' : 'application/xml' ,
444- 'Authorization' : this . _requestHeaders [ 'Authorization' ] ,
445444 'X-Workspace' : workspaceId ,
446445 } ,
447446 } ) ;
@@ -468,10 +467,7 @@ export class OsmApiClient extends BaseHttpClient implements ICancelableClient {
468467 body . append ( 'text' , message ) ;
469468
470469 await this . _post ( `changeset/${ changesetId } /comment` , body , {
471- headers : {
472- 'Authorization' : this . _requestHeaders [ 'Authorization' ] ,
473- 'X-Workspace' : workspaceId ,
474- } ,
470+ headers : { 'X-Workspace' : workspaceId } ,
475471 } ) ;
476472 }
477473
@@ -484,7 +480,6 @@ export class OsmApiClient extends BaseHttpClient implements ICancelableClient {
484480
485481 const response = await this . _get ( `notes/search.json?${ params } ` , {
486482 headers : {
487- ...this . _requestHeaders ,
488483 'Accept' : 'application/json' ,
489484 'X-Workspace' : workspaceId ,
490485 } ,
@@ -497,7 +492,6 @@ export class OsmApiClient extends BaseHttpClient implements ICancelableClient {
497492 const bboxParam = await this . getExportBbox ( workspaceId ) ;
498493 const response = await this . _get ( `map.json?bbox=${ bboxParam } ` , {
499494 headers : {
500- ...this . _requestHeaders ,
501495 'Accept' : 'application/json' ,
502496 'X-Workspace' : workspaceId
503497 }
@@ -510,7 +504,6 @@ export class OsmApiClient extends BaseHttpClient implements ICancelableClient {
510504 const bboxParam = await this . getExportBbox ( workspaceId ) ;
511505 const response = await this . _get ( `map?bbox=${ bboxParam } ` , {
512506 headers : {
513- ...this . _requestHeaders ,
514507 'Accept' : 'application/xml' ,
515508 'X-Workspace' : workspaceId
516509 }
@@ -525,17 +518,23 @@ export class OsmApiClient extends BaseHttpClient implements ICancelableClient {
525518 }
526519 }
527520
528- async _send ( url : string , method : string , body ?: any , config ?: object ) : Promise < Response > {
521+ override async _send (
522+ url : string ,
523+ method : string ,
524+ body ?: HttpBody ,
525+ config ?: FetchConfig ,
526+ ) : Promise < Response > {
529527 try {
530528 await this . #tdeiClient. tryRefreshAuth ( ) ;
531529 this . #setAuthHeader( ) ;
532530
533- const requestOptions = {
534- credentials : 'include'
535- }
531+ const requestOptions : FetchConfig = {
532+ credentials : 'include' ,
533+ } ;
536534
537535 return await super . _send ( url , method , body , { ...requestOptions , ...config } ) ;
538- } catch ( e : any ) {
536+ }
537+ catch ( e : unknown ) {
539538 if ( e instanceof BaseHttpClientError ) {
540539 throw new OsmApiClientError ( e . response ) ;
541540 }
0 commit comments