ServiceEmitter { public void emitTypescriptClass() { Set<String> endpointsToWarnAboutDuplicateNames = Sets.newHashSet(); if (!this.settings.emitDuplicateJavaMethodNames()) { endpointsToWarnAboutDuplicateNames = getDuplicateEndpointNames(); } writer.writeLine(""); writer.writeLine("export class " + model.name() + "Impl" + " implements " + settings.getSettings().addTypeNamePrefix + model.name() + " {"); writer.increaseIndent(); writer.writeLine(""); writer.writeLine(String.format("private httpApiBridge: %sHttpApiBridge;", settings.generatedInterfacePrefix())); writer.writeLine(String.format("constructor(httpApiBridge: %sHttpApiBridge) {", settings.generatedInterfacePrefix())); writer.increaseIndent(); writer.writeLine("this.httpApiBridge = httpApiBridge;"); writer.decreaseIndent(); writer.writeLine("}"); for (InnerServiceModel innerServiceModel : model.innerServiceModels()) { if (model.innerServiceModels().size() > 1) { writer.writeLine(""); writer.writeLine(" } for (ServiceEndpointModel endpointModel: innerServiceModel.endpointModels()) { if (endpointsToWarnAboutDuplicateNames.contains(endpointModel.endpointName())) { continue; } writer.writeLine(""); String line = "public " + endpointModel.endpointName() + "("; line += getEndpointParametersString(endpointModel); line += ") {"; writer.writeLine(line); writer.increaseIndent(); writer.writeLine(String.format("var httpCallData = <%sHttpEndpointOptions> {", settings.generatedInterfacePrefix())); writer.increaseIndent(); writer.writeLine("serviceIdentifier: \"" + Character.toLowerCase(model.name().charAt(0)) + model.name().substring(1) + "\","); writer.writeLine("endpointPath: \"" + getEndpointPathString(innerServiceModel, endpointModel) + "\","); writer.writeLine("endpointName: \"" + endpointModel.endpointName() + "\","); writer.writeLine("method: \"" + endpointModel.endpointMethodType() + "\","); writer.writeLine("requestMediaType: \"" + endpointModel.endpointRequestMediaType() + "\","); writer.writeLine("responseMediaType: \"" + optionalToString(endpointModel.endpointResponseMediaType()) + "\","); List<String> requiredHeaders = Lists.newArrayList(); List<String> pathArguments = Lists.newArrayList(); List<String> queryArguments = Lists.newArrayList(); String dataArgument = null; for (ServiceEndpointParameterModel parameterModel : endpointModel.parameters()) { if (parameterModel.headerParam() != null) { requiredHeaders.add("\"" + parameterModel.headerParam() + "\""); } else if (parameterModel.pathParam() != null) { pathArguments.add(parameterModel.getParameterName()); } else if (parameterModel.queryParam() != null) { queryArguments.add(parameterModel.queryParam()); } else { if (dataArgument != null) { throw new IllegalStateException("There should only be one data argument per endpoint. Found both" + dataArgument + " and " + parameterModel.getParameterName()); } dataArgument = parameterModel.getParameterName(); boolean isEnum = false; if (parameterModel.javaType() instanceof Class<?>) { isEnum = ((Class<?>) parameterModel.javaType()).isEnum(); } if (endpointModel.endpointRequestMediaType().equals(MediaType.APPLICATION_JSON) && (parameterModel.tsType().toString().equals("string") || isEnum)) { dataArgument = "`\"${" + parameterModel.getParameterName() + "}\"`"; } } } writer.writeLine("requiredHeaders: [" + Joiner.on(", ").join(requiredHeaders) + "],"); writer.writeLine("pathArguments: [" + Joiner.on(", ").join(pathArguments) + "],"); writer.writeLine("queryArguments: {"); writer.increaseIndent(); for (String queryArgument: queryArguments) { writer.writeLine(queryArgument + ": " + queryArgument + ","); } writer.decreaseIndent(); writer.writeLine("},"); writer.writeLine("data: " + dataArgument); writer.decreaseIndent(); writer.writeLine("};"); writer.writeLine("return this.httpApiBridge.callEndpoint<" + endpointModel.tsReturnType().toString() + ">(httpCallData);"); writer.decreaseIndent(); writer.writeLine("}"); } } writer.decreaseIndent(); writer.writeLine("}"); } ServiceEmitter(ServiceModel model, TypescriptServiceGeneratorConfiguration settings, IndentedOutputWriter writer); void emitTypescriptTypes(TypescriptServiceGeneratorConfiguration settings, List<Type> additionalTypesToOutput); void emitTypescriptClass(); void emitTypescriptInterface(); static Set<Class<?>> getReferencedClasses(Set<Type> referencedTypes, TypescriptServiceGeneratorConfiguration settings);  }
@Test public void testComplexServiceClassEmitClass() { ServiceModel model = serviceClassParser.parseServiceClass(TestComplexServiceClass.class, settings); ServiceEmitter serviceEmitter = new ServiceEmitter(model, settings, writer); serviceEmitter.emitTypescriptClass(); writer.close(); String expectedOutput = "\n" + "export class TestComplexServiceClassImpl implements TestComplexServiceClass {\n" + "\n" + " private httpApiBridge: HttpApiBridge;\n" + " constructor(httpApiBridge: HttpApiBridge) {\n" + " this.httpApiBridge = httpApiBridge;\n" + " }\n" + "\n" + " public allOptionsPost(a: string, dataObject: DataObject, b?: number) {\n" + " var httpCallData = <HttpEndpointOptions> {\n" + " serviceIdentifier: \"testComplexServiceClass\",\n" + " endpointPath: \"testComplexService/allOptionsPost/{a}\",\n" + " endpointName: \"allOptionsPost\",\n" + " method: \"POST\",\n" + " requestMediaType: \"application/json\",\n" + " responseMediaType: \"\",\n" + " requiredHeaders: [],\n" + " pathArguments: [a],\n" + " queryArguments: {\n" + " b: b,\n" + " },\n" + " data: dataObject\n" + " };\n" + " return this.httpApiBridge.callEndpoint<GenericObject<MyObject>>(httpCallData);\n" + " }\n" + "\n" + " public queryGetter(x?: boolean) {\n" + " var httpCallData = <HttpEndpointOptions> {\n" + " serviceIdentifier: \"testComplexServiceClass\",\n" + " endpointPath: \"testComplexService/queryGetter\",\n" + " endpointName: \"queryGetter\",\n" + " method: \"GET\",\n" + " requestMediaType: \"application/json\",\n" + " responseMediaType: \"\",\n" + " requiredHeaders: [],\n" + " pathArguments: [],\n" + " queryArguments: {\n" + " x: x,\n" + " },\n" + " data: null\n" + " };\n" + " return this.httpApiBridge.callEndpoint<MyObject>(httpCallData);\n" + " }\n" + "\n" + " public simplePut(dataObject: DataObject) {\n" + " var httpCallData = <HttpEndpointOptions> {\n" + " serviceIdentifier: \"testComplexServiceClass\",\n" + " endpointPath: \"testComplexService/simplePut\",\n" + " endpointName: \"simplePut\",\n" + " method: \"PUT\",\n" + " requestMediaType: \"application/json\",\n" + " responseMediaType: \"\",\n" + " requiredHeaders: [],\n" + " pathArguments: [],\n" + " queryArguments: {\n" + " },\n" + " data: dataObject\n" + " };\n" + " return this.httpApiBridge.callEndpoint<ImmutablesObject>(httpCallData);\n" + " }\n" + "}\n"; assertEquals(expectedOutput, new String(stream.toByteArray())); }
@Test public void testDuplicateMethodClass() { ServiceModel model = serviceClassParser.parseServiceClass(DuplicateMethodNamesService.class, settings); ServiceEmitter serviceEmitter = new ServiceEmitter(model, settings, writer); serviceEmitter.emitTypescriptClass(); writer.close(); String expectedOutput = "\n" + "export class DuplicateMethodNamesServiceImpl implements DuplicateMethodNamesService {\n" + "\n" + " private httpApiBridge: HttpApiBridge;\n" + " constructor(httpApiBridge: HttpApiBridge) {\n" + " this.httpApiBridge = httpApiBridge;\n" + " }\n" + "}\n"; assertEquals(expectedOutput, new String(stream.toByteArray())); }
@Test public void testConcreteObjectService() { ServiceModel model = serviceClassParser.parseServiceClass(ConcreteObjectService.class, settings); ServiceEmitter serviceEmitter = new ServiceEmitter(model, settings, writer); serviceEmitter.emitTypescriptClass(); writer.close(); String expectedOutput = "\n" + "export class ConcreteObjectServiceImpl implements ConcreteObjectService {\n" + "\n" + " private httpApiBridge: HttpApiBridge;\n" + " constructor(httpApiBridge: HttpApiBridge) {\n" + " this.httpApiBridge = httpApiBridge;\n" + " }\n" + "\n" + " public noPathGetter() {\n" + " var httpCallData = <HttpEndpointOptions> {\n" + " serviceIdentifier: \"concreteObjectService\",\n" + " endpointPath: \"concreteObject\",\n" + " endpointName: \"noPathGetter\",\n" + " method: \"GET\",\n" + " requestMediaType: \"application/json\",\n" + " responseMediaType: \"\",\n" + " requiredHeaders: [],\n" + " pathArguments: [],\n" + " queryArguments: {\n" + " },\n" + " data: null\n" + " };\n" + " return this.httpApiBridge.callEndpoint<string>(httpCallData);\n" + " }\n" + "}\n"; assertEquals(expectedOutput, new String(stream.toByteArray())); }
@Test public void testEnumDataParameter() { ServiceModel model = serviceClassParser.parseServiceClass(EnumClass.class, settings); ServiceEmitter serviceEmitter = new ServiceEmitter(model, settings, writer); serviceEmitter.emitTypescriptClass(); writer.close(); String expectedToContain = "data: `\"${myEnum}\"`"; assertTrue(new String(stream.toByteArray()).contains(expectedToContain)); }