我正在尝试遵循本教程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, creds, {'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());
});
}
}
答案 0 :(得分:1)
事实证明,这是grpc-web的特定问题,直到版本 1.2.0
特定版本的解决方法是使用@ ts-ignore,拦截器将起作用
从grpc-web版本 1.2.1 及更高版本开始,此问题已解决