Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NestJS: How to setup ClassSerializerInterceptor as global interceptor

I'm using in every Controller code like @UseInterceptors(ClassSerializerInterceptor) so I decided make it global and was trying to setup it with no luck.

I was trying without and with new and ended up with something like this totally not working.

app.useGlobalInterceptors(new ClassSerializerInterceptor(new Reflector()));

I checked NestJS source code and I assume that it cannot be used as global but it should.

like image 533
G. Bar Avatar asked Apr 17 '19 05:04

G. Bar


People also ask

How do you make an interceptor in Nestjs?

In order to set up the interceptor, we use the @UseInterceptors() decorator imported from the @nestjs/common package. Like pipes and guards, interceptors can be controller-scoped, method-scoped, or global-scoped. Hint The @UseInterceptors() decorator is imported from the @nestjs/common package.

What is serialization in Nestjs?

Serialization is a process that happens before objects are returned in a network response. This is an appropriate place to provide rules for transforming and sanitizing the data to be returned to the client. For example, sensitive data like passwords should always be excluded from the response.

Is interceptor a middleware?

They are basically express middleware functions.


1 Answers

Have you tried using this line of code:

app.useGlobalInterceptors(new ClassSerializerInterceptor(app.get(Reflector)));
like image 106
A. Maitre Avatar answered Sep 28 '22 03:09

A. Maitre