Package io.jooby.trpc

Class TrpcGenerator

java.lang.Object
io.jooby.trpc.TrpcGenerator

public class TrpcGenerator extends Object
A generator that orchestrates typescript-generator to produce a tRPC-compatible TypeScript API definition from compiled Jooby controllers.

This tool bypasses the standard REST scanner of typescript-generator. Instead, it:

  1. Scans the classpath via the provided ClassLoader for controllers marked with @Trpc.
  2. Extracts only the input and return types (DTOs) of the matching methods.
  3. Feeds those data models to the generator to produce clean TypeScript interfaces.
  4. Uses a fast, recursive type resolver to accurately map Java methods to tRPC { input, output } shapes.
  5. Appends a strict AppRouter definition to the generated file.
  • Constructor Details

    • TrpcGenerator

      public TrpcGenerator()
  • Method Details

    • generate

      public void generate() throws IOException
      Executes the full TypeScript and tRPC generation pipeline.
      Throws:
      IOException - If an I/O error occurs reading classes or writing the output file.
      IllegalStateException - If outputDir is not configured or if no controllers are found.
    • addController

      public void addController(Class<?> controller)
      Explicitly adds a controller class to the generation pipeline. Highly recommended for unit testing to avoid classpath scanning issues.
      Parameters:
      controller - The controller class to analyze.
    • getClassLoader

      public ClassLoader getClassLoader()
      Returns:
      The class loader used to load compiled controllers.
    • setClassLoader

      public void setClassLoader(ClassLoader classLoader)
      Parameters:
      classLoader - The class loader used to load compiled controllers. Defaults to context class loader.
    • getOutputDir

      public Path getOutputDir()
      Returns:
      The destination directory for the generated TypeScript file.
    • setOutputDir

      public void setOutputDir(Path outputDir)
      Parameters:
      outputDir - The destination directory for the generated TypeScript file.
    • getOutputFile

      public String getOutputFile()
      Returns:
      The name of the generated TypeScript file.
    • setOutputFile

      public void setOutputFile(String outputFile)
      Parameters:
      outputFile - The name of the generated TypeScript file. Defaults to trpc.d.ts.
    • getJsonLibrary

      public cz.habarta.typescript.generator.JsonLibrary getJsonLibrary()
      Returns:
      The target JSON library for data model generation.
    • setJsonLibrary

      public void setJsonLibrary(cz.habarta.typescript.generator.JsonLibrary jsonLibrary)
      Parameters:
      jsonLibrary - The target JSON library used to parse field annotations. Defaults to Jackson 2.
    • getCustomTypeMappings

      public Map<String,String> getCustomTypeMappings()
      Returns:
      Custom mapping overrides translating Java types to raw TypeScript strings.
    • setCustomTypeMappings

      public void setCustomTypeMappings(Map<String,String> customTypeMappings)
      Parameters:
      customTypeMappings - Custom mapping overrides translating Java types to raw TypeScript strings.
    • getCustomTypeNaming

      public Map<String,String> getCustomTypeNaming()
      Returns:
      Custom overrides for generating specific TypeScript interface names.
    • setCustomTypeNaming

      public void setCustomTypeNaming(Map<String,String> customTypeNaming)
      Parameters:
      customTypeNaming - Custom overrides for generating specific TypeScript interface names.
    • getImportDeclarations

      public List<String> getImportDeclarations()
      Returns:
      Raw import statements appended to the top of the generated file.
    • setImportDeclarations

      public void setImportDeclarations(List<String> importDeclarations)
      Parameters:
      importDeclarations - Raw import statements appended to the top of the generated file.
    • getMapDate

      public cz.habarta.typescript.generator.DateMapping getMapDate()
      Returns:
      The mapping strategy applied to Java date types.
    • setMapDate

      public void setMapDate(cz.habarta.typescript.generator.DateMapping mapDate)
      Parameters:
      mapDate - The mapping strategy applied to Java date types.
    • getMapEnum

      public cz.habarta.typescript.generator.EnumMapping getMapEnum()
      Returns:
      The mapping strategy applied to Java enum types.
    • setMapEnum

      public void setMapEnum(cz.habarta.typescript.generator.EnumMapping mapEnum)
      Parameters:
      mapEnum - The mapping strategy applied to Java enum types.