Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[dts] Generate PromiseClient type definitions in d.ts file #354

Merged
merged 1 commit into from
Nov 7, 2018

Conversation

at-ishikawa
Copy link
Contributor

@at-ishikawa at-ishikawa commented Nov 7, 2018

Issue

In the _grpc_web_pb.js file, PromiseClient is defined and some of its methods return the promise, instead of ClientReadableStream.
But there is no type definition for PromiseClient in d.ts file, so we can't use this class without warnings of type checking.

Proposal

Generate PromiseClient type definitions in d.ts file on top of the existing Client definition.
The methods of this PromiseClient have Promise return type only if the RPC method is not client streaming nor server streaming RPC.

Note that I did not change signature for the method which returns server streaming, because the generated js file does not return Promise, but not sure this is correct.
Please tell me if I need to change something.

Sample

The generated d.ts file of net/grpc/gateway/examples/helloworld/helloworld.proto is like following.

$ cat helloworld_grpc_web_pb.d.ts
import * as grpcWeb from 'grpc-web';
import {
  HelloReply,
  HelloRequest,
  RepeatHelloRequest} from './helloworld_pb';

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

  sayHello(
    request: HelloRequest,
    metadata: grpcWeb.Metadata,
    callback: (err: grpcWeb.Error,
               response: HelloReply) => void
  ): grpcWeb.ClientReadableStream<HelloReply>;

  sayRepeatHello(
    request: RepeatHelloRequest,
    metadata: grpcWeb.Metadata
  ): grpcWeb.ClientReadableStream<HelloReply>;

  sayHelloAfterDelay(
    request: HelloRequest,
    metadata: grpcWeb.Metadata,
    callback: (err: grpcWeb.Error,
               response: HelloReply) => void
  ): grpcWeb.ClientReadableStream<HelloReply>;

}

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

  sayHello(
    request: HelloRequest,
    metadata: grpcWeb.Metadata
  ): Promise<HelloReply>;

  sayRepeatHello(
    request: RepeatHelloRequest,
    metadata: grpcWeb.Metadata
  ): grpcWeb.ClientReadableStream<HelloReply>;

  sayHelloAfterDelay(
    request: HelloRequest,
    metadata: grpcWeb.Metadata
  ): Promise<HelloReply>;

}

Copy link
Collaborator

@stanley-cheung stanley-cheung left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your contributions!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants