Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add interceptor capabilities to ConnectRouter #527

Closed
fubhy opened this issue Mar 13, 2023 · 2 comments · Fixed by #982
Closed

Add interceptor capabilities to ConnectRouter #527

fubhy opened this issue Mar 13, 2023 · 2 comments · Fixed by #982
Labels
enhancement New feature or request

Comments

@fubhy
Copy link
Contributor

fubhy commented Mar 13, 2023

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?

@timostamm
Copy link
Member

timostamm commented Sep 6, 2023

Related to augmenting the HandlerContext, the following feature requests should be considered in the design:

Ideally, we really want context properties to be type-safe 🙂

Since the context is part of service implementation signatures, it's also important that it's possible to stub or mock them with createHandlerContext() with a user-friendly API.

@srikrsna-buf
Copy link
Member

You can now try out this feature using the next release of connect: npm i @connectrpc/connect@next. The docs for the next release are hosted here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
3 participants