diff --git a/packages/microservices/client/client-grpc.ts b/packages/microservices/client/client-grpc.ts index 6dd27379ab7..d91fc574934 100644 --- a/packages/microservices/client/client-grpc.ts +++ b/packages/microservices/client/client-grpc.ts @@ -20,6 +20,7 @@ let grpcProtoLoaderPackage: any = {}; export class ClientGrpcProxy extends ClientProxy implements ClientGrpc { protected readonly logger = new Logger(ClientProxy.name); + protected readonly clients = new Map(); protected readonly url: string; protected grpcClient: any; @@ -38,6 +39,25 @@ export class ClientGrpcProxy extends ClientProxy implements ClientGrpc { } public getService(name: string): T { + const grpcClient = this.createClientByServiceName(name); + const protoMethods = Object.keys(this.grpcClient[name].prototype); + const grpcService = {} as T; + + protoMethods.forEach(m => { + const key = m[0].toLowerCase() + m.slice(1, m.length); + grpcService[key] = this.createServiceMethod(grpcClient, m); + }); + return grpcService; + } + + public getClientByServiceName(name: string): T { + return this.clients.get(name) || this.createClientByServiceName(name); + } + + public createClientByServiceName(name: string) { + if (!this.grpcClient[name]) { + throw new InvalidGrpcServiceException(); + } const maxSendMessageLengthKey = 'grpc.max_send_message_length'; const maxReceiveMessageLengthKey = 'grpc.max_receive_message_length'; const maxMessageLengthOptions = { @@ -62,24 +82,17 @@ export class ClientGrpcProxy extends ClientProxy implements ClientGrpc { ...maxMessageLengthOptions, }; - if (!this.grpcClient[name]) { - throw new InvalidGrpcServiceException(); - } const credentials = options.credentials || grpcPackage.credentials.createInsecure(); - delete options.credentials; + + const { credentials: originalCreds, ...clientOptions } = options; const grpcClient = new this.grpcClient[name]( this.url, credentials, - options, + clientOptions, ); - const protoMethods = Object.keys(this.grpcClient[name].prototype); - const grpcService = {} as T; - protoMethods.forEach(m => { - const key = m[0].toLowerCase() + m.slice(1, m.length); - grpcService[key] = this.createServiceMethod(grpcClient, m); - }); - return grpcService; + this.clients.set(name, grpcClient); + return grpcClient; } public createServiceMethod( diff --git a/packages/microservices/interfaces/client-grpc.interface.ts b/packages/microservices/interfaces/client-grpc.interface.ts index 6a56ff688bf..ebb73f46641 100644 --- a/packages/microservices/interfaces/client-grpc.interface.ts +++ b/packages/microservices/interfaces/client-grpc.interface.ts @@ -1,3 +1,4 @@ export interface ClientGrpc { getService(name: string): T; + getClientByServiceName(name: string): T; }