首页 > 其他分享 >NestJs swagger接口文档

NestJs swagger接口文档

时间:2023-04-07 16:59:55浏览次数:43  
标签:description ApiProperty 接口 Controller 文档 NestJs swagger Method

文档:https://docs.nestjs.cn/9/recipes?id=swagger

安装

首先,您必须安装所需的包:

npm install --save @nestjs/swagger swagger-ui-express

如果你正在使用 fastify ,你必须安装 fastify-swagger 而不是 swagger-ui-express

npm install --save @nestjs/swagger fastify-swagger

在main.ts 注册swagger

import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';


// 注册Swagger
const options = new DocumentBuilder().setTitle('makalo接口文档').setDescription('描述,。。。').setVersion('1').build();
const document = SwaggerModule.createDocument(app,options);
SwaggerModule.setup('/api-docs',app,document);

image-20230407150624987

访问

http://localhost:3000/api-docs

image-20230407150827103

现在没有分组比较乱

使用ApiTags 添加分组

@ApiTags('makalo 分组')

image-20230407152905448

image-20230407152929728

ApiOperation 接口描述

 @ApiOperation({summary:"创建makalo的接口",description:"这是一个创建的接口的描述"})

image-20230407153256076

image-20230407153327216

ApiParam 动态参数描述

@ApiParam({name:"id",description:"用户id",required:true})

image-20230407153511612

ApiQuery 修饰get

@ApiQuery({name:"testGet",description:"bbb"})

image-20230407153713942

image-20230407153746137

ApiProperty 修饰DTO

import { ApiProperty } from '@nestjs/swagger';
export class CreateMakaloDto {
    @ApiProperty({ description: "姓名", example: "makalo" })
    name: string
    @ApiProperty({ description:"年龄", example: "18"})
    age: number
}

image-20230407161536559

image-20230407161646628

ApiResponse 自定义返回信息

@ApiResponse({status:403,description:"自定义返回信息"})

image-20230407161939125

ApiBearerAuth jwt token

main.ts 注册

.addBearerAuth()
例:
const options = new DocumentBuilder().addBearerAuth().setTitle('makalo接口文档').setDescription('描述,。。。').setVersion('1').build();

image-20230407162821993

控制器中修饰

@ApiBearerAuth()

image-20230407163134199

添加token

image-20230407163613750

image-20230407163316358

image-20230407163339000

测试

image-20230407163734832

image-20230407163756450

image-20230407163828487

其他装饰器

所有可用的 OpenAPI 装饰器都有一个 Api 前缀,可以清楚地区分核心装饰器。 以下是导出的装饰器的完整列表,以及可以应用装饰器的级别的名称。

@ApiOperation() Method
@ApiResponse() Method / Controller
@ApiProduces() Method / Controller
@ApiConsumes() Method / Controller
@ApiBearerAuth() Method / Controller
@ApiOAuth2() Method / Controller
@ApiBasicAuth() Method / Controller
@ApiSecurity() Method / Controller
@ApiExtraModels() Method / Controller
@ApiBody() Method
@ApiParam() Method
@ApiQuery() Method
@ApiHeader() Method / Controller
@ApiExcludeEndpoint() Method
@ApiTags() Method / Controller
@ApiProperty() Model
@ApiPropertyOptional() Model
@ApiHideProperty() Model
@ApiExtension() Model

标签:description,ApiProperty,接口,Controller,文档,NestJs,swagger,Method
From: https://www.cnblogs.com/makalochen/p/17296693.html

相关文章

  • TEB算法原理与代码分析 详细文档+代码分析+matlab程序包
    TEB算法原理与代码分析详细文档+代码分析+matlab程序包YID:4179654252546507......
  • Springboot和swagger或者knife4j版本冲突
    idea报错:Errorcreatingbeanwithname'serviceModelToOpenApiMapperImpl':出错原因(我的是这):springboot中自带的swagger版本,和knife4j-3.0.3自带的swagger的版本出现冲突解决:springboot(3.6.11)--knife4j-openapi2-spring-boot-starter(4.1.0)【将两个都更新到新版本,这两个不冲突......
  • 支付宝网站支付即时到帐(一) 参数的文档地址
    必备物品:企业支付宝账号官网: https://open.alipay.com/platform/home.htm第一步 创建网站应用 并完善信息 这一步很简单添加上网站支付的功能第二步 找到即时到帐的文档https://docs.open.alipay.com/62/103566上面有获取pid的方法https://docs.open.alipay.com/common/10473......
  • NestJs 异常过滤器
    文档:https://docs.nestjs.cn/9/exceptionfilters实现新建文件common下面新建filter.ts实现让我们创建一个异常过滤器它负责捕获作为HttpException类实例的异常并为它们设置自定义响应逻辑为此,我们需要访问底层平台Request和Response我们将访问Request对象,以便提取原......
  • 微信企业付款到零钱的官方文档
    用谷歌搜到的【企业付款到零钱】的官方地址:https://pay.weixin.qq.com/wiki/doc/api/tools/mch_pay.php?chapter=14_2这个接口后续官方公告(https://m.mpaypass.com.cn/news/202212/02172040.html)里说会下线,但是暂时已经开通的商户还是可以用的,支持0.3~200元的付款。 ......
  • 全网最详细中英文ChatGPT-GPT-4示例文档-会议笔记文档智能转摘要从0到1快速入门——官
    目录Introduce简介setting设置Prompt提示Sampleresponse回复样本APIrequest接口请求python接口请求示例node.js接口请求示例curl命令示例json格式示例其它资料下载ChatGPT是目前最先进的AI聊天机器人,它能够理解图片和文字,生成流畅和有趣的回答。如果你想跟上AI时代的潮流......
  • 流媒体技术学习笔记之(六)FFmpeg官方文档先进音频编码(AAC)
     先进音频编码(AAC)的后继格式到MP3,和以MPEG-4部分3(ISO/IEC14496-3)被定义。它通常用于MP4容器格式; 对于音乐,通常使用.m4a扩展名。第二最常见的用途是在MKV(Matroska)文件内,因为它比MP4更好地支持嵌入式基于文本的软字幕。本指南中的示例将使用扩展MP4和M4A。FFmpeg的可支持两个AA......
  • 流媒体技术学习笔记之(四)解决问题video.js 播放m3u8格式的文件,根据官方的文档添加vide
    源码地址:https://github.com/Tinywan/PHP_Experience总结:说明:测试环境:本测试全部来自阿里云直播和OSS存储点播以及本地服务器直播和点播播放器:VideoJs直播:1、阿里云直播,需要CDN设置HTTP头2、本地直播需要设置直播访问服务器的头部信息(本地为Nginx)add_header'Access-......
  • 【官方文档】Nginx模块Nginx-Rtmp-Module学习笔记(二)HLS 指令详解
    源码地址:https://github.com/Tinywan/PHP_Experience一、在Nginx配置文件的RTMP模块中配置hlshls_key_path/tmp/hlskeys;提示错误信息:nginx:[emerg]thesamepathname"/data/hlskeys"usedin/usr/local/nginx/conf/nginx.conf:178andin/usr/local/nginx/conf/nginx......
  • 【官方文档】Nginx模块Nginx-Rtmp-Module学习笔记(一) RTMP 命令详解
    源码地址:https://github.com/Tinywan/PHP_Experience说明:rtmp的延迟主要取决于播放器设置,但流式传输软件,流的比特率和网络速度(以及响应时间“ping”)可能会对延迟产生影响,具有播放器的本地rtmp服务器使用“否”缓冲区(如0.1-0.2秒缓冲区等)可能会在0.8-1.2秒之间总是延迟,当事情正......