Skip to content

UnaryInterceptor is not being called #1342

Description

@usertest123test

In My reactjs web app I am trying to use interceptors on grpc but the interceptor is not being called.

I tried some of the solutions provided around but could get it resolved. please someone assist me on this.

project details:
Package:
"react": "^18.2.0",
"grpc-web": "^1.3.1",

Below is my code:
Interceptor:

export const SimpleUnaryInterceptor = function () {};
SimpleUnaryInterceptor.prototype.intercept = function (request: any, invoker: any) {
  console.log("[Intercepting request]");
  return invoker(request).then((response: any) => {
    console.log("[Intercepting response]");
    return response;
  });
};

Biding interceptor:

export default class Api {
  myServicePromiseClient: MyServicePromiseClient;
  metadata: Metadata;

  constructor(public serviceURL: string) {
    this.myServicePromiseClient = new MyServicePromiseClient(serviceURL, null, { unaryInterceptors: [SimpleUnaryInterceptor] });
  }

  public user = new (class {
    constructor(public superThis: Api) {}
    public getMyUser = async (userId: string): Promise<UserObj> =>
      getMyUser( userId, this.superThis.myServicePromiseClient, this.superThis.metadata);
  })(this);
}

MyServicePromiseClient:

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

and getMyUser grpc method is:

getMyUser(
   request: user_pb.GetMyUserRequest,
   metadata?: grpcWeb.Metadata
 ): Promise<user_pb.MyUserResponse>;

Calling grpc-api:

const response = await new Api(MyServerUrl).user.getMyUser(123)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions