+
Skip to content
Merged
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
25 changes: 25 additions & 0 deletions src/_deprecated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,31 @@ export async function readFormDataBody(event: H3Event): Promise<FormData> {
export const readFormData: (event: H3Event) => Promise<FormData> =
readFormDataBody;

/** @deprecated Please use `event.req.formData()` */
export async function readMultipartFormData(event: H3Event): Promise<
Array<{
data: Uint8Array;
name?: string;
filename?: string;
type?: string;
}>
> {
const formData = await event.req.formData();

return Promise.all(
[...formData.entries()].map(async ([key, value]) => {
return value instanceof Blob
? {
name: key,
type: value.type,
filename: value.name,
data: await value.bytes(),
}
: { name: key, data: new TextEncoder().encode(value) };
}),
);
}

/** @deprecated Please use `event.req.body` */
export function getBodyStream(
event: H3Event,
Expand Down
48 changes: 47 additions & 1 deletion test/body.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createReadStream } from "node:fs";
import { readFile } from "node:fs/promises";
import { readBody } from "../src/index.ts";
import { readBody, readMultipartFormData } from "../src/index.ts";
import { describeMatrix } from "./_setup.ts";

describeMatrix("body", (t, { it, expect, describe }) => {
Expand Down Expand Up @@ -328,5 +328,51 @@ describeMatrix("body", (t, { it, expect, describe }) => {
expect(result.status).toBe(200);
expect(await result.json()).toMatchObject({ user: "john" });
});

it("parses multipart form data", async () => {
t.app.all("/api/*", async (event) => {
const multipartFormData = await readMultipartFormData(event);

expect(multipartFormData[0].name).toBe("baz");
expect(multipartFormData[0].data instanceof Uint8Array).toBe(true);
expect(new TextDecoder().decode(multipartFormData[0].data)).toBe(
"other",
);

expect(multipartFormData[1].name).toBe("号楼电表数据模版.xlsx");
expect(multipartFormData[1].data instanceof Uint8Array).toBe(true);
expect(new TextDecoder().decode(multipartFormData[1].data)).toBe(
"something",
);

return multipartFormData.map((part) => ({
...part,
data: new TextDecoder().decode(part.data),
}));
});

const result = await t.fetch("/api/test", {
method: "POST",
headers: {
"content-type":
"multipart/form-data; boundary=---------------------------12537827810750053901680552518",
},
body: '-----------------------------12537827810750053901680552518\r\nContent-Disposition: form-data; name="baz"\r\n\r\nother\r\n-----------------------------12537827810750053901680552518\r\nContent-Disposition: form-data; name="号楼电表数据模版.xlsx"\r\n\r\nsomething\r\n-----------------------------12537827810750053901680552518--\r\n',
});

expect(result.status).toBe(200);
expect(await result.json()).toMatchInlineSnapshot(`
[
{
"data": "other",
"name": "baz",
},
{
"data": "something",
"name": "号楼电表数据模版.xlsx",
},
]
`);
});
});
});
1 change: 1 addition & 0 deletions test/unit/package.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ describe("h3 package", () => {
"readBody",
"readFormData",
"readFormDataBody",
"readMultipartFormData",
"readRawBody",
"readValidatedBody",
"redirect",
Expand Down
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载