Skip to content

Commit 7eb26a0

Browse files
authored
Merge pull request #612 from InjectiveLabs/fix/grpc-options-parameter
fix: allow passing grpc options into client classes
2 parents 0f4304b + 6464da0 commit 7eb26a0

File tree

107 files changed

+3497
-10316
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

107 files changed

+3497
-10316
lines changed

packages/exceptions/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@injectivelabs/exceptions",
3-
"version": "1.17.2",
3+
"version": "1.17.3-alpha.0",
44
"description": "List of exceptions that can be reused throughout Injective's projects.",
55
"license": "Apache-2.0",
66
"author": {

packages/networks/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@injectivelabs/networks",
3-
"version": "1.17.2",
3+
"version": "1.17.3-alpha.0",
44
"description": "Endpoints, networks, etc. Can be reused throughout Injective's projects.",
55
"license": "Apache-2.0",
66
"author": {

packages/sdk-ts/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@injectivelabs/sdk-ts",
3-
"version": "1.17.2",
3+
"version": "1.17.3-alpha.0",
44
"description": "SDK in TypeScript for building Injective applications in a browser, node, and react native environment.",
55
"license": "Apache-2.0",
66
"author": {

packages/sdk-ts/src/client/abacus/grpc/AbacusGrpcApi.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,9 @@ import { AbacusGrpcTransformer } from './transformers/index.js'
66

77
export class AbacusGrpcApi extends BaseGrpcConsumer {
88
protected module: string = IndexerErrorModule.Abacus
9-
private client: PointsSvcClient
109

11-
constructor(endpoint: string) {
12-
super(endpoint)
13-
this.client = new PointsSvcClient(this.transport)
10+
private get client() {
11+
return this.initClient(PointsSvcClient)
1412
}
1513

1614
async fetchAccountLatestPoints(address: string) {

packages/sdk-ts/src/client/base/BaseGrpcConsumer.ts

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,43 +7,80 @@ import {
77
} from '@injectivelabs/exceptions'
88
import { GrpcWebRpcTransport } from './GrpcWebRpcTransport.js'
99
import 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
*/
1516
export 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 = {

packages/sdk-ts/src/client/base/GrpcWebRpcTransport.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import type {
88
ClientStreamingCall,
99
DuplexStreamingCall,
1010
} from '@protobuf-ts/runtime-rpc'
11+
import type { GrpcWebTransportAdditionalOptions } from '../../types'
1112

1213
/**
1314
* GrpcWebRpcTransport provides a simple wrapper around GrpcWebFetchTransport
@@ -18,15 +19,7 @@ import type {
1819
export class GrpcWebRpcTransport implements RpcTransport {
1920
private transport: RpcTransport
2021

21-
constructor(
22-
baseUrl: string,
23-
options?: {
24-
fetch?: typeof fetch
25-
headers?: Record<string, string>
26-
timeout?: number
27-
credentials?: RequestCredentials
28-
},
29-
) {
22+
constructor(baseUrl: string, options?: GrpcWebTransportAdditionalOptions) {
3023
this.transport = getGrpcWebTransport(baseUrl, options)
3124
}
3225

packages/sdk-ts/src/client/chain/grpc/ChainGrpcAuctionApi.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,14 @@ import { QueryClient as InjectiveAuctionV1Beta1QueryClient } from '@injectivelab
33
import { ChainModule } from '../types/index.js'
44
import BaseGrpcConsumer from '../../base/BaseGrpcConsumer.js'
55
import { ChainGrpcAuctionTransformer } from '../transformers/index.js'
6-
76
/**
87
* @category Chain Grpc API
98
*/
109
export class ChainGrpcAuctionApi extends BaseGrpcConsumer {
1110
protected module: string = ChainModule.Auction
1211

13-
private client: InjectiveAuctionV1Beta1QueryClient
14-
15-
constructor(endpoint: string) {
16-
super(endpoint)
17-
18-
this.client = new InjectiveAuctionV1Beta1QueryClient(this.transport)
12+
private get client() {
13+
return this.initClient(InjectiveAuctionV1Beta1QueryClient)
1914
}
2015

2116
async fetchModuleParams() {

packages/sdk-ts/src/client/chain/grpc/ChainGrpcAuthApi.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,14 @@ import BaseGrpcConsumer from '../../base/BaseGrpcConsumer.js'
55
import { ChainGrpcAuthTransformer } from '../transformers/ChainGrpcAuthTransformer.js'
66
import { ChainGrpcCommonTransformer } from '../transformers/ChainGrpcCommonTransformer.js'
77
import type { PaginationOption } from '../../../types/pagination.js'
8-
98
/**
109
* @category Chain Grpc API
1110
*/
1211
export class ChainGrpcAuthApi extends BaseGrpcConsumer {
1312
protected module: string = ChainModule.Auth
14-
private client: CosmosAuthV1BetaQueryClient
1513

16-
constructor(endpoint: string) {
17-
super(endpoint)
18-
this.client = new CosmosAuthV1BetaQueryClient(this.transport)
14+
private get client() {
15+
return this.initClient(CosmosAuthV1BetaQueryClient)
1916
}
2017

2118
async fetchModuleParams() {

packages/sdk-ts/src/client/chain/grpc/ChainGrpcAuthZApi.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,14 @@ import BaseGrpcConsumer from '../../base/BaseGrpcConsumer.js'
55
import { ChainGrpcAuthZTransformer } from '../transformers/ChainGrpcAuthZTransformer.js'
66
import { ChainGrpcCommonTransformer } from '../transformers/ChainGrpcCommonTransformer.js'
77
import type { PaginationOption } from '../../../types/pagination.js'
8-
98
/**
109
* @category Chain Grpc API
1110
*/
1211
export class ChainGrpcAuthZApi extends BaseGrpcConsumer {
1312
protected module: string = ChainModule.Authz
14-
private client: CosmosAuthzV1BetaQueryClient
1513

16-
constructor(endpoint: string) {
17-
super(endpoint)
18-
this.client = new CosmosAuthzV1BetaQueryClient(this.transport)
14+
private get client() {
15+
return this.initClient(CosmosAuthzV1BetaQueryClient)
1916
}
2017

2118
async fetchGrants({

packages/sdk-ts/src/client/chain/grpc/ChainGrpcBankApi.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,16 @@ import { ChainGrpcBankTransformer } from '../transformers/index.js'
66
import { fetchAllWithPagination } from '../../../utils/pagination.js'
77
import { ChainGrpcCommonTransformer } from '../transformers/ChainGrpcCommonTransformer.js'
88
import type { PaginationOption } from '../../../types/pagination.js'
9-
109
const MAX_LIMIT_FOR_SUPPLY = 10000
1110

1211
/**
1312
* @category Chain Grpc API
1413
*/
1514
export class ChainGrpcBankApi extends BaseGrpcConsumer {
1615
protected module: string = ChainModule.Bank
17-
private client: CosmosBankV1BetaQueryClient
1816

19-
constructor(endpoint: string) {
20-
super(endpoint)
21-
this.client = new CosmosBankV1BetaQueryClient(this.transport)
17+
private get client() {
18+
return this.initClient(CosmosBankV1BetaQueryClient)
2219
}
2320

2421
async fetchModuleParams() {

0 commit comments

Comments
 (0)