Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

swagger-codegen

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

swagger-codegen

Code generation for Swagger based API's.

  • 0.1.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
186
decreased by-45.61%
Maintainers
1
Weekly downloads
 
Created
Source

swagger-codegen

Travis-CI Build Prod Dependencies Dev Dependencies Coverage Status npm version

Stats Downloads

Code generation for Swagger based API's. Supports NodeJS 4.x+.

Overview

A code generation module for simplifying the consumption of Swagger services. Allows generation of content based on groupings of custom attributes on paths and per-entity/definition.

The code is intended to be as pluggable as reasonably practical, and pull requests to support additional features are very, very welcome.

Gulp Task

This module is leveraged in the gulp-swagger-codegen module, but can be used independently for generating code at runtime.

Usage

The code generation can be invoked with:

const codegen = require('swagger-codegen');

// Synchonrous method
codegen({
  // Parse your YAML or JSON and load here
  swagger: yourSwaggerObjectHere,

  // Templates that run per #/definition
  perDefinition: {
    // Substitute for your own handlebars template
    // and generate as many as you want.
    './path/to/def-template.hbs': {
      target: './target-folder',
      extension: '.js', // Default
      /* Add your own options for templates here */
    }
  },

  // Templates that run per grouping of 
  // path attributes
  perPath: {
    // Substitute for your own handlebars template
    // and generate as many as you want.
    './path/to/def-template.hbs': {
      groupBy: 'x-swagger-router-controller',
      target: './controllers',
      extension: '.js', // Default
      operations: ['get', 'put', 'post', 'delete'], // Default
      /* Add your own options for templates here */
    }
  }
});

Additional options

In addition to the swagger, perPath and perDefinition options, you can pass through the following values. All of the below have default implementations that can be used:

  • definitionMapper | (model) -> map
    • Compute a map/array of definition names to definitions. Allows you to selectively filter or substitute definitions during code generation.
  • failureHandler | (err) -> action
    • Run a command when an error occurs. Processing of the entire code generation task is abandoned. Default implementation exits the process with an error code of -1 and writes the message/stack trace to stdout.
  • templateEngine | (taskOptions) --> engine
    • Function to create an instance of the template engine. Recieves the entire task options as an input. There is a default .helpers property that contains the well-known helper functions.
  • templateLoader | (templater) -> templateLoader
    • Function that generates types to handle template loading. Default implementation is a class that reads from disk. Returned object must have a .loadTemplate object that returns a template-function.
  • textEncoding | (string)
    • Default text encoding when loading templates from files. Default is utf8

Custom Template Engines

To use a custom template engine besides handlebars:

  • Create a function such as: function createTemplateEngine(opts) { return new MyTemplateEngine(); }

  • Pass the template loader as: codegen({ ... task options ...,

      templateEngine: createTemplateEngine
    });
    

Note that 'MyTemplateEngineType' instances must have a .loadTemplate(content) implementation, where content is the loaded text of the template file. The .loadTemplate operation must return a function such as:

// Normally called by the swagger-codegen
const engine = new MyTemplateEngine();

// Must have a .loadTemplate(string) method
const templateInstance = engine.loadTemplate(someTextContent);

// Must be a function that takes context data
const output = templateInstance(contextData);

Where contextData is the template-type specific context data, as described in the templating context data section. This model allows most pluggable template engines to be used in lieu of the default handlebars.

Licence

This code is MIT licensed and comes without any warranties. Please see the LICENSE file for specifics.

Keywords

FAQs

Package last updated on 02 May 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc