#If I have a controller for a module, what's the best way to change the HTTP Responses?

4 messages · Page 1 of 1 (latest)

brittle crater
#

I'm using TypeORM and want to make sure I am returning instances that then get serialized. But I am trying to follow: https://jsonapi.org/format/ which recommends adding a "type" property and putting the response under a "data" property.

I know that whatever gets returned at the end of a controller method is the HTTP Response body so I could write a wrapper function that does this, but is there a standardized way of doing this? I feel like doing my own version of it might cause problems with Swagger etc...

gaunt creek
#

Interceptors. We even have a section under them about response mapping

brittle crater
#

I've been looking at that in the docs, but I didn't see the response mapping part. Only about excluding. But yeah if I could do an interceptor for the base app module it'd transform the API responses for all my controllers right?

#

This is the link if anybody else has this question. Thanks jmcdo29. I should have read the docs more thoroughly. https://docs.nestjs.com/interceptors#response-mapping