swagger.mjs文件用于api自动生成声明文件

import * as fs from 'fs'
import * as http from 'http'
import * as iconv from 'iconv-lite'
import path from 'path'
import openapiTS from 'openapi-typescript'
const __dirname = path.resolve()

iconv.default.skipDecodeWarning = true

function start ({ url, name }) {
http.get(url, function (res) {
console.log('http.get url:' + url)
let data = ''
res.setEncoding('binary')
res.on('data', function (chunk) {
data += chunk
})
res.on('end', async function () {
openapiTS(JSON.parse(iconv.default.decode(data, 'utf8')))
.then(output => {
fs.writeFile(${__dirname}/src/api/swaggerTypes/${name}.ts, output, 'utf8', function (err) {
if (err) {
console.log(err)
console.log('down fail')
} else {
console.info(${name} 下载完成!)
}
})
})
})
})
}

// swagger 文档地址 json 格式的文档
const arr = [
{
url: 'http://xxx.dev.zwy.17ecity.cc/v2/api-docs?group=默认接口',
name: 'mobile'
}
]

for (let index = 0; index < arr.length; index++) {
const item = arr[index]
console.log('start url:' + item.url)
start({ url: item.url, name: item.name })
}

posted @ 2022-08-29 14:02  举个栗子走天下  阅读(43)  评论(0编辑  收藏  举报