Added Angular 2 module structure.

This commit is contained in:
Sebastian Haas
2016-10-05 23:48:13 +02:00
committed by wing328
parent 0f8ce8b8e2
commit 7ea419dc5d
8 changed files with 124 additions and 19 deletions

View File

@@ -55,7 +55,8 @@ public abstract class AbstractTypeScriptClientCodegen extends DefaultCodegen imp
"Array",
"Date",
"number",
"any"
"any",
"File"
));
instantiationTypes.put("array", "Array");
@@ -81,6 +82,7 @@ public abstract class AbstractTypeScriptClientCodegen extends DefaultCodegen imp
typeMapping.put("binary", "string");
typeMapping.put("ByteArray", "string");
typeMapping.put("UUID", "string");
typeMapping.put("File", "any");
cliOptions.add(new CliOption(CodegenConstants.MODEL_PROPERTY_NAMING, CodegenConstants.MODEL_PROPERTY_NAMING_DESC).defaultValue("camelCase"));
cliOptions.add(new CliOption(CodegenConstants.SUPPORTS_ES6, CodegenConstants.SUPPORTS_ES6_DESC).defaultValue("false"));

View File

@@ -3,6 +3,7 @@ package io.swagger.codegen.languages;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -37,7 +38,7 @@ public class TypeScriptAngular2ClientCodegen extends AbstractTypeScriptClientCod
embeddedTemplateDir = templateDir = "typescript-angular2";
modelTemplateFiles.put("model.mustache", ".ts");
apiTemplateFiles.put("api.mustache", ".ts");
apiTemplateFiles.put("api.service.mustache", ".ts");
typeMapping.put("Date","Date");
apiPackage = "api";
modelPackage = "model";
@@ -71,6 +72,8 @@ public class TypeScriptAngular2ClientCodegen extends AbstractTypeScriptClientCod
supportingFiles.add(new SupportingFile("models.mustache", modelPackage().replace('.', File.separatorChar), "models.ts"));
supportingFiles.add(new SupportingFile("apis.mustache", apiPackage().replace('.', File.separatorChar), "api.ts"));
supportingFiles.add(new SupportingFile("index.mustache", getIndexDirectory(), "index.ts"));
supportingFiles.add(new SupportingFile("api.module.mustache", getIndexDirectory(), "api.module.ts"));
supportingFiles.add(new SupportingFile("rxjs-operators.mustache", getIndexDirectory(), "rxjs-operators.ts"));
supportingFiles.add(new SupportingFile("configuration.mustache", getIndexDirectory(), "configuration.ts"));
supportingFiles.add(new SupportingFile("variables.mustache", getIndexDirectory(), "variables.ts"));
supportingFiles.add(new SupportingFile("gitignore", "", ".gitignore"));
@@ -135,19 +138,13 @@ public class TypeScriptAngular2ClientCodegen extends AbstractTypeScriptClientCod
if(languageSpecificPrimitives.contains(swaggerType)) {
return swaggerType;
}
return addModelPrefix(swaggerType);
applyLocalTypeMapping(swaggerType);
return swaggerType;
}
private String addModelPrefix(String swaggerType) {
String type = null;
if (typeMapping.containsKey(swaggerType)) {
type = typeMapping.get(swaggerType);
} else {
type = swaggerType;
}
if (!startsWithLanguageSpecificPrimitiv(type)) {
type = "models." + swaggerType;
private String applyLocalTypeMapping(String type) {
if (typeMapping.containsKey(type)) {
type = typeMapping.get(type);
}
return type;
}
@@ -164,12 +161,16 @@ public class TypeScriptAngular2ClientCodegen extends AbstractTypeScriptClientCod
@Override
public void postProcessParameter(CodegenParameter parameter) {
super.postProcessParameter(parameter);
parameter.dataType = addModelPrefix(parameter.dataType);
parameter.dataType = applyLocalTypeMapping(parameter.dataType);
}
@Override
public Map<String, Object> postProcessOperations(Map<String, Object> operations) {
Map<String, Object> objs = (Map<String, Object>) operations.get("operations");
// Add filename information for api imports
objs.put("apiFilename", getApiFilenameFromClassname(objs.get("classname").toString()));
List<CodegenOperation> ops = (List<CodegenOperation>) objs.get("operation");
for (CodegenOperation op : ops) {
// Convert httpMethod to Angular's RequestMethod enum
@@ -204,9 +205,67 @@ public class TypeScriptAngular2ClientCodegen extends AbstractTypeScriptClientCod
op.path = op.path.replaceAll("\\{(.*?)\\}", "\\$\\{$1\\}");
}
// Add additional filename information for model imports in the services
List<Map<String, Object>> imports = (List<Map<String, Object>>) operations.get("imports");
for(Map<String, Object> im : imports) {
im.put("filename", im.get("import"));
im.put("classname", getModelnameFromModelFilename(im.get("filename").toString()));
}
return operations;
}
@Override
public Map<String, Object> postProcessModels(Map<String, Object> objs) {
Map<String, Object> result = super.postProcessModels(objs);
// Add additional filename information for imports
List<Object> models = (List<Object>) postProcessModelsEnum(result).get("models");
for (Object _mo : models) {
Map<String, Object> mo = (Map<String, Object>) _mo;
CodegenModel cm = (CodegenModel) mo.get("model");
Map<String, String> tsImports = new HashMap<String, String>();
for(String im : cm.imports) {
tsImports.put("classname", im);
tsImports.put("filename", toModelFilename(im));
mo.put("tsImports", tsImports);
}
}
return result;
}
@Override
public String toApiName(String name) {
if (name.length() == 0) {
return "DefaultService";
}
return initialCaps(name) + "Service";
}
@Override
public String toApiFilename(String name) {
if (name.length() == 0) {
return "default.service";
}
return camelize(name, true) + ".service";
}
@Override
public String toApiImport(String name) {
return apiPackage() + "/" + toApiFilename(name);
}
@Override
public String toModelFilename(String name) {
return camelize(toModelName(name), true);
}
@Override
public String toModelImport(String name) {
return modelPackage() + "/" + toModelFilename(name);
}
public String getNpmName() {
return npmName;
}
@@ -230,4 +289,14 @@ public class TypeScriptAngular2ClientCodegen extends AbstractTypeScriptClientCod
public void setNpmRepository(String npmRepository) {
this.npmRepository = npmRepository;
}
private String getApiFilenameFromClassname(String classname) {
String name = classname.substring(0, classname.length() - "Service".length());
return toApiFilename(name);
}
private String getModelnameFromModelFilename(String filename) {
String name = filename.substring((modelPackage() + "/").length());
return camelize(name);
}
}

View File

@@ -0,0 +1,17 @@
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { HttpModule } from '@angular/http';
{{#apiInfo}}
{{#apis}}
import { {{classname}} } from './{{importPath}}';
{{/apis}}
{{/apiInfo}}
@NgModule({
imports: [ CommonModule, HttpModule ],
declarations: [],
exports: [],
providers: [ {{#apiInfo}}{{#apis}}{{classname}}{{#hasMore}}, {{/hasMore}}{{/apis}}{{/apiInfo}} ]
})
export class ApiModule {}

View File

@@ -5,9 +5,12 @@ import { RequestMethod, RequestOptions, RequestOptionsArgs } from '@angular/http
import { Response, ResponseContentType } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';
import '../rxjs-operators';
{{#imports}}
import { {{classname}} } from '../{{filename}}';
{{/imports}}
import * as models from '../model/models';
import { BASE_PATH } from '../variables';
import { Configuration } from '../configuration';

View File

@@ -1,7 +1,7 @@
{{#apiInfo}}
{{#apis}}
{{#operations}}
export * from './{{ classname }}';
export * from './{{ apiFilename }}';
{{/operations}}
{{/apis}}
{{/apiInfo}}

View File

@@ -1,7 +1,10 @@
{{>licenseInfo}}
{{#models}}
{{#model}}
import * as models from './models';
{{#tsImports}}
import { {{classname}} } from './{{filename}}';
{{/tsImports}}
{{#description}}
/**

View File

@@ -1,5 +1,5 @@
{{#models}}
{{#model}}
export * from './{{{ classname }}}';
export * from './{{{ classFilename }}}';
{{/model}}
{{/models}}

View File

@@ -0,0 +1,11 @@
// RxJS imports according to https://angular.io/docs/ts/latest/guide/server-communication.html#!#rxjs
// See node_module/rxjs/Rxjs.js
// Import just the rxjs statics and operators we need for THIS app.
// Statics
import 'rxjs/add/observable/throw';
// Operators
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/map';