Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Like swagger/swashbuckle but for node.js?

Is there any tool for node express where you can automatically generate swagger documentation for an existing project? Similar to swashbuckle?

like image 515
PvPlatten Avatar asked Nov 17 '15 20:11

PvPlatten


1 Answers

I've been looking into this as well, the project which will help you is swagger-node-express. swagger-ui, should come as a dependency of swagger-node-express. Swagger-node-express wraps express and exposes it as a new interface meaning there will be code changes you to make it work. This is what a route would look like (taken from their docs)

 var findById = {
  'spec': {
    "description" : "Operations about pets",
    "path" : "/pet.{format}/{petId}",
    "notes" : "Returns a pet based on ID",
    "summary" : "Find pet by ID",
    "method": "GET",
    "parameters" : [swagger.pathParam("petId", "ID of pet that needs to be fetched", "string")],
    "type" : "Pet",
    "errorResponses" : [swagger.errors.invalid('id'), swagger.errors.notFound('pet')],
    "nickname" : "getPetById"
  },
  'action': function (req,res) {
    if (!req.params.petId) {
      throw swagger.errors.invalid('id');
    }
    var id = parseInt(req.params.petId);
    var pet = petData.getPetById(id);

    if (pet) {
      res.send(JSON.stringify(pet));
    } else {
      throw swagger.errors.notFound('pet');
    }
  }
};

Where the type "Pet" is still for you to define, I wont rewrite their docs here.

This will then produce a file which swagger-ui can use to give you a self contained self documenting system. The documentation for swagger-node-express is more than good enough to get it setup (dont forget to set swagger path, I did).

swagger.configureSwaggerPaths("", "/docs", "");

Having shown you the tools that in theory offer what your asking for, let me explain why I've come to conclusion that I'm not going to use them.

  1. A lot of code change needed - is it really less work than creating your own swagger.yml file? I dont think so.
  2. Hand creating a swagger.yml file is much less likely to brake your project.
  3. Whilst swagger-node-express hasnt been depricated it's github repo doesnt exist anymore, its been wrapped into swagger-node, but that project doesnt really mention it
  4. I'd be wary of any tool that means I need to wrap express - it's not something I'd look to do.

TL;DR: It's possible with a lot of code change - probably not what your after though.

like image 91
cconolly Avatar answered Sep 28 '22 06:09

cconolly