grpc/grpc-node

How to use InterceptorProvider for GRPC clients

rathsai opened this issue · 1 comments

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

  • Can the team provide a path on how to use interceptor_providers ?
  • Support for MethodType for a grpc call
  • Change InterceptorProvider type from (methodDefinition: ClientMethodDefinition<any, any>): Interceptor to (methodDefinition: ClientMethodDefinition<any, any>): Interceptor | undefined since it is a provider and can evaluate to a condition where interceptors shouldn't be added.

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);
}