https://github.com/fmvilas/swagger-node-codegen
An OpenAPI 3.x/Swagger 2 code generator for Node.js
https://github.com/fmvilas/swagger-node-codegen
codegen nodejs openapi openapi3 swagger
Last synced: 10 months ago
JSON representation
An OpenAPI 3.x/Swagger 2 code generator for Node.js
- Host: GitHub
- URL: https://github.com/fmvilas/swagger-node-codegen
- Owner: fmvilas
- License: apache-2.0
- Created: 2015-11-22T12:22:22.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2021-11-18T19:24:38.000Z (about 4 years ago)
- Last Synced: 2024-10-11T03:44:18.209Z (over 1 year ago)
- Topics: codegen, nodejs, openapi, openapi3, swagger
- Language: JavaScript
- Homepage:
- Size: 255 KB
- Stars: 200
- Watchers: 8
- Forks: 55
- Open Issues: 10
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
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:
```bash
npm install -g swagger-node-codegen
```
To use it as a module in your project:
```bash
npm install --save swagger-node-codegen
```
## Requirements
* Node.js v7.6+
## Usage
### From the command-line interface (CLI)
```bash
Usage: snc [options]
Options:
-V, --version output the version number
-o, --output directory where to put the generated files (defaults to current directory)
-t, --templates directory where templates are located (defaults to internal nodejs templates)
-h, --help output usage information
```
#### Examples
The shortest possible syntax:
```bash
snc swagger.yaml
```
Specify where to put the generated code:
```bash
snc swagger.yaml -o ./my-api
```
### As a module in your project
```js
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.
```js
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:
```js
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.
### 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](./templates/README.md).
## Authors
* Fran Méndez ([@fmvilas](http://twitter.com/fmvilas))
* Richard Klose ([@richardklose](http://github.com/richardklose))