Allow routes to be registered in multiple files for `connectNodeAdapter`

This issue has been tracked since 2023-02-26.

Is your feature request related to a problem? Please describe.
When using connect-node, the current implementation of the routes option for connectNodeAdapter only accepts a function, which is a bit limiting when trying to register services and RPCs that are in multiple files.

Describe the solution you'd like
Have an object that can imported into multiple files, register the service/RPC, and then be passed to the adapter.

Describe alternatives you've considered
Have everything in a single file, which is not ideal.

fubhy wrote this answer on 2023-02-28

Hi Shane,

You can already have multiple files that each expose a function that adds one (or multiple) rpc methods each to the ConnectRouter instance. E.g. like so:

// methodA.ts

export function addMethodA(router: ConnectRouter) {
  router.rpc(...);
}
// methodB.ts

export function addMethodB(router: ConnectRouter) {
  router.rpc(...);
}
// index.ts

import { addMethodA } from './methodA.js';
import { addMethodB } from './methodB.js';

export function routes(router: ConnectRouter) {
  addMethodA(router);
  addMethodB(router);
}
timostamm wrote this answer on 2023-02-28

@killbasa, you can split up your code with @fubhy's suggestion, or you can make use of the helper types, which let you do exactly what you are asking for.

You can think of ServiceImpl as a generic interface for services that you can use however you prefer. For example:

export class Eliza implements ServiceImpl<typeof ElizaService> {
  async say(req: SayRequest) {
    return {
      sentence: `You said ${req.sentence}`,
    };
  }
}

Using a connect.ts is a recommendation, but not a requirement. With every server plugin we provide, it's also possible to give the registration function in place, for example:

await server.register(fastifyConnectPlugin, {
  routes(router) {
    router.service(ElizaService, new Eliza());
  },
});

We've just expanded the examples a bit. I think this should be exactly what you need, so I'm closing this issue, but please feel free to reopen if I missed something.

killbasa wrote this answer on 2023-02-28

Exactly what I needed. Thank you!

unicomp23 wrote this answer on 2023-03-08

Me too, thanks @timostamm !

unicomp23 wrote this answer on 2023-03-08

Have the updated docs been deployed to the website?

More Details About Repo
Owner Name bufbuild
Repo Name connect-es
Full Name bufbuild/connect-es
Language TypeScript
Created Date 2022-02-16
Updated Date 2023-03-24
Star Count 852
Watcher Count 20
Fork Count 33
Issue Count 17

YOU MAY BE INTERESTED

Issue Title Created Date Updated Date