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

simple-swagger-codegen

v0.9.0

Published

A swagger processor & mustache template rendered for code generation from swagger JSON documents.

Downloads

3

Readme

Simple Swagger Codegen

Swagger Tools + Mustache codegen, simply renders a provided mustache template against a swagger swagger definition.

TODO

  • add tests

Installation

Download/clone this repo for now using curl, git or your favorite github client app. Requires Node.js version 10.0 or higher. Use nvm to easily switch between different versions of node runtimes.

Usage

const fs = require('fs');
const codegen = require('./lib/codegen');
const swaggerDoc = require('./swagger.json');

const template = fs.readFileSync('./templates/classGenerator.mustache', 'utf-8');

codegen.processSwagger(swaggerDoc, imports).then((swagger) => {
  let code = codegen.renderTemplate(template, swagger);
  fs.writeFileSync('./controller.js', code);
});

Example

Run example with the command node example.js

Options

processSwagger(...) accepts one argument: a swagger document parsed into a JSON object. You can do this

  swagger:
    type: object
    required: true
    description: a json swagger document
  imports:
    type: array
    required: false
    description: optional data structure for node.js module imports
  customParams:
    type: object
    required: false
    description: optional variables to add to the object passed to mustache

renderTemplate({...}) accepts the following parameters:

  swagger:
    type: object
    required: true
    description: any json object, intended to be the formatted swagger object from processSwagger()
  template:
    type: object
    required: true
    description: a .mustache template

Template Variables

The following data are passed to the mustache templates:

domain:
  type: string
  description: If all options defined: swagger.schemes[0] + '://' + swagger.host + swagger.basePath
securityDefinitions:
  type: array
  items:
    type: object
    properties:
      name:
        type: string
      type:
        type: string
      authorizationUrl:
        type: string
      flow:
        type: string
      scopes:
        type: object
definitions:
  type: array
  items:
    type: object
    properties:
      name:
        type: string
      type:
        type: string
      properties:
        type: object
operations:
  type: array
  items:
    type: object
    properties:
      summary:
        type: string
      description:
        type: string
      operationId:
        type: string
      path:
        type: string
      method:
        type: string
      tags:
        type: array
        items:
          type: string
      consumes:
        type: array
        items:
          type: string
      produces:
        type: array
        items:
          type: string
      parameters:
        type: array
        items:
          type: object
          properties:
            in:
              type: string
            name:
              type: string
            description:
              type: string
            required:
              type: boolean
            schema:
              type: object
                $ref:
                  type: string
        responses:
          type: object
        security:
          type: array
          items:
            type: object

Custom Mustache Parameters

You can also pass in your own parameters for the mustache templates by adding a customParams object:

var source = codegen.processSwagger({
    ...
    customParams: {
      foo: 'bar',
      app_build_id: env.BUILD_ID,
      app_version: pkg.version
    }
});