Skip to content

Commit af47285

Browse files
authored
V2: Update makeAnyClient signature to carry type information to narrow down method kinds (#1292)
1 parent 0323619 commit af47285

File tree

2 files changed

+22
-20
lines changed

2 files changed

+22
-20
lines changed

packages/connect/src/any-client.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
import type { DescMethod, DescService } from "@bufbuild/protobuf";
15+
import type {
16+
DescService,
17+
DescMethodUnary,
18+
DescMethodStreaming,
19+
} from "@bufbuild/protobuf";
1620

1721
/**
1822
* AnyClient is an arbitrary service client with any method signature.
@@ -24,7 +28,9 @@ export type AnyClient = Record<string, AnyClientMethod>;
2428

2529
type AnyClientMethod = (...args: any[]) => any; // eslint-disable-line @typescript-eslint/no-explicit-any
2630

27-
type CreateAnyClientMethod = (method: DescMethod) => AnyClientMethod | null;
31+
type CreateAnyClientMethod = (
32+
method: DescMethodUnary | DescMethodStreaming,
33+
) => AnyClientMethod | null;
2834

2935
/**
3036
* Create any client for the given service.

packages/connect/src/promise-client.ts

+14-18
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import type {
2020
DescMethodBiDiStreaming,
2121
DescMethodClientStreaming,
2222
DescMethodServerStreaming,
23-
DescMethodStreaming,
2423
DescMethodUnary,
2524
} from "@bufbuild/protobuf";
2625
import type { Transport } from "./transport.js";
@@ -54,23 +53,20 @@ export function createClient<T extends DescService>(
5453
service: T,
5554
transport: Transport,
5655
) {
57-
return makeAnyClient(
58-
service,
59-
(method: DescMethodUnary | DescMethodStreaming) => {
60-
switch (method.methodKind) {
61-
case "unary":
62-
return createUnaryFn(transport, method);
63-
case "server_streaming":
64-
return createServerStreamingFn(transport, method);
65-
case "client_streaming":
66-
return createClientStreamingFn(transport, method);
67-
case "bidi_streaming":
68-
return createBiDiStreamingFn(transport, method);
69-
default:
70-
return null;
71-
}
72-
},
73-
) as Client<T>;
56+
return makeAnyClient(service, (method) => {
57+
switch (method.methodKind) {
58+
case "unary":
59+
return createUnaryFn(transport, method);
60+
case "server_streaming":
61+
return createServerStreamingFn(transport, method);
62+
case "client_streaming":
63+
return createClientStreamingFn(transport, method);
64+
case "bidi_streaming":
65+
return createBiDiStreamingFn(transport, method);
66+
default:
67+
return null;
68+
}
69+
}) as Client<T>;
7470
}
7571

7672
/**

0 commit comments

Comments
 (0)