-
Notifications
You must be signed in to change notification settings - Fork 1.1k
[don't merge]poc: customize statusCode using response spec #1260
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
Changes from all commits
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 |
|---|---|---|
|
|
@@ -6,6 +6,7 @@ | |
| import {ServerResponse as Response} from 'http'; | ||
| import {OperationRetval} from './internal-types'; | ||
| import {HttpError} from 'http-errors'; | ||
| import {ResolvedRoute} from './router/routing-table'; | ||
| import {Readable} from 'stream'; | ||
|
|
||
| /** | ||
|
|
@@ -20,6 +21,7 @@ export function writeResultToResponse( | |
| response: Response, | ||
| // result returned back from invoking controller method | ||
| result: OperationRetval, | ||
| route?: ResolvedRoute, | ||
| ): void { | ||
| if (result) { | ||
| if (result instanceof Readable || typeof result.pipe === 'function') { | ||
|
|
@@ -48,6 +50,14 @@ export function writeResultToResponse( | |
| result = result.toString(); | ||
| break; | ||
| } | ||
| if (route) { | ||
| const spec = route.spec; | ||
| const responsesCode = Object.keys(spec.responses || {}); | ||
| if (responsesCode.length >= 1) { | ||
| const successStatusCode = responsesCode[0]; | ||
|
Contributor
Author
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. The assumption here is risky.
Contributor
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. We might need to allow
Member
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. What are the risks? What if we filtered
Contributor
Author
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.
Much better than my approach here! |
||
| response.statusCode = parseInt(successStatusCode); | ||
| } | ||
| } | ||
| response.write(result); | ||
| } | ||
| response.end(); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -58,6 +58,12 @@ describe('HttpHandler', () => { | |
| .withOperationReturningString('get', '/hello', 'hello') | ||
| .withOperationReturningString('get', '/bye', 'bye') | ||
| .withOperationReturningString('post', '/hello', 'postHello') | ||
| .withOperationReturningStringWithStatusCode( | ||
|
Member
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. Not needed for this spike, but required for the real implementation: Please create a new |
||
| 'post', | ||
| '/createHello', | ||
| 201, | ||
| 'createHello', | ||
| ) | ||
| .build(); | ||
|
|
||
| class HelloController { | ||
|
|
@@ -72,11 +78,19 @@ describe('HttpHandler', () => { | |
| public async postHello(): Promise<string> { | ||
| return 'hello posted'; | ||
| } | ||
|
|
||
| public async createHello(): Promise<string> { | ||
| return 'hello created'; | ||
|
Contributor
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. We should be able to allow a method to return an instance of return HttpResponse.statusCode(201).header('Location', '...').body(...);
Member
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 have a similar proposal in #788 (comment): @post('/products')
async create(@requestBody() obj: Product)
: Promise<ControllerResponse<Product>> {
const model = await this.productRepository.create(obj);
return {
statusCode: 201,
// FIXME: Do not hard-code `model.id`, find the actual PK property name.
// FIXME: Can Location contain a path only, with no hostname and scheme?
headers: {location: `/products/${model.id}`},
body: model,
}
}IIUC, @jannyHou is exploring a different approach in this spike pull request, one where the status code is automatically inferred from the OpenAPI spec. |
||
| } | ||
| } | ||
|
|
||
| givenControllerClass(HelloController, spec); | ||
| }); | ||
|
|
||
| it('returns 201 for "POST /createHello"', () => { | ||
| return client.post('/createHello').expect(201, 'hello created'); | ||
| }); | ||
|
|
||
| it('executes hello() for "GET /hello"', () => { | ||
| return client.get('/hello').expect('hello'); | ||
| }); | ||
|
|
||
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.
Instead of adding more functions can we just add
statusCodeas an optional parameter for the existing functions?