grpc / grpc-web

gRPC for Web Clients
https://grpc.io
Apache License 2.0
8.57k stars 763 forks source link

repeated bytes does not have the corresponding "_asB64" method in its .d.ts file #1033

Open samueltangz opened 3 years ago

samueltangz commented 3 years ago

I am making something with grpc-web and found repeated bytes having a missing method in its generated .d.ts file. I have made a simplified .proto file for testing:

service Service {
  rpc SampleAPI (SampleRequest) returns (SampleResponse);
}

message SampleRequest {
  bytes foo = 1;
  repeated bytes bar = 2;
}

message SampleResponse {
  bytes foo = 1;
  repeated bytes bar = 2;
}

Generating the typescript & javascript files from the command suggested by the doc, i.e,:

protoc -I=$DIR echo.proto \
  --js_out=import_style=commonjs,binary:$OUT_DIR \
  --grpc-web_out=import_style=typescript,mode=grpcwebtext:$OUT_DIR

Although there is a getBarList_asB64 in the generated pb.js file, the method prototype is not available in pb.d.js.

// *_pb.js

/**
 * repeated bytes bar = 2;
 * This is a type-conversion wrapper around `getBarList()`
 * @return {!Array<string>}
 */
proto.enfs.v1.SampleRequest.prototype.getBarList_asB64 = function() {
  return /** @type {!Array<string>} */ (jspb.Message.bytesListAsB64(
      this.getBarList()));
};
// *_pb.d.ts

export class SampleRequest extends jspb.Message {
  getFoo(): Uint8Array | string;
  getFoo_asU8(): Uint8Array;
  getFoo_asB64(): string;
  setFoo(value: Uint8Array | string): SampleRequest;

  getBarList(): Array<Uint8Array | string>;
  setBarList(value: Array<Uint8Array | string>): SampleRequest;
  clearBarList(): SampleRequest;
  addBar(value: Uint8Array | string, index?: number): SampleRequest;

  // omitted
}

Here is the versions for protoc & grpc web:

Many thanks for noticing this!

samueltangz commented 3 years ago

As the current workaround, we use the following line:

jspb.Message.bytesListAsB64(res.getBarList() as Uint8Array[])
// res.getBarList_asB64()`

We see that the return type for getBarList() is either Uint8Array[] or string. Is this workaround valid (this won't work if there be a case that it is returning string)? Thanks!