首页
/ 【亲测免费】 Postman to OpenAPI 项目使用教程

【亲测免费】 Postman to OpenAPI 项目使用教程

2026-01-17 08:53:20作者:薛曦旖Francesca

1. 项目的目录结构及介绍

Postman to OpenAPI 项目的目录结构如下:

postman-to-openapi/
├── bin/
│   └── postman-to-openapi
├── lib/
│   ├── converters/
│   ├── models/
│   ├── utils/
│   └── index.js
├── test/
│   ├── fixtures/
│   ├── test-cases/
│   └── test.js
├── .editorconfig
├── .eslintrc.json
├── .gitignore
├── .npmignore
├── .travis.yml
├── CHANGELOG.md
├── LICENSE
├── README.md
├── package.json
└── postman-to-openapi.js

目录介绍

  • bin/: 包含可执行文件。
  • lib/: 包含项目的主要代码,包括转换器、模型和工具函数。
  • test/: 包含测试文件和测试用例。
  • .editorconfig, .eslintrc.json, .gitignore, .npmignore, .travis.yml: 配置文件。
  • CHANGELOG.md: 项目更新日志。
  • LICENSE: 项目许可证。
  • README.md: 项目说明文档。
  • package.json: 项目依赖和脚本配置。
  • postman-to-openapi.js: 项目入口文件。

2. 项目的启动文件介绍

项目的启动文件是 postman-to-openapi.js,它位于项目根目录下。这个文件是整个项目的入口点,负责初始化和调用转换逻辑。

#!/usr/bin/env node

const { convert } = require('./lib');
const fs = require('fs');
const path = require('path');

const args = process.argv.slice(2);
const inputFile = args[0];
const outputFile = args[1] || 'openapi.json';

if (!inputFile) {
  console.error('Please provide a Postman collection file as the first argument.');
  process.exit(1);
}

const inputFilePath = path.resolve(inputFile);
const outputFilePath = path.resolve(outputFile);

fs.readFile(inputFilePath, 'utf8', (err, data) => {
  if (err) {
    console.error(`Error reading file ${inputFilePath}:`, err);
    process.exit(1);
  }

  try {
    const openapi = convert(JSON.parse(data));
    fs.writeFile(outputFilePath, JSON.stringify(openapi, null, 2), (err) => {
      if (err) {
        console.error(`Error writing file ${outputFilePath}:`, err);
        process.exit(1);
      }
      console.log(`OpenAPI spec written to ${outputFilePath}`);
    });
  } catch (e) {
    console.error('Error converting Postman collection to OpenAPI:', e);
    process.exit(1);
  }
});

启动文件功能

  • 解析命令行参数,获取输入的 Postman 集合文件和输出的 OpenAPI 文件路径。
  • 读取输入文件内容,调用 convert 函数进行转换。
  • 将转换后的 OpenAPI 规范写入输出文件。

3. 项目的配置文件介绍

项目中主要的配置文件包括 package.json.eslintrc.json

package.json

package.json 文件包含了项目的依赖、脚本和其他元数据。

{
  "name": "postman-to-openapi",
  "version": "3.0.1",
  "description": "Convert postman collection to OpenAPI spec",
  "main": "postman-to-openapi.js",
  "bin": {
    "postman-to-openapi": "./bin/postman-to-openapi"
  },
  "scripts": {
    "test": "mocha",
    "lint": "eslint ."
  },
  "keywords": [
    "postman",
    "openapi",
    "swagger",
    "converter"
  ],
  "author": "joolfe",
  "license": "MIT",
  "dependencies": {
    "ajv": "^8.0.0",
    "yaml": "^1.10
登录后查看全文
热门项目推荐
相关项目推荐