smart-api-generator

smart-api-generator是一个高效能的工具,专门用来自动化创建API接口及其相关文档
功能点
- 以项目维度生成格式统一的接口配置,可直接用于生产环境中
- 良好的入参、出参注释,可直接拷贝使用接口调用代码
- 接口调用支持typescript智能提示与校验
- 支持ts文件生成与接口结构interface的生成
安装
// npm
npm install smart-api-generator -D
// yarn
yarn add smart-api-generator -D
// pnpm
pnpm install smart-api-generator -D
使用步骤
1、在 package.json 中配置命令,配置文件可指定路径,如果不指定路径默认取根目录下的.api.config.js
文件
"scripts": {
......
"api": "cross-env smart-api create --config /path/to/.api.config.js"
},
2、在工程中添加配置文件·api.config.js
module.exports = {
output: './src/request',
projects: [
{
projectName: 'xxx',
basePath: 'xxx',
}
]
}
3、在根目录中运行命令
npm run api
配置项
参数 | 说明 | 必填 | 类型 | 默认值 | 版本 |
---|
output | 接口文件输出目录 | true | string | - | 1.0.0 |
host | swagger平台地址 | false | string | https://letao.cnstrong.cn | 1.0.0 |
projects | 接口文档配置 | true | array | - | 1.0.0 |
projects
参数 | 说明 | 必填 | 类型 | 默认值 | 版本 |
---|
projectName | 项目名称,对应swagger接口文档地址,如:https://letao.cnstrong.cn/letao-official-document-center/doc.html#/home 中,projectName 为 letao-official-document-center | true | string | - | 1.0.0 |
basePath | 接口统一前缀,默认会使用swagger文档中的basePath字段,如果文档与实际有差异,请手动配置 | false | string | swagger文档主页中的basePath字段 | 1.0.0 |
comment | 是否需要生成注释 | false | boolean | true | 1.0.0 |
folderName | 生成文件夹的名称 | false | string | 默认使用 projectName | 1.0.0 |
tsNotStrictInclude | 生成的ts代码时,控制请求参数/返回结果是否严格模式,req 参数部分关闭严格模式,res 返回部分关闭严格模式 | false | string[] | ['res'] | 1.1.0 |
extractResponseKeys | 对接口返回结构进行提取,应对axios拦截器对请求返回做提取的场景,如['data']则提取接口返回中的data字段作为Res,可以配置多个,返回匹配到的第一个 | false | string[] | - | 1.2.0 |
tags | 可以只生成项目下指定分类下的接口, 如: ['user'] | false | string[] | - | 1.2.0 |
extractAxiosPromise | 是否提取axios返回的data层级 | false | boolean | - | 1.2.3 |
mergeConfig |
打开,不会删除本地配置有但是接口文档中没有的接口,可以手动删除,而且新增的接口都会排列到最后面
关闭,文件的生成完全按照接口文档生成
| true | boolean | - | 1.3.0 |