grpc / grpc-node

gRPC for Node.js
https://grpc.io
Apache License 2.0
4.43k stars 640 forks source link

How to use InterceptorProvider for GRPC clients #2810

Closed rathsai closed 2 weeks ago

rathsai commented 1 month ago

Is your feature request related to a problem? Please describe.

Hi, I have implemented a retry interceptor as per documentation https://github.com/grpc/proposal/blob/master/L5-node-client-interceptors.md and wanted to use InterceptorProvider. However, when I try to use the example provided in the doc, it doesn't work and has import errors. Next, was also able to see that method_descriptor.method_type and MethodType.UNARY is not present in the library as well.

var interceptor_providers = [
    new InterceptorProvider(function(method_descriptor) {
        if (method_descriptor.method_type === MethodType.UNARY) {
            return unary_interceptor;
        }
    }),
    new InterceptorProvider(function(method_descriptor) {
        if (method_descriptor.method_type === MethodType.SERVER_STREAMING) {
            return streaming_interceptor;
        }
    })
];
var constructor_options = {
    interceptor_providers: interceptor_providers
};
var client = new InterceptingClient('localhost:8080', credentials, constructor_options);

Describe the solution you'd like

murgatroid99 commented 2 weeks ago

The missing method type is a good point. That should be added. Until that happens, you can use method_descriptor.requestStream and method_descriptor.responseStream in combination to determine the method type.

I don't believe an undefined return type is necessary for the InterceptorProvider type. In a situation where you don't need an interceptor that does anything, you can just return an interceptor that passes through the call unchanged:

function passthroughInterceptor(options, nextCall) {
  return nextCall(options);
}