[Feature Request]: HttpResponse
class for better return status ✨
#15244
Labels
HttpResponse
class for better return status ✨
#15244
Uh oh!
There was an error while loading. Please reload this page.
Is there an existing issue that is already proposing this?
Is your feature request related to a problem? Please describe it
The Issue
Very often in development, we'd like to return a different status code rather than the obvious ones:
GET
/PUT
/DELETE
/PATCH
=>200
POST
=>201
For instance, we can return
204 - No Content
when there are no results for the query, or304 - Not Modified
to reduce traffic when there are no changes.The Current Usage
Nest allows us to directly use the Response instance, by default from
express
:Based on the official Nest.js documentation, this approach needs to be handled very elegantly, as by default it disables the standard way that Nest handles endpoints.
Official Docs
Proposal - Return
HttpResponse
Exists API for Exceptions
Just like throwing errors with
HttpException
instance, for example theForbiddenException
class:New API
It would be nice to have the same capabilities for a non-error response, so we can control the status code with a specific class dynamically, and potentially more custom options that are not currently provided with the robust all-included controller engine of Nest.js.
The Solution
Just like
HttpException
is handled by the default Exceptions Handler, we could introduce a new Interceptor that developers could register globally, per controller, or per route.This Interceptor will check if the return type is an instance of the new classes and change the behavior accordingly.
Side Note
I'm open to ideas about what else we can add here, not just the status. Technically, with the right API, it could be customizable at any level, allowing the end user to define whatever they want with 1-2 lines of code, potentially removing the need to directly touch the response object 80% of the time!
I'll be more than happy to be involved in this development if the community finds it interesting enough 🥷
I guess developers like me would find it beneficial as well 🙏
The text was updated successfully, but these errors were encountered: