From e1566689e365ab9991f638d85348c73877b33538 Mon Sep 17 00:00:00 2001
From: Peter
Date: Wed, 13 Mar 2019 18:02:37 +0300
Subject: [PATCH 01/58] Add splitter
---
.gitignore | 1 +
.jshintignore | 1 +
.jshintrc | 5 +-
README.md | 28 +++
lib/codegen.js | 64 ++++--
lib/splitter.js | 145 ++++++++++++
package-lock.json | 383 ++++++++++++++++++++++++++------
package.json | 5 +-
templates/method.mustache | 170 +++++++-------
templates/multi-class.mustache | 20 ++
templates/multi-method.mustache | 59 +++++
templates/node-class.mustache | 320 +++++++++++++-------------
12 files changed, 871 insertions(+), 330 deletions(-)
create mode 100644 .jshintignore
create mode 100644 lib/splitter.js
create mode 100644 templates/multi-class.mustache
create mode 100644 templates/multi-method.mustache
diff --git a/.gitignore b/.gitignore
index 311312ea..b0f128e1 100644
--- a/.gitignore
+++ b/.gitignore
@@ -6,3 +6,4 @@ wks.*
*.swp
tmp-*
+zz/
diff --git a/.jshintignore b/.jshintignore
new file mode 100644
index 00000000..c7bd5c3c
--- /dev/null
+++ b/.jshintignore
@@ -0,0 +1 @@
+lib/splitter.js
diff --git a/.jshintrc b/.jshintrc
index d7950bea..768d6862 100644
--- a/.jshintrc
+++ b/.jshintrc
@@ -7,7 +7,7 @@
"curly": true,
"eqeqeq": true,
"immed": true,
- "indent": 4,
+ "indent": 2,
"latedef": true,
"newcap": false,
"noarg": true,
@@ -18,6 +18,5 @@
"strict": true,
"trailing": true,
"smarttabs": true,
- "globals": {
- }
+ "globals": {}
}
diff --git a/README.md b/README.md
index 0a725330..1c4b856a 100644
--- a/README.md
+++ b/README.md
@@ -80,6 +80,34 @@ In addition to the common options listed below, `getCustomCode()` *requires* a `
description: swagger object
```
+If it is required to generate multiple files for Node (i. e. multiple methods based on the initial JSON) provide the following option:
+
+ multiple: true
+
+When this option is provided, the module will return an array of objects, that have the following structure:
+
+ { content: ,
+ file: ,
+ functions: ,
+ directory: ,
+ isWrapper: }
+
+This structure describes a file, that contains generated REST APIs
+
+`content` - file contents, single string
+
+`file` - file name
+
+`functions` - array of function names
+
+`directory` - name of the directory, that should be created for this file
+
+`isWrapper` - will be **true** for wrapper file
+
+Resulting array should always contain a single wrapper file
+
+**Please notice**: when using this option, the module returns a **promise**
+
### Template Variables
The following data are passed to the [mustache templates](https://github.com/janl/mustache.js):
diff --git a/lib/codegen.js b/lib/codegen.js
index 54dbe7ef..db0c0b32 100644
--- a/lib/codegen.js
+++ b/lib/codegen.js
@@ -5,7 +5,9 @@ var Mustache = require('mustache');
var beautify = require('js-beautify').js_beautify;
var lint = require('jshint').JSHINT;
var _ = require('lodash');
+
var ts = require('./typescript');
+var splitter = require('./splitter');
var normalizeName = function(id) {
return id.replace(/\.|\-|\{|\}|\s/g, '_');
@@ -47,41 +49,44 @@ var getViewForSwagger2 = function(opts, type){
definitions: []
};
- _.forEach(swagger.paths, function(api, path){
+ _.forEach(swagger.paths, function(api, path) {
var globalParams = [];
/**
* @param {Object} op - meta data for the request
* @param {string} m - HTTP method name - eg: 'get', 'post', 'put', 'delete'
*/
- _.forEach(api, function(op, m){
- if(m.toLowerCase() === 'parameters') {
+ _.forEach(api, function(op, m) {
+ if (m.toLowerCase() === 'parameters') {
globalParams = op;
}
});
- _.forEach(api, function(op, m){
+ _.forEach(api, function(op, m) {
var M = m.toUpperCase();
- if(M === '' || authorizedMethods.indexOf(M) === -1) {
+ // check if this is a supported method
+ if (M === '' || authorizedMethods.indexOf(M) === -1) {
return;
}
var secureTypes = [];
- if(swagger.securityDefinitions !== undefined || op.security !== undefined) {
- var mergedSecurity = _.merge([], swagger.security, op.security).map(function(security){
+ if (swagger.securityDefinitions !== undefined || op.security !== undefined) {
+ var mergedSecurity = _.merge([], swagger.security, op.security).map(function(security) {
return Object.keys(security);
});
- if(swagger.securityDefinitions) {
- for(var sk in swagger.securityDefinitions) {
- if(mergedSecurity.join(',').indexOf(sk) !== -1){
+ if (swagger.securityDefinitions) {
+ for (var sk in swagger.securityDefinitions) {
+ if (mergedSecurity.join(',').indexOf(sk) !== -1) {
secureTypes.push(swagger.securityDefinitions[sk].type);
}
}
}
}
+
var methodName = (op.operationId ? normalizeName(op.operationId) : getPathToMethodName(opts, m, path));
+
// Make sure the method name is unique
- if(methods.indexOf(methodName) !== -1) {
+ if (methods.indexOf(methodName) !== -1) {
var i = 1;
- while(true) {
- if(methods.indexOf(methodName + '_' + i) !== -1) {
+ while (true) {
+ if (methods.indexOf(methodName + '_' + i) !== -1) {
i++;
} else {
methodName = methodName + '_' + i;
@@ -105,8 +110,14 @@ var getViewForSwagger2 = function(opts, type){
isSecureApiKey: secureTypes.indexOf('apiKey') !== -1,
isSecureBasic: secureTypes.indexOf('basic') !== -1,
parameters: [],
- headers: []
+ headers: [],
};
+
+ // add 'destination' field if 'multiple: true'
+ if (opts.multiple) {
+ method.destination = op.tags[0].toLowerCase();
+ }
+
if(method.isSecure && method.isSecureToken) {
data.isSecureToken = method.isSecureToken;
}
@@ -117,7 +128,7 @@ var getViewForSwagger2 = function(opts, type){
data.isSecureBasic = method.isSecureBasic;
}
var produces = op.produces || swagger.produces;
- if(produces) {
+ if (produces) {
method.headers.push({
name: 'Accept',
value: `'${produces.map(function(value) { return value; }).join(', ')}'`,
@@ -253,9 +264,16 @@ var getViewForSwagger1 = function(opts, type){
return data;
};
+/**
+ * Generate code based on the input file
+ * @param opts