codegen-swagger-express

1.0.1 • Public • Published

OpenAPI Node.js
Code Generator



Use your API OpenAPI 3.x/Swagger 2 definition to generate Node.js ES7-compliant code for your API.

The generated code features:

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

Install

To use it from the CLI:

npm install -g codegen-swagger-express

To use it as a module in your project:

npm install --save codegen-swagger-express

Requirements

  • Node.js v7.6+

Usage

From the command-line interface (CLI)

  Usage: cse [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:

cse swagger.yaml

Specify where to put the generated code:

cse swagger.yaml -o ./my-api

As a module in your project

const path = require('path');
const codegen = require('codegen-swagger-express');
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('codegen-swagger-express');

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('codegen-swagger-express');

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.

generatePromise

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

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.

Param Type Description
config Object Configuration options
config.swagger Object | String OpenAPI/Swagger JSON or a string pointing to an OpenAPI/Swagger file.
config.target_dir String Path to the directory where the files will be generated.
config.templates String Path to the directory where custom templates are (optional).

Templates

You can create your own templates.

Authors

  • AndreOneti

Package Sidebar

Install

npm i codegen-swagger-express

Weekly Downloads

0

Version

1.0.1

License

MIT

Unpacked Size

78 kB

Total Files

33

Last publish

Collaborators

  • andreoneti