• Stars
    star
    200
  • Rank 194,112 (Top 4 %)
  • Language
    JavaScript
  • License
    Apache License 2.0
  • Created almost 9 years ago
  • Updated almost 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

An OpenAPI 3.x/Swagger 2 code generator for Node.js

THIS PACKAGE IS NOT MAINTAINED ANYMORE. IF YOU WANT TO MAINTAIN IT DROP ME A LINE AT fran.mendez[at]hey.com.

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 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.

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

More Repositories

1

topic-definition

AsyncAPI topic structure definition
57
star
2

Didgeridoo-IDE

Web-based IDE
JavaScript
12
star
3

didgeridoo

Your custom IDE on the browser
JavaScript
12
star
4

Mastering-Javascript-Prototypal-Inheritance

Slides from my talk at Itnig (Barcelona) on July 17, 2014
CSS
6
star
5

swagger-node-codegen-cli

CLI for swagger-node-codegen. Use your API Swagger definition to generate the Node.js ES6-compliant code of your API.
JavaScript
6
star
6

asyncapi-websockets-example

An example demoing how to use AsyncAPI and WebSockets
JavaScript
5
star
7

shapeup

JavaScript
3
star
8

sse

HTTP Server Sent Events implementation in Node.js
JavaScript
2
star
9

tol

Thinking Out Loud website
JavaScript
2
star
10

hermesjs-kafka

Kafka adapter for Hermes
JavaScript
2
star
11

asyncapi-to-postman

Creates a Postman collection from an AsyncAPI file.
JavaScript
2
star
12

Classical-Inheritance-vs-Modular-Patterns

Slides from my conference on Classical Inheritance, Prototypical Inheritance and Modular Patterns at Itnig, Barcelona.
JavaScript
2
star
13

baby

JavaScript
1
star
14

hermes-ws

WebSockets adapter for HermesJS. Powered by Socket.IO.
JavaScript
1
star
15

slack-classifier

Small Slack classifier utility
TypeScript
1
star
16

hermes-amqp

AMQP adapter for HermesJS
JavaScript
1
star
17

next-route-to-express-route

Converts Next.js routes to Express.js routes.
HTML
1
star
18

blog

Personal blog
JavaScript
1
star
19

didgeridoo-plugins

Plugins list for Didgeridoo IDE
1
star
20

hermes-initial

CSS
1
star
21

asyncapi-ui-kit

JavaScript
1
star
22

workshop-ride-app

HTML
1
star
23

pseudo-yaml-ast

Fork of pseudo-yaml-ast. Parse a YAML string into an object with location properties
JavaScript
1
star
24

making-sense-eda-book-landing

JavaScript
1
star