Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
swagger-codegen
Advanced tools
Code generation for Swagger based API's. Supports NodeJS 4.x+.
A code generation module for simplifying the consumption of Swagger services. Allows generation of content based on groupings of custom attributes on paths and per-entity/definition.
The code is intended to be as pluggable as reasonably practical, and pull requests to support additional features are very, very welcome.
This module is leveraged in the gulp-swagger-codegen module, but can be used independently for generating code at runtime.
The code generation can be invoked with:
const codegen = require('swagger-codegen');
// Synchonrous method
codegen({
// Parse your YAML or JSON and load here
swagger: yourSwaggerObjectHere,
// Templates that run per #/definition
perDefinition: {
// Substitute for your own handlebars template
// and generate as many as you want.
'./path/to/def-template.hbs': {
target: './target-folder',
extension: '.js', // Default
/* Add your own options for templates here */
}
},
// Templates that run per grouping of
// path attributes
perPath: {
// Substitute for your own handlebars template
// and generate as many as you want.
'./path/to/def-template.hbs': {
groupBy: 'x-swagger-router-controller',
target: './controllers',
extension: '.js', // Default
operations: ['get', 'put', 'post', 'delete'], // Default
/* Add your own options for templates here */
}
}
});
Each template is provided with a collection of objects to use when generating code. This collection varies based on the type of template being processed and there are some fields common across all types. This object is passed as a map to the template engine as the root-object.
The following fields are common for all template types:
Generated per entity/#definition in the Swaggerfile, fields are:
Fields are:
You can register your own handlebars
helper functions by adding a top-level configuration
property called helpers
and assigning the functions as key-values. The key will be the
exact block-helper name and the value must be the helper function itself.
Does the array contain an item?
{{#arrayContains arrayProp value}}
// Template if value is in array
{{else}}
// Template if value is not in array
{{/arrayContains}}
Perform a comparison operation.
{{#compare leftVal operand rightVal}}
// Template if leftVal op rightVal true
{{else}}
// False template
{{/compare}}
Supported operands are ==, ===, !=, >, >=, <, <= and typeof
Converts a block to lowercase.
{{#lowercase}}MAKE ME LOWERCASE{{/lowercase}}
Makes the first character of a block lowercase, but leaves the rest of the block untouched.
{{#lowerFirst}}MAKES FIRST M LOWERCASE{{/lowerFirst}}
Property name complication helper.
{{#property someProp "property-name-with-bad-characters" "resultName"}}
This scope will be `someProp` but with an extra sub-property of resultName
{{else}
This scope will be returned if property-name-with-bad-characters does not exist
{[/property
This is used primarily because handlebars does not natively seem to permit invalid characters in variable names. Some of the extended swaggerfile attributes are prefixed with x- and so to reason about them in templates, you'll need this.
Converts a block to uppercase.
{{#uppercase}}i want to be tall{{/uppercase}}
Makes the first character of a block uppercas, but leaves the rest of the block untouched.
{{#upperFirst}}mAKES THE FIRST m UPPERCASE{{/upperFirst}}
Creates a child scope using the specified definition as the context:
{{#withDef defReferencePath}}
// Generate a sub-section that relates to a definition.
{{/withDef}}
This allows creation of cross-entity relations/nesting, and is shown being used in the included ES6 definition template.
In addition to the swagger
, perPath
and perDefinition
options, you
can pass through the following values. All of the below have default
implementations that can be used:
To use a custom template engine besides handlebars:
Create a function such as: function createTemplateEngine(opts) { return new MyTemplateEngine(); }
Pass the template loader as: codegen({ ... task options ...,
templateEngine: createTemplateEngine
});
Note that 'MyTemplateEngineType' instances must have a .loadTemplate(content) implementation, where content is the loaded text of the template file. The .loadTemplate operation must return a function such as:
// Normally called by the swagger-codegen
const engine = new MyTemplateEngine();
// Must have a .loadTemplate(string) method
const templateInstance = engine.loadTemplate(someTextContent);
// Must be a function that takes context data
const output = templateInstance(contextData);
Where contextData is the template-type specific context data, as described in the templating context data section. This model allows most pluggable template engines to be used in lieu of the default handlebars.
This code is MIT licensed and comes without any warranties. Please see the LICENSE file for specifics.
FAQs
Code generation for Swagger based API's.
The npm package swagger-codegen receives a total of 145 weekly downloads. As such, swagger-codegen popularity was classified as not popular.
We found that swagger-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 a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.