@@ -7,43 +7,80 @@ import {
77} from '@injectivelabs/exceptions'
88import { GrpcWebRpcTransport } from './GrpcWebRpcTransport.js'
99import type { UnaryCall , RpcOptions } from '@protobuf-ts/runtime-rpc'
10+ import type { GrpcWebTransportAdditionalOptions } from '../../types'
1011
1112/**
1213 * BaseGrpcConsumer provides base functionality for all gRPC consumers.
1314 * It uses the GrpcWebRpcTransport with GrpcWebFetchTransport from @protobuf-ts/grpcweb-transport.
1415 */
1516export default class BaseGrpcConsumer {
16- protected transport : GrpcWebRpcTransport
17+ private _client : unknown
18+ protected endpoint : string
1719 protected module : string = ''
20+ protected transport : GrpcWebRpcTransport
1821 protected metadata ?: Record < string , string >
19- protected endpoint : string
22+ protected options ?: GrpcWebTransportAdditionalOptions
2023
21- constructor ( endpoint : string ) {
24+ constructor ( endpoint : string , options ?: GrpcWebTransportAdditionalOptions ) {
25+ this . options = options
2226 this . endpoint = endpoint
23- this . transport = new GrpcWebRpcTransport ( endpoint , {
24- headers : { } ,
25- } )
27+ this . transport = new GrpcWebRpcTransport ( endpoint , options )
2628 }
2729
30+ /**
31+ * @deprecated Pass options into the constructor instead
32+ */
2833 public setMetadata ( map : Record < string , string > ) {
2934 this . metadata = map
30- // Recreate transport with new metadata
35+ // Recreate transport with new metadata, preserving existing options
3136 this . transport = new GrpcWebRpcTransport ( this . endpoint , {
32- headers : this . metadata ,
37+ ...this . options ,
38+ meta : this . metadata ,
3339 } )
40+
41+ // Invalidate cached client so initClient creates a new client with updated transport
42+ this . _client = undefined
43+
3444 return this
3545 }
3646
47+ /**
48+ * @deprecated Manage options within the constructor instead
49+ */
3750 public clearMetadata ( ) {
3851 this . metadata = undefined
52+ // Recreate transport without metadata, preserving existing options
53+ this . transport = new GrpcWebRpcTransport ( this . endpoint , this . options )
54+ // Invalidate cached client so initClient creates a new client with updated transport
55+ this . _client = undefined
3956 }
4057
4158 public getTransport ( ) : GrpcWebRpcTransport {
4259 return this . transport
4360 }
4461
62+ /**
63+ * Lazily initializes and returns the gRPC client.
64+ * Call this from a getter in subclasses to avoid constructor boilerplate.
65+ *
66+ * @example
67+ * private get client() {
68+ * return this.initClient(MyGrpcClient)
69+ * }
70+ */
71+ protected initClient < TClient > (
72+ ClientClass : new ( transport : GrpcWebRpcTransport ) => TClient ,
73+ ) : TClient {
74+ if ( ! this . _client ) {
75+ this . _client = new ClientClass ( this . transport )
76+ }
77+
78+ return this . _client as TClient
79+ }
80+
4581 /**
4682 * Builds RpcOptions with metadata
83+ * @deprecated Options should be managed externally and passed into the constructor instead
4784 */
4885 protected getRpcOptions ( ) : RpcOptions {
4986 const options : RpcOptions = {
0 commit comments