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

gatsby-plugin-swagger-jsdoc

v1.0.1

Published

A Gatsby plugin to automatically generate Swagger OpenAPI spec from JSDoc-style comments

Downloads

20

Readme

gatsby-plugin-swagger-jsdoc

Provides drop-in support for generating a Swagger UI docs page for your REST API backend, automatically generated from JSDoc-style comments.

This plugin uses swagger-jsdoc to generate the OpenAPI Specification definition required by Swagger UI, and then renders the result using the official swagger-ui-react package.

Install

npm install gatsby-plugin-swagger-jsdoc

How to use

Add in your gatsby-config.js:

plugins: [
  {
    resolve: 'gatsby-plugin-swagger-jsdoc',
    options: {
      uiRoute: '/api', // Path to your desired API docs page
      source: [`${__dirname}/src/api/**/*.js`], // Recursively scan `api` folder
      definition: {
        info: {
          title: 'Your API Title',
          description: 'Your API description',
          version: '1.0.0',
        },
      },
    },
  },
];

Now you can add your JSDoc-style comments to your source code, and your API docs page will be built automatically. You could follow the sample JSDoc in examples below.

Configuration options

uiRoute [array|string][required]

The path to your desired API docs page, where the generated Swagger UI is shown. This page will be auto-generated by this plugin.

source [array|string][optional]

Paths of the source files to scan for @openapi annotations. By default, it scans the api folder and all its subfolders (['/src/api/**/*.js']). You can change the value to scan any files, but only JSDoc-style comments are scanned. **/* means recursively scan subfolders.

definition [object][optional]

Extra properties to pass down to Swagger spec definition. For example, you could put your API info definition here:

definition: {
  info: {
    title: 'Your API Title',
    description: 'Your API description',
    version: '1.0.0'
  }
}

Examples

Add @openapi annotated JSDoc-style comments to any source file you wish, as long as your source file is listed under the source option:

// src/api/letters/[value].js
const handler = async (req, res) => {
  switch (req.method) {
    case 'GET':
      /**
       * @openapi
       * /api/letters/{value}:
       *   get:
       *     summary: Get details of a letter by value.
       *     description: Returns the details information about a letter.
       *     tags:
       *       - Letters
       *     parameters:
       *       - name: value
       *         in: path
       *         description: Value of the letter
       *         schema:
       *           type: string
       *     responses:
       *       200:
       *         description: Success
       *         content:
       *           application/json:
       *             schema:
       *               type: object
       *               properties:
       *                 value:
       *                   type: string
       *                 name:
       *                   type: string
       *                 greekAlt:
       *                   type: string
       */

      // You could generate a static .json result by plugin `gatsby-plugin-copy-files`,
      // or using GraphQL query by plugin `gatsby-plugin-json-output`.

      return res.sendFile(`api/letters/${req.params.value}.json`);

    case 'POST':
    /**
     * @openapi
     * /api/letters:
     *   post:
     *     summary: Add a letter.
     *     description: This API will make changes and push changes to git remote
     *     tags:
     *       - Letters
     *     responses:
     *       200:
     *         description: OK
     */

    // ... YOUR POST METHOD

    default:
      return res.sendStatus(405);
  }
};

export default handler;