-
Notifications
You must be signed in to change notification settings - Fork 0
Home
npm i @kimyu0218/swagger-decorator-builder
This project is a Swagger Decorator Builder designed to simplify the creation of Swagger decorators for API documentation.
The existing Swagger decorators often result in lengthy code, impacting readability. In response to this issue, I initiated this project to create custom decorators using a builder pattern, aiming for more concise and efficient decorator composition.
export const FindAllCatsDecorator = (target: string, returnType: any) =>
new SwaggerDecoratorBuilder(target, 'GET', returnType) // Automatically set API operation summary. (target: 'cats' -> GET cats)
.removeResponse(401) // Remove the default 401 API response.
.removeResponse(403) // Remove the default 403 API response.
.removeResponse(404) // Remove the default 404 API response.
.build();
export const UpdateCatDecorator = (
target: string,
param: ApiParamOptions,
body: ApiBodyOptions,
) =>
new SwaggerDecoratorBuilder(target, 'PATCH') // Automatically set API operation summary. (target: 'cat' -> PATCH cat)
.addParam(param) // Set the request param.
.setBody(body) // Set the request body.
.addResponse({ status: 403, description: 'Forbidden - Unauthorized User' }) // Overwrite the default 403 API response.
.build();
- Import
SwaggerDecoratorBuilder
class.
import { SwaggerDecoratorBuilder } from '@kimyu0218/swagger-decorator-builder';
- Pass
target
,method
parameters to the constructor. The builder will automatically create a summary of@ApiOperation()
decorator. If you want to put areturnType
in@ApiOkResponse()
, you can optionally hand over thereturnType
!
new SwaggerDecoratorBuilder(target, 'GET', returnType) ...
- If needed, use the
addParam
andsetBody
methods to add@ApiParam()
and@ApiBody()
decorators.
new SwaggerDecoratorBuilder(target, 'PATCH').addParam(param).setBody(body) ...
- Use the
addResponse
method to set additional API response decorators. (By default, responses for status codes 200, 401, 403, 404, and 500 are generated.) You can also remove default responses byremove
method.
new SwaggerDecoratorBuilder(target, 'POST')
.setBody(body)
.removeResponse(200)
.removeResponse(403)
.removeResponse(404)
.addResponse({ status: 201, description: 'create cat' }) ...
- Call the
build
method to get the final decorator!!
new SwaggerDecoratorBuilder(target, 'GET', returnType)
.removeResponse(401)
.removeResponse(403)
.removeResponse(404)
.build();
The Swagger Decorator Builder facilitates the creation of concise and readable API documentation.
The introduction of the builder pattern streamlines code writing, making API documentation management more efficient. Through this project, users can write cleaner code and effectively manage API documentation.
@Patch(':id')
@UpdateCatDecorator(
'Cat',
{ type: 'uuid', name: 'id', description: 'identifier of cat' },
{ type: UpdateCatDto, description: 'update dto of cat' },
)
update(@Param('id') id: string, @Body() updateCatDto: UpdateCatDto) {}
It's important to note that this project is still in development. My goal is to align with and enhance the Swagger decorators based on the official Swagger documentation.
[KR] ๋๋ ์ Swagger Decorator Builder๋ฅผ ๋ง๋ค์์๊น?