Skip to content

Commit 0219ef0

Browse files
committed
More buildUrl changes
1 parent 7f4cc05 commit 0219ef0

File tree

1 file changed

+31
-23
lines changed

1 file changed

+31
-23
lines changed

src/FetchClient.ts

+31-23
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ export class FetchClient {
414414
options: RequestOptions,
415415
init?: RequestInit,
416416
): Promise<FetchClientResponse<T>> {
417-
url = this.buildUrl(url, options);
417+
const { builtUrl, absoluteUrl } = this.buildUrl(url, options);
418418

419419
const accessToken = this.options.accessTokenFunc?.() ?? null;
420420
if (accessToken !== null) {
@@ -488,17 +488,10 @@ export class FetchClient {
488488
let request: Request | null = null;
489489

490490
try {
491-
request = new Request(url, init);
491+
request = new Request(builtUrl, init);
492492
} 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);
502495
}
503496

504497
const context: FetchClientContext = {
@@ -635,7 +628,10 @@ export class FetchClient {
635628
};
636629
}
637630

638-
private buildUrl(url: string, options: RequestOptions | undefined): string {
631+
private buildUrl(
632+
url: string,
633+
options: RequestOptions | undefined,
634+
): { builtUrl: string; absoluteUrl: string } {
639635
let builtUrl = url;
640636

641637
if (!builtUrl.startsWith("http") && this.options?.baseUrl) {
@@ -647,15 +643,26 @@ export class FetchClient {
647643
}
648644

649645
const isAbsoluteUrl = builtUrl.startsWith("http");
650-
const localhostOrigin = builtUrl.startsWith("/")
651-
? "http://localhost"
652-
: "http://localhost/";
653646

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+
}
659666

660667
if (options?.params) {
661668
for (const [key, value] of Object.entries(options?.params)) {
@@ -665,15 +672,16 @@ export class FetchClient {
665672
parsed.searchParams.set(key, value as string);
666673
}
667674
}
668-
669-
builtUrl = parsed.toString();
670675
}
671676

677+
builtUrl = parsed.toString();
678+
672679
const result = isAbsoluteUrl
673680
? builtUrl
674681
: `${parsed.pathname}${parsed.search}`;
675682

676-
return result;
683+
console.log(builtUrl, result);
684+
return { builtUrl: result, absoluteUrl: builtUrl };
677685
}
678686

679687
private validateResponse(

0 commit comments

Comments
 (0)