grpc-web Typescript中的绑定拦截器

问题描述

我正在尝试遵循本教程https://grpc.io/blog/grpc-web-interceptor/来为我的grpc-web客户请求添加拦截器。最后使用解释如何将拦截器绑定到调用的最后一行,在打字稿中使用该调用,我首先得到一个错误,因为protoc-gen编译器在构造函数中使用以下参数创建了客户端服务

constructor (hostname: string,credentials?: null | { [index: string]: string; },options?: null | { [index: string]: string; })

所以我不能真正使用本教程中解释的fromat

const promiseClient = new MyServicePromiseClient(
    host,creds,{'unaryInterceptors': [interceptor1,interceptor2,interceptor3]});

我尝试使用类似的格式

const promiseClient = new MyServicePromiseClient(
    host,{'unaryInterceptors': 'interceptor1'});

但是它不起作用,我的拦截器中有断点,但它们从未达到该点。 如何将拦截器绑定到呼叫?

编辑

我的拦截代码并不花哨,只是教程中的代码,有些转换为Typescript,但在这里

export class interceptor1 {

intercept = function (request: any,invoker: any) {
    // Update the request message before the RPC.
    const reqMsg = request.getRequestMessage();
    reqMsg.setMessage('[Intercept request]' + reqMsg.getMessage());
    // After the RPC returns successfully,update the response.
    return invoker(request).then((response: any) => {
        // You can also do something with response Metadata here.
        console.log(response.getMetadata());
    });
}

}

解决方法

事实证明,这是grpc-web的特定问题,直到版本 1.2.0

特定版本的解决方法是使用@ ts-ignore,拦截器将起作用

从grpc-web版本 1.2.1 及更高版本开始,此问题已解决

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...