import { Context, Middleware } from "../../mod.ts";
export type BodyState = {
json?: unknown;
formData?: FormData;
text?: string;
bytes?: Uint8Array;
bodyError?: Error;
_parsed?: boolean;
};
export type BodyParserContext = Context & { state?: BodyState };
export const bodyParser: Middleware = async (req, ctx, next) => {
const method = req.method;
if (!["POST", "PUT", "PATCH"].includes(method)) return next();
const extendedCtx = ctx as BodyParserContext;
if (!extendedCtx.state) extendedCtx.state = {};
const state = extendedCtx.state;
if (state._parsed) return next();
const ct = req.headers.get("content-type") || "";
if (ct.length === 0) return next();
try {
if (ct.includes("application/json")) {
state.json = await req.json();
} else if (ct.includes("multipart/form-data")) {
state.formData = await req.formData();
} else if (ct.includes("application/x-www-form-urlencoded")) {
state.formData = await req.formData();
} else if (ct.startsWith("text/")) {
state.text = await req.text();
} else {
state.bytes = new Uint8Array(await req.arrayBuffer());
}
state._parsed = true;
} catch (e) {
console.error("Body parse failed:", e);
state.bodyError = e as Error;
}
return next();
};