Skip to content

Commit

Permalink
create an axios config for api token for extensibility
Browse files Browse the repository at this point in the history
  • Loading branch information
dev8723 committed Nov 27, 2023
1 parent de791c3 commit beb8c1d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
7 changes: 4 additions & 3 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,14 +25,14 @@ export class APIRequester {
private axios: AxiosInstance;
private readonly baseURL: string;

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

if (apiToken) {
if (axiosConfig?.apiToken) {
this.axios = Axios.create({
headers: {
Accept: 'application/json',
Authorization: `Bearer ${apiToken}`,
Authorization: `Bearer ${axiosConfig.apiToken}`,
},
timeout: 30000,
});
Expand Down
17 changes: 12 additions & 5 deletions src/client/lcd/LCDClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,23 @@ 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 base URL to which LCD requests will be made.
* The Axios configuration to use when making requests to the LCD.
*/
lcd: string;
axiosConfig?: AxiosConfig;

/**
* The API key to be included in requests sent to the LCD.
* The base URL to which LCD requests will be made.
*/
apiToken?: string;
lcd: string;

/**
* Chain ID of the blockchain to connect to.
Expand Down Expand Up @@ -149,7 +156,7 @@ export class LCDClient {
(result: Record<string, APIRequester>, chainID) => {
result[chainID] = new APIRequester(
chains[chainID].lcd,
chains[chainID].apiToken
chains[chainID].axiosConfig
);
return result;
},
Expand Down

0 comments on commit beb8c1d

Please sign in to comment.