Skip to content

Enhancer Responsibility and Execution ​

Choose one responsibility per component, then choose its binding reach and lifetime separately:

ComponentFocused responsibilityInventory example
MiddlewareTransport setup or correlationRecords arrival before Nest enhancers
GuardWhether this caller may proceedChecks a fixture credential before reading the command
PipeParse/transform/validate one boundary valueRejects strings as quantities and unexpected fields
InterceptorWrap an operation for one cross-cutting outcomeRecords entry, result or error without changing the command
FilterFinal transport mapping for an uncaught failureMaps stock failure to safe 409 and unknown failure to safe 500
Application operationCoordinate the capabilityRequests an inventory reservation
Domain objectPreserve its invariant for every callerRejects nonpositive or excessive stock reservations

Global binding describes reach. Provider scope describes lifetime. Module imports/exports describe visibility. A global authentication guard still has one responsibility; a route-bound class can still mix unrelated responsibilities. Neither global binding nor provider count establishes a design defect or performance cost. Do not store a request's principal in singleton mutable fields.

Order is a control-flow contract ​

For the fixture's single body argument and HTTP registration, successful execution is:

  1. Middleware; global, controller, then route guards.
  2. Global, controller, then route interceptor entry.
  3. Global, controller, route, then parameter pipes; controller/application operation.
  4. Route, controller, then global interceptor response handling.

A denied guard stops before interceptors and pipes. A pipe/application failure can unwind interceptor error handling before the selected filter maps the uncaught error. A consuming route filter prevents a later global filter from handling that same exception. Multiple parameters have their own pipe ordering; interceptors that recover or short-circuit also alter the path. Test the exact composition instead of applying this list unconditionally.

The runtime fixture and its HTTP tests record these paths. They also prove that APP_FILTER registration and useGlobalFilters(app.get(GlobalFilter)) both preserve a singleton's injected collaborator. A missing constructor dependency fails startup. This does not establish request-scoped bootstrap resolution, Fastify parity or hybrid/gateway coverage.

The trace array is a sequential test recorder, not a production request-context design. Existing provider-lifetime tests separately prove manual Nest-context isolation. Follow api-design.md, security.md, exception-filters-transports.md and testing.md for the underlying contracts.


Canonical source: skills/nestjs-features-performance/references/enhancer-composition.md. This page is generated during the documentation build.

Open-source guidance for deliberate NestJS engineering.

Open-source guidance for deliberate NestJS engineering.

Released under the Apache License 2.0.