@@ -414,7 +414,7 @@ export class FetchClient {
414
414
options : RequestOptions ,
415
415
init ?: RequestInit ,
416
416
) : Promise < FetchClientResponse < T > > {
417
- url = this . buildUrl ( url , options ) ;
417
+ const { builtUrl , absoluteUrl } = this . buildUrl ( url , options ) ;
418
418
419
419
const accessToken = this . options . accessTokenFunc ?.( ) ?? null ;
420
420
if ( accessToken !== null ) {
@@ -488,17 +488,10 @@ export class FetchClient {
488
488
let request : Request | null = null ;
489
489
490
490
try {
491
- request = new Request ( url , init ) ;
491
+ request = new Request ( builtUrl , init ) ;
492
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
- }
493
+ // try using absolute URL
494
+ request = new Request ( absoluteUrl , init ) ;
502
495
}
503
496
504
497
const context : FetchClientContext = {
@@ -635,7 +628,10 @@ export class FetchClient {
635
628
} ;
636
629
}
637
630
638
- private buildUrl ( url : string , options : RequestOptions | undefined ) : string {
631
+ private buildUrl (
632
+ url : string ,
633
+ options : RequestOptions | undefined ,
634
+ ) : { builtUrl : string ; absoluteUrl : string } {
639
635
let builtUrl = url ;
640
636
641
637
if ( ! builtUrl . startsWith ( "http" ) && this . options ?. baseUrl ) {
@@ -647,15 +643,26 @@ export class FetchClient {
647
643
}
648
644
649
645
const isAbsoluteUrl = builtUrl . startsWith ( "http" ) ;
650
- const localhostOrigin = builtUrl . startsWith ( "/" )
651
- ? "http://localhost"
652
- : "http://localhost/" ;
653
646
654
- const origin : string | undefined = isAbsoluteUrl
655
- ? undefined
656
- : globalThis . location ?. origin ?? localhostOrigin ;
657
-
658
- const parsed = origin ? new URL ( builtUrl , origin ) : new URL ( builtUrl ) ;
647
+ let parsed : URL | undefined = undefined ;
648
+ if ( isAbsoluteUrl ) {
649
+ parsed = new URL ( builtUrl ) ;
650
+ } else if (
651
+ globalThis . location ?. origin &&
652
+ globalThis . location ?. origin . startsWith ( "http" )
653
+ ) {
654
+ if ( builtUrl . startsWith ( "/" ) ) {
655
+ parsed = new URL ( builtUrl , globalThis . location . origin ) ;
656
+ } else {
657
+ parsed = new URL ( builtUrl , globalThis . location . origin + "/" ) ;
658
+ }
659
+ } else {
660
+ if ( builtUrl . startsWith ( "/" ) ) {
661
+ parsed = new URL ( builtUrl , "http://localhost" ) ;
662
+ } else {
663
+ parsed = new URL ( builtUrl , "http://localhost/" ) ;
664
+ }
665
+ }
659
666
660
667
if ( options ?. params ) {
661
668
for ( const [ key , value ] of Object . entries ( options ?. params ) ) {
@@ -665,15 +672,16 @@ export class FetchClient {
665
672
parsed . searchParams . set ( key , value as string ) ;
666
673
}
667
674
}
668
-
669
- builtUrl = parsed . toString ( ) ;
670
675
}
671
676
677
+ builtUrl = parsed . toString ( ) ;
678
+
672
679
const result = isAbsoluteUrl
673
680
? builtUrl
674
681
: `${ parsed . pathname } ${ parsed . search } ` ;
675
682
676
- return result ;
683
+ console . log ( builtUrl , result ) ;
684
+ return { builtUrl : result , absoluteUrl : builtUrl } ;
677
685
}
678
686
679
687
private validateResponse (
0 commit comments