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

@shapediver/util.oas3

v1.0.1

Published

Tool to generate a OpenAPI v3 specification from JsDocs

Downloads

13

Readme

util.oas3

This utility-library enables to generate a OpenAPI v3 Specification (OAS) that has been defined by using JsDoc-like comments. The library also allows merging multiple existing OAS specifications. This enables the usage of partial specifications in separate projects and linking them dynamically later on.

Comments

Individual elements of the specification are created via JSDoc-like comments which the structure @swagger {<path>} <name> <value>.\

Notes:

  • Only /** */-comments starting with the @swagger-tag are processed.
  • Multiple tags can be used inside a single comment.
  • When passing an object or an array as value, watch strict validation of JSON-objects!
  • OpenAPI v3 features like inheritance or $ref can be used normally.
  • Object and Array-values can be created and extended via multiple comments (see examples 2 and 3).
  • When an array-value is extended via multiple comments, similar elements are ignored (see example 2).
  • When an object-value is extended via multiple comments, similar properties are overwritten (see example 3).

Example 1

The following example

/**
 * @swagger {components.schemas} Foo {
    "description": "Foo or Bar",
    "type": "string",
    "enum": [ "''", "foo", "bar" ]
  }
 */

results in

{
  "components": {
    "schemas": {
      "Foo": {
        "description": "Foo or Bar",
        "type": "string",
        "enum": [
          "''",
          "foo",
          "bar"
        ]
      }
    }
  }
}

Example 2

In this example, an array is created and extended via multiple comments. However, the similar element "bar" is ignored:

/**
 * @swagger {components.schemas.Foo} required [ "foo", "bar" ]
 * @swagger {components.schemas.Foo} required [ "bar", "baz" ]
 */

Result:

{
  "components": {
    "schemas": {
      "Foo": {
        "required": [
          "foo",
          "bar",
          "baz"
        ]
      }
    }
  }
}

Example 3

In this example, an object is created, extended and partly overwritten via multiple comments:

/**
 * @swagger {components.schemas} Foo {
    "foo": "bar"
    "baz": "qux"
  }
 * @swagger {components.schemas} Foo {
    "baz": "bar"
  }
 */

Result:

{
  "components": {
    "schemas": {
      "Foo": {
        "foo": "bar",
        "baz": "bar"
      }
    }
  }
}

Example 4

This example shows how to specify and extend an object inside an array of objects:

/**
 * @swagger {components.schemas} Foo {
     "allOf": [
       { "$ref": "#/components/schemas/Bar" },
       { "type": "object" }
     ]
   }
 * @swagger {components.schemas.Foo.allOf.[1].properties} something {
     "type": "number"
   }
 */

Result:

{
  "components": {
    "schemas": {
      "Foo": {
        "allOf": [
          {
            "$ref": "#/components/schemas/Bar"
          },
          {
            "type": "object",
            "properties": {
              "something": {
                "type": "number"
              }
            }
          }
        ]
      }
    }
  }
}

Usage

The library can be installed globally or as a npm dependency and run via a CLI.

# List all commands and their options
./node_modules/.bin/sd-oas3 --help

# When adding ./node_modules/.bin to $PATH, the command can be invoked directly
sd-oas3 --help

# Generate a new specification
sd-oas3 generate -s ./src/ -e ts -o ./swagger.json

# Merge two existing specifications and write the result into a new file
sd-oas3 merge -s ./specA.json ./specB.json -f ./res.json

# Merge three existing specifications into another specification
sd-oas3 merge -s ./specA.json ./specB.json ./specC.json -t ./target.json