Skip to content

Commit 7f4cc05

Browse files
committed
More tweaks to buildUrl
1 parent 0714412 commit 7f4cc05

File tree

2 files changed

+10
-17
lines changed

2 files changed

+10
-17
lines changed

src/FetchClient.test.ts

+5-15
Original file line numberDiff line numberDiff line change
@@ -767,31 +767,21 @@ Deno.test("can getJSON relative URL", async () => {
767767
let requestedUrl = "";
768768
const fakeFetch = (req: URL | Request | string): Promise<Response> =>
769769
new Promise((resolve) => {
770-
if (typeof req === "string") {
771-
requestedUrl = req;
772-
} else if (req instanceof Request) {
770+
if (req instanceof Request) {
773771
requestedUrl = req.url;
774772
} else {
775773
requestedUrl = req.toString();
776774
}
777-
const data = JSON.stringify({});
778-
resolve(new Response(data));
775+
resolve(new Response());
779776
});
780777

781778
provider.fetch = fakeFetch;
782779
const client = provider.getFetchClient();
783780

784-
await client.getJSON<Products>(
785-
`/todos/1`,
786-
{
787-
params: {
788-
limit: 3,
789-
},
790-
},
791-
);
792-
assertEquals(requestedUrl, "http://localhost/todos/1?limit=3");
781+
await client.getJSON(`/todos/1`);
782+
assertEquals(requestedUrl, "http://localhost/todos/1");
793783

794-
await client.getJSON<Products>(
784+
await client.getJSON(
795785
`todos/1`,
796786
{
797787
params: {

src/FetchClient.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -647,12 +647,15 @@ export class FetchClient {
647647
}
648648

649649
const isAbsoluteUrl = builtUrl.startsWith("http");
650+
const localhostOrigin = builtUrl.startsWith("/")
651+
? "http://localhost"
652+
: "http://localhost/";
650653

651654
const origin: string | undefined = isAbsoluteUrl
652655
? undefined
653-
: globalThis.location?.origin ?? "http://localhost";
656+
: globalThis.location?.origin ?? localhostOrigin;
654657

655-
const parsed = new URL(builtUrl, origin);
658+
const parsed = origin ? new URL(builtUrl, origin) : new URL(builtUrl);
656659

657660
if (options?.params) {
658661
for (const [key, value] of Object.entries(options?.params)) {

0 commit comments

Comments
 (0)