From 7ec3b9f7b369a608ad17666ccc9a13eb8d355b60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20M=C3=B6ller?= Date: Thu, 11 Nov 2021 12:44:14 +0100 Subject: [PATCH 1/6] Add strongly types to MethodDescriptor constructor In the generated typescript code this will ensure that the generic parameters will be infered from the values passed to the constructor --- packages/grpc-web/index.d.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/grpc-web/index.d.ts b/packages/grpc-web/index.d.ts index 4b537ce8..ed446f76 100644 --- a/packages/grpc-web/index.d.ts +++ b/packages/grpc-web/index.d.ts @@ -70,11 +70,11 @@ declare module "grpc-web" { export class MethodDescriptor { constructor(name: string, - methodType: any, - requestType: any, - responseType: any, - requestSerializeFn: any, - responseDeserializeFn: any); + methodType: string, + requestType: new (...args: unknown[]) => REQ, + responseType: new (...args: unknown[]) => RESP, + requestSerializeFn: (request: REQ) => Uint8Array, + responseDeserializeFn: (bytes: Uint8Array) => RESP); createRequest(requestMessage: REQ, metadata?: Metadata, callOptions?: CallOptions): Request; From 7b149dd3e5aa3559e4d36464b91b9ac78b94d5f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20M=C3=B6ller?= Date: Thu, 11 Nov 2021 14:31:41 +0100 Subject: [PATCH 2/6] Replace method return types of MethodDescriptor --- packages/grpc-web/index.d.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/grpc-web/index.d.ts b/packages/grpc-web/index.d.ts index ed446f76..064a7270 100644 --- a/packages/grpc-web/index.d.ts +++ b/packages/grpc-web/index.d.ts @@ -83,10 +83,10 @@ declare module "grpc-web" { status?: Status): UnaryResponse; getName(): string; getMethodType(): string; - getResponseMessageCtor(): any; - getRequestMessageCtor(): any; - getResponseDeserializeFn(): any; - getRequestSerializeFn(): any; + getRequestMessageCtor(): new (...args: unknown[]) => REQ; + getResponseMessageCtor(): new (...args: unknown[]) => RESP; + getRequestSerializeFn(): (request: REQ) => Uint8Array; + getResponseDeserializeFn(): (bytes: Uint8Array) => RESP; } export class Request { From 177aebc70f9056dac4f71a8f4e76131a510b224b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20M=C3=B6ller?= Date: Fri, 19 Nov 2021 10:56:29 +0100 Subject: [PATCH 3/6] Generate type annotations in ts mode - Adds attribute type declaratiosn to method descriptor - Adds return type declaration to server streaming call --- javascript/net/grpc/web/generator/grpc_generator.cc | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/javascript/net/grpc/web/generator/grpc_generator.cc b/javascript/net/grpc/web/generator/grpc_generator.cc index 1ea58dc2..a73d5d32 100644 --- a/javascript/net/grpc/web/generator/grpc_generator.cc +++ b/javascript/net/grpc/web/generator/grpc_generator.cc @@ -600,7 +600,8 @@ void PrintTypescriptFile(Printer* printer, const FileDescriptor* file, : "grpcWeb.MethodType.UNARY"; if (!method->client_streaming()) { printer->Print(vars, - "methodDescriptor$method_name$ = " + "methodDescriptor$method_name$: " + "grpcWeb.MethodDescriptor<$input_type$, $output_type$> = " "new grpcWeb.MethodDescriptor(\n"); printer->Indent(); printer->Print(vars, @@ -619,7 +620,8 @@ void PrintTypescriptFile(Printer* printer, const FileDescriptor* file, printer->Indent(); printer->Print(vars, "request: $input_type$,\n" - "metadata?: grpcWeb.Metadata) {\n"); + "metadata?: grpcWeb.Metadata): " + "grpcWeb.ClientReadableStream<$output_type$> {\n"); printer->Print(vars, "return this.client_.serverStreaming(\n"); printer->Indent(); printer->Print(vars, From c20a5f5f985fb18f402ccd51217fe8ec469eee08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20M=C3=B6ller?= Date: Fri, 19 Nov 2021 10:57:48 +0100 Subject: [PATCH 4/6] Revert serialize / deserialize fn ts type to any --- packages/grpc-web/index.d.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/grpc-web/index.d.ts b/packages/grpc-web/index.d.ts index 064a7270..1860cab8 100644 --- a/packages/grpc-web/index.d.ts +++ b/packages/grpc-web/index.d.ts @@ -71,8 +71,8 @@ declare module "grpc-web" { export class MethodDescriptor { constructor(name: string, methodType: string, - requestType: new (...args: unknown[]) => REQ, - responseType: new (...args: unknown[]) => RESP, + requestType: any, + responseType: any, requestSerializeFn: (request: REQ) => Uint8Array, responseDeserializeFn: (bytes: Uint8Array) => RESP); createRequest(requestMessage: REQ, @@ -85,8 +85,8 @@ declare module "grpc-web" { getMethodType(): string; getRequestMessageCtor(): new (...args: unknown[]) => REQ; getResponseMessageCtor(): new (...args: unknown[]) => RESP; - getRequestSerializeFn(): (request: REQ) => Uint8Array; - getResponseDeserializeFn(): (bytes: Uint8Array) => RESP; + getRequestSerializeFn(): any; + getResponseDeserializeFn(): any; } export class Request { From f063db00af8bc16ea7b71f81001a75377d3ecd72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20M=C3=B6ller?= Date: Fri, 19 Nov 2021 11:02:28 +0100 Subject: [PATCH 5/6] add req / resp type, revert serialize / deserialize changes --- packages/grpc-web/index.d.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/grpc-web/index.d.ts b/packages/grpc-web/index.d.ts index 1860cab8..7b22b91d 100644 --- a/packages/grpc-web/index.d.ts +++ b/packages/grpc-web/index.d.ts @@ -71,10 +71,10 @@ declare module "grpc-web" { export class MethodDescriptor { constructor(name: string, methodType: string, - requestType: any, - responseType: any, - requestSerializeFn: (request: REQ) => Uint8Array, - responseDeserializeFn: (bytes: Uint8Array) => RESP); + requestType: new (...args: unknown[]) => REQ, + responseType: new (...args: unknown[]) => RESP, + requestSerializeFn: any, + responseDeserializeFn: any); createRequest(requestMessage: REQ, metadata?: Metadata, callOptions?: CallOptions): Request; From 358f188b8186a4ff8db48b1b646b6e5ab285025a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20M=C3=B6ller?= Date: Fri, 19 Nov 2021 23:11:59 +0100 Subject: [PATCH 6/6] Revert method descriptor type annotation change --- javascript/net/grpc/web/generator/grpc_generator.cc | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/javascript/net/grpc/web/generator/grpc_generator.cc b/javascript/net/grpc/web/generator/grpc_generator.cc index a73d5d32..15e9c66b 100644 --- a/javascript/net/grpc/web/generator/grpc_generator.cc +++ b/javascript/net/grpc/web/generator/grpc_generator.cc @@ -600,8 +600,7 @@ void PrintTypescriptFile(Printer* printer, const FileDescriptor* file, : "grpcWeb.MethodType.UNARY"; if (!method->client_streaming()) { printer->Print(vars, - "methodDescriptor$method_name$: " - "grpcWeb.MethodDescriptor<$input_type$, $output_type$> = " + "methodDescriptor$method_name$ = " "new grpcWeb.MethodDescriptor(\n"); printer->Indent(); printer->Print(vars,