Code generation for Swagger based API's.
npm install swagger-codegen




Code generation for Swagger based API's. Supports NodeJS 4.x+.
The code is intended to be as pluggable as reasonably practical, and pull requests
to support additional features are _very, __very__ welcome_.
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 /
}
}
});
handlebars helper functions by adding a top-level configurationhelpers and assigning the functions as key-values. The key will be the#### arrayContains
Does the array contain an item?
{{#arrayContains arrayProp value}}
// Template if value is in array
{{else}}
// Template if value is not in array
{{/arrayContains}}
#### compare
Perform a comparison operation.
{{#compare leftVal operand rightVal}}
// Template if leftVal op rightVal true
{{else}}
// False template
{{/compare}}
Supported operands are ==, ===, !=, >, >=, <, <= and typeof
#### lowercase
Converts a block to lowercase.
{{#lowercase}}MAKE ME LOWERCASE{{/lowercase}}
#### lowerFirst
Makes the first character of a block lowercase, but leaves the rest
of the block untouched.
{{#lowerFirst}}MAKES FIRST M LOWERCASE{{/lowerFirst}}
{{#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.
#### uppercase
Converts a block to uppercase.
{{#uppercase}}i want to be tall{{/uppercase}}
#### upperFirst
Makes the first character of a block uppercas, but leaves the rest
of the block untouched.
{{#upperFirst}}mAKES THE FIRST m UPPERCASE{{/upperFirst}}
#### withDef
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.
swagger, perPath and perDefinition options, you - __definitionMapper__ | (model) -> map
- Compute a map/array of definition names to definitions. Allows you to
selectively filter or substitute definitions during code generation.
- __failureHandler__ | (err) -> action
- Run a command when an error occurs. Processing of the entire code
generation task is abandoned. Default implementation exits the process
with an error code of -1 and writes the message/stack trace to stdout.
- __templateEngine__ | (taskOptions) --> engine
- Function to create an instance of the template engine. Recieves the entire
task options as an input. There is a default .helpers property that contains
the well-known helper functions.
- __templateLoader__ | (templater) -> templateLoader
- Function that generates types to handle template loading. Default
implementation is a class that reads from disk. Returned object
must have a .loadTemplate object that returns a template-function.
- __textEncoding__ | (string)
- Default text encoding when loading templates from files. Default is utf8
- 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.