From beb8c1d423810375a6f3d17faa8089ccafce964b Mon Sep 17 00:00:00 2001 From: llllllluc <58892938+llllllluc@users.noreply.github.com> Date: Sun, 26 Nov 2023 23:40:50 -0800 Subject: [PATCH] create an axios config for api token for extensibility --- src/client/lcd/APIRequester.ts | 7 ++++--- src/client/lcd/LCDClient.ts | 17 ++++++++++++----- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/client/lcd/APIRequester.ts b/src/client/lcd/APIRequester.ts index 11a08637..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,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, }); diff --git a/src/client/lcd/LCDClient.ts b/src/client/lcd/LCDClient.ts index dc5e782a..6185bbbc 100644 --- a/src/client/lcd/LCDClient.ts +++ b/src/client/lcd/LCDClient.ts @@ -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. @@ -149,7 +156,7 @@ export class LCDClient { (result: Record, chainID) => { result[chainID] = new APIRequester( chains[chainID].lcd, - chains[chainID].apiToken + chains[chainID].axiosConfig ); return result; },