@@ -485,12 +485,29 @@ export class FetchClient {
485
485
this . #counter. increment ( ) ;
486
486
this . #provider. counter . increment ( ) ;
487
487
488
+ let request : Request | null = null ;
489
+
490
+ try {
491
+ request = new Request ( url , init ) ;
492
+ } catch {
493
+ // try converting to absolute URL
494
+ const origin = globalThis . location ?. origin ?? "http://localhost" ;
495
+ if ( ! url . startsWith ( "http" ) ) {
496
+ if ( url . startsWith ( "/" ) ) {
497
+ request = new Request ( origin + url , init ) ;
498
+ } else {
499
+ request = new Request ( origin + url , init ) ;
500
+ }
501
+ }
502
+ }
503
+
488
504
const context : FetchClientContext = {
489
505
options,
490
- request : new Request ( url , init ) ,
506
+ request : request ! ,
491
507
response : null ,
492
508
meta : { } ,
493
509
} ;
510
+
494
511
await this . invokeMiddleware ( context , middleware ) ;
495
512
496
513
this . #counter. decrement ( ) ;
@@ -619,21 +636,23 @@ export class FetchClient {
619
636
}
620
637
621
638
private buildUrl ( url : string , options : RequestOptions | undefined ) : string {
622
- if ( url . startsWith ( "/" ) ) {
623
- url = url . substring ( 1 ) ;
624
- }
639
+ let builtUrl = url ;
625
640
626
- if ( ! url . startsWith ( "http" ) && this . options ?. baseUrl ) {
627
- url = this . options . baseUrl + "/" + url ;
641
+ if ( ! builtUrl . startsWith ( "http" ) && this . options ?. baseUrl ) {
642
+ if ( this . options . baseUrl . endsWith ( "/" ) || builtUrl . startsWith ( "/" ) ) {
643
+ builtUrl = this . options . baseUrl + builtUrl ;
644
+ } else {
645
+ builtUrl = this . options . baseUrl + "/" + builtUrl ;
646
+ }
628
647
}
629
648
630
- const isAbsoluteUrl = url . startsWith ( "http" ) ;
649
+ const isAbsoluteUrl = builtUrl . startsWith ( "http" ) ;
631
650
632
651
const origin : string | undefined = isAbsoluteUrl
633
652
? undefined
634
- : globalThis . location ?. origin ?? undefined ;
653
+ : globalThis . location ?. origin ?? "http://localhost" ;
635
654
636
- const parsed = new URL ( url , origin ) ;
655
+ const parsed = new URL ( builtUrl , origin ) ;
637
656
638
657
if ( options ?. params ) {
639
658
for ( const [ key , value ] of Object . entries ( options ?. params ) ) {
@@ -644,10 +663,13 @@ export class FetchClient {
644
663
}
645
664
}
646
665
647
- url = parsed . toString ( ) ;
666
+ builtUrl = parsed . toString ( ) ;
648
667
}
649
668
650
- const result = isAbsoluteUrl ? url : `${ parsed . pathname } ${ parsed . search } ` ;
669
+ const result = isAbsoluteUrl
670
+ ? builtUrl
671
+ : `${ parsed . pathname } ${ parsed . search } ` ;
672
+
651
673
return result ;
652
674
}
653
675
0 commit comments