Reputation: 195
I'm implementing a DELETE route using NestJS.
Since the route is only ever supposed to return a HTTP 204 No Content
on successful deletion, utilizing the @nestjs/swagger
decorators, I've annotated it as follows:
@ApiParam({
name: 'id',
required: true,
type: 'string',
example: 'abc123',
})
@ApiNoContentResponse({
description: 'All items for the given id have been deleted',
})
@Delete('/accounts/:id/items')
async deleteItems(
@Param('id') id: string,
) {
// do stuff
}
@nestjs/cli: 9.1.6
@nestjs/common: 9.1.6
@nestjs/core: 9.1.6
@nestjs/platform-express: 9.1.6
@nestjs/swagger: 6.1.2
{
"$schema": "https://json.schemastore.org/nest-cli",
"collection": "@nestjs/schematics",
"sourceRoot": "src",
"compilerOptions": {
"plugins": [
"@nestjs/swagger/plugin"
]
}
}
How can I prevent this (default?) response from being generated?
When running the server with npm run start:dev
, the swagger description for my route is created. Although I've not explicitly defined it, the description now contains an entry for HTTP 200
in the Response
section of the route. The entry contains no description and no indicator that it's not explicitly defined.
When running the server with npm run start:dev
, the swagger description for my route is created. The description only contains and entry for HTTP 204
in the Response
section of the route.
Explicitly defined an @ApiOkResponse
:
// they all have no effect
@ApiOkResponse()
// OR
@ApiOkResponse({})
// OR
@ApiOkResponse({ status: 204 })
Defined an @ApiDefaultResponse
:
// they all create a response 'default' with no description
@ApiDefaultResponse()
// OR
@ApiDefaultResponse({})
// OR
@ApiDefaultResponse({ status: 204 })
Upvotes: 3
Views: 2419
Reputation: 3570
You can just add @HttpCode(204)
as an annotation to your code. 200 is added by default, because this is the default response status code for DELETE
requests.This default behavior would be overwritten with the annotation mentioned above.
More information about the status codes can be found here: https://docs.nestjs.com/controllers
Upvotes: 5