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 -t, --templates <templateDir> directory where templates are located -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 = ;const codegen = ;const swagger = ; codegen;
The swagger
parameter can be either JSON or a path pointing to a JSON or YAML file.
const path = ;const codegen = ; codegen;
Using async/await
The function codegen.generate
returns a Promise, so it means you can use async/await:
const path = ;const codegen = ; try await codegen; console; catch err console;
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.
codegen
This module generates a code skeleton for an API using OpenAPI/Swagger.
Promise
generate ⇒ 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
- Fran Méndez (@fmvilas)
- Richard Klose (@richardklose)