55 */
66package io .jooby .trpc ;
77
8+ import java .util .List ;
9+ import java .util .stream .Stream ;
10+
811import edu .umd .cs .findbugs .annotations .NonNull ;
912import io .jooby .Extension ;
1013import io .jooby .Jooby ;
2831 * <pre>{@code
2932 * {
3033 * // 1. Install a JSON engine (Prerequisite)
31- * install(new JacksonModule ());
34+ * install(new Jackson3Module ())
3235 *
33- * // 2. Install the tRPC extension
34- * install(new TrpcModule ());
36+ * // 2. JSON implementation of protocol
37+ * install(new TrpcJackson3Module ());
3538 *
36- * // 3. Register your @Trpc annotated controllers
37- * mvc (new MovieService_( ));
39+ * // 3. Install the tRPC extension
40+ * install (new TrpcModule(MovieServiceTrpc_() ));
3841 * }
3942 * }</pre>
4043 */
4144public class TrpcModule implements Extension {
4245
46+ private final String path ;
47+ private final List <TrpcService > services ;
48+
49+ /**
50+ * Creates a new instance of {@code TrpcModule} with the specified base path and tRPC services.
51+ *
52+ * @param path The base path for all tRPC routes. This is the root URL prefix where all tRPC
53+ * services will be registered. Must not be {@code null}.
54+ * @param service The primary tRPC service to register. Must not be {@code null}.
55+ * @param services Additional tRPC services to register (if any). Optional and may be omitted.
56+ */
57+ public TrpcModule (String path , TrpcService service , TrpcService ... services ) {
58+ this .path = path ;
59+ this .services = Stream .concat (Stream .of (service ), Stream .of (services )).toList ();
60+ }
61+
62+ /**
63+ * Constructs a new {@code TrpcModule} with the default base path: <code>trpc</code> and the
64+ * provided services.
65+ *
66+ * @param service The primary tRPC service to register. Must not be {@code null}.
67+ * @param services Additional tRPC services to register (if any). Optional and may be omitted.
68+ */
69+ public TrpcModule (TrpcService service , TrpcService ... services ) {
70+ this ("/trpc" , service , services );
71+ }
72+
4373 /**
4474 * Installs the tRPC extension into the Jooby application.
4575 *
@@ -59,15 +89,19 @@ public class TrpcModule implements Extension {
5989 */
6090 @ Override
6191 public void install (@ NonNull Jooby app ) throws Exception {
62- var services = app .getServices ();
92+ var registry = app .getServices ();
6393
6494 // Ensure a JSON module has provided the necessary parser
65- services .require (TrpcParser .class );
95+ registry .require (TrpcParser .class );
6696
6797 // Initialize the custom exception mapping registry
68- services .mapOf (Class .class , TrpcErrorCode .class );
98+ registry .mapOf (Class .class , TrpcErrorCode .class );
6999
70100 // Register the specialized JSON-RPC error formatter
71101 app .error (new TrpcErrorHandler ());
102+
103+ for (var service : services ) {
104+ service .install (path , app );
105+ }
72106 }
73107}
0 commit comments