ServiceEmitter { public void emitTypescriptInterface() { Set<String> endpointsToWarnAboutDuplicateNames = Sets.newHashSet(); if (!this.settings.emitDuplicateJavaMethodNames()) { endpointsToWarnAboutDuplicateNames = getDuplicateEndpointNames(); } writer.writeLine(""); writer.writeLine("export interface " + settings.getSettings().addTypeNamePrefix + model.name() + " {"); writer.increaseIndent(); for (InnerServiceModel innerServiceModel : model.innerServiceModels()) { if (model.innerServiceModels().size() > 1) { writer.writeLine(""); writer.writeLine(" } for (ServiceEndpointModel endpointModel: innerServiceModel.endpointModels()) { if (!endpointsToWarnAboutDuplicateNames.contains(endpointModel.endpointName())) { String line = endpointModel.endpointName() + "("; line += getEndpointParametersString(endpointModel); line += String.format("): " + settings.genericEndpointReturnType(), endpointModel.tsReturnType().toString()) + ";"; writer.writeLine(line); } } } if (!endpointsToWarnAboutDuplicateNames.isEmpty()) { writer.writeLine(""); } for (String endpointName : endpointsToWarnAboutDuplicateNames) { writer.writeLine(String.format(" } 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 testComplexServiceClassEmitInterface() { ServiceModel model = serviceClassParser.parseServiceClass(TestComplexServiceClass.class, settings); ServiceEmitter serviceEmitter = new ServiceEmitter(model, settings, writer); serviceEmitter.emitTypescriptInterface(); writer.close(); String expectedOutput = "\n" + "export interface TestComplexServiceClass {\n" + " allOptionsPost(a: string, dataObject: DataObject, b?: number): FooType<GenericObject<MyObject>>;\n" + " queryGetter(x?: boolean): FooType<MyObject>;\n" + " simplePut(dataObject: DataObject): FooType<ImmutablesObject>;\n" + "}\n"; assertEquals(expectedOutput, new String(stream.toByteArray())); }
@Test public void testDuplicateMethodInterface() { ServiceModel model = serviceClassParser.parseServiceClass(DuplicateMethodNamesService.class, settings); ServiceEmitter serviceEmitter = new ServiceEmitter(model, settings, writer); serviceEmitter.emitTypescriptInterface(); writer.close(); String expectedOutput = "\n" + "export interface DuplicateMethodNamesService {\n" + "\n" + " "}\n"; assertEquals(expectedOutput, new String(stream.toByteArray())); }
@Test public void testMultipleClasses() { ServiceModel model = serviceClassParser.parseServiceClass(SimpleService1.class, settings, SimpleService2.class); ServiceEmitter serviceEmitter = new ServiceEmitter(model, settings, writer); serviceEmitter.emitTypescriptInterface(); writer.close(); String expectedOutput = "\n" + "export interface SimpleService1 {\n" + "\n" + " " method1(): FooType<string>;\n" + "\n" + " " method2(): FooType<string>;\n" + "}\n"; assertEquals(expectedOutput, new String(stream.toByteArray())); }