completecoding / serverless-auto-swagger

80 stars 48 forks source link

Allow use of 'serverless-http' when reading functions #85

Open SebaSOFT opened 2 years ago

SebaSOFT commented 2 years ago

Currently the plugin does not support proxy installations with custom handlers. This is understandable but many templates for "express.js project for lambda functions" come with this feature built in. As the handler should be available for the plugin API I wanted to show my setup:

in my serverless.yml

...
plugins:
  - serverless-auto-swagger
  - serverless-dotenv-plugin
  - serverless-webpack
  - serverless-offline
  - serverless-plugin-scripts
  - serverless-apigw-binary
...
functions:
  app:
    handler: app.serverless # reference the file and exported method
    events:
      - http:
          path: / # this matches the base path
          method: ANY
      - http:
          path: /{proxy+} # this matches any path, the token 'any' doesn't mean anything special
          method: ANY

and then on app.js

const sls = require('serverless-http');
...
...
...
module.exports = app;
module.exports.serverless = sls(app);

With this setup, the sls generate-swagger command will generate a json description that looks like this:

{
  "swagger": "2.0",
  "info": {
    "title": "swagger",
    "version": "1"
  },
  "paths": {
    "/": {
      "any": {
        "summary": "app",
        "description": "",
        "operationId": "app.any./",
        "consumes": [
          "application/json"
        ],
        "produces": [
          "application/json"
        ],
        "parameters": [],
        "responses": {
          "200": {
            "description": "200 response"
          }
        },
        "security": [
          {
            "Authorization": [],
            "x-api-key": []
          }
        ]
      }
    },
    "/{proxy+}": {
      "any": {
        "summary": "app",
        "description": "",
        "operationId": "app.any./{proxy+}",
        "consumes": [
          "application/json"
        ],
        "produces": [
          "application/json"
        ],
        "parameters": [
          {
            "name": "proxy+",
            "in": "path",
            "required": true,
            "type": "string"
          }
        ],
        "responses": {
          "200": {
            "description": "200 response"
          }
        },
        "security": [
          {
            "Authorization": [],
            "x-api-key": []
          }
        ]
      }
    }
  },
  "definitions": {},
  "securityDefinitions": {
    "Authorization": {
      "type": "apiKey",
      "name": "Authorization",
      "in": "header"
    },
    "x-api-key": {
      "type": "apiKey",
      "name": "x-api-key",
      "in": "header"
    }
  },
  "basePath": "/"
}

But the expected result would be that the plugin generates a json that would be representative of the express app which is distributed in many files/folders for big APIs.

Tested with node/lambda

bfaulk96 commented 2 years ago

I'm not following what the question is here.

Are you saying you'd expect the output to look like the code you posted? Or are you saying the output currently looks like the code you posted?

SebaSOFT commented 2 years ago

Sorry about my english and lack of clarity. I updated the OP to be more descriptive. Congratulations BTW as this project is a great plugin that allows for keeping the source project nice a tidy without Swagger UI's custom CSS/JS/HTML laying around

bfaulk96 commented 2 years ago

In what order are your serverless plugins?

If serverless-auto-swagger is before serverless-http, it would almost certainly need to come after it.

If you already have serverless-auto-swagger after serverless-http, I'm afraid this probably cannot be avoided, as serverless-auto-swagger builds the swagger docs off of the serverless config supplied to it.

SebaSOFT commented 2 years ago

In what order are your serverless plugins?

If serverless-auto-swagger is before serverless-http, it would almost certainly need to come after it.

If you already have serverless-auto-swagger after serverless-http, I'm afraid this probably cannot be avoided, as serverless-auto-swagger builds the swagger docs off of the serverless config supplied to it.

I've added my plugins list

bfaulk96 commented 2 years ago

Ah, yeah.. If serverless-http is not a serverless plugin (and therefore would happen after serverless plugins are installed), then I don't know that there's any way to do what you're requesting.

Perhaps if we exposed a function to generate autoswagger via Node?

SebaSOFT commented 2 years ago

serverless-http is not a plugin, but a handler to be invoked by the schema handler to generate a "final" lambda functions list. Autoswagger via node is always nice

bfaulk96 commented 2 years ago

Right, auto-swagger via node would probably be the only way to make what you're asking possible. I unfortunately don't have a lot of free time right now, but PRs are always welcome 😄