@fxjs/swagger
v0.1.1
Published
Swagger to fibjs Codegen
Downloads
12
Readme
Swagger to fibjs Codegen
This package generates a fibjs class from a swagger specification file. The code is generated using mustache templates and is quality checked by jshint and beautified by js-beautify.
The generator is based on superagent and can be used for fibjs.
This fork was made to simplify some parts, add some more features, and tailor it more to specific use cases.
Installation
npm i @fxjs/swagger
cli
fswagger gen -c Test test.json test.js
Example Code
const fs = require("fs");
const CodeGen = require("@fxjs/swagger").CodeGen;
const file = "swagger/spec.json";
const swagger = JSON.parse(fs.readFile(file, "UTF-8"));
const tsSourceCode = CodeGen.getFibjsCode({
className: "Test",
swagger: swagger
}, {
templateType: 'typescript',
});
console.log(tsSourceCode);
Use custom templates:
const fs = require("fs");
const CodeGen = require("@fxjs/swagger").CodeGen;
const file = "swagger/spec.json";
const swagger = JSON.parse(fs.readFile(file, "UTF-8"));
const tsSourceCode = CodeGen.getFibjsCode({
className: "Test",
swagger: swagger,
template: {
class: fs.readFile("templates/class.mustache", "utf-8"),
method: fs.readFile("templates/method.mustache", "utf-8"),
type: fs.readFile("templates/type.mustache", "utf-8"),
}
}, {
// if templateType set, will use built-in template as default, which would be overrided by opt.template above
// templateType: 'javascript',
});
console.log(tsSourceCode);