Skip to content

Commit

Permalink
opti: swagger 导入自动增加 basePath 到项目配置
Browse files Browse the repository at this point in the history
  • Loading branch information
苏文雄 committed Oct 19, 2019
1 parent 2fb5dd9 commit 25b6682
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* 改善 swagger 自动导入,不再支持秒级别的 cron 表达式,默认使用 10分钟更新一次的频率
* 修复输入空的 swagger地址 ,会发起请求的 bug
* 优化 swagger 数据导入,不会导入空的分类,不会使用版本号作为分类名称
* swagger 导入自动增加 basePath 到项目配置

## v1.8.4
* 修复 swagger 导入数据时,如果数据格式中缺少 in 的参数,会丢失请求参数
Expand Down
28 changes: 25 additions & 3 deletions common/HandleImportData.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ async function handle(
token,
port
) {

const taskNotice = _.throttle((index, len)=>{
messageSuccess(`正在导入,已执行任务 ${index+1} 个,共 ${len} 个`)
}, 3000)

const handleAddCat = async cats => {
let catsObj = {};
if (cats && Array.isArray(cats)) {
Expand Down Expand Up @@ -52,12 +57,13 @@ async function handle(
return catsObj;
};

const handleAddInterface = async res => {
const cats = await handleAddCat(res.cats);
const handleAddInterface = async info => {
const cats = await handleAddCat(info.cats);
if (cats === false) {
return;
}
res = res.apis;

const res = info.apis;
let len = res.length;
let count = 0;
let successNum = len;
Expand All @@ -67,6 +73,19 @@ async function handle(
callback({ showLoading: false });
return;
}

if(info.basePath){
let projectApiPath = '/api/project/up';
if (isNode) {
projectApiPath = 'http:https://127.0.0.1:' + port + projectApiPath;
}

await axios.post(projectApiPath, {
id: projectId,
basepath: info.basePath
})
}

for (let index = 0; index < res.length; index++) {
let item = res[index];
let data = Object.assign(item, {
Expand Down Expand Up @@ -126,7 +145,10 @@ async function handle(
if (count === len) {
callback({ showLoading: false });
messageSuccess(`成功导入接口 ${successNum} 个, 已存在的接口 ${existNum} 个`);
return;
}

taskNotice(index, res.length)
}
};

Expand Down
2 changes: 2 additions & 0 deletions exts/yapi-plugin-import-swagger/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ const compareVersions = require('compare-versions');
res = await handleSwaggerData(res);
SwaggerData = res;

interfaceData.basePath = res.basePath || '';

if (res.tags && Array.isArray(res.tags)) {
res.tags.forEach(tag => {
interfaceData.cats.push({
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "yapi-vendor",
"version": "1.8.4",
"version": "1.8.5",
"description": "YAPI",
"main": "server/app.js",
"scripts": {
Expand Down

0 comments on commit 25b6682

Please sign in to comment.