added nodejs server

This commit is contained in:
Tony Tam 2014-10-02 00:10:15 -07:00
parent c10cf7bf7f
commit b834ba436f
5 changed files with 89 additions and 11 deletions

View File

@ -69,6 +69,8 @@ public class Codegen extends DefaultGenerator {
return new JavaClientCodegen(); return new JavaClientCodegen();
else if("jaxrs".equals(name)) else if("jaxrs".equals(name))
return new JaxRSServerCodegen(); return new JaxRSServerCodegen();
else if("nodejs".equals(name))
return new NodeJSServerCodegen();
else if("static".equals(name)) else if("static".equals(name))
return new StaticDocCodegen(); return new StaticDocCodegen();
else if(name.indexOf(".") > 0) { else if(name.indexOf(".") > 0) {

View File

@ -413,8 +413,8 @@ public class DefaultCodegen {
property.name = toVarName(name); property.name = toVarName(name);
property.baseName = name; property.baseName = name;
property.description = p.getDescription(); property.description = p.getDescription();
property.getter = "get" + name.substring(0, 1).toUpperCase() + name.substring(1); property.getter = "get" + initialCaps(name);
property.setter = "set" + name.substring(0, 1).toUpperCase() + name.substring(1); property.setter = "set" + initialCaps(name);
property.example = p.getExample(); property.example = p.getExample();
property.defaultValue = toDefaultValue(p); property.defaultValue = toDefaultValue(p);
@ -508,7 +508,7 @@ public class DefaultCodegen {
if(builder.toString().length() == 0) if(builder.toString().length() == 0)
part = Character.toLowerCase(part.charAt(0)) + part.substring(1); part = Character.toLowerCase(part.charAt(0)) + part.substring(1);
else else
part = Character.toUpperCase(part.charAt(0)) + part.substring(1); part = initialCaps(part);
builder.append(part); builder.append(part);
} }
} }

View File

@ -0,0 +1,71 @@
package com.wordnik.swagger.codegen.languages;
import com.wordnik.swagger.codegen.*;
import com.wordnik.swagger.models.properties.*;
import java.util.*;
import java.io.File;
public class NodeJSServerCodegen extends DefaultCodegen implements CodegenConfig {
protected String invokerPackage = "com.wordnik.client";
protected String groupId = "com.wordnik";
protected String artifactId = "swagger-client";
protected String artifactVersion = "1.0.0";
public NodeJSServerCodegen() {
super();
outputFolder = "generated-code/nodejs";
apiTemplateFiles.put("api.mustache", ".js");
templateDir = "nodejs";
apiPackage = "app.apis";
modelPackage = "app";
additionalProperties.put("invokerPackage", invokerPackage);
additionalProperties.put("groupId", groupId);
additionalProperties.put("artifactId", artifactId);
additionalProperties.put("artifactVersion", artifactVersion);
supportingFiles.add(new SupportingFile("package.mustache", "", "package.json"));
supportingFiles.add(new SupportingFile("models.mustache", modelPackage, "models.js"));
supportingFiles.add(new SupportingFile("main.mustache", "", "main.js"));
supportingFiles.add(new SupportingFile("README.mustache", "", "README.js"));
languageSpecificPrimitives = new HashSet<String>(
Arrays.asList(
"String",
"boolean",
"Boolean",
"Double",
"Integer",
"Long",
"Float")
);
typeMapping.put("array", "array");
}
@Override
public String escapeReservedWord(String name) {
return "_" + name;
}
@Override
public String apiFileFolder() {
return outputFolder + File.separator + apiPackage().replaceAll("\\.", File.separator);
}
public String modelFileFolder() {
return outputFolder + File.separator + modelPackage().replaceAll("\\.", File.separator);
}
@Override
public String getSwaggerType(Property p) {
String swaggerType = super.getSwaggerType(p);
String type = null;
if(typeMapping.containsKey(swaggerType)) {
return typeMapping.get(swaggerType);
}
else
type = swaggerType;
return toModelName(type);
}
}

View File

@ -33,7 +33,17 @@ exports.{{nickname}} = {
{{/headerParams}}]).concat([{{#bodyParams}} {{/headerParams}}]).concat([{{#bodyParams}}
params.body("body", "{{swaggerDataType}}", "{{description}}", {{#required}}{{required}}{{/required}}{{^required}}false{{/required}}) params.body("body", "{{swaggerDataType}}", "{{description}}", {{#required}}{{required}}{{/required}}{{^required}}false{{/required}})
{{/bodyParams}}]), {{/bodyParams}}]),
{{#returnContainer}}
"type": "{{returnType}}", "type": "{{returnType}}",
"items": {
{{#returnTypeIsPrimitive}}"type": "{{returnContainer}}"{{/returnTypeIsPrimitive}}
{{^returnTypeIsPrimitive}}"$ref": "{{returnContainer}}"{{/returnTypeIsPrimitive}}
},
// container
{{/returnContainer}}
{{^returnContainer}}
"type" : "{{returnType}}",
{{/returnContainer}}
"responseMessages" : [errors.invalid('id'), errors.notFound('{{returnType}}')], "responseMessages" : [errors.invalid('id'), errors.notFound('{{returnType}}')],
"nickname" : "{{nickname}}" "nickname" : "{{nickname}}"
}, },

View File

@ -23,7 +23,7 @@ app.use(cors(corsOptions));
swagger.setAppHandler(app); swagger.setAppHandler(app);
swagger.configureSwaggerPaths("", "api-docs", "") swagger.configureSwaggerPaths("", "api-docs", "")
var models = require("./models.js"); var models = require("./app/models.js");
{{#apiInfo}} {{#apiInfo}}
{{#apis}} {{#apis}}
@ -32,13 +32,8 @@ var {{classname}} = require("./{{apiFolder}}/{{classname}}.js");
{{/apiInfo}} {{/apiInfo}}
swagger.addModels(models) swagger.addModels(models)
{{#apiInfo}} {{#apiInfo}}{{#apis}}{{#operations}}{{#operation}}.add{{httpMethod}}({{classname}}.{{nickname}}){{/operation}}{{/operations}}
{{#apis}} {{/apis}}{{/apiInfo}};
{{#operations}}
{{#operation}}.add{{httpMethod}}({{classname}}.{{nickname}}){{newline}}{{/operation}}
{{/operations}}
{{/apis}};
{{/apiInfo}}
// configures the app // configures the app
swagger.configure("http://localhost:8002", "0.1"); swagger.configure("http://localhost:8002", "0.1");