【问题标题】:Binding interceptor in grpc-web Typescriptgrpc-web Typescript中的绑定拦截器
【发布时间】:2020-08-14 10:03:11
【问题描述】:

我正在尝试按照本教程https://grpc.io/blog/grpc-web-interceptor/ 为我的 grpc-web 客户端请求添加拦截器。使用解释如何将拦截器绑定到调用的最后一行,在 typescript 中使用该调用我首先得到一个错误,因为 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());
    });
}

}

【问题讨论】:

    标签: reactjs typescript grpc-web


    【解决方案1】:

    事实证明,这是 grpc-web 直到版本 1.2.0

    中的一个特定问题

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

    grpc-web版本1.2.1及以后,问题已修复

    【讨论】:

    • 现在应该发布了
    • @dmaixner 谢谢,我更新了我的答案和我的节点模块
    猜你喜欢
    • 1970-01-01
    • 2021-05-11
    • 1970-01-01
    • 1970-01-01
    • 2023-01-22
    • 1970-01-01
    • 1970-01-01
    • 2020-10-23
    • 1970-01-01
    相关资源
    最近更新 更多