From 754495e17982dad2ee1df4c96fe7981abccbc306 Mon Sep 17 00:00:00 2001 From: Anand Chowdhary Date: Tue, 27 Oct 2020 20:15:02 +0530 Subject: [PATCH] :sparkles: Add OpenAPI docs --- src/main.ts | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/main.ts b/src/main.ts index 765002fda..01e35e066 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,12 +1,29 @@ import { ValidationPipe } from '@nestjs/common'; import { NestFactory } from '@nestjs/core'; -import { AppModule } from './app.module'; import helmet from 'helmet'; +import { AppModule } from './app.module'; +import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger'; +import { promises } from 'fs'; +import { join } from 'path'; async function bootstrap() { const app = await NestFactory.create(AppModule); + app.useGlobalPipes(new ValidationPipe()); app.use(helmet()); + + const pkg = JSON.parse( + await promises.readFile(join('.', 'package.json'), 'utf8'), + ); + + const options = new DocumentBuilder() + .setTitle('API') + .setDescription('API description') + .setVersion(pkg.version) + .build(); + const document = SwaggerModule.createDocument(app, options); + SwaggerModule.setup('api', app, document); + await app.listen(3000); } bootstrap();