@@ -20,6 +20,7 @@ let grpcProtoLoaderPackage: any = {};
2020
2121export class ClientGrpcProxy extends ClientProxy implements ClientGrpc {
2222 protected readonly logger = new Logger ( ClientProxy . name ) ;
23+ protected readonly clients = new Map < string , any > ( ) ;
2324 protected readonly url : string ;
2425 protected grpcClient : any ;
2526
@@ -38,6 +39,25 @@ export class ClientGrpcProxy extends ClientProxy implements ClientGrpc {
3839 }
3940
4041 public getService < T extends { } > ( name : string ) : T {
42+ const grpcClient = this . createClientByServiceName ( name ) ;
43+ const protoMethods = Object . keys ( this . grpcClient [ name ] . prototype ) ;
44+ const grpcService = { } as T ;
45+
46+ protoMethods . forEach ( m => {
47+ const key = m [ 0 ] . toLowerCase ( ) + m . slice ( 1 , m . length ) ;
48+ grpcService [ key ] = this . createServiceMethod ( grpcClient , m ) ;
49+ } ) ;
50+ return grpcService ;
51+ }
52+
53+ public getClientByServiceName < T = any > ( name : string ) : T {
54+ return this . clients . get ( name ) || this . createClientByServiceName ( name ) ;
55+ }
56+
57+ public createClientByServiceName ( name : string ) {
58+ if ( ! this . grpcClient [ name ] ) {
59+ throw new InvalidGrpcServiceException ( ) ;
60+ }
4161 const maxSendMessageLengthKey = 'grpc.max_send_message_length' ;
4262 const maxReceiveMessageLengthKey = 'grpc.max_receive_message_length' ;
4363 const maxMessageLengthOptions = {
@@ -62,24 +82,17 @@ export class ClientGrpcProxy extends ClientProxy implements ClientGrpc {
6282 ...maxMessageLengthOptions ,
6383 } ;
6484
65- if ( ! this . grpcClient [ name ] ) {
66- throw new InvalidGrpcServiceException ( ) ;
67- }
6885 const credentials =
6986 options . credentials || grpcPackage . credentials . createInsecure ( ) ;
70- delete options . credentials ;
87+
88+ const { credentials : originalCreds , ...clientOptions } = options ;
7189 const grpcClient = new this . grpcClient [ name ] (
7290 this . url ,
7391 credentials ,
74- options ,
92+ clientOptions ,
7593 ) ;
76- const protoMethods = Object . keys ( this . grpcClient [ name ] . prototype ) ;
77- const grpcService = { } as T ;
78- protoMethods . forEach ( m => {
79- const key = m [ 0 ] . toLowerCase ( ) + m . slice ( 1 , m . length ) ;
80- grpcService [ key ] = this . createServiceMethod ( grpcClient , m ) ;
81- } ) ;
82- return grpcService ;
94+ this . clients . set ( name , grpcClient ) ;
95+ return grpcClient ;
8396 }
8497
8598 public createServiceMethod (
0 commit comments