Skip to content

Commit 6f803ed

Browse files
Merge pull request #24 from BuildOnBeam/feat/OB-870-improved-switch-chain-support
feat: improved switch chain support
2 parents f4a223c + 2f7d36a commit 6f803ed

File tree

2 files changed

+46
-13
lines changed

2 files changed

+46
-13
lines changed

packages/sdk/src/BeamClient.ts

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import { AXIOS_INSTANCE } from './lib/api/beam-axios-client';
2+
import { getPlayerAPI } from './lib/api/beam.player-api.generated';
23
import { BeamConfiguration } from './lib/config';
4+
import { beamIcon } from './lib/icon';
35
import {
46
BeamProvider,
57
announceProvider,
68
beamProviderInfo,
79
} from './lib/provider';
810
import { StorageKeys, StorageService } from './lib/storage';
911
import { SessionManager } from './sessionManager';
10-
import { ClientConfig } from './types';
11-
import { getPlayerAPI } from './lib/api/beam.player-api.generated';
12-
import { beamIcon } from './lib/icon';
12+
import { ChainId, ClientConfig } from './types';
1313

1414
export class BeamClient {
1515
readonly #config: BeamConfiguration;
@@ -42,14 +42,7 @@ export class BeamClient {
4242
storage: this.#storage,
4343
});
4444

45-
AXIOS_INSTANCE.interceptors.request.use((config) => {
46-
config.baseURL = this.#config.getChainConfig().apiUrl;
47-
config.headers.set(
48-
'x-api-key',
49-
this.#config.getChainConfig().publishableKey,
50-
);
51-
return config;
52-
});
45+
this.#setAxiosInterceptors();
5346
}
5447

5548
/**
@@ -61,6 +54,25 @@ export class BeamClient {
6154
this.#storage = new StorageService<StorageKeys>(storage);
6255
}
6356

57+
/**
58+
* Get the current chain configuration
59+
*/
60+
public get chain() {
61+
return this.#config.getChainConfig();
62+
}
63+
64+
/**
65+
* Set the chainId for the current client
66+
* @param chainId
67+
*/
68+
public switchChain(chainId: ChainId) {
69+
if (this.#config.chainId === chainId) return;
70+
71+
this.#config.setChainId(chainId);
72+
73+
this.#setAxiosInterceptors();
74+
}
75+
6476
/**
6577
* Instantiate a new provider
6678
* @param options
@@ -156,6 +168,22 @@ export class BeamClient {
156168
);
157169
}
158170

171+
/**
172+
* Set the axios interceptors for the current chain
173+
*/
174+
#setAxiosInterceptors() {
175+
if (!this.#config.chainId) return;
176+
177+
AXIOS_INSTANCE.interceptors.request.use((config) => {
178+
config.baseURL = this.#config.getChainConfig().apiUrl;
179+
config.headers.set(
180+
'x-api-key',
181+
this.#config.getChainConfig().publishableKey,
182+
);
183+
return config;
184+
});
185+
}
186+
159187
/**
160188
* Base64 encoded SVG Beam logo
161189
*/

packages/sdk/src/lib/config.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { ChainId } from '../types';
44
export class BeamConfiguration {
55
readonly chains: ClientConfig['chains'];
66

7-
#chainId: ChainId;
7+
#chainId?: ChainId;
88

99
readonly debug?: boolean;
1010

@@ -22,7 +22,7 @@ export class BeamConfiguration {
2222
throw new Error(`Chain ${config.chainId} not found in configuration`);
2323
}
2424

25-
this.#chainId = config.chainId ?? config.chains[0].id;
25+
if (config.chainId) this.#chainId = config.chainId;
2626

2727
this.debug = config.debug || false;
2828
}
@@ -41,6 +41,11 @@ export class BeamConfiguration {
4141

4242
getChainConfig() {
4343
const chainId = this.chainId;
44+
45+
if (!chainId) {
46+
throw new Error('ChainId not configured, call setChainId first');
47+
}
48+
4449
const chain = this.chains.find((chain) => chain.id === chainId);
4550

4651
if (!chain) {

0 commit comments

Comments
 (0)