Skip to content

Commit

Permalink
fix: handle existing arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed Dec 13, 2022
1 parent 94c2830 commit e09702a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
10 changes: 6 additions & 4 deletions src/utils/body.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,14 @@ export async function readBody<T=any> (event: H3Event): Promise<T> {
const form = new URLSearchParams(body);
const parsedForm: Record<string, any> = Object.create(null);
for (const [key, value] of form.entries()) {
if (key in parsedForm && !Array.isArray(parsedForm[key])) {
parsedForm[key] = [parsedForm[key]];
parsedForm[key].push(value);
} else {
if (!(key in parsedForm)) {
parsedForm[key] = value;
continue;
}
if (!Array.isArray(parsedForm[key])) {
parsedForm[key] = [parsedForm[key]];
}
parsedForm[key].push(value);
}
return parsedForm as unknown as T;
}
Expand Down
4 changes: 2 additions & 2 deletions test/body.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,12 @@ describe("", () => {
expect(body).toMatchObject({
field: "value",
another: "true",
number: ["20", "30"]
number: ["20", "30", "40"]
});
return "200";
}));
const result = await request.post("/api/test")
.send("field=value&another=true&number=20&number=30");
.send("field=value&another=true&number=20&number=30&number=40");

expect(result.text).toBe("200");
});
Expand Down

0 comments on commit e09702a

Please sign in to comment.