如何一键复制所有接口

如何一键复制所有接口

第一步: 封装转化接口的函数(不会也没事)
getApiUrl () {
    
    
    // 接口文件转存http://192.168.0.193:9020/v2/api-docs
    window.$get("/v2/api-docs").then((res) => {
    
    
      // console.log('接口转存文档为' + res.paths)
      formatApiUrl(res.paths);
    })
  }

formatApiUrl (apiSource, apiPrefix) {
    
    
    apiPrefix = apiPrefix ? apiPrefix : 'user-center';
    const paths = apiSource;
    const getKey = (str) => {
    
    
      return str.replace(/\//, '').replace(/\/./g, function (e) {
    
    
        return e.toUpperCase().replace(/\//g, '')
      })
    }
    const newObj = {
    
    }
    for (let i in paths) {
    
    
      newObj[getKey(i)] = {
    
    
        name: paths[i]['post'] ? paths[i]['post']['summary'] 
        : paths[i]['get']['summary'],
        url: i,
        method: paths[i]['post'] ? 'post' : 'get',
        center: apiPrefix,
      }
      paths[i]['post'] && (newObj[getKey(i)].headers = paths[i]
      ['post']['consumes']);
    }
    console.log(JSON.stringify(newObj))
    console.log(JSON.parse(JSON.stringify(newObj)))
    return newObj;
  }
第二步:将函数放入控制台 let formatApiUrl =(apiSource, apiPrefix) =>{

上面函数内容
}

第三步:查看函数在控制台呈现function类型

注释:formatApiUrl (apiSource, apiPrefix) =>说明这两个参数的作用 只针对于swagger ui
1、apiSource 参数一:将swagger 返回的数据中的paths 对象进行copy value 。
2、apiPrefix 默认就是user-center 然后就是project-center 直接执行

第四步:打开接口文档 (此刻我用的swagger ui)

打开控制台Network 返回的数据preview 查看接口 api-docs 等
右键复制 paths 出现 {
Copy value
Copy property path
Copy object
}
选择第一个Copy value

第五步:回到控制台 函数formatApiUrl ()此时呈现这样 但是类型是function

此时这函数需要传入两个参数
第一个参数是:上面复制的(Copy value)
第二个参数是:http://192.168.0.41:8000/dqApi/project-center/swagger-ui.html#/
swagger 中 project-center 类型为string 格式传入

回车 出现json格式的字符串 进行选择Copy

第六步:打开新页面 json.cn ,将第五步最后Copy的数据进行转化

在这里插入图片描述
完美收工!!! 仅限自己明白!!

猜你喜欢

转载自blog.csdn.net/weixin_53532986/article/details/124131258