Skip to content

Commit

Permalink
feat: add swagger
Browse files Browse the repository at this point in the history
  • Loading branch information
rajdip-b committed Jan 7, 2024
1 parent cd79172 commit b15dbb0
Show file tree
Hide file tree
Showing 9 changed files with 320 additions and 252 deletions.
2 changes: 2 additions & 0 deletions apps/api/src/app/app.controller.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Controller, Get } from '@nestjs/common'
import { Public } from '../decorators/public.decorator'
import { ApiTags } from '@nestjs/swagger'

@ApiTags('App Controller')
@Controller()
export class AppController {
constructor() {}
Expand Down
54 changes: 52 additions & 2 deletions apps/api/src/auth/controller/auth.controller.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,70 @@
import { Controller, Param, Post, Query } from '@nestjs/common'
import { Controller, HttpStatus, Param, Post, Query } from '@nestjs/common'
import { AuthService } from '../service/auth.service'
import { UserAuthenticatedResponse } from '../auth.types'
import { Public } from '../../decorators/public.decorator'
import { ApiOperation, ApiParam, ApiResponse, ApiTags } from '@nestjs/swagger'

@ApiTags('Auth Controller')
@Controller('auth')
export class AuthController {
constructor(private authService: AuthService) {}

@Public()
@Post('send-otp/:email')
async sendOtp(@Param('email') email: string): Promise<void> {
@ApiOperation({
summary: 'Send OTP',
description:
'This endpoint sends OTPs to an email address. The OTP can then be used to generate valid tokens'
})
@ApiParam({
name: 'email',
description: 'Email to send OTP',
required: true
})
@ApiResponse({
status: HttpStatus.OK,
description: 'Send OTP successfully'
})
@ApiResponse({
status: HttpStatus.BAD_REQUEST,
description: 'Email is invalid'
})
async sendOtp(
@Param('email')
email: string
): Promise<void> {
await this.authService.sendOtp(email)
}

@Public()
@Post('validate-otp')
@ApiOperation({
summary: 'Validate OTP',
description:
'This endpoint validates OTPs. If the OTP is valid, it returns a valid token along with the user details'
})
@ApiParam({
name: 'email',
description: 'Email to send OTP',
required: true
})
@ApiParam({
name: 'otp',
description: 'OTP to validate',
required: true
})
@ApiResponse({
status: HttpStatus.OK,
description: 'Validate OTP successfully'
})
@ApiResponse({
status: HttpStatus.NOT_FOUND,
description: 'Email not found'
})
@ApiResponse({
status: HttpStatus.UNAUTHORIZED,
description: 'OTP is invalid'
})
async validateOtp(
@Query('email') email: string,
@Query('otp') otp: string
Expand Down
2 changes: 2 additions & 0 deletions apps/api/src/environment/controller/environment.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ import { CreateEnvironment } from '../dto/create.environment/create.environment'
import { User } from '@prisma/client'
import { AdminGuard } from '../../auth/guard/admin.guard'
import { UpdateEnvironment } from '../dto/update.environment/update.environment'
import { ApiTags } from '@nestjs/swagger'

@ApiTags('Environment Controller')
@Controller('environment')
export class EnvironmentController {
constructor(private readonly environmentService: EnvironmentService) {}
Expand Down
8 changes: 8 additions & 0 deletions apps/api/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { AppModule } from './app/app.module'
import chalk from 'chalk'
import moment from 'moment'
import { QueryTransformPipe } from './common/query.transform.pipe'
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger'

class CustomLogger implements LoggerService {
log(message: string) {
Expand Down Expand Up @@ -56,6 +57,13 @@ async function bootstrap() {
new QueryTransformPipe()
)
const port = 4200
const swaggerConfig = new DocumentBuilder()
.setTitle('keyshade')
.setDescription('The keyshade API description')
.setVersion('1.0')
.build()
const document = SwaggerModule.createDocument(app, swaggerConfig)
SwaggerModule.setup('docs', app, document)
await app.listen(port)
logger.log(
`🚀 Application is running on: http://localhost:${port}/${globalPrefix}`
Expand Down
2 changes: 2 additions & 0 deletions apps/api/src/project/controller/project.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ import {
} from '../dto/create.project/create.project'
import { UpdateProject } from '../dto/update.project/update.project'
import { AdminGuard } from '../../auth/guard/admin.guard'
import { ApiTags } from '@nestjs/swagger'

@ApiTags('Project Controller')
@Controller('project')
export class ProjectController {
constructor(private readonly service: ProjectService) {}
Expand Down
2 changes: 2 additions & 0 deletions apps/api/src/secret/controller/secret.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ import { User } from '@prisma/client'
import { CreateSecret } from '../dto/create.secret/create.secret'
import { UpdateSecret } from '../dto/update.secret/update.secret'
import { AdminGuard } from '../../auth/guard/admin.guard'
import { ApiTags } from '@nestjs/swagger'

@ApiTags('Secret Controller')
@Controller('secret')
export class SecretController {
constructor(private readonly secretService: SecretService) {}
Expand Down
2 changes: 2 additions & 0 deletions apps/api/src/user/controller/user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import { CurrentUser } from '../../decorators/user.decorator'
import { User } from '@prisma/client'
import { UpdateUserDto } from '../dto/update.user/update.user'
import { AdminGuard } from '../../auth/guard/admin.guard'
import { ApiTags } from '@nestjs/swagger'

@ApiTags('User Controller')
@Controller('user')
export class UserController {
constructor(private readonly userService: UserService) {}
Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@
"@types/passport-jwt": "^3.0.13",
"@types/react": "18.2.33",
"@types/react-dom": "18.2.14",
"@typescript-eslint/eslint-plugin": "^6.17.0",
"@typescript-eslint/parser": "^6.17.0",
"@typescript-eslint/eslint-plugin": "^6.18.0",
"@typescript-eslint/parser": "^6.18.0",
"autoprefixer": "^10.4.16",
"babel-jest": "^29.7.0",
"eslint": "~8.48.0",
Expand All @@ -78,11 +78,11 @@
"jest-environment-jsdom": "^29.7.0",
"jest-environment-node": "^29.7.0",
"nx": "17.2.7",
"postcss": "^8.4.32",
"postcss": "^8.4.33",
"prettier": "^2.8.8",
"prisma": "^5.7.1",
"react-refresh": "^0.10.0",
"tailwindcss": "^3.4.0",
"tailwindcss": "^3.4.1",
"ts-jest": "^29.1.1",
"ts-node": "10.9.1",
"typescript": "~5.2.2",
Expand All @@ -99,7 +99,7 @@
"@nestjs/swagger": "^7.1.17",
"@prisma/client": "^5.7.1",
"@supabase/supabase-js": "^2.39.2",
"axios": "^1.6.3",
"axios": "^1.6.5",
"chalk": "4.1.2",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.0",
Expand Down
Loading

0 comments on commit b15dbb0

Please sign in to comment.