Nest: Content Negotiation in Nest

Created on 29 Apr 2020  路  6Comments  路  Source: nestjs/nest

Is there anything similar to res.format in express that you can use without needing to directly interact with the native response? I've looked through docs and tried discord a few times but haven't got anywhere with this.

If this isn't currently supported would writing an interceptor be the recommended way of achieving it or would it be necessary to use another method?

needs triage question 馃檶

All 6 comments

@btilford I know we talked briefly on Discord about this. Looking into how the Express adapter sends responses, this is probably something you can do in an interceptor, as you'll have full access to the request (for incoming headers), the response (for outgoing headers), and the response body (via the map() operator from rxjs). I planned on getting to doing a small test with it today 馃樃

@btilford got it! Here's the repo. This is absolutely possible in interceptors. Sorry for steering you wrong yesterday.

Awesome, looks like that's exactly what I was looking for. I assume I could use the ExecutionContext to read in metadata for things like template paths if I were using a template engine there as well?

It may require a bit of metadata reading, but it should be possible. I'm not sure where that information would be (if it is on the req or res objects), but if not you can always rely on the reflector and get metadata from the current handler.

Sounds good, I'd probably need to write some decorators to add metadata but I think I've got enough info to get started.

@jmcdo29 thanks for helping out!

As we are using GitHub to track bugs, feature requests, and potential improvements, please, use either StackOverflow or Discord next time. :)

Was this page helpful?
0 / 5 - 0 ratings