Skip to main content
Middleware is the right primitive for request flow control. Use it for:
  • auth
  • rate limiting
  • caching
  • tenancy resolution
  • request shaping
  • short-circuiting before the handler runs

Example

import { middleware } from "redop";

const tenantMiddleware = middleware(async ({ request, ctx, next }) => {
  ctx.tenantId = request.headers["x-tenant-id"] ?? "public";
  return next();
});

app.use(tenantMiddleware);

When to use middleware instead of hooks

Choose middleware when the logic can:
  • block execution
  • change what happens next
  • depend on the result of next()
  • wrap the handler path
Choose hooks when you are observing lifecycle events rather than controlling them.

Common mistake

Do not use global onAfterHandle when you really need to prevent execution. If something should stop a tool call, it belongs in middleware.