Skip to content

Commit

Permalink
feat: create a swagger for this api
Browse files Browse the repository at this point in the history
  • Loading branch information
alice-sebego committed Sep 26, 2022
1 parent 5f167f1 commit 7b264f3
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 6 deletions.
79 changes: 73 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"@nestjs/jwt": "^9.0.0",
"@nestjs/passport": "^9.0.0",
"@nestjs/platform-express": "^9.0.0",
"@nestjs/swagger": "^6.1.2",
"@prisma/client": "^4.3.1",
"@prisma/studio": "^0.473.0",
"argon2": "^0.29.1",
Expand Down
6 changes: 6 additions & 0 deletions src/auth/dto/auth.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,31 @@ import {
IsOptional,
IsDateString,
} from 'class-validator';
import { ApiProperty } from '@nestjs/swagger';

export class AuthDto {
@IsString()
@IsNotEmpty()
@ApiProperty()
userName: string;

@IsEmail()
@IsNotEmpty()
@ApiProperty()
email: string;

@IsString()
@IsNotEmpty()
@ApiProperty()
password: string;

@IsOptional()
@IsDateString()
@ApiProperty()
createdAt?: Date;

@IsOptional()
@IsDateString()
@ApiProperty()
updatedAt?: Date;
}
24 changes: 24 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,38 @@
import { ValidationPipe } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';
import { AppModule } from './app.module';

async function bootstrap() {
const app = await NestFactory.create(AppModule);

app.useGlobalPipes(
new ValidationPipe({
whitelist: true,
}),
);

const config = new DocumentBuilder()
.setTitle('To-do List')
.setDescription('A ToDoList API description')
.setVersion('1.0')
.addTag('todolist')
.addBearerAuth(
{
// I was also testing it without prefix 'Bearer ' before the JWT
description: `Please enter token in following format: Bearer <JWT>`,
name: 'Authorization',
bearerFormat: 'Bearer', // I`ve tested not to use this field, but the result was the same
scheme: 'Bearer',
type: 'http', // I`ve attempted type: 'apiKey' too
in: 'Header',
},
'access-token', // This name here is important for matching up with @ApiBearerAuth() in your controller!
)
.build();
const document = SwaggerModule.createDocument(app, config);
SwaggerModule.setup('api', app, document);

await app.listen(process.env.PORT || 3000);
}
bootstrap();
6 changes: 6 additions & 0 deletions src/task/dto/task.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import {
IsBoolean,
} from 'class-validator';
import { Transform } from 'class-transformer';
import { ApiProperty } from '@nestjs/swagger';

export class TaskDto {
@IsNotEmpty()
@IsString()
@ApiProperty()
title: string;

@IsBoolean()
Expand All @@ -19,17 +21,21 @@ export class TaskDto {
if (value === 'false') return false;
return value;
})
@ApiProperty()
isDone?: boolean;

@IsOptional()
@IsDateString()
@ApiProperty()
createdAt?: Date;

@IsOptional()
@IsDateString()
@ApiProperty()
updatedAt?: Date;

@IsString()
@IsOptional()
@ApiProperty()
userId?: string;
}
2 changes: 2 additions & 0 deletions src/task/task.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ import {
import { JwtGuard } from 'src/auth/guard';
import { TaskService } from './task.service';
import { TaskDto } from './dto';
import { ApiBearerAuth } from '@nestjs/swagger';

@UseGuards(JwtGuard)
@ApiBearerAuth('access-token')
@Controller('task')
export class TaskController {
constructor(private taskService: TaskService) {}
Expand Down
6 changes: 6 additions & 0 deletions src/user/dto/user.dto.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ApiProperty } from '@nestjs/swagger';
import {
IsString,
IsNotEmpty,
Expand All @@ -9,20 +10,25 @@ import {
export class UserDto {
@IsNotEmpty()
@IsString()
@ApiProperty()
userName: string;

@IsEmail()
@IsNotEmpty()
@ApiProperty()
email: string;

@IsString()
@ApiProperty()
hash?: string;

@IsOptional()
@IsDateString()
@ApiProperty()
createdAt?: Date;

@IsOptional()
@IsDateString()
@ApiProperty()
updatedAt?: Date;
}
2 changes: 2 additions & 0 deletions src/user/user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ import {
import { JwtGuard } from 'src/auth/guard';
import { UserService } from './user.service';
import { UserDto } from './dto';
import { ApiBearerAuth } from '@nestjs/swagger';

@UseGuards(JwtGuard)
@ApiBearerAuth('access-token')
@Controller('user')
export class UserController {
constructor(private userService: UserService) {}
Expand Down

0 comments on commit 7b264f3

Please sign in to comment.