New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

routing-controllers-to-openapi

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

routing-controllers-to-openapi

Buildtime OpenAPI v3 spec generation for routing-controllers.

  • 0.0.7
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
3
decreased by-72.73%
Maintainers
1
Weekly downloads
 
Created
Source

routing-controllers-to-openapi

npm version Test codecov

routing-controllers项目构建时生成 openapi v3 schema。
通过TS文件生成AST语法树,分析AST语法树生成openapiv3数据。进而导入到postmanswagger 等平台进行数据展示。

使用

1. 安装依赖

yarn add routing-controllers-to-openapi --dev

2. 新增script命令

"scripts": {
  "gen-openapi": "gen-openapi",
}

3. 生成openapi数据

yarn gen-openapi

案例

  • Controller案例代码
import { HeaderParam, JsonController, BodyParam, Post } from 'routing-controllers';
export interface Response {
  // code返回码
  code: number;
  // 返回信息
  message: string;
}

/**
 * 测试案例controller
 *
 * @export
 * @class TestController
 */
@JsonController('/example')
export default class ExampleController {
  /**
   * do something
   *
   * @param {string} orgcode 租户号
   * @param {string} name 姓名
   * @param {number} age 年龄
   * @returns {Promise<Response>}
   * @memberof ExampleController
   */
  @Post('/test')
  async getTest(
    @HeaderParam('orgcode') orgcode: string,
    @BodyParam('name') name: string,
    @BodyParam('age') age: number,
  ): Promise<Response> {
    return {
      code: 0,
      message: 'success'
    }
  }
}
  • generate openapi schema
{
    "openapi": "3.0.3",
    "info": {
        "title": "routing-coontrollers-to-openapi-test",
        "description": "Buildtime OpenAPI v3 spec generation for routing-controllers",
        "version": "1.0.0"
    },
    "tags": [
        {
            "name": "ExampleController",
            "description": "测试案例controller"
        }
    ],
    "paths": {
        "/example/test": {
            "post": {
                "tags": [
                    "ExampleController"
                ],
                "summary": "do something",
                "description": "ExampleController.getTest 测试案例controller ",
                "operationId": "ExampleController.post.getTest.rhBCjZZSMY",
                "responses": {
                    "200": {
                        "description": "",
                        "content": {
                            "application/json": {
                                "schema": {
                                    "type": "object",
                                    "properties": {
                                        "code": {
                                            "type": "number",
                                            "description": "接口返回code码字段"
                                        },
                                        "data": {
                                            "type": "object",
                                            "properties": {
                                                "code": {
                                                    "type": "number",
                                                    "description": "code返回码"
                                                },
                                                "message": {
                                                    "type": "string",
                                                    "description": "返回信息"
                                                }
                                            },
                                            "required": [
                                                "code",
                                                "message"
                                            ],
                                            "additionalProperties": false
                                        },
                                        "msg": {
                                            "type": "string",
                                            "description": "接口返回信息字段"
                                        }
                                    },
                                    "required": [
                                        "code",
                                        "data"
                                    ]
                                }
                            }
                        }
                    }
                },
                "parameters": [
                    {
                        "name": "orgcode",
                        "in": "header",
                        "description": "租户号 (@HeaderParam, 类型:string)",
                        "required": true,
                        "schema": {
                            "type": "string"
                        }
                    }
                ],
                "requestBody": {
                    "description": "",
                    "content": {
                        "application/json": {
                            "schema": {
                                "type": "object",
                                "properties": {
                                    "name": {
                                        "type": "string",
                                        "description": "@BodyParam:姓名"
                                    },
                                    "age": {
                                        "type": "number",
                                        "description": "@BodyParam:年龄"
                                    }
                                },
                                "required": [
                                    "name",
                                    "age"
                                ]
                            }
                        }
                    }
                }
            }
        }
    },
    "components": {
        "schemas": {}
    }
}

Config 配置

项目根目录下创建yundoc.config.js文件

// yundoc.config.js
const path = require('path');
module.exports = {
  routePrefix: '/api', // routing-controllers路由前缀
  controllers: [path.join(process.cwd(), "./src/controller/*")],
  filterFiles: [],
  requiredType: 'typescript', // typescript | routing-controllers
  servers: [
    {
      url: 'http://127.0.0.1:3000',
      description: '开发环境域名前缀',
    }
  ],
  // 自定义统一 response 返回结构(可选)
  responseSchema: {
    type: 'object',
    properties: {
      code: {
        type: "number",
        description: "接口返回code码字段",
      },
      // data配置必填,格式固定请不要更改
      data: {
        $ref: "#Response",
      },
      msg: {
        type: "string",
        description: "接口返回信息字段",
      }
    },
    required: [
      "code",
      "data",
    ]
  }
}

参数说明

字段类型必填说明
routePrefixstring路由前缀,跟routing-controllers保持一致
controllersstring|string[]需要解析的controller文件或需要解析的controller文件夹,默认:/src/controller/*,可根据文件名匹配,例如: /src/**/*Controller.ts,备注: 必须是绝对路径
requiredTypetypescript | routing-controllerscontroller参数必填模式,默认使用typescript
filterFilesstring[]过滤解析的npm包或者ts文件
outfilestringopenapi数据存放文件,备注:必须是绝对路径
servers{url:string,description?:string}[]api根路由与描述
responseSchemaResponseSchemaObject返回数据包裹层数据格式,由于大部分BFF使用了内置的返回包裹,在controller级别无法解析,因此开发给使用方进行自定义。

responseSchema 参数说明

responseSchema严格遵循JsonSchema数据格式

字段类型必填说明
typestring值为object,固定值不要更改(描述当前schema数据类型)
properties{[prop:string]: {type?:string;$ref?:string;description?:string}}当前对象的属性描述信息。
requiredstring[]描述当前object的必填字段,若无必填字段则不需要此参数

  • properties 字段描述
字段类型必填说明
typestring此处type只需要填写简单类型,例如:string,number,boolean
$refstring此处为固定值:#Response, 不要更改
descriptionstring当前字段描述信息

备注: properties 中 $reftype必须有一项为真。


 routing-controllers 支持项

支持的类型解析列表

example 案例

git clone https://github.com/yunke-yunfly/routing-controllers-to-openapi.git
cd routing-controllers-to-openapi
yarn install
yarn build
yarn example

贡献

我们非常欢迎您的贡献,您可以通过以下方式与我们共建。

Keywords

FAQs

Package last updated on 10 Jan 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc