FASTRO v1.0.6

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:

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:

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!