🪺
NestJS に OpenAPI を組み込む
NestJS に Open API / Swagger UI を組み込みます。
1.公式でライブラリが用意されているのでインストールする。
npm install --save @nestjs/swagger
2.設定を追加する。
main.ts の bootstrap に処理を追加する。
main.ts
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
// ここを追加
const config = new DocumentBuilder()
.setTitle('Swagger UI のタイトル')
.setDescription('Swagger UI の説明文')
.setVersion('1.0')
.addTag('cats')
.build();
const document = SwaggerModule.createDocument(app, config);
SwaggerModule.setup('api', app, document);
await app.listen(3000);
}
bootstrap();
最低限はこれだけでOK
3.確認
npm start して /api や /api-json にアクセス
Discussion