Interceptors wrap controller method execution and can transform input, output, or return early. They run after guards and around the controller method.
Interceptors form a dispatch chain. Each interceptor calls next() to invoke the next interceptor or the final controller method:
After guards pass
|
v
+-------------------------------------------+
| Global interceptors |
+-------------------------------------------+
|
v
+-------------------------------------------+
| Controller interceptors |
| @UseInterceptors on the class |
+-------------------------------------------+
|
v
+-------------------------------------------+
| Method interceptors |
| @UseInterceptors on the method |
+-------------------------------------------+
|
v
+-------------------------------------------+
| Controller method |
+-------------------------------------------+
|
v
Return value
|
v
Interceptors unwind in reverse order and
each layer can transform the result.
The context provides the same fields as GuardContext plus the controller instance and handler name. The next() function calls the next interceptor in the chain or the controller method.
If validateResponse is enabled, the transformed output from interceptors still needs to match the declared @Returns schema for the final status code. Validation runs after the full interceptor chain unwinds.