Skip to content

Commit

Permalink
Merge pull request #62 from llllllluc/l/support-api-token
Browse files Browse the repository at this point in the history
Support api token
  • Loading branch information
emidev98 authored Nov 27, 2023
2 parents f6b6185 + beb8c1d commit 329eae6
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
25 changes: 18 additions & 7 deletions src/client/lcd/APIRequester.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Axios, { AxiosInstance } from 'axios';
import { OrderBy as OrderBy_pb } from '@terra-money/legacy.proto/cosmos/tx/v1beta1/service';
import { AxiosConfig } from './LCDClient';

export type APIParams = Record<string, string | number | null | undefined>;

Expand All @@ -24,15 +25,25 @@ export class APIRequester {
private axios: AxiosInstance;
private readonly baseURL: string;

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

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

private computeEndpoint(endpoint: string) {
Expand Down
17 changes: 16 additions & 1 deletion src/client/lcd/LCDClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,19 @@ import { GovV1API } from './api/GovV1API';
import { ICAv1API } from './api/ICAv1API';
import { ICQv1API } from './api/ICQv1API';

export type AxiosConfig = {
/**
* The API key to be included in requests sent to the LCD.
*/
apiToken?: string;
};

export interface LCDClientConfig {
/**
* The Axios configuration to use when making requests to the LCD.
*/
axiosConfig?: AxiosConfig;

/**
* The base URL to which LCD requests will be made.
*/
Expand Down Expand Up @@ -142,7 +154,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].axiosConfig
);
return result;
},
{}
Expand Down

0 comments on commit 329eae6

Please sign in to comment.