Skip to content

Commit

Permalink
support api token
Browse files Browse the repository at this point in the history
  • Loading branch information
dev8723 committed Nov 26, 2023
1 parent f6b6185 commit de791c3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
24 changes: 17 additions & 7 deletions src/client/lcd/APIRequester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,25 @@ export class APIRequester {
private axios: AxiosInstance;
private readonly baseURL: string;

constructor(baseURL: string) {
constructor(baseURL: string, apiToken?: string) {
this.baseURL = baseURL;

this.axios = Axios.create({
headers: {
Accept: 'application/json',
},
timeout: 30000,
});
if (apiToken) {
this.axios = Axios.create({
headers: {
Accept: 'application/json',
Authorization: `Bearer ${apiToken}`,
},
timeout: 30000,
});
} else {
this.axios = Axios.create({
headers: {
Accept: 'application/json',
},
timeout: 30000,
});
}
}

private computeEndpoint(endpoint: string) {
Expand Down
10 changes: 9 additions & 1 deletion src/client/lcd/LCDClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ export interface LCDClientConfig {
*/
lcd: string;

/**
* The API key to be included in requests sent to the LCD.
*/
apiToken?: string;

/**
* Chain ID of the blockchain to connect to.
*/
Expand Down Expand Up @@ -142,7 +147,10 @@ export class LCDClient {

this.apiRequesters = Object.keys(chains).reduce(
(result: Record<string, APIRequester>, chainID) => {
result[chainID] = new APIRequester(chains[chainID].lcd);
result[chainID] = new APIRequester(
chains[chainID].lcd,
chains[chainID].apiToken
);
return result;
},
{}
Expand Down

0 comments on commit de791c3

Please sign in to comment.