agreatfool / grpc_tools_node_protoc_ts

Generate TypeScript d.ts definitions for generated js files from grpc_tools_node_protoc
MIT License
498 stars 57 forks source link

Support @grpc/proto-loader serialization and promises #35

Open tatemz opened 5 years ago

tatemz commented 5 years ago

The @grpc/proto-loader package allows for on-demand loading of .proto files in order to dynamically create a node server or client. There is a pretty good example here:

https://github.com/grpc/grpc/tree/v1.17.1/examples/node/dynamic_codegen

A few things are different about the way proto-loader generates the method definitions. Instead of using request/response objects with getters and setters, the method definitions use plain objects. The typings generated usually include an AsObject version of the request/response objects, however, the client definitions generated by your plugin do not match the client implementation created by proto-loader

The following example is the definitions generated by your plugin:

export interface IGreeterClient {
    greet(request: greeter_pb.GreetingRequest, callback: (error: grpc.ServiceError | null, response: greeter_pb.GreetingResponse) => void): grpc.ClientUnaryCall;
    greet(request: greeter_pb.GreetingRequest, metadata: grpc.Metadata, callback: (error: grpc.ServiceError | null, response: greeter_pb.GreetingResponse) => void): grpc.ClientUnaryCall;
    greet(request: greeter_pb.GreetingRequest, metadata: grpc.Metadata, options: Partial<grpc.CallOptions>, callback: (error: grpc.ServiceError | null, response: greeter_pb.GreetingResponse) => void): grpc.ClientUnaryCall;
}

export class GreeterClient extends grpc.Client implements IGreeterClient {
    constructor(address: string, credentials: grpc.ChannelCredentials, options?: object);
    public greet(request: greeter_pb.GreetingRequest, callback: (error: grpc.ServiceError | null, response: greeter_pb.GreetingResponse) => void): grpc.ClientUnaryCall;
    public greet(request: greeter_pb.GreetingRequest, metadata: grpc.Metadata, callback: (error: grpc.ServiceError | null, response: greeter_pb.GreetingResponse) => void): grpc.ClientUnaryCall;
    public greet(request: greeter_pb.GreetingRequest, metadata: grpc.Metadata, options: Partial<grpc.CallOptions>, callback: (error: grpc.ServiceError | null, response: greeter_pb.GreetingResponse) => void): grpc.ClientUnaryCall;
}

The following is what it should look like if one was using proto-loader:


export interface IGreeterClient {
    greet(request: greeter_pb.GreetingRequest.AsObject, callback: (error: grpc.ServiceError | null, response: greeter_pb.GreetingResponse.AsObject) => void): grpc.ClientUnaryCall;
    greet(request: greeter_pb.GreetingRequest.AsObject, metadata: grpc.Metadata, callback: (error: grpc.ServiceError | null, response: greeter_pb.GreetingResponse.AsObject) => void): grpc.ClientUnaryCall;
    greet(request: greeter_pb.GreetingRequest.AsObject, metadata: grpc.Metadata, options: Partial<grpc.CallOptions>, callback: (error: grpc.ServiceError | null, response: greeter_pb.GreetingResponse.AsObject) => void): grpc.ClientUnaryCall;
}

export class GreeterClient extends grpc.Client implements IGreeterClient {
    constructor(address: string, credentials: grpc.ChannelCredentials, options?: object);
    public greet(request: greeter_pb.GreetingRequest.AsObject, callback: (error: grpc.ServiceError | null, response: greeter_pb.GreetingResponse.AsObject) => void): grpc.ClientUnaryCall;
    public greet(request: greeter_pb.GreetingRequest.AsObject, metadata: grpc.Metadata, callback: (error: grpc.ServiceError | null, response: greeter_pb.GreetingResponse.AsObject) => void): grpc.ClientUnaryCall;
    public greet(request: greeter_pb.GreetingRequest.AsObject, metadata: grpc.Metadata, options: Partial<grpc.CallOptions>, callback: (error: grpc.ServiceError | null, response: greeter_pb.GreetingResponse.AsObject) => void): grpc.ClientUnaryCall;
}

Also not included in my above example is the support for Promises. In general your examples probably need to be updated to match the vanilla JS ones provided in the sample link above.

agreatfool commented 5 years ago

Hi @tatemz,

Thank you for your information. I just knew this trend in your issue.

And I have several questions:

tatemz commented 5 years ago

The official grpc-node project provides the proto-loader package. https://github.com/grpc/grpc-node/tree/master/packages/proto-loader

tatemz commented 5 years ago

proto-loader uses protobuf.js behind the scenes and there is a lot of documentation there with further details

tatemz commented 5 years ago

On further investigation, it seems as if protobuf.js supplies Typescript definitions