-
Notifications
You must be signed in to change notification settings - Fork 293
Labels
Description
Description
This PR introduces two new features to improve middleware handling in h3:
- Middleware meta support: Adds metadata support to middleware, similar to how routes can have meta
- defineMiddlewareRoute utility: A new utility function for defining middleware as plugins, following the same pattern as
defineRoute
Motivation
Currently, middleware cannot carry metadata like routes can. This limits the ability to attach configuration or other information to middleware that can be accessed at runtime. Additionally, there's no plugin-based way to define middleware similar to defineRoute
.
Changes
1. Middleware Meta Support
- Added
meta?: H3RouteMeta
toMiddlewareOptions
type - Updated
normalizeMiddleware
to handle meta and store it inevent.context.matchedMiddleware
- Added
matchedMiddleware
array toH3EventContext
type to track matched middleware with their metadata
2. defineMiddlewareRoute Utility
- New utility function that returns an H3Plugin for middleware registration
- Supports path patterns, HTTP method filtering, and metadata
- Uses the existing
defineMiddleware
function internally - Consistent API with
defineRoute
Usage Examples
// Define middleware with meta
const authMiddleware = defineMiddlewareRoute({
path: '/api/**',
methods: ['POST', 'PUT', 'DELETE'],
meta: {
requiresAuth: true,
rateLimit: { interval: '1m', tokensPerInterval: 10 }
},
handler: async (event, next) => {
if (\!event.context.user) {
return new Response('Unauthorized', { status: 401 });
}
return next();
}
});
// Register the middleware
app.register(authMiddleware);
// Access meta in handlers
app.get('/api/users', (event) => {
// Access route meta
const routeMeta = event.context.matchedRoute?.meta;
// Access middleware meta
const middlewareMeta = event.context.matchedMiddleware;
// [{ route: '/api/**', meta: { requiresAuth: true, ... } }]
return { users: [] };
});
Benefits
- Consistent API between routes and middleware
- Ability to attach configuration to middleware
- Plugin-based middleware definition for better modularity
- Follows existing h3 patterns and conventions