ServiceEmitter { public void emitTypescriptTypes(TypescriptServiceGeneratorConfiguration settings, List<Type> additionalTypesToOutput) { Settings settingsToUse = settings.getSettings(); TypeProcessor baseTypeProcessor = settingsToUse.customTypeProcessor; Set<Type> referencedTypes = Sets.newHashSet(model.referencedTypes().iterator()); referencedTypes.addAll(additionalTypesToOutput); Set<Class<?>> referencedClasses = getReferencedClasses(referencedTypes, settings); final Set<Type> discoveredTypes = Sets.newHashSet(referencedClasses.iterator()); referencedClasses = filterInputClasses(referencedClasses); TypeProcessor discoveringProcessor = new TypeProcessor() { @Override public Result processType(Type javaType, Context context) { discoveredTypes.add(javaType); return null; } }; settingsToUse.customTypeProcessor = discoveringProcessor; if (baseTypeProcessor != null) { settingsToUse.customTypeProcessor = new TypeProcessor.Chain(discoveringProcessor, baseTypeProcessor); } TypeScriptGenerator typescriptGenerator = new TypeScriptGenerator(settingsToUse); ByteArrayOutputStream typeDeclarations = new ByteArrayOutputStream(); Type[] types = new Type[referencedClasses.size()]; referencedClasses.toArray(types); int intendationLevel = 1; if (!settings.typescriptModule().isPresent()) { intendationLevel = 0; } typescriptGenerator.generateEmbeddableTypeScript(Input.from(types), Output.to(typeDeclarations), true, intendationLevel); writer.write(new String(typeDeclarations.toByteArray())); } 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 testComplexServiceClassEmitTypes() { ServiceModel model = serviceClassParser.parseServiceClass(TestComplexServiceClass.class, settings); ServiceEmitter serviceEmitter = new ServiceEmitter(model, settings, writer); serviceEmitter.emitTypescriptTypes(settings, Lists.newArrayList()); writer.close(); String expectedOutput = "" + "\n" + " export interface DataObject {\n" + " y: MyObject;\n" + " }\n" + "\n" + " export interface GenericObject<T> {\n" + " y: T;\n" + " }\n" + "\n" + " export interface ImmutablesObject {\n" + " y: string;\n" + " }\n" + "\n" + " export interface MyObject {\n" + " y: MyObject;\n" + " }\n\n\n ""; assertEquals(expectedOutput, new String(stream.toByteArray())); }
@Test public void testAdditionalClassesToOutput() { ServiceModel model = serviceClassParser.parseServiceClass(TestServiceClass.class, settings); ServiceEmitter serviceEmitter = new ServiceEmitter(model, settings, writer); serviceEmitter.emitTypescriptTypes(settings, Lists.newArrayList(MyObject.class)); writer.close(); String expectedOutput = "\n" + " export interface MyObject {\n" + " y: MyObject;\n" + " }\n\n\n assertEquals(expectedOutput, new String(stream.toByteArray())); }
@Test public void testEnumClass() { ServiceModel model = serviceClassParser.parseServiceClass(EnumClass.class, settings); ServiceEmitter serviceEmitter = new ServiceEmitter(model, settings, writer); serviceEmitter.emitTypescriptTypes(settings, Lists.newArrayList()); writer.close(); String expectedOutput = "\n" + " export type MyEnum = \"VALUE1\" | \"VALUE2\";\n" + "\n\n " export const MyEnum = {\n" + " VALUE1: <MyEnum>\"VALUE1\",\n" + " VALUE2: <MyEnum>\"VALUE2\",\n" + " }\n"; assertEquals(expectedOutput, new String(stream.toByteArray())); }