技术标签: apifox nestjs javascript/typescript/node javascript/typescript javascript swagger
$ npm install --save @nestjs/swagger
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);
const config = new DocumentBuilder()
.setTitle('Cats example')
.setDescription('The cats API description')
.setVersion('1.0')
.addTag('cats')
.build();
const document = SwaggerModule.createDocument(app, config);
SwaggerModule.setup('api', app, document);
await app.listen(3000);
}
bootstrap();
import './init/init';
import {
NestFactory } from '@nestjs/core';
import {
AppModule } from './app.module';
import {
getLogger } from 'xmcommon';
import {
NestLogger } from './common/nest.logger';
import {
RequestInterceptor } from './common/request.interceptor';
import {
HttpFilterFilter } from './common/http_filter.filter';
import {
NestExpressApplication } from '@nestjs/platform-express';
import session from 'express-session';
import path from 'path';
import {
AuthGuard } from './common/auth.guard';
import {
EnvUtils } from './env_utils';
import {
ConfigUtils } from './init/config_utils';
import {
ValidationPipe } from './common/validation_pipe';
const log = getLogger(__filename);
log.info('程序开始启动... 当前环境:' + EnvUtils.env + ' 开发环境:' + EnvUtils.isDev);
async function bootstrap() {
const globalConfig = ConfigUtils.getConfig();
const app = await NestFactory.create<NestExpressApplication>(AppModule, {
logger: new NestLogger(),
});
app.use(session(ConfigUtils.buildSessionOptions()));
// app.useStaticAssets(path.join(process.cwd(), 'public'), { prefix: '/static/' });
app.useStaticAssets(path.join(process.cwd(), 'public'), {
});
app.setBaseViewsDir(path.join(process.cwd(), 'view')); // 放视图的文件
app.setViewEngine('ejs');
app.useGlobalPipes(new ValidationPipe());
app.useGlobalInterceptors(new RequestInterceptor());
app.useGlobalFilters(new HttpFilterFilter());
app.useGlobalGuards(new AuthGuard());
if (EnvUtils.isDev) {
// 如果是开发模式,则加载文档
// eslint-disable-next-line @typescript-eslint/no-var-requires
const {
DocumentBuilder, SwaggerModule } = require('@nestjs/swagger');
const config = new DocumentBuilder()
.setTitle('Cats example')
.setDescription('The cats API description')
.setVersion('1.0')
.addTag('cats')
.build();
const document = SwaggerModule.createDocument(app, config);
SwaggerModule.setup('apidoc', app, document);
log.info(`swagger url: http://localhost:4000/apidoc`);
}
await app.listen(4000);
log.info(`开始侦听:4000...`);
}
bootstrap();
import {
Controller, Get } from '@nestjs/common';
import {
ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger';
import {
AppService } from './app.service';
@ApiTags('应用系列接口')
@Controller()
export class AppController {
constructor(private readonly appService: AppService) {
}
@Get()
@ApiOperation({
summary: '返回hello' })
@ApiResponse({
type: String })
getHello(): string {
return this.appService.getHello();
}
}
$ run run start
@ApiTags('登录相关接口')
@Controller('login')
export class CLoginController {
}
@ApiProperty({
title: '手机号', maxLength: 11, minLength: 11 })
@ApiBearerAuth('JWT')
apifox的请求参数
auth类型列表
@ApiOperation({
summary: '使用验证码登录', description: '要求先获得验证码' })
// 登录返回的VO
@ApiTags('登录返回的VO')
export class XDoLoginVO extends XRetVO {
@ApiProperty({
title: '具体返回的数据 ' })
data?: XTokenInfo;
}
// 使用验证码登录的请求参数
@ApiTags('使用验证码登录的请求参数')
export class XDTODoLogin {
@ApiProperty({
title: '手机号', maxLength: 11, minLength: 11 })
mobile: string;
@ApiProperty({
title: '验证码', maxLength: 8, minLength: 1 })
code: string;
}
// 登录请求
@Post('login')
@ApiOperation({
summary: '使用验证码登录', description: '要求先获得验证码' })
@ApiOkResponse({
type: XDoLoginVO })
private async doLogin(@Body() paramBody: XDTODoLogin) {
const r = new XCommonRet();
do {
const result = (await this.user.adminLogin(
paramBody.mobile,
paramBody.code,
)) as XCommonRet<ITokenString>;
r.assignFrom(result);
} while (false);
return r;
}
@ApiOkResponse()
@ApiCreatedResponse()
@ApiAcceptedResponse()
@ApiNoContentResponse()
@ApiMovedPermanentlyResponse()
@ApiFoundResponse()
@ApiBadRequestResponse()
@ApiUnauthorizedResponse()
@ApiNotFoundResponse()
@ApiForbiddenResponse()
@ApiMethodNotAllowedResponse()
@ApiNotAcceptableResponse()
@ApiRequestTimeoutResponse()
@ApiConflictResponse()
@ApiPreconditionFailedResponse()
@ApiTooManyRequestsResponse()
@ApiGoneResponse()
@ApiPayloadTooLargeResponse()
@ApiUnsupportedMediaTypeResponse()
@ApiUnprocessableEntityResponse()
@ApiInternalServerErrorResponse()
@ApiNotImplementedResponse()
@ApiBadGatewayResponse()
@ApiServiceUnavailableResponse()
@ApiGatewayTimeoutResponse()
@ApiDefaultResponse()
每次都用apifox更新接口文档是一个很痛苦的事情,还好有可以导入
apifox可以支持很多种导入,这里只针对nestjs集成的swagger
用apifox创建一个项目后,再点导入
显示导入选择框,可以看到apifox集居了很多种格式的导入, 这里选择OpenAPI/Swagger和URL导入
选择URL试,输入URL http://localhost:3000/api-json
然后会弹出导出导入预览,第一次导入默认就可以了,然后点确认导入
完后会弹出一个导入成功的按钮
最后点在接口管理,就可以看到你导入的接口了
这样我们就完成了导入
文章浏览阅读265次。本章详细介绍了shuffle工作机制(自用)_shuffle阶段是干什么的
文章浏览阅读778次。使用快捷键:默认情况下,可以使用快捷键Ctrl + Alt + L (在Windows和Linux下) 或者 Command + Option + L (在Mac OS下) 来格式化选定的代码块或整个文件。使用自动保存功能:可以在Android Studio的设置中启用"Editor | General | Auto Save"选项,这样当你修改代码时,代码会在一定时间内自动格式化。无论使用哪种方式,Android Studio都会根据你的代码风格设置自动为你格式化代码,保持统一的代码风格和可读性。_android studio 代码格式化
文章浏览阅读531次,点赞23次,收藏6次。博主介绍:程序员陈师兄、8年大厂程序员经历。csdn博客专家、掘金/华为云/阿里云/InfoQ等平台优质作者、专注于Java技术领域和毕业项目实战 精彩专栏推荐订阅 不然下次找不到哟。
文章浏览阅读1.1k次。一、LINUX常用命令文件管理类命令:pwd 打印当前目录 cd 改变目录 cd / 转到根目录 cd ~ 转到用户目录 cd /XX/XX 转到绝对路径 cd XX 转到当前目录下的相对路径 ls 查看目录内容 ls -a 列举全部文件,包括隐藏文件 ls -l 列举目录中的细节(权限,所有者等) ls -l XX 列举某一文件信息 ls -R 递归列举该目录所有子目录内容 ls -s 按文件大小排序 cat XX 显示..._gvim grep命令
文章浏览阅读2.6k次。fatal error LNK1179: invalid or corrupt file: duplicate comdat "XXX" 解决方法,找到(ocx和dll都是类似的)#import "Flash.ocx" named_guids改为#import "Flash.ocx" named_guids, exclude("IFlashObject_无效或损坏的文件:重复的comdat
文章浏览阅读300次。Word Search(C++单词搜索)_c++ccc word hunt
文章浏览阅读10w+次,点赞11次,收藏103次。《朱有鹏老师单片机完全学习系列课程》针对有志于物联网技术开发行业的零基础学员设计,共分6季,总200余小时的课程。第1季课程的主要内容包括:《朱有鹏老师单片机系列课程》的整体规划、课程设计、学习路线介绍,单片机学习的意义、单片机与物联网的关系,51单片机工作原理、电路原理图识别、C语言基础知识等学习单片机软件开发必备的基础知识,常用开发工具Keil uVision软件的使用、模拟单步调试方法、IS..._盗版先锋单片机
文章浏览阅读963次。尽量使用最新的版本,之需要改下面的第一个问题,就可以全部跑通,新的版本中beam size 是6, 就不会有问题了。按照github上的版本直接跑的过程中遇到两个问题: 1. 在 make word graph的过程中字典正确的没有拷贝到本地: 在 14.04 下面没有问题,在16.04 下面出现问题了。 cat $thchs/resource/dict/lexicon.txt $thchs/d..._thchs30错误总结
文章浏览阅读1w次,点赞6次,收藏32次。PConv 使用方式_yolov7-tiny模型修改
文章浏览阅读1.6k次。安装的软件有miniconda,CUDA,Pycharm,需要安装的包有Pytorch,jupyter,d2l_window如何搭建深度学习环境
文章浏览阅读958次。Python中对象可变的(mutable)和不可变的(immutable)介绍_immutable in python
文章浏览阅读5.0k次,点赞2次,收藏13次。利用Markdown可以解析html的特点,我们可以很方便的转载他人的文章。_csdn 创作中心 设置禁止转载