Interceptors have a set of useful capabilities which are inspired by the Chain of Responsibility
Pattern.
Create a Interceptor
An Interceptor
must return a intercept
method.
We now support interceptor as middleware for Express (opens in a new tab)
ExpressInterceptor
- create an interceptorExpressErrorInterceptor
- create an error interceptor
The ExpressErrorInterceptor
should be used for handle global error.
src/interceptors/tracking.interceptor.ts
Or Error Interceptor
export const GlobalApiErrorInterceptor = (err: Error, req: Request, res: Response, next: Next) => {
// ... logic here
return next();
};
Interceptor on Demand
The @UseInterceptors
decorator is a decorator that accept an Array
of Interceptor
.
The @UseInterceptors
accept both Interceptor
and 3rd parties express
middlewares like multer
, cors
, helmet
.
src/controllers/todos.controller.ts
Or Use multer (opens in a new tab) for file upload with other interceptors.
src/controllers/todos.controller.ts