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

Generate Promise based overloads for unary calls in Typescript #746

Merged
merged 2 commits into from
Mar 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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