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' ;
@@ -205,7 +210,9 @@ export class OsmApiClient extends BaseHttpClient implements ICancelableClient {
205210 this . #webUrl = webUrl ;
206211 this . #tdeiClient = tdeiClient ;
207212 this . #setAuthHeader( ) ;
208- this . _requestHeaders [ 'Accept' ] = 'text/plain' ;
213+
214+ // OSM API can return XML or JSON based on the header or file extension:
215+ this . _requestHeaders [ 'Accept' ] = '*/*' ;
209216 this . _requestHeaders [ 'Content-Type' ] = 'text/plain' ;
210217 }
211218
@@ -232,8 +239,8 @@ export class OsmApiClient extends BaseHttpClient implements ICancelableClient {
232239 display_name : this . auth . displayName
233240 } ;
234241
235- await this . _put ( `user/${ this . auth . subject } ` , JSON . stringify ( body ) , {
236- headers : { ... this . _requestHeaders , 'Content-Type' : 'application/json' }
242+ await this . _put ( `user/${ this . auth . subject } ` , body , {
243+ headers : { 'Content-Type' : 'application/json' }
237244 } ) ;
238245 }
239246
@@ -284,7 +291,6 @@ export class OsmApiClient extends BaseHttpClient implements ICancelableClient {
284291 ) : Promise < OsmElement > {
285292 const response = await this . _get ( `${ type } /${ id } /${ version } ` , {
286293 headers : {
287- ...this . _requestHeaders ,
288294 'Accept' : 'application/json' ,
289295 'X-Workspace' : workspaceId ,
290296 } ,
@@ -299,7 +305,6 @@ export class OsmApiClient extends BaseHttpClient implements ICancelableClient {
299305 async getNodes ( workspaceId : WorkspaceId , nodeIds : ( number | string ) [ ] ) : Promise < OsmNode [ ] > {
300306 const response = await this . _get ( `nodes?nodes=${ nodeIds . join ( ',' ) } ` , {
301307 headers : {
302- ...this . _requestHeaders ,
303308 'Accept' : 'application/json' ,
304309 'X-Workspace' : workspaceId ,
305310 } ,
@@ -317,7 +322,6 @@ export class OsmApiClient extends BaseHttpClient implements ICancelableClient {
317322 async getWays ( workspaceId : WorkspaceId , wayIds : ( number | string ) [ ] ) : Promise < OsmWay [ ] > {
318323 const response = await this . _get ( `ways?ways=${ wayIds . join ( ',' ) } ` , {
319324 headers : {
320- ...this . _requestHeaders ,
321325 'Accept' : 'application/json' ,
322326 'X-Workspace' : workspaceId ,
323327 } ,
@@ -335,7 +339,6 @@ export class OsmApiClient extends BaseHttpClient implements ICancelableClient {
335339 async getWaysForNode ( workspaceId : WorkspaceId , nodeId : number ) : Promise < OsmElement [ ] > {
336340 const response = await this . _get ( `node/${ nodeId } /ways` , {
337341 headers : {
338- ...this . _requestHeaders ,
339342 'Accept' : 'application/json' ,
340343 'X-Workspace' : workspaceId ,
341344 } ,
@@ -346,7 +349,7 @@ export class OsmApiClient extends BaseHttpClient implements ICancelableClient {
346349
347350 async listChangesets ( workspaceId : WorkspaceId ) : Promise < OsmChangeset [ ] > {
348351 const response = await this . _get ( `changesets.json` , {
349- headers : { ... this . _requestHeaders , 'X-Workspace' : workspaceId } ,
352+ headers : { 'X-Workspace' : workspaceId } ,
350353 } ) ;
351354
352355 const changesets = ( await response . json ( ) ) ?. changesets ?? [ ] ;
@@ -372,7 +375,6 @@ export class OsmApiClient extends BaseHttpClient implements ICancelableClient {
372375
373376 const response = await this . _get ( url , {
374377 headers : {
375- ...this . _requestHeaders ,
376378 'Accept' : 'application/json' ,
377379 'X-Workspace' : workspaceId ,
378380 } ,
@@ -398,7 +400,6 @@ export class OsmApiClient extends BaseHttpClient implements ICancelableClient {
398400 {
399401 const response = await this . _get ( `changeset/${ changesetId } /download` , {
400402 headers : {
401- ...this . _requestHeaders ,
402403 'Accept' : 'application/xml' ,
403404 'X-Workspace' : workspaceId ,
404405 } ,
@@ -424,7 +425,7 @@ export class OsmApiClient extends BaseHttpClient implements ICancelableClient {
424425
425426 const body = xml . serialize ( doc ) ;
426427 const response = await this . _put ( 'changeset/create' , body , {
427- headers : { ... this . _requestHeaders , 'X-Workspace' : workspaceId } ,
428+ headers : { 'X-Workspace' : workspaceId } ,
428429 } ) ;
429430
430431 return Number ( await response . text ( ) ) ;
@@ -438,7 +439,6 @@ export class OsmApiClient extends BaseHttpClient implements ICancelableClient {
438439 await this . _post ( `changeset/${ changesetId } /upload` , changesetXml , {
439440 headers : {
440441 'Content-Type' : 'application/xml' ,
441- 'Authorization' : this . _requestHeaders [ 'Authorization' ] ,
442442 'X-Workspace' : workspaceId ,
443443 } ,
444444 } ) ;
@@ -465,10 +465,7 @@ export class OsmApiClient extends BaseHttpClient implements ICancelableClient {
465465 body . append ( 'text' , message ) ;
466466
467467 await this . _post ( `changeset/${ changesetId } /comment` , body , {
468- headers : {
469- 'Authorization' : this . _requestHeaders [ 'Authorization' ] ,
470- 'X-Workspace' : workspaceId ,
471- } ,
468+ headers : { 'X-Workspace' : workspaceId } ,
472469 } ) ;
473470 }
474471
@@ -481,7 +478,6 @@ export class OsmApiClient extends BaseHttpClient implements ICancelableClient {
481478
482479 const response = await this . _get ( `notes/search.json?${ params } ` , {
483480 headers : {
484- ...this . _requestHeaders ,
485481 'Accept' : 'application/json' ,
486482 'X-Workspace' : workspaceId ,
487483 } ,
@@ -494,7 +490,6 @@ export class OsmApiClient extends BaseHttpClient implements ICancelableClient {
494490 const bboxParam = await this . getExportBbox ( workspaceId ) ;
495491 const response = await this . _get ( `map.json?bbox=${ bboxParam } ` , {
496492 headers : {
497- ...this . _requestHeaders ,
498493 'Accept' : 'application/json' ,
499494 'X-Workspace' : workspaceId
500495 }
@@ -507,7 +502,6 @@ export class OsmApiClient extends BaseHttpClient implements ICancelableClient {
507502 const bboxParam = await this . getExportBbox ( workspaceId ) ;
508503 const response = await this . _get ( `map?bbox=${ bboxParam } ` , {
509504 headers : {
510- ...this . _requestHeaders ,
511505 'Accept' : 'application/xml' ,
512506 'X-Workspace' : workspaceId
513507 }
@@ -522,17 +516,23 @@ export class OsmApiClient extends BaseHttpClient implements ICancelableClient {
522516 }
523517 }
524518
525- override async _send ( url : string , method : string , body ?: any , config ?: object ) : Promise < Response > {
519+ override async _send (
520+ url : string ,
521+ method : string ,
522+ body ?: HttpBody ,
523+ config ?: FetchConfig ,
524+ ) : Promise < Response > {
526525 try {
527526 await this . #tdeiClient. tryRefreshAuth ( ) ;
528527 this . #setAuthHeader( ) ;
529528
530- const requestOptions = {
531- credentials : 'include'
532- }
529+ const requestOptions : FetchConfig = {
530+ credentials : 'include' ,
531+ } ;
533532
534533 return await super . _send ( url , method , body , { ...requestOptions , ...config } ) ;
535- } catch ( e : any ) {
534+ }
535+ catch ( e : unknown ) {
536536 if ( e instanceof BaseHttpClientError ) {
537537 throw new OsmApiClientError ( e . response ) ;
538538 }
0 commit comments