-
Notifications
You must be signed in to change notification settings - Fork 13
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
Changes from 3 commits
0526bf8
50da197
5d2cf68
ff64d08
8c34059
e7e94f6
96ef4f7
7aa82fd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { CreatedResponseInterceptor } from './created-response.interceptor' | ||
|
||
describe('CreatedResponseInterceptor', () => { | ||
it('should be defined', () => { | ||
expect(new CreatedResponseInterceptor()).toBeDefined() | ||
}) | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { CallHandler, ExecutionContext, Injectable, NestInterceptor } from '@nestjs/common' | ||
import { Observable } from 'rxjs' | ||
import { map } from 'rxjs/operators' | ||
|
||
@Injectable() | ||
export class CreatedResponseInterceptor implements NestInterceptor { | ||
intercept(context: ExecutionContext, next: CallHandler): Observable<any> { | ||
return next.handle().pipe(map((data) => (data === undefined ? {} : data))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Awesome! ✨ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @shogo-mitomo I guess we need to use this value as There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Oops, I'll add it soon 🙇♂️ Thank you. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I'm sorry, I didn't quite understand. What are the benefits of this? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
} | ||
} |
There was a problem hiding this comment.
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 theresource
in the response payload.https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/201
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 think200
would be the most appropriate response code! 🙇 🙏