Skip to content

Commit

Permalink
Merge pull request nestjs#137 from nartc/api-responses-swagger-doc
Browse files Browse the repository at this point in the history
feature: added common ApiResponses for Swagger portion
  • Loading branch information
kamilmysliwiec authored Aug 17, 2018
2 parents c6851f6 + c97a213 commit bdc5250
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
68 changes: 68 additions & 0 deletions src/app/homepage/pages/recipes/swagger/swagger.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,74 @@ <h4>Responses</h4>
To define a custom HTTP response, we use <code>@ApiResponse()</code> decorator.
</p>
<pre><code class="language-typescript">{{ response }}</code></pre>
<p>
Same as common <strong>HTTP Exceptions</strong> defined in <a routerLink="/exception-filters"><strong>Exception Filters</strong></a>,
Nest also provides a set of usable <strong>API Responses</strong> that inherits from the core <code>@ApiResponse</code> decorator:
</p>
<ul>
<li>
<code>@ApiOkResponse()</code>
</li>
<li>
<code>@ApiCreatedResponse()</code>
</li>
<li>
<code>@ApiBadRequestResponse()</code>
</li>
<li>
<code>@ApiUnauthorizedResponse()</code>
</li>
<li>
<code>@ApiNotFoundResponse()</code>
</li>
<li>
<code>@ApiForbiddenResponse()</code>
</li>
<li>
<code>@ApiMethodNotAllowedResponse()</code>
</li>
<li>
<code>@ApiNotAcceptableResponse()</code>
</li>
<li>
<code>@ApiRequestTimeoutResponse()</code>
</li>
<li>
<code>@ApiConflictResponse()</code>
</li>
<li>
<code>@ApiGoneResponse()</code>
</li>
<li>
<code>@ApiPayloadTooLargeResponse()</code>
</li>
<li>
<code>@ApiUnsupportedMediaTypeResponse()</code>
</li>
<li>
<code>@ApiUnprocessableEntityResponse()</code>
</li>
<li>
<code>@ApiInternalServerErrorResponse()</code>
</li>
<li>
<code>@ApiNotImplementedResponse()</code>
</li>
<li>
<code>@ApiBadGatewayResponse()</code>
</li>
<li>
<code>@ApiServiceUnavailableResponse()</code>
</li>
<li>
<code>@ApiGatewayTimeoutResponse()</code>
</li>
</ul>
<p>
In addition to the available <strong>HTTP Exceptions</strong>, Nest provides short-hand decorators for:
<code>HttpStatus.OK</code>, <code>HttpStatus.CREATED</code> and <code>HttpStatus.METHOD_NOT_ALLOWED</code>
</p>
<pre><code class="language-typescript">{{ customResponse }}</code></pre>
<h4>Authentication</h4>
<p>
You can enable the bearer authorization using <code>addBearerAuth()</code> method of the <code>DocumentBuilder</code> class.
Expand Down
10 changes: 10 additions & 0 deletions src/app/homepage/pages/recipes/swagger/swagger.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,16 @@ async create(@Body() createCatDto: CreateCatDto) {
}`;
}

get customResponse() {
return `
@Post()
@ApiCreatedResponse({ description: 'The record has been successfully created.'})
@ApiForbiddenResponse({ description: 'Forbidden.'})
async create(@Body() createCatDto: CreateCatDto) {
this.catsService.create(createCatDto);
}`;
}

get bearerAuth() {
return `
@ApiUseTags('cats')
Expand Down

0 comments on commit bdc5250

Please sign in to comment.