Skip to content

Commit

Permalink
support strict option
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Jul 10, 2023
1 parent c4b9c8e commit 4c36817
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
32 changes: 20 additions & 12 deletions src/utils/body.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type InternalRequest<T = any> = IncomingMessage & {
const PayloadMethods: HTTPMethod[] = ["PATCH", "POST", "PUT", "DELETE"];

/**
* Reads body of the request and returns encoded raw string (default), or `Buffer` if encoding is not set.
* Reads body of the request and returns encoded raw string (default), or `Buffer` if encoding is falsy.
* @param event {H3Event} H3 event or req passed by h3 handler
* @param encoding {Encoding} encoding="utf-8" - The character encoding to use.
*
Expand Down Expand Up @@ -91,7 +91,8 @@ export function readRawBody<E extends Encoding = "utf8">(
* ```
*/
export async function readBody<T = any>(
event: H3Event
event: H3Event,
options: { strict?: boolean } = {}
): Promise<T | undefined | string> {
const request = event.node.req as InternalRequest<T>;
if (ParsedBodySymbol in request) {
Expand All @@ -104,21 +105,13 @@ export async function readBody<T = any>(
let parsed: T;

if (contentType === "application/json") {
try {
parsed = destr(body, { strict: true }) as T;
} catch {
throw createError({
statusCode: 400,
statusMessage: "Bad Request",
message: "Invalid JSON body",
});
}
parsed = _parseJSON(body, options.strict ?? true) as T;
} else if (contentType === "application/x-www-form-urlencoded") {
parsed = _parseURLEncodedBody(body!) as T;
} else if (contentType.startsWith("text/")) {
parsed = body as T;
} else {
parsed = destr(body, { strict: false }) as T;
parsed = _parseJSON(body, options.strict ?? false) as T;
}

request[ParsedBodySymbol] = parsed;
Expand All @@ -143,6 +136,21 @@ export async function readMultipartFormData(event: H3Event) {

// --- Internal ---

function _parseJSON(body = "", strict: boolean) {
if (!body) {
return undefined;
}
try {
return destr(body, { strict });
} catch {
throw createError({
statusCode: 400,
statusMessage: "Bad Request",
message: "Invalid JSON body",
});
}
}

function _parseURLEncodedBody(body: string) {
const form = new URLSearchParams(body);
const parsedForm: Record<string, any> = Object.create(null);
Expand Down
2 changes: 1 addition & 1 deletion test/body.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ describe("", () => {
});

it("handles non-present body", async () => {
let _body = "initial";
let _body;
app.use(
"/",
eventHandler(async (request) => {
Expand Down

0 comments on commit 4c36817

Please sign in to comment.