From ba54833dc72a4df52a3caff08216c11637d77de9 Mon Sep 17 00:00:00 2001 From: Pridon Tetradze Date: Sat, 2 Sep 2023 11:49:29 +0400 Subject: [PATCH] feat: add initial swagger docs (#8) --- package-lock.json | 41 +++++++++++++++++-- package.json | 1 + src/app.controller.ts | 2 + src/main.ts | 11 +++++ src/modules/shop/cart/carts.controller.ts | 3 ++ .../shop/product/products.controller.ts | 2 + src/modules/user/auth/auth.controller.ts | 2 + 7 files changed, 59 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index c72ff57..610d5cd 100644 --- a/package-lock.json +++ b/package-lock.json @@ -18,6 +18,7 @@ "@nestjs/mongoose": "^10.0.1", "@nestjs/passport": "^10.0.0", "@nestjs/platform-express": "^10.0.0", + "@nestjs/swagger": "^7.1.10", "bcrypt": "^5.1.1", "class-transformer": "^0.5.1", "class-validator": "^0.14.0", @@ -1904,6 +1905,37 @@ "yarn": ">= 1.13.0" } }, + "node_modules/@nestjs/swagger": { + "version": "7.1.10", + "resolved": "https://registry.npmjs.org/@nestjs/swagger/-/swagger-7.1.10.tgz", + "integrity": "sha512-qreCcxgHFyFX1mOfK36pxiziy4xoa/XcxC0h4Zr9yH54WuqMqO9aaNFhFyuQ1iyd/3YBVQB21Un4gQnh9iGm0w==", + "dependencies": { + "@nestjs/mapped-types": "2.0.2", + "js-yaml": "4.1.0", + "lodash": "4.17.21", + "path-to-regexp": "3.2.0", + "swagger-ui-dist": "5.4.2" + }, + "peerDependencies": { + "@fastify/static": "^6.0.0", + "@nestjs/common": "^9.0.0 || ^10.0.0", + "@nestjs/core": "^9.0.0 || ^10.0.0", + "class-transformer": "*", + "class-validator": "*", + "reflect-metadata": "^0.1.12" + }, + "peerDependenciesMeta": { + "@fastify/static": { + "optional": true + }, + "class-transformer": { + "optional": true + }, + "class-validator": { + "optional": true + } + } + }, "node_modules/@nestjs/testing": { "version": "10.1.3", "resolved": "https://registry.npmjs.org/@nestjs/testing/-/testing-10.1.3.tgz", @@ -3028,8 +3060,7 @@ "node_modules/argparse": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/argparse/-/argparse-2.0.1.tgz", - "integrity": "sha512-8+9WqebbFzpX9OR+Wa6O29asIogeRMzcGtAINdpMHHyAg10f05aSFVBbcEqGf/PXw1EjAZ+q2/bEBg3DvurK3Q==", - "dev": true + "integrity": "sha512-8+9WqebbFzpX9OR+Wa6O29asIogeRMzcGtAINdpMHHyAg10f05aSFVBbcEqGf/PXw1EjAZ+q2/bEBg3DvurK3Q==" }, "node_modules/array-flatten": { "version": "1.1.1", @@ -7085,7 +7116,6 @@ "version": "4.1.0", "resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-4.1.0.tgz", "integrity": "sha512-wpxZs9NoxZaJESJGIZTyDEaYpl0FKSA+FB9aJiyemKhMwkxQg63h4T1KJgUGHpTqPDNRcmmYLugrRjJlBtWvRA==", - "dev": true, "dependencies": { "argparse": "^2.0.1" }, @@ -10430,6 +10460,11 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/swagger-ui-dist": { + "version": "5.4.2", + "resolved": "https://registry.npmjs.org/swagger-ui-dist/-/swagger-ui-dist-5.4.2.tgz", + "integrity": "sha512-vT5QxP/NOr9m4gLZl+SpavWI3M9Fdh30+Sdw9rEtZbkqNmNNEPhjXas2xTD9rsJYYdLzAiMfwXvtooWH3xbLJA==" + }, "node_modules/symbol-observable": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/symbol-observable/-/symbol-observable-4.0.0.tgz", diff --git a/package.json b/package.json index 099b181..64b142d 100644 --- a/package.json +++ b/package.json @@ -29,6 +29,7 @@ "@nestjs/mongoose": "^10.0.1", "@nestjs/passport": "^10.0.0", "@nestjs/platform-express": "^10.0.0", + "@nestjs/swagger": "^7.1.10", "bcrypt": "^5.1.1", "class-transformer": "^0.5.1", "class-validator": "^0.14.0", diff --git a/src/app.controller.ts b/src/app.controller.ts index 300e5af..3ea7498 100644 --- a/src/app.controller.ts +++ b/src/app.controller.ts @@ -1,7 +1,9 @@ import { Controller, Get } from '@nestjs/common'; import { AppService } from './app.service'; import { ExceptionService } from './shared'; +import { ApiTags } from '@nestjs/swagger'; +@ApiTags('root') @Controller() export class AppController { constructor( diff --git a/src/main.ts b/src/main.ts index d7bf164..5ead977 100644 --- a/src/main.ts +++ b/src/main.ts @@ -2,6 +2,7 @@ import { NestFactory } from '@nestjs/core'; import { AppModule } from './app.module'; import { ValidationPipe } from '@nestjs/common'; import { HttpExceptionsFilter } from './http-exceptions.filter'; +import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger'; import * as cookieParser from 'cookie-parser'; async function bootstrap() { @@ -14,6 +15,16 @@ async function bootstrap() { ); app.use(cookieParser()); app.useGlobalFilters(new HttpExceptionsFilter()); + + const config = new DocumentBuilder() + .setTitle('EverREST') + .setDescription('EverREST API description') + .setVersion('0.0.0') + .build(); + const document = SwaggerModule.createDocument(app, config); + // TODO: Add swagger 'ApiProperty' to DTOs + SwaggerModule.setup('docs/swagger', app, document); + await app.listen(process.env.PORT || 3000); } bootstrap(); diff --git a/src/modules/shop/cart/carts.controller.ts b/src/modules/shop/cart/carts.controller.ts index 6a73ca7..4d813c7 100644 --- a/src/modules/shop/cart/carts.controller.ts +++ b/src/modules/shop/cart/carts.controller.ts @@ -12,6 +12,9 @@ import { CartsService } from './carts.service'; import { CurrentUser, CurrentUserInterceptor, JwtGuard } from 'src/shared'; import { UserPayload } from 'src/interfaces'; import { AddProductToCartDto, ProductIdDto } from '../dtos'; +import { ApiTags } from '@nestjs/swagger'; + +@ApiTags('cart') @Controller('shop/cart') @UseGuards(JwtGuard) @UseInterceptors(CurrentUserInterceptor) diff --git a/src/modules/shop/product/products.controller.ts b/src/modules/shop/product/products.controller.ts index 3517b56..23ea036 100644 --- a/src/modules/shop/product/products.controller.ts +++ b/src/modules/shop/product/products.controller.ts @@ -21,7 +21,9 @@ import { import { CurrentUser, CurrentUserInterceptor, JwtGuard } from 'src/shared'; import { UserPayload } from 'src/interfaces'; import { MongooseValidatorService } from 'src/shared/mongoose-validator.service'; +import { ApiTags } from '@nestjs/swagger'; +@ApiTags('products') @Controller('shop/products') export class ProductsController { constructor( diff --git a/src/modules/user/auth/auth.controller.ts b/src/modules/user/auth/auth.controller.ts index ac13753..bf9e451 100644 --- a/src/modules/user/auth/auth.controller.ts +++ b/src/modules/user/auth/auth.controller.ts @@ -22,7 +22,9 @@ import { LocalAuthGuard, RefreshJwtGuard } from './guards'; import { Response } from 'express'; import { CurrentUser, CurrentUserInterceptor, JwtGuard } from 'src/shared'; import { UserPayload } from 'src/interfaces'; +import { ApiTags } from '@nestjs/swagger'; +@ApiTags('auth') @Controller('auth') export class AuthController { constructor(private authService: AuthService) {}