I think is a great idea to remove responses inside controllers and use REST patterns to manage response status code, for example, POST should return 201 status code, GET should return 200.
Reduce some controller complexity, great for controller testing. :-D
What do you think? :)
@lsilva-kununu it's not good approach to remove status codes at all, because post doesn't have to return 201 all the time. You can also do a POST|PUT|PATCH /resource/id to UPDATE the entity, and this will return 200, POST /resource/subresource can also return 204 as NO CONTENT, GET might return 202 as ACCEPTED.
But I totally agree that controllers should be able to return a promise from method and nest should respond to the ether with resolved value from promise && status 200 with content type suitable to the requested.
@cojack I didnt say "remove status codes", the idea is to not use anymore res.status(HttpStatus.OK).json(user);
inside controllers. I think nest should follow rest patterns and apply "status code" out of the box.
A good idea is to have annotations for status code to have custom status code responses or if annotations are not setted, nest should apply status code based on rest patterns.
A lot of frameworks just expect arrays from controllers because normally, the application have layers/middlewares to check if user is authenticated, if resource exists, to check content request, to make validations, so, you dont need to use res
inside of your controller :P
Thumbs up if you like :)
@lsilva-kununu If you like it, try routing-controllers. You return a value from action there and it's automatically wrapped into response.
@19majkel94 I love this way. Is better and avoid a lot of mock in controller tests.
@cojack watch https://github.com/pleerock/routing-controllers. Very usefull
Hi @luqezman,
It's possible since the v4 release. Now, you can return plain value, promise and even Observable
. Hope you'll enjoy! http://www.docs.nestjs.com/controllers
Thanks, Kamil
@kamilmysliwiec Love you again <3
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Most helpful comment
@lsilva-kununu it's not good approach to remove status codes at all, because post doesn't have to return 201 all the time. You can also do a POST|PUT|PATCH /resource/id to UPDATE the entity, and this will return 200, POST /resource/subresource can also return 204 as NO CONTENT, GET might return 202 as ACCEPTED.
But I totally agree that controllers should be able to return a promise from method and nest should respond to the ether with resolved value from promise && status 200 with content type suitable to the requested.