1.0.2 • Published 3 years ago
icinfo-tool-api v1.0.2
api自动化生成代码工具
解析后端的微服务api文档,自动生成所需要的接口函数(TS格式的)
引入的命令
npm i -D git+ssh://git@gitlab.icinfo.co:fed/tool.api.git#v0.2.7
基本用法案例
在需要的项目中创建一个文件,比如 api.ts
import axios from 'axios';
import { cleanDir } from 'tutils/fileSystemSync';
import { generate } from 'api-server';
async function main() {
try {
cleanDir('src/api');
} catch (e) {
// eslint-disable-next-line no-console
console.log(e);
}
[
'http://api.fed.icinfo.co/xyxf/xyxf_zzgl/develop/api.json',
'http://api.fed.icinfo.co/xyxf/origin-framework-cloud/member/api.json',
'http://api.fed.icinfo.co/business-component/check-form/develop/api.json',
].forEach(async (url) => {
const result = await axios(url);
generate(result.data, {
tsDir: 'src/api',
merge: true,
request: 'request',
prepend: 'import {request, RequestOptions} from "@/untils/http"',
optionsType: 'RequestOptions',
dataDescription: '要提交给服务器的数据',
optionsDescription: '附加选项',
});
// eslint-disable-next-line no-console
console.log('已成功生成:', url.replace(/\/api\.json$/, ''));
});
}
main();
同时在package.json
配置scripts
"api": "ts-node ./scripts/api.ts"
执行方法为
npm run api
可配置的参数
名称 | 类型 | 是否必填 | 说明 | 默认值 | 备注 |
---|---|---|---|---|---|
getMockData | function | 否 | 自定义个别字段的模拟数据 | - | - |
mockCount | function | 否 | 自定义个别字段的模拟个数 | - | - |
mockDir | string | 否 | 模拟数据文件夹地址 | - | - |
merge | boolean | 否 | 是否合并已有的模拟数据 | - | - |
mockDepth | number | 否 | 嵌套对象的最多模拟次数 | - | - |
mockPrefix | string | 否 | 模拟数据的前缀 | - | - |
tsDir | string | 否 | 接口文件夹地址 | - | - |
docFile | string | 否 | 生成的文档文件名 | - | - |
prepend | string | 否 | 模块前追加的内容 | - | - |
optionsType | string | 否 | 选项的类型 | - | - |
dataDescription | string | 否 | 数据的描述 | - | - |
optionsDescription | string | 否 | 附加选项的描述 | - | - |
request | string | 否 | 发起请求接口名 | - | - |
newLine | string | 否 | 使用的换行符 | - | - |
apiType | 'jquery'|'axios' | 否 | 参数配置的类型, 默认为jquery.ajax的方案,可选axios方案 | 'jquery' | - |
1.0.2
3 years ago