⚠️ 本项目已停止维护,已迁移至新项目 nestjs-knife4j-plus,请使用新项目以获取最新功能和修复。
⚠️ This project is no longer maintained and has been migrated to the new project nestjs-knife4j-plus. Please use the new project to get the latest features and fixes.
The provided code sets up Knife4j for enhancing Swagger/OpenAPI documentation in a NestJS application, supporting both Express and Fastify HTTP adapters. Thanks to @xiaoymin for providing the WebUI.
# Install the package
npm install nestjs-knife4j2 @nestjs/swagger
# For Fastify adapter, also install:
npm install @fastify/static| @fastify/static version | Fastify version |
|---|---|
^8.x |
^5.x |
^7.x |
^4.x |
^5.x |
^3.x |
^2.x |
^2.x |
^1.x |
^1.x |
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger'
import { knife4jSetup } from 'nestjs-knife4j2'
// To use this with Fastify, first install @fastify/static. Please ensure version compatibility.
async function bootstrap() {
const options = new DocumentBuilder()
.setTitle('Cats example')
.setDescription('The cats API description')
.setVersion('1.0')
.addTag('cats')
.build()
const document = SwaggerModule.createDocument(app, options)
SwaggerModule.setup('api', app, document)
// if you want to use express adapter, you can use the following code
knife4jSetup(app, [
{
name: '2.0 version',
url: `/api-json`,
},
])
await app.listen(3000)
}then you can browse on http://127.0.0.1:3000/doc.html
- 优化传参&整合对 fastify 的支持
- 增加对 openapi 中 operationId 重复的处理
- 删除多余静态资源,减小体积
- 修复 bug issues/2