Add interceptor capabilities to ConnectRouter

This issue has been tracked since 2023-03-13.

Is your feature request related to a problem? Please describe.
It's currently not easily possible to implement generic middlewares for service methods. This makes it rather tedious to enhance connect services with generic logic across multiple methods.

Describe the solution you'd like
I'd like to be able to implement interceptors / middlewares for my service methods or groups of service methods (Connect for Node.js). The recently introduced ConnectRouter appears to be a good candidate to register these.

These interceptors should have the ability to augment the HandlerContext. Ideally, this should be done with TypeScript in mind ... That said, I'm not a big fan of blanket type augmentation as used in e.g. Express, etc. for this problem. Maybe there's a better way.

Describe alternatives you've considered
We are currently "intercepting" requests inconveniently by wrapping all service methods with higher order functions. To keep things simple and because that's the extent of our requirements currently, we've only implemented this pattern for unary requests. So this is only a temporary workaround at the moment.

Additional context
Implementing interceptors would allow the development of generic, community provided interceptor modules. E.g. for integrations with OpenTelemetry (metrics & tracing): #523.

Other possible use cases:

  • Automatic request (& response) validation (protoc-gen-validate)
  • AuthN & AuthZ
  • Rate limiting
  • Caching

These could all be nicely paired with proto options based code generation.

The ConnectRouter is currently only handling POST requests. That means that, in its current form, it wouldn't be the right place to also handle things like OPTIONS requests (CORS). I don't know whether (and how) "fixing" that is within the scope of this issue. However, I can definitely see value in being able to handle CORS at service method granularity... So maybe there's room for that here somehow?

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