Swagger
Swagger用于给前端提供接口文档,而且每个接口都支持现场测试,官网
安装依赖:
1
| npm install @nestjs/swagger swagger-ui-express
|
注册Swagger
在main.ts注册:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| import { NestFactory } from '@nestjs/core'; import { VersioningType } from '@nestjs/common'; import { AppModule } from './app.module'; import { NestExpressApplication } from '@nestjs/platform-express'; import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';
async function bootstrap() { const app = await NestFactory.create<NestExpressApplication>(AppModule, { cors: true, });
const options = new DocumentBuilder() .setTitle('接口文档标题') .setDescription('1145141919810') .setVersion('1') .build(); const document = SwaggerModule.createDocument(app, options); SwaggerModule.setup('/api-docs', app, document); await app.listen(3000); } bootstrap();
|
生成了接口文档:
整理接口文档
添加分组
使用@ApiTags
1 2 3 4 5 6 7
| import { ApiTags } from '@nestjs/swagger';
@ApiTags('守卫') export class GuardController { }
|
接口描述
使用@ApiOperation描述接口
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| import { ApiOperation, ApiTags } from '@nestjs/swagger';
@ApiTags('守卫') @Controller('guard') @UseGuards(RoleGuard) export class GuardController { @Get() @Role('admin') @ApiOperation({ summary: '测试admin', description: '请求该接口需要admin权限', }) findAll(@ReqUrl('I am data') url: string) { console.log(url, 'url'); return this.guardService.findAll(); } }
|
Param参数描述
使用@ApiParam描述Param参数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| import { ApiOperation, ApiParam, ApiTags } from '@nestjs/swagger';
@ApiTags('守卫') @Controller('guard') @UseGuards(RoleGuard) export class GuardController { @Get(':id') @ApiParam({ name: 'id', description: '主键', required: true, type: 'number' }) findOne(@Param('id') id: string) { return this.guardService.findOne(+id); } }
|
Query参数描述
使用@ApiQuery描述Query参数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| import { ApiOperation, ApiParam, ApiQuery, ApiTags } from '@nestjs/swagger';
@ApiTags('守卫') @Controller('guard') @UseGuards(RoleGuard) export class GuardController { @Get() @ApiQuery({ name: 'page', description: '分页信息' }) findAll(@ReqUrl('I am data') url: string) { console.log(url, 'url'); return this.guardService.findAll(); } }
|
返回信息描述
使用@ApiResponse自定义返回信息
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| import { ApiOperation, ApiParam, ApiQuery, ApiResponse, ApiTags } from '@nestjs/swagger';
@ApiTags('守卫') @Controller('guard') @UseGuards(RoleGuard) export class GuardController { @Get() @ApiResponse({ status: 403, description: '自定义返回信息' }) findAll(@ReqUrl('I am data') url: string) { console.log(url, 'url'); return this.guardService.findAll(); } }
|
Post示例描述
使用@ApiProperty描述Post示例
1 2 3 4 5 6 7 8
| import { ApiProperty } from '@nestjs/swagger';
export class CreateGuardDto { @ApiProperty({ example: 'yajue' }) name: string; @ApiProperty({ example: 24 }) age: number; }
|
携带Bearer Token
需要先开启配置:
1 2 3 4 5 6
| const options = new DocumentBuilder() .addBearerAuth() .setTitle('接口文档标题') .setDescription('1145141919810') .setVersion('1') .build();
|
在Controller中使用,也可以用于单个函数:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| import { ApiBearerAuth, ApiOperation, ApiParam, ApiQuery, ApiResponse, ApiTags, } from '@nestjs/swagger';
@Controller('guard') @ApiBearerAuth() export class GuardController { }
|
在接口文档的这个按钮处设置Token
被修饰的接口在请求时会自动携带设置的Token
更多接口