Skip to content

Commit

Permalink
Generate Promise based overloads for unary calls in Typescript
Browse files Browse the repository at this point in the history
Fixes #580
  • Loading branch information
Orphis authored and stanley-cheung committed Mar 19, 2020
1 parent 410ca10 commit 7f9f7e8
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
28 changes: 28 additions & 0 deletions javascript/net/grpc/web/grpc_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -734,13 +734,31 @@ void PrintTypescriptFile(Printer* printer, const FileDescriptor* file,
printer->Outdent();
printer->Print("}\n\n");
} else {
printer->Print(vars, "$js_method_name$(\n");
printer->Indent();
printer->Print(vars,
"request: $input_type$,\n"
"metadata: grpcWeb.Metadata | null): Promise<$output_type$>;\n\n");
printer->Outdent();

printer->Print(vars, "$js_method_name$(\n");
printer->Indent();
printer->Print(vars,
"request: $input_type$,\n"
"metadata: grpcWeb.Metadata | null,\n"
"callback: (err: grpcWeb.Error,\n"
" response: $output_type$) => void): grpcWeb.ClientReadableStream<$output_type$>;\n\n");
printer->Outdent();

printer->Print(vars, "$js_method_name$(\n");
printer->Indent();
printer->Print(vars,
"request: $input_type$,\n"
"metadata: grpcWeb.Metadata | null,\n"
"callback?: (err: grpcWeb.Error,\n"
" response: $output_type$) => void) {\n");
printer->Print(vars, "if (callback !== undefined) {\n");
printer->Indent();
printer->Print(vars, "return this.client_.rpcCall(\n");
printer->Indent();
printer->Print(vars,
Expand All @@ -752,6 +770,16 @@ void PrintTypescriptFile(Printer* printer, const FileDescriptor* file,
"callback);\n");
printer->Outdent();
printer->Outdent();
printer->Print(vars,
"}\n"
"return this.client_.unaryCall(\n");
printer->Print(vars,
"this.hostname_ +\n"
" '/$package_dot$$service_name$/$method_name$',\n"
"request,\n"
"metadata || {},\n"
"this.methodInfo$method_name$);\n");
printer->Outdent();
printer->Print("}\n\n");
}
}
Expand Down
6 changes: 6 additions & 0 deletions packages/grpc-web/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ declare module "grpc-web" {
}

export class AbstractClientBase {
unaryCall<Request, Response> (method: string,
request: Request,
metadata: Metadata,
methodInfo: AbstractClientBase.MethodInfo<Request, Response>
): Promise<Response>;

rpcCall<Request, Response> (method: string,
request: Request,
metadata: Metadata,
Expand Down

0 comments on commit 7f9f7e8

Please sign in to comment.