NestJS : transform responses
Typically you would use the built-in ClassSerializerInterceptor
in combination with the ValidationPipe
(with transform: true
). It automatically calls classToPlain
on the response:
In your dto (with toPlainOnly
):
@Transform((money: ExchangeableMoney) => money.localValues, {toPlainOnly: true})public foobar: ExchangeableMoney;
In your controller:
@UseInterceptors(ClassSerializerInterceptor)
or globally in your main.ts:
app.useGlobalInterceptors(new ClassSerializerInterceptor(app.get(Reflector)));