Swagger

Swagger — это набор инструментов и спецификация для описания, разработки и тестирования RESTful API.

Подключение:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// main.tsimport { NestFactory } from "@nestjs/core";import { AppModule } from "./app.module";import { DocumentBuilder, SwaggerModule } from "@nestjs/swagger"; async function bootstrap() { const app = await NestFactory.create(AppModule); const config = new DocumentBuilder() .setTitle("Online Store") .setDescription("Online Store Documentation") .setVersion("1.0.0") .addTag("online-store") .build(); const document = SwaggerModule.createDocument(app, config); SwaggerModule.setup("/docs", app, document); await app.listen(process.env.PORT ?? 3000);} bootstrap();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// create-user.dto.tsimport { ApiProperty } from '@nestjs/swagger';import { IsEmail, IsString, MaxLength, MinLength } from 'class-validator'; export class CreateUserDto { @ApiProperty({ example: 'user@gmail.ru', description: 'Email', }) @IsEmail( {}, { message: 'Wrong email', }, ) readonly email: string; @ApiProperty({ example: '123456', description: 'Password', }) @IsString({ message: 'Must be string', }) @MinLength(6, { message: 'Min length must be more or equl 6', }) @MaxLength(12, { message: 'Max length must be less 12', }) readonly password: string;}