Skip to content

Commit 93004db

Browse files
committed
update nest.js
1 parent 054b59f commit 93004db

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

basic/framework/nest.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,33 @@ bootstrap();
9999

100100
Module 代码可以参考 Express Swagger 的示例项目: <https://github.com/nestjs/nest/tree/master/sample/11-swagger>
101101

102+
生成 Open-API.json 文件示例代码:
103+
104+
```js
105+
import { resolve } from 'path';
106+
import { writeFileSync } from 'fs';
107+
108+
import { NestFactory } from '@nestjs/core';
109+
import { FastifyAdapter, NestFastifyApplication } from '@nestjs/platform-fastify';
110+
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
111+
import { AppModule } from './app.module';
112+
113+
async function bootstrap() {
114+
const app = await NestFactory.create<NestFastifyApplication>(AppModule, new FastifyAdapter());
115+
const options = new DocumentBuilder()
116+
.setTitle('Cats example')
117+
.setDescription('The cats API description')
118+
.setVersion('1.0')
119+
.addTag('cats')
120+
.addBearerAuth()
121+
.build();
122+
// 生成的 JSON 格式文档,可以导出静态化
123+
const document = SwaggerModule.createDocument(app, options);
124+
writeFileSync(resolve(__dirname, '../api.json'), JSON.stringify(document, null, 2), { encoding: 'utf8' });
125+
}
126+
bootstrap();
127+
```
128+
102129

103130
## E2E Testing
104131

0 commit comments

Comments
 (0)