From ee3e7b6688d8a8757a9815aeec47a84fcdbe7a80 Mon Sep 17 00:00:00 2001 From: Sri Krishna Paritala Date: Thu, 3 Oct 2024 11:03:03 -0400 Subject: [PATCH 1/3] Add typed `DescMethod` variants Signed-off-by: Sri Krishna Paritala --- packages/protobuf/src/types.ts | 68 +++++++++++++++++++++++++++++++++- 1 file changed, 67 insertions(+), 1 deletion(-) diff --git a/packages/protobuf/src/types.ts b/packages/protobuf/src/types.ts index a05183f6a..4570fb6bd 100644 --- a/packages/protobuf/src/types.ts +++ b/packages/protobuf/src/types.ts @@ -13,7 +13,12 @@ // limitations under the License. import type { GenEnum, GenExtension, GenMessage } from "./codegenv1/types.js"; -import type { DescEnum, DescExtension, DescMessage } from "./descriptors.js"; +import type { + DescEnum, + DescExtension, + DescMessage, + DescMethod, +} from "./descriptors.js"; import type { OneofADT } from "./reflect/guard.js"; import type { WireType } from "./wire/index.js"; import type { JsonValue } from "./json-value.js"; @@ -89,6 +94,65 @@ export type UnknownField = { readonly data: Uint8Array; }; +/** + * Describes a streaming RPC declaration. + */ +export type DescMethodStreaming< + I extends DescMessage = DescMessage, + O extends DescMessage = DescMessage, +> = + | DescMethodClientStreaming + | DescMethodServerStreaming + | DescMethodBiDiStreaming; + +/** + * Describes a unary RPC declaration. + */ +export type DescMethodUnary< + I extends DescMessage = DescMessage, + O extends DescMessage = DescMessage, +> = DescMethodCommon & { + methodKind: "unary"; + input: I; + output: O; +}; + +/** + * Describes a server streaming RPC declaration. + */ +export type DescMethodServerStreaming< + I extends DescMessage = DescMessage, + O extends DescMessage = DescMessage, +> = DescMethodCommon & { + methodKind: "server_streaming"; + input: I; + output: O; +}; + +/** + * Describes a client streaming RPC declaration. + */ +export type DescMethodClientStreaming< + I extends DescMessage = DescMessage, + O extends DescMessage = DescMessage, +> = DescMethodCommon & { + methodKind: "client_streaming"; + input: I; + output: O; +}; + +/** + * Describes a bidi streaming RPC declaration. + */ +export type DescMethodBiDiStreaming< + I extends DescMessage = DescMessage, + O extends DescMessage = DescMessage, +> = DescMethodCommon & { + methodKind: "bidi_streaming"; + input: I; + output: O; +}; + /** * The init type for a message, which makes all fields optional. * The init type is accepted by the function create(). @@ -119,3 +183,5 @@ type OneofSelectedMessage = { case: K; value: M; }; + +type DescMethodCommon = Omit; From 52587cca35825e3171d7bbf0478dc64172969f32 Mon Sep 17 00:00:00 2001 From: Sri Krishna Paritala Date: Tue, 8 Oct 2024 09:56:23 -0400 Subject: [PATCH 2/3] Improve types Signed-off-by: Sri Krishna Paritala --- packages/protobuf/src/types.ts | 39 ++++++++++++++++------------------ 1 file changed, 18 insertions(+), 21 deletions(-) diff --git a/packages/protobuf/src/types.ts b/packages/protobuf/src/types.ts index 4570fb6bd..a0dce4f30 100644 --- a/packages/protobuf/src/types.ts +++ b/packages/protobuf/src/types.ts @@ -111,11 +111,7 @@ export type DescMethodStreaming< export type DescMethodUnary< I extends DescMessage = DescMessage, O extends DescMessage = DescMessage, -> = DescMethodCommon & { - methodKind: "unary"; - input: I; - output: O; -}; +> = DescMethodTyped<"unary", I, O>; /** * Describes a server streaming RPC declaration. @@ -123,11 +119,7 @@ export type DescMethodUnary< export type DescMethodServerStreaming< I extends DescMessage = DescMessage, O extends DescMessage = DescMessage, -> = DescMethodCommon & { - methodKind: "server_streaming"; - input: I; - output: O; -}; +> = DescMethodTyped<"server_streaming", I, O>; /** * Describes a client streaming RPC declaration. @@ -135,11 +127,7 @@ export type DescMethodServerStreaming< export type DescMethodClientStreaming< I extends DescMessage = DescMessage, O extends DescMessage = DescMessage, -> = DescMethodCommon & { - methodKind: "client_streaming"; - input: I; - output: O; -}; +> = DescMethodTyped<"client_streaming", I, O>; /** * Describes a bidi streaming RPC declaration. @@ -147,11 +135,7 @@ export type DescMethodClientStreaming< export type DescMethodBiDiStreaming< I extends DescMessage = DescMessage, O extends DescMessage = DescMessage, -> = DescMethodCommon & { - methodKind: "bidi_streaming"; - input: I; - output: O; -}; +> = DescMethodTyped<"bidi_streaming", I, O>; /** * The init type for a message, which makes all fields optional. @@ -184,4 +168,17 @@ type OneofSelectedMessage = { value: M; }; -type DescMethodCommon = Omit; +type DescMethodTyped = Omit & { + /** + * One of the four available method types. + */ + readonly methodKind: K; + /** + * The message type for requests. + */ + readonly input: I; + /** + * The message type for responses. + */ + readonly output: O; +} \ No newline at end of file From 44cfba7a2a5f19710ce23ade19f861f178ad4eb5 Mon Sep 17 00:00:00 2001 From: Sri Krishna Paritala Date: Tue, 8 Oct 2024 09:58:59 -0400 Subject: [PATCH 3/3] format Signed-off-by: Sri Krishna Paritala --- packages/protobuf/src/types.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/protobuf/src/types.ts b/packages/protobuf/src/types.ts index a0dce4f30..466a7af7a 100644 --- a/packages/protobuf/src/types.ts +++ b/packages/protobuf/src/types.ts @@ -168,7 +168,11 @@ type OneofSelectedMessage = { value: M; }; -type DescMethodTyped = Omit & { +type DescMethodTyped< + K extends DescMethod["methodKind"], + I extends DescMessage, + O extends DescMessage, +> = Omit & { /** * One of the four available method types. */ @@ -181,4 +185,4 @@ type DescMethodTyped