npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

swagger-doc-gen-cli

v2.3.0

Published

Dynamically generate a OpenAPI specification boilerplate that has your API endpoints added in already

Downloads

6

Readme

swagger-doc-gen-cli

https://www.npmjs.com/package/swagger-doc-gen-cli

What it is

This is an npm package that can be used to dynamically generate a Swagger documentation specification file from your express app which can then be used along with the swagger-ui-express package to create your API documentation.

Why use it?

This relieves the developer from creating a spec with thousands of lines as your API may have many different endpoints. This package generates majority of the specification (generates an entry for each API endpoint, an entry for each HTTP method for that endpoint and adds in all the route parameters for that endpoint), the only part that needs to be filled in is the details for the request and response body format for your API endpoints, the template for that is given as well.

Installation

npm i swagger-doc-gen-cli

Requirements

The tool requires that the express app that needs to documented exports the app as follows: module.exports = app

Usage

From the command line: make-spec <relativePathToYourExpressServer> yaml || json <relativePathToWhereYouWantTheOutputFile>

The 2nd argument specifies whether the spec is desired in YAML or JSON format. Only one must be provided

You can then use the generated swagger.yaml or swagger.json along with the swagger-ui-express package to view your API documentation

Example

Sample express app

const express = require('express');
const app = express();

app.get('/api/1/:someParam', (req,res) => res.send('myapi'));
app.post('/api/1/:someParam', (req,res) => res.send('myapi'));
app.delete('/api/1/:someParam', (req,res) => res.send('myapi'));
app.put('/api/1/:someParam', (req,res) => res.send('myapi'));

app.listen(3000);
module.exports = app;

Assuming the code is inside index.js and the file structure is as follows ?

Then if we are inside the routes directory, from the command line we can run: make-spec ./index.js json ./

This means we want to generate the spec for the API endpoints found in index.js and ./index.js is the relative path to it, we want it in json format and we want the file to be generated in the current directory, hence ./

Which will the generate the OpenAPI specification and will be named swagger.json. If you want the YAML equivalent then run make-spec ./index.js yaml ./

There should now be a swagger.jsoninside the routes directory as follows

?

Note that if your server is listening on a port, after running the command it will appear as if the program has not exited, this is because the app is listening for requests, you can simply Ctrl^C to exit

Now this file can be used along with the swagger-ui-express package to view and host your API documentation as follows

const express = require('express');
const app = express();
const swaggerUi = require('swagger-ui-express');
const swaggerDocument = require('./swagger.json');
 
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument));

You can find more about the package here

For our example the documentation looks like

?

As you can the tool does much of the heavy lifting and leaves very little parts that need to be filled out

You can also paste the contents of the file here and get a preview. If you are using Gitlab and push the swagger file to your repository, Gitlab allows to you preview the documentation simply by opening the file (No plugins required)

Open to contributions and ideas for improvement!

License

MIT