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

Add typed DescMethod variants #985

Merged
merged 3 commits into from
Oct 8, 2024
Merged
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
69 changes: 68 additions & 1 deletion packages/protobuf/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -89,6 +94,49 @@ export type UnknownField = {
readonly data: Uint8Array;
};

/**
* Describes a streaming RPC declaration.
*/
export type DescMethodStreaming<
I extends DescMessage = DescMessage,
O extends DescMessage = DescMessage,
> =
| DescMethodClientStreaming<I, O>
| DescMethodServerStreaming<I, O>
| DescMethodBiDiStreaming<I, O>;

/**
* Describes a unary RPC declaration.
*/
export type DescMethodUnary<
I extends DescMessage = DescMessage,
O extends DescMessage = DescMessage,
> = DescMethodTyped<"unary", I, O>;

/**
* Describes a server streaming RPC declaration.
*/
export type DescMethodServerStreaming<
I extends DescMessage = DescMessage,
O extends DescMessage = DescMessage,
> = DescMethodTyped<"server_streaming", I, O>;

/**
* Describes a client streaming RPC declaration.
*/
export type DescMethodClientStreaming<
I extends DescMessage = DescMessage,
O extends DescMessage = DescMessage,
> = DescMethodTyped<"client_streaming", I, O>;

/**
* Describes a bidi streaming RPC declaration.
*/
export type DescMethodBiDiStreaming<
I extends DescMessage = DescMessage,
O extends DescMessage = DescMessage,
> = DescMethodTyped<"bidi_streaming", I, O>;

/**
* The init type for a message, which makes all fields optional.
* The init type is accepted by the function create().
Expand Down Expand Up @@ -119,3 +167,22 @@ type OneofSelectedMessage<K extends string, M extends Message> = {
case: K;
value: M;
};

type DescMethodTyped<
K extends DescMethod["methodKind"],
I extends DescMessage,
O extends DescMessage,
> = Omit<DescMethod, "methodKind" | "input" | "output"> & {
/**
* 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;
};