Introducing Fastro Body Parser: Efficient Request Handling with 100% Coverage
Handling request bodies is a fundamental task for any modern web API. Whether you are receiving JSON from a frontend app, processing form submissions, or handling raw binary uploads, you need a reliable way to parse and access that data.
Today we are excited to introduce the official bodyParser middleware for Fastro.
Versatile Content Support
The bodyParser middleware is designed to be a "set and forget" solution. It automatically detects the Content-Type header and parses the body into the most appropriate format, storing it in ctx.state for easy access:
- JSON Processing: Automatically handles
application/jsonand parses it intoctx.state.json. - Form Handling: Supports both
multipart/form-dataandapplication/x-www-form-urlencoded, populatingctx.state.formData. - Text & Binary: Handles
text/*formats as strings and falls back to rawUint8Array(bytes) for unknown or binary types.
Built for Performance and Security
Efficiency is at the core of Fastro. The bodyParser middleware includes several optimizations to keep your server running at peak performance:
- Method Filtering: Only attempts to parse bodies for
POST,PUT, andPATCHrequests, completely skipping others likeGETorHEAD. - Idempotent Parsing: Modern applications often use multiple middlewares. Our body parser implements a
_parsedflag to ensure the body is never processed twice, saving valuable CPU cycles. - Fail-Safe Mechanism: If parsing fails (e.g., malformed JSON), the middleware captures the error in
ctx.state.bodyErrorinstead of crashing your server, allowing your handlers to decide how to respond to the client.
100% Unit Test Coverage
Following our standard for core features, the bodyParser middleware was developed with a test-driven mindset. We have achieved 100% branch and line coverage, ensuring that every conditional check and every parsing logic path is verified.
We tested everything from standard JSON payloads to edge cases requiring raw byte handling and even simulated environment-specific quirks in request headers.
Implementation Example
Integrating the body parser into your app is straightforward. Because it's a middleware, you can apply it globally or just to specific route groups:
import Fastro from "./mod.ts";
import { bodyParser } from "./middlewares/bodyparser/mod.ts";
const app = new Fastro();
// Apply globally to all POST/PUT/PATCH routes
app.use(bodyParser);
app.post("/api/user", (req, ctx) => {
// Access parsed data safely from ctx.state
const { json, bodyError } = ctx.state;
if (bodyError) {
return new Response("Invalid request body", { status: 400 });
}
console.log("Registered user:", json.username);
return { success: true };
});
await app.serve();Conclusion
The addition of the bodyParser middleware further simplifies the development of complex APIs on Fastro. By providing a secure, efficient, and fully tested way to handle incoming data, we continue our mission to make Fastro the best bridge between Deno's power and a top-tier developer experience.
Happy coding!