Skip to content
This repository has been archived by the owner on Mar 30, 2021. It is now read-only.

Commit

Permalink
correct typings for promise return
Browse files Browse the repository at this point in the history
  • Loading branch information
mekanoe committed Aug 10, 2019
1 parent fb81404 commit 6b7b3ca
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 98 deletions.
14 changes: 4 additions & 10 deletions examples/generated/proto/examplecom/simple_service_pb_service.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

60 changes: 22 additions & 38 deletions examples/generated/proto/examplecom/simple_service_pb_service.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 2 additions & 5 deletions examples/generated/proto/orphan_pb_service.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 11 additions & 19 deletions examples/generated/proto/orphan_pb_service.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 15 additions & 26 deletions src/service/grpcweb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -324,33 +324,25 @@ function printServiceStub(methodPrinter: Printer, service: RPCDescriptor) {
function printUnaryStubMethod(printer: CodePrinter, method: RPCMethodDescriptor) {
printer
.printLn(`${method.serviceName}Client.prototype.${method.nameAsCamelCase} = function ${method.functionName}(requestMessage, metadata) {`)
.indent().printLn(`let cancelled = false;`)
.printLn(`const client = grpc.unary(${method.serviceName}.${method.nameAsPascalCase}, {`)
.indent().printLn(`request: requestMessage,`)
.printLn(`host: this.serviceHost,`)
.printLn(`metadata: metadata,`)
.printLn(`transport: this.options.transport,`)
.printLn(`debug: this.options.debug,`)
.printLn(`onEnd: function (response) {`)
.indent().printLn(`if (cancelled === false) {`)
.indent().printLn(`return new Promise((resolve, reject) => {`)
.indent().printLn(`grpc.unary(${method.serviceName}.${method.nameAsPascalCase}, {`)
.indent().printLn(`request: requestMessage,`)
.printLn(`host: this.serviceHost,`)
.printLn(`metadata: metadata,`)
.printLn(`transport: this.options.transport,`)
.printLn(`debug: this.options.debug,`)
.printLn(`onEnd: function (response) {`)
.indent().printLn(`if (response.status !== grpc.Code.OK) {`)
.indent().printLn(`var err = new Error(response.statusMessage);`)
.printLn(`err.code = response.status;`)
.printLn(`err.metadata = response.trailers;`)
.printLn(`Promise.reject(err);`)
.printLn(`err.code = response.status;`)
.printLn(`err.metadata = response.trailers;`)
.printLn(`reject(err);`)
.dedent().printLn(`} else {`)
.indent().printLn(`Promise.resolve(response.message);`)
.indent().printLn(`resolve(response.message);`)
.dedent().printLn(`}`)
.dedent().printLn(`}`)
.dedent().printLn(`}`)
.dedent().printLn(`});`)
.dedent().printLn(`});`)
.printLn(`return {`)
.indent().printLn(`cancel: function () {`)
.indent().printLn(`cancelled = true;`)
.printLn(`Promise.resolve(null);`)
.printLn(`client.close();`)
.dedent().printLn(`}`)
.dedent().printLn(`};`)
.dedent().printLn(`};`);
}

Expand Down Expand Up @@ -515,11 +507,8 @@ function printUnaryStubMethodTypes(printer: CodePrinter, method: RPCMethodDescri
printer
.printLn(`${method.nameAsCamelCase}(`)
.indent().printLn(`requestMessage: ${method.requestType},`)
.printLn(`metadata: grpc.Metadata,`)
.dedent().printLn(`): Promise<UnaryResponse | null>;`)
.printLn(`${method.nameAsCamelCase}(`)
.indent().printLn(`requestMessage: ${method.requestType},`)
.dedent().printLn(`): Promise<UnaryResponse | null>;`);
.printLn(`metadata?: grpc.Metadata,`)
.dedent().printLn(`): Promise<${method.responseType}>;`);
}

function printServerStreamStubMethodTypes(printer: CodePrinter, method: RPCMethodDescriptor) {
Expand Down

0 comments on commit 6b7b3ca

Please sign in to comment.