Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 59 additions & 4 deletions src/utils.url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,67 @@ export function withBase(input = "", base = ""): string {
return input;
}

const _base = withoutTrailingSlash(base);
if (input.startsWith(_base)) {
return input;
if (!base.includes("?") && !base.includes("#")) {
const _base = withoutTrailingSlash(base);
if (input.startsWith(_base)) {
return input;
}

return joinURL(_base, input);
}

const baseHashIndex = base.indexOf("#");
const baseHash = baseHashIndex === -1 ? "" : base.slice(baseHashIndex);
const baseWithoutHash =
baseHashIndex === -1 ? base : base.slice(0, baseHashIndex);

const baseQueryIndex = baseWithoutHash.indexOf("?");
const baseQuery =
baseQueryIndex === -1 ? "" : baseWithoutHash.slice(baseQueryIndex + 1);
const basePath =
baseQueryIndex === -1
? baseWithoutHash
: baseWithoutHash.slice(0, baseQueryIndex);

const inputHashIndex = input.indexOf("#");
const inputHash = inputHashIndex === -1 ? "" : input.slice(inputHashIndex);
const inputWithoutHash =
inputHashIndex === -1 ? input : input.slice(0, inputHashIndex);

const inputQueryIndex = inputWithoutHash.indexOf("?");
const inputQuery =
inputQueryIndex === -1 ? "" : inputWithoutHash.slice(inputQueryIndex + 1);
const inputPath =
inputQueryIndex === -1
? inputWithoutHash
: inputWithoutHash.slice(0, inputQueryIndex);

const _basePath = withoutTrailingSlash(basePath);
const joinedPath = inputPath.startsWith(_basePath)
? inputPath
: joinURL(_basePath, inputPath);

let query = "";
if (baseQuery && inputQuery) {
const searchParams = new URLSearchParams(baseQuery);
const inputParams = new URLSearchParams(inputQuery);
const seen = new Set<string>();
for (const [key, value] of inputParams) {
if (seen.has(key)) {
searchParams.append(key, value);
} else {
seen.add(key);
searchParams.set(key, value);
}
}
query = searchParams.toString();
} else if (baseQuery || inputQuery) {
query = baseQuery || inputQuery;
}

return joinURL(_base, input);
const hash = inputHash || baseHash;

return `${joinedPath}${query ? `?${query}` : ""}${hash}`;
}

function withoutTrailingSlash(path?: string): string {
Expand Down
78 changes: 78 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
import { Readable } from "node:stream";
import { H3, HTTPError, readBody, serve } from "h3";
import { $fetch } from "../src/index.ts";
import { withBase } from "../src/utils.url.ts";

describe("ofetch", () => {
let listener: ReturnType<typeof serve>;
Expand Down Expand Up @@ -116,6 +117,28 @@ describe("ofetch", () => {
);
});

it("baseURL with query string", async () => {
expect(await $fetch("/users", { baseURL: getURL("url?key=abc") })).to.equal(
"/url/users?key=abc"
);
expect(
await $fetch("/users", {
baseURL: getURL("url?key=abc"),
query: { page: 2 },
})
).to.equal("/url/users?key=abc&page=2");
expect(
await $fetch("/users?foo=bar", {
baseURL: getURL("url?key=abc"),
})
).to.equal("/url/users?key=abc&foo=bar");
expect(
await $fetch("/users?key=override", {
baseURL: getURL("url?key=initial"),
})
).to.equal("/url/users?key=override");
});

it("stringifies posts body automatically", async () => {
const { body } = await $fetch(getURL("post"), {
method: "POST",
Expand Down Expand Up @@ -525,3 +548,58 @@ describe("ofetch", () => {
});
});
});

describe("withBase", () => {
it("joins simple base and path", () => {
expect(withBase("/users", "/api")).toBe("/api/users");
expect(withBase("/users", "http://localhost:3000/api")).toBe(
"http://localhost:3000/api/users"
);
});

it("handles base with query string", () => {
expect(withBase("/users", "http://localhost:3000/api?key=abc")).toBe(
"http://localhost:3000/api/users?key=abc"
);
expect(withBase("users", "/api?key=abc")).toBe("/api/users?key=abc");
});

it("merges query strings when both base and input have queries", () => {
expect(withBase("/users?page=2", "http://localhost:3000/api?key=abc")).toBe(
"http://localhost:3000/api/users?key=abc&page=2"
);

expect(
withBase("/users?key=new", "http://localhost:3000/api?key=old&other=1")
).toBe("http://localhost:3000/api/users?key=new&other=1");
});

it("preserves hash fragments", () => {
expect(
withBase("/users#results", "http://localhost:3000/api?key=abc")
).toBe("http://localhost:3000/api/users?key=abc#results");

expect(withBase("/users", "http://localhost:3000/api?key=abc#base")).toBe(
"http://localhost:3000/api/users?key=abc#base"
);
});

it("preserves base when input is empty or root", () => {
expect(withBase("", "http://localhost:3000/api?key=abc")).toBe(
"http://localhost:3000/api?key=abc"
);
expect(withBase("/", "http://localhost:3000/api?key=abc")).toBe(
"http://localhost:3000/api?key=abc"
);
});

it("does not duplicate base path if input already starts with it", () => {
expect(withBase("/api/users", "/api")).toBe("/api/users");
expect(
withBase(
"http://localhost:3000/api/users",
"http://localhost:3000/api?key=abc"
)
).toBe("http://localhost:3000/api/users?key=abc");
});
});