From 0fca8f38313f2d45f3ee9acb851cf1e80ae66c9d Mon Sep 17 00:00:00 2001 From: Igor Propisnov Date: Wed, 22 May 2024 11:28:20 +0200 Subject: [PATCH] Added some swagger api docs --- backend/src/main.ts | 1 - .../auth-module/controller/auth.controller.ts | 18 ++++++++++++++++++ .../auth-module/models/dto/tokens.dto.ts | 11 +++++++++++ .../models/dto/user-credentials.dto.ts | 12 ++++++++++++ 4 files changed, 41 insertions(+), 1 deletion(-) diff --git a/backend/src/main.ts b/backend/src/main.ts index 4b8e604..6963b6c 100644 --- a/backend/src/main.ts +++ b/backend/src/main.ts @@ -8,7 +8,6 @@ async function setupSwagger(app) { .setTitle('Tickets API') .setDescription('Description of the API') .setVersion('0.0.0') - .addTag('tickets') .build(); const document = SwaggerModule.createDocument(app, config); SwaggerModule.setup('api', app, document); diff --git a/backend/src/modules/auth-module/controller/auth.controller.ts b/backend/src/modules/auth-module/controller/auth.controller.ts index a9bda8d..91de9b2 100644 --- a/backend/src/modules/auth-module/controller/auth.controller.ts +++ b/backend/src/modules/auth-module/controller/auth.controller.ts @@ -10,11 +10,17 @@ import { AuthService } from '../services/auth.service'; import { TokensDto, UserCredentialsDto } from '../models/dto'; import { RefreshTokenGuard } from '../common/guards'; import { GetCurrentUser, GetCurrentUserId, Public } from '../common/decorators'; +import { ApiCreatedResponse, ApiTags } from '@nestjs/swagger'; +@ApiTags('Authentication') @Controller('auth') export class AuthController { constructor(private readonly authService: AuthService) {} + @ApiCreatedResponse({ + description: 'User signed up successfully', + type: TokensDto, + }) @Public() @Post('signup') @HttpCode(HttpStatus.CREATED) @@ -24,6 +30,10 @@ export class AuthController { return this.authService.signup(userCredentials); } + @ApiCreatedResponse({ + description: 'User signin successfully', + type: TokensDto, + }) @Public() @Post('signin') @HttpCode(HttpStatus.OK) @@ -33,12 +43,20 @@ export class AuthController { return this.authService.signin(userCredentials); } + @ApiCreatedResponse({ + description: 'User signed out successfully', + type: Boolean, + }) @Post('logout') @HttpCode(HttpStatus.OK) public async logout(@GetCurrentUserId() userId: number): Promise { return this.authService.logout(userId); } + @ApiCreatedResponse({ + description: 'User tokens refreshed successfully', + type: TokensDto, + }) @Public() @UseGuards(RefreshTokenGuard) @Post('refresh') diff --git a/backend/src/modules/auth-module/models/dto/tokens.dto.ts b/backend/src/modules/auth-module/models/dto/tokens.dto.ts index c4ad37b..2d6e111 100644 --- a/backend/src/modules/auth-module/models/dto/tokens.dto.ts +++ b/backend/src/modules/auth-module/models/dto/tokens.dto.ts @@ -1,10 +1,21 @@ +import { ApiProperty } from '@nestjs/swagger'; import { IsNotEmpty, IsString } from 'class-validator'; export class TokensDto { + @ApiProperty({ + title: 'Access token', + description: 'Access token', + example: 'eyJhbGci', + }) @IsNotEmpty() @IsString() public access_token: string; + @ApiProperty({ + title: 'Refresh token', + description: 'Refresh token', + example: 'eyJhbGci', + }) @IsNotEmpty() @IsString() public refresh_token: string; diff --git a/backend/src/modules/auth-module/models/dto/user-credentials.dto.ts b/backend/src/modules/auth-module/models/dto/user-credentials.dto.ts index 3fd7563..b258423 100644 --- a/backend/src/modules/auth-module/models/dto/user-credentials.dto.ts +++ b/backend/src/modules/auth-module/models/dto/user-credentials.dto.ts @@ -1,10 +1,22 @@ +import { ApiProperty } from '@nestjs/swagger'; import { IsEmail, IsNotEmpty, IsString, MinLength } from 'class-validator'; export class UserCredentialsDto { + @ApiProperty({ + title: 'E-Mail', + description: 'User email', + example: 'foo@bar.com', + }) @IsNotEmpty() @IsEmail() public email: string; + @ApiProperty({ + title: 'Password', + description: 'User password', + example: '$tr0ngP@$$w0rd', + minLength: 8, + }) @IsNotEmpty() @IsString() @MinLength(8)