是否可以从Node.js中可用的路由自动生成Swagger Yaml?

时间:2019-02-17 20:00:01

标签: javascript node.js yaml swagger

Node.js Express应用程序中提供了REST Api服务,并正确指定了路由。我计划使用node.js自动生成庞大的YAML Open API规范。如果您有更多端点,那么手动编写一个庞大的YAML会很困难,并且添加更多端点的意图将需要在任何地方进行更新。

示例:考虑一个REST API服务包含两个基本端点,并在以下YAML中指定

swagger: "2.0"
info:
  version: "0.0.1"
  title: Hello World App
# during dev, should point to your local machine
host: localhost:10010
# basePath prefixes all resource paths 
basePath: /
# 
schemes:
  # tip: remove http to make production-grade
  - http
  - https
# format of bodies a client can send (Content-Type)
consumes:
  - application/json
# format of the responses to the client (Accepts)
produces:
  - application/json
paths:
  /hello:
    # binds a127 app logic to a route
    x-swagger-router-controller: hello_world
    get:
      description: Returns 'Hello' to the caller
      # used as the method name of the controller
      operationId: hello
      parameters:
        - name: name
          in: query
          description: The name of the person to whom to say hello
          required: false
          type: string
      responses:
        "200":
          description: Success
          schema:
            # a pointer to a definition
            $ref: "#/definitions/HelloWorldResponse"
        # responses may fall through to errors
        default:
          description: Error
          schema:
            $ref: "#/definitions/ErrorResponse"
  /try:
    x-swagger-router-controller: hello_world
    get:
      description: Returns 'Try' to the caller
      operationId: try
      parameters:
        - name: name
          in: query
          description: The content to try
          required: false
          type: string
      responses:
        "200":
          description: Success
          schema:
            $ref: "#/definitions/HelloWorldResponse"
        default:
          description: Error
          schema:
            $ref: "#/definitions/ErrorResponse"
  /swagger:
    x-swagger-pipe: swagger_raw
# complex objects have schema definitions
definitions:
  HelloWorldResponse:
    required:
      - message
    properties:
      message:
        type: string
  ErrorResponse:
    required:
      - message
    properties:
      message:
        type: string

我的疑问是如何在Node.js中生成此YAML?是否有特定的NPM软件包或任何灵活的模块可用于生成这种YAML或自行编写代码?

尝试分析执行此操作的更好方法。我感谢所有建议,并在此先感谢!

0 个答案:

没有答案