
Research
Security News
The Growing Risk of Malicious Browser Extensions
Socket researchers uncover how browser extensions in trusted stores are used to hijack sessions, redirect traffic, and manipulate user behavior.
swagger-node-codegen
Advanced tools
OpenAPI Node.js
Code Generator
The generated code features:
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
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
The shortest possible syntax:
snc swagger.yaml
Specify where to put the generated code:
snc swagger.yaml -o ./my-api
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}`);
});
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}`);
}
This module generates a code skeleton for an API using OpenAPI/Swagger.
Promise
Generates a code skeleton for an API given an OpenAPI/Swagger file.
This module generates a code skeleton for an API using OpenAPI/Swagger.
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). |
You can create your own templates.
FAQs
An OpenAPI 3.x/Swagger 2 code generator for Node.js
We found that swagger-node-codegen demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover how browser extensions in trusted stores are used to hijack sessions, redirect traffic, and manipulate user behavior.
Research
Security News
An in-depth analysis of credential stealers, crypto drainers, cryptojackers, and clipboard hijackers abusing open source package registries to compromise Web3 development environments.
Security News
pnpm 10.12.1 introduces a global virtual store for faster installs and new options for managing dependencies with version catalogs.