Skip to content

Conditional middleware execution#370

Merged
A6dulmalik merged 3 commits intoMindBlockLabs:mainfrom
shamoo53:Conditional-Middleware-Execution
Mar 26, 2026
Merged

Conditional middleware execution#370
A6dulmalik merged 3 commits intoMindBlockLabs:mainfrom
shamoo53:Conditional-Middleware-Execution

Conversation

@shamoo53
Copy link
Copy Markdown
Contributor

🚀 PR: Add Conditional Middleware Execution (unless & onlyFor)

🧭 Overview

This PR introduces conditional middleware execution utilities that allow developers to selectively apply or skip middleware based on route patterns. This significantly improves developer experience (DX) by removing the need for complex guard-based workarounds.


🎯 Problem

In the current setup:

  • Middleware is applied globally or per route without flexibility
  • Developers must use awkward guards or manual checks to exclude routes
  • No built-in support for conditional middleware execution

💡 Solution

  • Introduce two reusable helpers:

    • unless() → skips middleware for specified routes
    • onlyFor() → applies middleware only to specified routes
  • Support flexible route matching using:

    • Exact paths
    • Regular expressions
    • Glob patterns (via micromatch)

🛠 Scope of Work

⚙️ Conditional Middleware Utilities

  • Created src/middleware/utils/conditional.middleware.ts

  • Implemented:

    • unless(middleware, patterns)
    • onlyFor(middleware, patterns) (inverse logic)

🔍 Route Matching Support

  • Added support for:

    • Exact string paths ('/health')
    • Regex patterns (/^\/api\/v1/)
    • Glob patterns ('/api/**') using micromatch

⚡ Performance Consideration

  • Designed as a stateless higher-order wrapper
  • Ensures zero/minimal overhead on non-matching routes

📦 Exports

  • Exported both helpers from src/index.ts for easy consumption

📊 Acceptance Criteria

  • ✔️ unless(['/health']) skips middleware for specified routes
  • ✔️ Supports exact string, regex, and glob pattern matching
  • ✔️ No measurable overhead on non-matching routes
  • ✔️ Both helpers are exported from src/index.ts

🧪 Testing

  • Added unit tests for:

    • Matching logic (string, regex, glob)
    • unless() behavior
    • onlyFor() behavior
  • Added integration tests to verify:

    • Middleware is skipped/applied correctly in real request flows

📚 Usage Example

import { unless, onlyFor } from '@/middleware/utils/conditional.middleware';

// Skip middleware for health and metrics
app.use(unless(authMiddleware, ['/health', '/metrics']));

// Apply middleware only for API routes
app.use(onlyFor(authMiddleware, ['/api/**']));

🏁 Summary

This PR adds a powerful and flexible middleware control layer, improving code clarity, reducing boilerplate, and enabling cleaner route-based logic in NestJS applications.
Closes #354
Closes #351
Closes #318
Closes #314

@drips-wave
Copy link
Copy Markdown

drips-wave bot commented Mar 26, 2026

@shamoo53 Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

@A6dulmalik A6dulmalik merged commit 475bff5 into MindBlockLabs:main Mar 26, 2026
5 of 6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants