openapi-backend icon indicating copy to clipboard operation
openapi-backend copied to clipboard

Add endpoint to serve openapi definition

Open cthiebault opened this issue 4 years ago • 1 comments

I would like to serve my openapi.yaml definition on /api-docs endpoint so I can use it in Swagger UI.

Using Express, I would do: app.use('/api-docs', Express.static('openapi.yaml'));

Maybe we could use a new configuration property to support this new endpoint? Something like:

new OpenAPIBackend({
    definition: 'openapi.yaml',
    serveDefinition: '/api-docs',
    handlers: ...
  });

What do you think?

BTW, thanks for your great job :+1:

cthiebault avatar Jan 22 '20 13:01 cthiebault

Hi @cthiebault ! Thanks for this suggestion.

It's a good idea, and I've wondered about this too before. However, I'm just not sure how to implement this.

See the problem is that the handlers you register are dependent on your platform / framework.

For express the definition handler would look something like this:

function serveDefinition(c, req, res) {
  const definition = c.api.document;
  return res.status(200).json(definition);
}

For hapi:

function serveDefinition(c, req, h) {
  const definition = c.api.document;
  return definition;
}

For aws-lambda:

function serveDefinition(c, event) {
  const definition = c.api.document;
  return {
    statusCode: 200,
    body: JSON.stringify(definition),
    headers: {
      'content-type': 'application/json',
    },
  };
}

See the issue?

I definitely see the convenience of having a default way to serve the input definition through the api.handleRequest method somehow but I'm just not sure how the API should look without making it too difficult.

Feedback would be appreciated!

anttiviljami avatar Feb 07 '20 15:02 anttiviljami