Skip to content

Commit

Permalink
V2: Update makeAnyClient signature to carry type information to narro…
Browse files Browse the repository at this point in the history
…w down method kinds (#1292)
  • Loading branch information
timostamm authored Oct 31, 2024
1 parent 0323619 commit af47285
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 20 deletions.
10 changes: 8 additions & 2 deletions packages/connect/src/any-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import type { DescMethod, DescService } from "@bufbuild/protobuf";
import type {
DescService,
DescMethodUnary,
DescMethodStreaming,
} from "@bufbuild/protobuf";

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

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

type CreateAnyClientMethod = (method: DescMethod) => AnyClientMethod | null;
type CreateAnyClientMethod = (
method: DescMethodUnary | DescMethodStreaming,
) => AnyClientMethod | null;

/**
* Create any client for the given service.
Expand Down
32 changes: 14 additions & 18 deletions packages/connect/src/promise-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import type {
DescMethodBiDiStreaming,
DescMethodClientStreaming,
DescMethodServerStreaming,
DescMethodStreaming,
DescMethodUnary,
} from "@bufbuild/protobuf";
import type { Transport } from "./transport.js";
Expand Down Expand Up @@ -54,23 +53,20 @@ export function createClient<T extends DescService>(
service: T,
transport: Transport,
) {
return makeAnyClient(
service,
(method: DescMethodUnary | DescMethodStreaming) => {
switch (method.methodKind) {
case "unary":
return createUnaryFn(transport, method);
case "server_streaming":
return createServerStreamingFn(transport, method);
case "client_streaming":
return createClientStreamingFn(transport, method);
case "bidi_streaming":
return createBiDiStreamingFn(transport, method);
default:
return null;
}
},
) as Client<T>;
return makeAnyClient(service, (method) => {
switch (method.methodKind) {
case "unary":
return createUnaryFn(transport, method);
case "server_streaming":
return createServerStreamingFn(transport, method);
case "client_streaming":
return createClientStreamingFn(transport, method);
case "bidi_streaming":
return createBiDiStreamingFn(transport, method);
default:
return null;
}
}) as Client<T>;
}

/**
Expand Down

0 comments on commit af47285

Please sign in to comment.