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

feature(microservices): Return correct client type in ClientProxyFactory #2240

Conversation

BrunnerLivio
Copy link
Member

@BrunnerLivio BrunnerLivio commented May 20, 2019

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

[ ] Bugfix
[x] Feature
[ ] Code style update (formatting, local variables)
[ ] Refactoring (no functional changes, no api changes)
[ ] Build related changes
[ ] CI related changes
[ ] Other... Please describe:

What is the current behavior?

At the moment ClientProxyFactory.create will always return the type ClientProxy & Closeable. Therefore it will not take the given transport option into consideration, even though we would know what type should be returned.

const client = ClientProxyFactory.create({ transport: Transport.GRPC, options: { ... }); // The type is `ClientProxy & Closeable`

// To get the correct type you have to use this workaround
const grpcClient = (client as any) as ClientGrpc;

On top of that ClientProxy & Closeable is straight up wrong with some clients, such as the GrpcClient.
Calling .connect() from ClientProxy & Closeable on ClientGrpc would result in this error: Error: The "connect()" method is not supported in gRPC mode.

What is the new behavior?

Adds overloads to the ClientProxyFactory for better type security.
This allows the user to not cast the client to any after creating it
using the factory.

const client = ClientProxyFactory.create({ transport: Transport.GRPC, options: { .. }) // The type is `ClientGrpcProxy`

Does this PR introduce a breaking change?

[ ] Yes
[x] No

Other information

The ClientProxyFactory is used in @nestjs/terminus to dynamically create a microservice client on the fly. Better type security would lead to a better code :)

public static create(
clientOptions: { transport: Transport.MQTT } & ClientOptions,
): ClientMqtt;
public static create(clientOptions: ClientOptions): ClientProxy & Closeable;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line has to be "duplicated" so a parameter of type ClientOptions is still valid (used in microservices/module/ClientsModule)

@BrunnerLivio BrunnerLivio changed the title feature(microservices): Add clientOptions overloads for ClientProxyFactory feature(microservices): Return correct client type in ClientProxyFactory May 20, 2019
@BrunnerLivio BrunnerLivio force-pushed the feature/client-proxy-factory-overload branch from 63382ed to 7e4f4e4 Compare May 20, 2019 17:56
@coveralls
Copy link

coveralls commented May 20, 2019

Pull Request Test Coverage Report for Build 2930

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage remained the same at 95.265%

Totals Coverage Status
Change from base Build 2927: 0.0%
Covered Lines: 3380
Relevant Lines: 3548

💛 - Coveralls

@kamilmysliwiec
Copy link
Member

So actually the main idea behind ClientProxyFactory was to isolate, hide these types and set ClientProxy as a sort of contract. Hence, if you switch between Redis or NATS, you still interact with ClientProxy, regardless of what sits behind.

However, ClientGrpc is a different story because their interfaces don't overlap. As you can see in the docs https://docs.nestjs.com/microservices/grpc, we don't recommend using ClientProxy anymore. Instead, the type is explicitly set to ClientGrpc.

Could you change this PR so this method still returns ClientProxy when transport isn't gRPC, otherwise, return ClientGrpc? What do you think?

Add overload to the ClientProxyFactory for better type security.
This allows the user to not cast the GRPC client to any
after creating it using the factory.
@BrunnerLivio BrunnerLivio force-pushed the feature/client-proxy-factory-overload branch from 7e4f4e4 to 17cb7ff Compare May 22, 2019 16:45
@kamilmysliwiec kamilmysliwiec added this to the 6.3.0 milestone May 30, 2019
@kamilmysliwiec kamilmysliwiec merged commit dddf7b9 into nestjs:master May 30, 2019
@lock
Copy link

lock bot commented Sep 23, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Sep 23, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants