herbsjs / herbs

A domain-first framework. Code your domain and your infrastructure will follow
https://herbsjs.org/
MIT License
427 stars 14 forks source link

herbs2swagger #9

Open italojs opened 2 years ago

italojs commented 2 years ago

I think would be great if we could to use the herbs2rest metadata to generate a swagger page and a SDK-client for apis.

Following the @expresso router lib from @roziscoding, it receive a openAPIspecification and the swagger lib generates the documentation aaaaand the SDK-client. thinking in herbsjs, would be great if I give the route matadata for a usecase or routes so it generates a swagger endpoint into my Express app.


const express = require('express')
const { generateSwagger } = require('@herbsjs/herbs2swagger')

const app = express()
const openAPISpecifications =     {
      "title": "Sample Pet Store App",
      "description": "This is a sample server for a pet store.",
      "termsOfService": "http://example.com/terms/",
      "contact": {
        "name": "API Support",
        "url": "http://www.example.com/support",
        "email": "support@example.com"
      },
      "license": {
        "name": "Apache 2.0",
        "url": "https://www.apache.org/licenses/LICENSE-2.0.html"
      },
      "version": "1.0.1",
    "routes":     {
      "get": {
        "description": "Returns pets based on ID",
        "summary": "Find pets by ID",
        "operationId": "getPetsById",
        "responses": {
          "200": {
            "description": "pet response",
            "content": {
              "*/*": {
                "schema": {
                  "type": "array",
                  "items": {
                    "$ref": "#/components/schemas/Pet"
                  }
                }
              }
            }
          },
          "default": {
            "description": "error payload",
            "content": {
              "text/html": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorModel"
                }
              }
            }
          }
        }
      },
      "parameters": [
        {
          "name": "id",
          "in": "path",
          "description": "ID of pet to use",
          "required": true,
          "schema": {
            "type": "array",
            "items": {
              "type": "string"
            }
          },
          "style": "simple"
        }
      ]
    }
generateSwagger(app, routes)

we could use it together herbs2rest like

const express = require('express')
const { generateRoutes } = require('@herbsjs/herbs2rest')
const { useSwagger } = require('@herbsjs/herbs2swagger')

const app = express()
const routes = new express.Router()

const metadatas = generateRoutes(controllerList, routes, true)
useSwagger(app,metadatas)

app.use(routes)
jhomarolo commented 2 years ago

I like the idea, but I think this could be a feature inside herbs2rest instead of a new library. What do you think?

italojs commented 2 years ago

I thought that too, but I think it could be used without herbs2rest, imagine the APIs that implements herbs but doenst use the herbs2rest yet, they can manually(or automatically) write the openAPISpecification so pass it to herbs2swagger, they dont need to do a big change into they projects to have a autogenerated swagger/sdk-client

dalssoft commented 2 years ago

Nice idea!

I would start by making it part of herbs2rest in order to move fast, delivery value and, if it make sense, refactoring to split the glues in the future.