1.6.3 • Published 5 years ago

swagger-node-codegen v1.6.3

Weekly downloads
700
License
-
Repository
github
Last release
5 years ago

The generated code features:

  • ES7
  • ESLint
  • YAML config file
  • Express
  • No transpiling

Install

To use it from the CLI:

npm install -g swagger-node-codegen

To use it as a module in your project:

npm install --save swagger-node-codegen

Requirements

  • Node.js v7.6+

Usage

From the command-line interface (CLI)

  Usage: snc [options] <swaggerFile>


  Options:

    -V, --version                  output the version number
    -o, --output <outputDir>       directory where to put the generated files (defaults to current directory)
    -t, --templates <templateDir>  directory where templates are located (defaults to internal nodejs templates)
    -h, --help                     output usage information

Examples

The shortest possible syntax:

snc swagger.yaml

Specify where to put the generated code:

snc swagger.yaml -o ./my-api

As a module in your project

const path = require('path');
const codegen = require('swagger-node-codegen');
const swagger = require('./swagger.json');

codegen.generate({
  swagger,
  target_dir: path.resolve(__dirname, './my-api')
}).then(() => {
  console.log('Done!');
}).catch(err => {
  console.error(`Something went wrong: ${err.message}`);
});

The swagger parameter can be either JSON or a path pointing to a JSON or YAML file.

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

codegen.generate({
  swagger: path.resolve(__dirname, './swagger.yml'),
  target_dir: path.resolve(__dirname, './my-api')
}).then(() => {
  console.log('Done!');
}).catch(err => {
  console.error(`Something went wrong: ${err.message}`);
});

Using async/await

The function codegen.generate returns a Promise, so it means you can use async/await:

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

try {
  await codegen.generate({
    swagger: path.resolve(__dirname, './swagger.yml'),
    target_dir: path.resolve(__dirname, './my-api')
  });
  console.log('Done!');
} catch (err) {
  console.error(`Something went wrong: ${err.message}`);
}

API Documentation

Modules

codegen

This module generates a code skeleton for an API using OpenAPI/Swagger.

generate ⇒ Promise

Generates a code skeleton for an API given an OpenAPI/Swagger file.

ParamTypeDescription
configObjectConfiguration options
config.swaggerObject | StringOpenAPI/Swagger JSON or a string pointing to an OpenAPI/Swagger file.
config.target_dirStringPath to the directory where the files will be generated.
config.templatesStringPath to the directory where custom templates are (optional).

Templates

You can create your own templates.

Authors

1.6.3

5 years ago

1.6.2

5 years ago

1.6.1

5 years ago

1.6.0

5 years ago

1.5.0

5 years ago

1.4.0

6 years ago

1.3.1

6 years ago

1.3.0

6 years ago

1.2.0

6 years ago

1.1.3

6 years ago

1.1.2

6 years ago

1.1.1

6 years ago

1.1.0

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago

0.2.0

6 years ago

0.1.0

7 years ago

0.0.10

7 years ago

0.0.9

8 years ago

0.0.8

8 years ago

0.0.7

8 years ago

0.0.6

8 years ago

0.0.5

8 years ago

0.0.4

8 years ago

0.0.3

8 years ago

0.0.2

8 years ago

0.0.1

8 years ago