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

openapi-jsdoc

v0.0.2

Published

Generates OpenAPI doc based on JSDoc

Downloads

6,179

Readme

openapi-jsdoc

Dependencies GitHub license

Overview

openapi-jsdoc parses your OpenAPI (formerly known as Swagger) documentation from your JSDoc code comments.

Heavily inspired by swagger-jsdoc.

Supported versions

Getting started

To install run

$ npm install openapi-jsdoc

You need to define initial OpenAPI root object in definition property. For more detail see here.

openapi-jsdoc returns OpenAPI specification in json format.

const openapiJSDoc = require('openapi-jsdoc')

const api = openapiJSDoc({
  definition: {
    // info object, see https://swagger.io/specification/#infoObject
    info: {
      title: 'Example app', // required
      version: '1.0.0', // required
      description: 'A sample API for example app'
    }
  },
  // Paths to the API docs
  apis: ['./example/routes/*.js', './example/parameters.yaml']
})

Note that openapi-jsdoc uses node glob module in the background when taking your files. This means that you can use patterns such as *.js to select all javascript files or **/*.js to select all javascript files in sub-folders recursively.

How to document the API

The API can now be documented in JSDoc-style with swagger spec in YAML. Root property can be tags or components, everything else is considered to be API path names. See the example app in the example subdirectory to get sense of it.

  /**
   * @openapi
   * /login:
   *   post:
   *     operationId: login
   *     description: Login to the application
   *     tags: '/auth'
   *     requestBody:
   *       content:
   *         application/json:
   *           schema:
   *             type: object
   *             required:
   *               - name
   *               - password
   *             properties:
   *               name:
   *                 type: string
   *               password:
   *                 type: string
   *     responses:
   *       '200':
   *         description: Logged in user info
   *         content:
   *           application/json:
   *             schema:
   *               type: object
   *               $ref: '#/components/schemas/User'
   */
  app.post('/login', function (req, res) {
    res.json({ id: 1, name: req.body.name })
  })

To define a list of tags used by the specification:

  /**
   * @openapi
   * tags:
   *   - name: '/users'
   *     description: Users routes
   */

Components object holds various schemas and other reusable things for the specification:

/**
 * @openapi
 * components:
 *   schemas:
 *     UserExpanded:
 *       allOf:
 *         - $ref: '#/components/schemas/User'
 *         - type: object
 *           properties:
 *             address:
 *               type: string
 *     User:
 *       type: object
 *       properties:
 *         id:
 *           type: int
 *           format: uuid
 *         name:
 *           type: string
 *   parameters:
 *     - name: id
 *       in: path
 *       description: User ID
 *       required: true
 */

Example app

There is an example app in the example subdirectory. To use it you can use the following commands:

$ git clone https://github.com/dotaxe/openapi-jsdoc.git
$ cd openapi-jsdoc
$ npm install
$ npm start

The OpenAPI specification will be served at http://localhost:3000/api-docs.json