Skip to content

Conversation

svozza
Copy link
Contributor

@svozza svozza commented Sep 5, 2025

Summary

This PR adds the functionality to register middleware and to compose mutliple middlewares to create reusable middleware stacks.

Note: currently there is no way to alter the response inside a middleware function, this will be done in the next PR.

Changes

1. Middleware Composition Logic (src/rest/utils.ts)

  • Implemented composeMiddleware() function for chaining middleware execution
  • Added protection against multiple next() calls
  • Supports middleware short-circuiting by returning responses

2. Router Integration (src/rest/BaseRouter.ts)

  • Added use() method for middleware registration
  • Updated resolve() method to execute middleware chain before route handlers
  • Proper scope binding for class-based handlers

3. Added unit tests (tests/unit/rest/BaseRouter.test.ts)

  • Execution Order: Middleware runs before handlers in registration order
  • Short-circuiting: Middleware can return responses to bypass remaining chain
  • Parameter Passing: Route params and request options passed correctly
  • Error Handling: Errors in middleware before/after next() and in handlers
  • Edge Cases: Multiple next() calls, middleware not calling next()
  • Response Types: Support for both Response objects and JSON objects
  • Class Decorators: Integration with class-based handlers preserving this scope

Issue number: closes #4427


By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.

@svozza svozza requested a review from dreamorosi September 5, 2025 09:08
@svozza svozza self-assigned this Sep 5, 2025
@pull-request-size pull-request-size bot added the size/L PRs between 100-499 LOC label Sep 5, 2025
@boring-cyborg boring-cyborg bot added event-handler This item relates to the Event Handler Utility tests PRs that add or change tests labels Sep 5, 2025
@svozza svozza force-pushed the event-handler/middleware branch from f5ba571 to b327588 Compare September 5, 2025 09:12
@svozza svozza changed the title feat(event-handler): add middleare registration and composition to rest handler feat(event-handler): add middleware registration and composition to rest handler Sep 5, 2025
dreamorosi
dreamorosi previously approved these changes Sep 5, 2025
Copy link
Contributor

@dreamorosi dreamorosi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only left a couple notes, great work!

Copy link

sonarqubecloud bot commented Sep 5, 2025

Copy link
Contributor

@dreamorosi dreamorosi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great stuff!

After we're done with the feature, let's see if we can bring down the code duplication in the unit test files a bit.

@dreamorosi dreamorosi merged commit fc87eb3 into main Sep 5, 2025
38 checks passed
@dreamorosi dreamorosi deleted the event-handler/middleware branch September 5, 2025 13:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
event-handler This item relates to the Event Handler Utility size/L PRs between 100-499 LOC tests PRs that add or change tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature request: Implement middleware composition in event handler
2 participants