我想使用 Node js 将 postman 文件转换为 openAPI 3.0

Lak*_*han 3 node.js postman openapi

您能帮我将 Postman 文件转换为 openAPI 3.0 并将它们下载到机器吗?

这必须在 Node.js 中实现,我对它非常陌生。

谢谢。

小智 7

我认为你可以使用这个 npm 库。 https://www.npmjs.com/package/postman-to-openapi 它会解决你的问题。

有一个示例代码如下。你可以根据你的代码更改它。

const postmanToOpenApi = require('postman-to-openapi')
const postmanCollection = './path/to/postman/collection.json'
const outputFile = './api/collection.yml'

// Async/await
try {
    const result = await postmanToOpenApi(postmanCollection, outputFile, { defaultTag: 'General' })
    // Without save the result in a file
    const result2 = await postmanToOpenApi(postmanCollection, null, { defaultTag: 'General' })
    console.log(`OpenAPI specs: ${result}`)
} catch (err) {
    console.log(err)
}

// Promise callback style
postmanToOpenApi(postmanCollection, outputFile, { defaultTag: 'General' })
    .then(result => {
        console.log(`OpenAPI specs: ${result}`)
    })
    .catch(err => {
        console.log(err)
    })
Run Code Online (Sandbox Code Playgroud)

您可以在此链接中找到更多详细信息。https://joolfe.github.io/postman-to-openapi/

最后,要下载文件,您可以使用Javascript文件下载器。使用下面的链接。 https://www.npmjs.com/package/js-file-download