Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 If the response body is empty, return an empty JSON instead #59

Merged
merged 8 commits into from
Apr 24, 2020

Conversation

shogo-mitomo
Copy link
Member

@shogo-mitomo shogo-mitomo self-assigned this Apr 24, 2020
@shogo-mitomo shogo-mitomo added the bug Something isn't working label Apr 24, 2020
@Controller('auth')
export class AuthController {
constructor(private authService: AuthService) {}

@UsePipes(new ValidationPipe(VALIDATION_PIPE_OPTIONS))
@ApiOperation({ summary: 'Login endpoint for normal user' })
@ApiNoContentResponse()
@ApiCreatedResponse()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@shogo-mitomo
After thinking a bit, I think finally it would be better to just use 200.
Because 201 spec states a new resource is created, and must return the resource in the response payload.
https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/201

The HTTP 201 Created success status response code indicates that the request has succeeded and has led to the creation of a resource. The new resource is effectively created before this response is sent back and the new resource is returned in the body of the message, its location being either the URL of the request, or the content of the Location header.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yashmurty
Oh, I understood. Then all of our POST endpoints should return 200, right?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@shogo-mitomo
You are absolutely right.
Since we never return the created object, and we want to return a non empty response {}, I think 200 would be the most appropriate response code! 🙇 🙏

@shogo-mitomo shogo-mitomo added ⚠️ DO NOT MERGE The PR author will merge when ready. and removed ⚠️ DO NOT MERGE The PR author will merge when ready. labels Apr 24, 2020
@Injectable()
export class CreatedResponseInterceptor implements NestInterceptor {
intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
return next.handle().pipe(map((data) => (data === undefined ? {} : data)))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome! ✨

Copy link
Member

@yashmurty yashmurty Apr 24, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@shogo-mitomo I guess we need to use this value as type: {} somehow for swagger documentation?
Also, let's use this interceptor as backup, but also start explicitly returning {} in the response body from controllers as well? 🙇

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yashmurty

I guess we need to use this value as type: {} somehow for swagger documentation?

Oops, I'll add it soon 🙇‍♂️ Thank you.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yashmurty

Also, let's use this interceptor as backup, but also start explicitly returning {} in the response body from controllers as well? 🙇

I'm sorry, I didn't quite understand. What are the benefits of this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@shogo-mitomo I mean to say, if we new team mate looks at controller file, he will see just return now and guess return response should be empty.
So to make things clear, in controller file as well, let's add return {} explicitly.
But in situation where we forget to add, the interceptor that you added will still take care of it. 🙇

@shogo-mitomo
Copy link
Member Author

@yashmurty I updated them all. Please check it again 🙇‍♂️

@ApiBadRequestResponse()
@Post('/users')
async postAdminUser(@Request() req, @Body() createAdminRequest: CreateAdminRequestDto) {
@HttpCode(200)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@shogo-mitomo I believe we do not need this?
If we were using @ApiResponse, we would need it I guess.
But since we use @ApiOkResponse, it will automatically add 200 code.

Please correct me if I'm wrong? :bow

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yashmurty
This is not for Swagger.
NestJS returns 201 for POST method by default, so I have to overwrite it 😅
https://docs.nestjs.com/controllers#status-code

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@shogo-mitomo
Oh I See! I understand now. 🙏
Thanks for the clarification! 🙇

@HttpCode(200)
async setPositiveFlag(@Body() setPositiveFlag: SetPositiveFlagDto): Promise<CreatedResponse> {
this.adminsService.setPositiveFlag(setPositiveFlag)
return {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome! ⭐

async adminUserLoginFirebase(@Request() req): Promise<void> {
return this.authService.adminUserlogin(req.user)
@HttpCode(200)
async adminUserLoginFirebase(@Request() req): Promise<CreatedResponse> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Promise<CreatedResponse>

This is great!
Even the return type is explicit now! 🎉

Copy link
Member

@yashmurty yashmurty left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! 🎉
Thanks for the clarifications! 🙇

@shogo-mitomo shogo-mitomo merged commit d21239a into master Apr 24, 2020
@shogo-mitomo shogo-mitomo deleted the mitomo/empty_json branch April 24, 2020 07:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants