From de791c33e76f3d1c3d9a8bfc3e743d7f19622902 Mon Sep 17 00:00:00 2001 From: llllllluc <58892938+llllllluc@users.noreply.github.com> Date: Sun, 26 Nov 2023 12:35:03 -0800 Subject: [PATCH] support api token --- src/client/lcd/APIRequester.ts | 24 +++++++++++++++++------- src/client/lcd/LCDClient.ts | 10 +++++++++- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/src/client/lcd/APIRequester.ts b/src/client/lcd/APIRequester.ts index a9d2d5e0..11a08637 100644 --- a/src/client/lcd/APIRequester.ts +++ b/src/client/lcd/APIRequester.ts @@ -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) { diff --git a/src/client/lcd/LCDClient.ts b/src/client/lcd/LCDClient.ts index 87c63eb6..dc5e782a 100644 --- a/src/client/lcd/LCDClient.ts +++ b/src/client/lcd/LCDClient.ts @@ -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. */ @@ -142,7 +147,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].apiToken + ); return result; }, {}