Contributing to Fastro
Thank you for your interest in contributing to Fastro! This guide will help you get started with contributing to the framework, with a specific focus on creating middlewares.
How to Create a Middleware
Middlewares in Fastro are simple functions that allow you to intercept and modify requests and responses.
1. Basic Middleware Structure
A middleware is a function that receives the Request, the Fastro Context
(ctx), and a next function. To allow the request to proceed to the next
handler, you must return next().
export function myMiddleware(req: Request, ctx: any, next: any) {
console.log(`${req.method} ${ctx.url.pathname}`);
// Perform logic here...
// Return next() to continue execution
return next();
}2. Registering Middleware
You can register middleware globally or for specific routes.
import app from "./mod.ts";
import { myMiddleware } from "./middlewares/my_middleware.ts";
// Global middleware
app.use(myMiddleware);
app.get("/", () => "Hello World");
app.serve();3. Practical Example: Request Logger & State
Here is a simple logger that also attaches a timestamp to the request state:
export function logger(req: Request, ctx: any, next: any) {
const start = Date.now();
// Attach data to the shared context state
ctx.state.startTime = start;
console.log(
`[${new Date().toISOString()}] ${req.method} ${ctx.url.pathname}`,
);
return next();
}Contributing Modules
Fastro's modular architecture allows you to contribute entire sets of functionality as modules. To create a module:
- Create a directory in
modules/. - Add a
mod.tswith a default export function. - Use
createRouterto define your module's routes.
import { createRouter } from "../../mod.ts";
const myModule = createRouter()
.get("/hello", () => "Hello from module!");
export default myModule.build();Development Workflow
- Fork the repository
- Clone your fork
- Create a branch:
git checkout -b feature/my-new-middleware - Make your changes
- Run tests:
deno task test - Submit a Pull Request
Coding Standards
- Use TypeScript.
- Follow the existing code style.
- Add tests for new features or bug fixes.
- Update documentation if necessary.
We look forward to your contributions!