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
1 change: 1 addition & 0 deletions src/utils.url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ export function withQuery(input: string, query?: QueryObject): string {
if (value === undefined) {
searchParams.delete(key);
} else if (Array.isArray(value)) {
searchParams.delete(key);
for (const item of value) {
searchParams.append(key, normalizeQueryValue(item));
}
Expand Down
14 changes: 14 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,20 @@ describe("ofetch", () => {
});
});

it("replaces existing array query params instead of accumulating", async () => {
// When the URL already carries a value for a key and the query option
// supplies an array for that same key, the existing value should be
// replaced, not accumulated alongside the new ones. The /url/** endpoint
// echoes the raw path+search so we can inspect all repeated values.
const result = await $fetch(getURL("url/check?tag=old&other=1"), {
query: { tag: ["a", "b"] },
});
// "tag=old" must not appear; only "tag=a" and "tag=b" should be present
expect(result).toContain("tag=a");
expect(result).toContain("tag=b");
expect(result).not.toContain("tag=old");
});

it("deep merges defaultOptions", async () => {
const _customFetch = $fetch.create({
query: {
Expand Down