connectrpc / connect-es

The TypeScript implementation of Connect: Protobuf RPC that works.
https://connectrpc.com/
Apache License 2.0
1.36k stars 78 forks source link

Models not exported on Connect package #1145

Closed lynicis closed 3 months ago

lynicis commented 3 months ago

Is your feature request related to a problem? Please describe. I want to register a single RPC method without ServiceType but I can't. I searched the whole project, then I realized that the method doesn't work because the method wants some models but they are not exported.

Does this method work or not? If it works, why are these models not exported?

Describe the solution you'd like You should export these models;

Describe alternatives you've considered You should delete this method in interface

Additional context

timostamm commented 3 months ago

Hey Emre, you are looking for MethodImpl from @connectrpc/connect.

See the documentation and examples here.

lynicis commented 3 months ago

Hey Emre, you are looking for MethodImpl from @connectrpc/connect.

See the documentation and examples here.

Sorry Timo but I didn't get an answer,

What does this method do? Or what is the purpose of this method? also it isn't documented

rpc<M extends MethodInfo>(service: ServiceType, method: M, impl: MethodImpl<M>, options?: Partial<UniversalHandlerOptions>): this;
timostamm commented 3 months ago

In Protobuf, remote procedures (RPC) are always defined as part of a service:

service ElizaService {
  rpc Say(SayRequest) returns (SayResponse) {}
}

You typically implement the service with router.service:

import { ConnectRouter, HandlerContext } from "@connectrpc/connect";
import { ElizaService } from "./gen/eliza_connect";
import { SayRequest, SayResponse } from "./gen/eliza_pb";

export default (router: ConnectRouter) =>
  router.service(ElizaService, {
    async say(req: SayRequest, context: HandlerContext) {
      return new SayResponse({
        sentence: `You said ${req.sentence}`,
      });
    }
  });

You can register single procedures with router.rpc:

export const say: MethodImpl<typeof ElizaService.methods.say> = ...

...

// using const say
router.service(ElizaService, { say });

// alternative for using const say
router.rpc(
  ElizaService,
  ElizaService.methods.say,
  say
);

The method registers just this RPC.

So if you need to register RPCs one-by-one, for example because you wrap methods, you can use this method to avoid the unimplemented fallback, see https://github.com/connectrpc/connectrpc.com/pull/186 and https://github.com/connectrpc/connect-es/pull/1146. This should only be necessary very rare circumstances.

We recommend to use router.service and to keep the code simple and straight-forward for ease of maintenance.

lynicis commented 3 months ago

I just wanted to get this info, because I saw this method in the interface, I thought I could use it like the chaining method, but it's not what I thought :)

Thanks Timo...