Skip to content

Feat/microservices pre request hook v12#16429

Merged
kamilmysliwiec merged 9 commits intonestjs:v12.0.0from
suuuuuuminnnnnn:feat/microservices-pre-request-hook-v12
Feb 25, 2026
Merged

Feat/microservices pre request hook v12#16429
kamilmysliwiec merged 9 commits intonestjs:v12.0.0from
suuuuuuminnnnnn:feat/microservices-pre-request-hook-v12

Conversation

@suuuuuuminnnnnn
Copy link
Copy Markdown
Contributor

PR Checklist

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Other... Please describe:

What is the current behavior?

There is no way to execute logic before Guards in microservice message handlers. This makes it impossible to set up request-scoped context (e.g., AsyncLocalStorage-based correlation ID propagation) that needs to be available to Guards and downstream handlers.

Issue Number: #1627

What is the new behavior?

NestMicroservice now exposes useGlobalPreRequestHooks(...hooks) which registers hooks that execute before Guards for every pattern handler.

Each hook receives ExecutionContext and an Observable-returning next() function:

Execution order per request:

preRequest hooks -> Guards -> Interceptors -> Pipes -> Handler

Key design decisions:

  • Hooks run inside RpcProxy try/catch, so exceptions are handled by existing ExceptionFilters
  • next() returns Observable, allowing rxjs operators (tap, catchError) to be chained naturally
  • Fast-path when no hooks are registered: zero runtime overhead
  • PreRequestHook interface exported from @nestjs/common

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

WebSocket gateway support is intentionally out of scope and can be addressed in a follow-up PR.

# Conflicts:
#	packages/common/interfaces/index.ts
#	packages/common/interfaces/nest-microservice.interface.ts
#	packages/microservices/context/rpc-context-creator.ts
#	packages/microservices/test/context/rpc-context-creator.spec.ts
@kamilmysliwiec kamilmysliwiec added this to the 12.0.0 milestone Feb 25, 2026
@kamilmysliwiec
Copy link
Copy Markdown
Member

Would you like to create a PR to the docs (just to show a usage example)?

@kamilmysliwiec kamilmysliwiec merged commit 519c877 into nestjs:v12.0.0 Feb 25, 2026
1 check passed
@suuuuuuminnnnnn
Copy link
Copy Markdown
Contributor Author

yes, i'd be happy to do that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants