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-helper

v1.1.0

Published

Help to create consistent swagger documentation for an api

Downloads

89

Readme

swagger-doc-helper

Help to create consistent swagger v2.0 documentation for an api. It makes a lot of assumptions in an attempt to be less verbose most of the time. Where it doesn't provide the flexibility required, just merge swagger fragments as you might otherwise do.

Refer to https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md for details of the specification.

It provides a method to get the base documentation containing the structural pieces and some common items. It also provides a method to get the documentation fragment for a specific operation - i.e. something like GET /user

See test/index.js for a contrived usage example.

Example usage

  1. Get the base documentation. This returns a Javascript Object with the required swagger doc structure and some common bits such as security and standard responses. The intent is that swagger paths, definitions, etc. specific to each operation are added to this.

     const swaggerHelper = require('swagger-doc-helper')
    
     // Get the base JSON with the swagger doc structure and some common bits.
     // This gives valid swagger documentation, just with no paths as yet
    
     const baseDoc = swaggerHelper.getBase({
       title: 'My api',
       description: 'I like my api',
     })
  2. Get the documentation for an operation. This example shows how you might structure the document fragments for a specific entity (e.g. user). The resulting Javascript Object can be deep-merged into the base. The swagger operation spec is here: https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#operationObject. This helper supports the features most commonly used. There is reducing benefit in supporting more features as it starts to become as complex as just following the specification directly.

     const tag = 'user';
    
     const userDoc = {
       tags: [{ name: tag, description: tag }],
       paths: {
         '/user/{username}': {
           get: swaggerHelper.getOperation({
             tag,
             summary: 'Get a user by username',
             param: 'username',
             resBody: 'userModel',
           }),
         },
       },
       definitions: {
         userModel: {
           type: 'object',
           properties: {
             username: { type: 'string', description: 'The username' },
             firstName: { type: 'string', description: 'The user first name' },
             lastName: { type: 'string', description: 'The user last name' },
           },
         },
       },
       parameters: {
         username: {
           name: 'username',
           in: 'path',
           description: 'Identifies the user by username',
           type: 'string',
           required: true,
         },
       },
     }
  3. Because each entity requires entries in multiple swagger doc locations (paths, defintiions, etc.), these need to be deep-merged with other entities into the base doc.

     const merge = require('deepmerge')
    
     const finalDoc = merge(baseDoc, userDoc)

getBase(options)

  1. title: A title for your api
  2. description: A description for your api

getOperation(options)

  1. tag: The tag name(s). String or array.
  2. summary: Short summary of the operation.
  3. description: Longer description (defaults to summary if not specified).
  4. reqBody: The name of the swagger definitions item holding the request body definition.
  5. resBody: The name of the swagger definitions item holding the response body definition. If specified, this will be the 200 response. If not specified, 204 is the success response.
  6. param: The name(s) of the request parameter swagger definition(s). String or array.
  7. n0400: A 400 (validation error) response will be included unless this is set to true.
  8. n0401: A 401 (auth failure) response will be included unless this is set to true.
  9. n0404: A 404 (not found) response will be included unless this is set to true.