From harness-claude
Apply NestJS class-based and functional middleware via consumer.forRoutes for pre-routing logic like logging, rate limiting, correlation IDs, and Express integration on specific routes or controllers.
npx claudepluginhub intense-visions/harness-engineering --plugin harness-claudeThis skill uses the workspace's default tool permissions.
> Apply NestMiddleware and functional middleware with consumer.forRoutes binding
Implements NestJS guards and interceptors for authentication, authorization, logging, and request/response transformation. Covers CanActivate, ExecutionContext, and JWT patterns for cross-cutting concerns.
Implements NestJS Interceptors to transform responses, log execution times, add timeouts, cache results, and handle errors across routes.
Provides Laravel middleware best practices: before/after patterns, terminable middleware, groups, parameters, and examples for auth checks, security headers, and request logging.
Share bugs, ideas, or general feedback.
Apply NestMiddleware and functional middleware with consumer.forRoutes binding
helmet, compression, cors) into a NestJS appNestMiddleware:@Injectable()
export class CorrelationIdMiddleware implements NestMiddleware {
use(req: Request, res: Response, next: NextFunction): void {
req['correlationId'] = req.headers['x-correlation-id'] ?? randomUUID();
res.setHeader('X-Correlation-ID', req['correlationId']);
next();
}
}
export function requestLogger(req: Request, res: Response, next: NextFunction): void {
console.log(`${req.method} ${req.url}`);
next();
}
configure(consumer: MiddlewareConsumer):@Module({ controllers: [UsersController] })
export class UsersModule implements NestModule {
configure(consumer: MiddlewareConsumer): void {
consumer.apply(CorrelationIdMiddleware).forRoutes(UsersController); // apply to entire controller
consumer.apply(requestLogger).forRoutes({ path: 'users', method: RequestMethod.GET }); // specific method
}
}
consumer
.apply(AuthMiddleware)
.exclude({ path: 'auth/login', method: RequestMethod.POST })
.forRoutes(UsersController);
main.ts (no NestJS wiring needed):import helmet from 'helmet';
app.use(helmet());
app.use(compression());
.apply(Middleware1, Middleware2).forRoutes(...) — they execute in order.NestJS middleware is equivalent to Express middleware — it receives (req, res, next) and must call next() or terminate the response. It runs before any NestJS-specific pipeline elements (guards, interceptors, pipes).
Middleware vs Guards: Middleware runs before routing. Guards run after routing with handler/controller metadata available. For authentication, use guards (they have ExecutionContext). For infrastructure concerns (logging, tracing, rate limiting), use middleware.
forRoutes() target types:
{ path, method } object — applies to specific HTTP method + path combinationsClass vs functional: Class middleware can inject providers (e.g., LoggerService, ConfigService) through the constructor. Functional middleware is simpler and slightly faster but cannot use DI. Choose functional when no injection is needed.
Execution order: Middleware declared in a module applies only to routes in that module (unless applied globally via app.use()). Import order in AppModule determines middleware execution order across modules.
Async middleware: Both class and functional middleware can be async. Errors thrown (or passed to next(err)) propagate to NestJS exception filters.
https://docs.nestjs.com/middleware