diff --git a/src/client/lcd/APIRequester.ts b/src/client/lcd/APIRequester.ts index a9d2d5e0..0324652f 100644 --- a/src/client/lcd/APIRequester.ts +++ b/src/client/lcd/APIRequester.ts @@ -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; @@ -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) { diff --git a/src/client/lcd/LCDClient.ts b/src/client/lcd/LCDClient.ts index 87c63eb6..6185bbbc 100644 --- a/src/client/lcd/LCDClient.ts +++ b/src/client/lcd/LCDClient.ts @@ -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. */ @@ -142,7 +154,10 @@ export class LCDClient { this.apiRequesters = Object.keys(chains).reduce( (result: Record, chainID) => { - result[chainID] = new APIRequester(chains[chainID].lcd); + result[chainID] = new APIRequester( + chains[chainID].lcd, + chains[chainID].axiosConfig + ); return result; }, {}