diff --git a/README.md b/README.md index a0212ab0..3ed95086 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,7 @@ npm install esmify --no-save ### axios -This package can make HTTP requests using `axios`, but it is not bundled by default. If you plan to use the API network provider or Proxy network provider, make sure to install `axios`: +This package can make HTTP requests using `axios`, which is not bundled by default. If you plan to use the API network provider or Proxy network provider, make sure to install `axios`: ```bash npm install axios @@ -44,7 +44,7 @@ npm install axios ### @multiversx/sdk-bls-wasm -This package requires `@multiversx/sdk-bls-wasm` for BLS (Boneh-Lynn-Shacham) cryptographic functions, but it is not bundled by default. If you plan to use BLS functionality, make sure to install this optional dependency: +This package requires `@multiversx/sdk-bls-wasm` for BLS (Boneh-Lynn-Shacham) cryptographic functions, which is not bundled by default. If you plan to use BLS functionality, make sure to install this optional dependency: ```bash npm install @multiversx/sdk-bls-wasm diff --git a/src/networkProviders/apiNetworkProvider.ts b/src/networkProviders/apiNetworkProvider.ts index eae0bc97..177f3ab3 100644 --- a/src/networkProviders/apiNetworkProvider.ts +++ b/src/networkProviders/apiNetworkProvider.ts @@ -26,12 +26,14 @@ export class ApiNetworkProvider implements INetworkProvider { private config: NetworkProviderConfig; private backingProxyNetworkProvider; private userAgentPrefix = `${BaseUserAgent}/api`; + private axios: any; constructor(url: string, config?: NetworkProviderConfig) { this.url = url; const proxyConfig = this.getProxyConfig(config); this.config = { ...defaultAxiosConfig, ...config }; this.backingProxyNetworkProvider = new ProxyNetworkProvider(url, proxyConfig); + this.axios = getAxios(); extendUserAgent(this.userAgentPrefix, this.config); } @@ -202,12 +204,10 @@ export class ApiNetworkProvider implements INetworkProvider { } private async doGet(resourceUrl: string): Promise { - let axios = await getAxios(); - const url = `${this.url}/${resourceUrl}`; try { - const response = await axios.default.get(url, this.config); + const response = await this.axios.default.get(url, this.config); return response.data; } catch (error) { this.handleApiError(error, resourceUrl); @@ -215,11 +215,10 @@ export class ApiNetworkProvider implements INetworkProvider { } private async doPost(resourceUrl: string, payload: any): Promise { - let axios = await getAxios(); const url = `${this.url}/${resourceUrl}`; try { - const response = await axios.default.post(url, payload, { + const response = await this.axios.default.post(url, payload, { ...this.config, headers: { "Content-Type": "application/json", diff --git a/src/networkProviders/proxyNetworkProvider.ts b/src/networkProviders/proxyNetworkProvider.ts index 4fd93384..e62327ea 100644 --- a/src/networkProviders/proxyNetworkProvider.ts +++ b/src/networkProviders/proxyNetworkProvider.ts @@ -22,10 +22,12 @@ export class ProxyNetworkProvider implements INetworkProvider { private url: string; private config: NetworkProviderConfig; private userAgentPrefix = `${BaseUserAgent}/proxy`; + private axios: any; constructor(url: string, config?: NetworkProviderConfig) { this.url = url; this.config = { ...defaultAxiosConfig, ...config }; + this.axios = getAxios(); extendUserAgent(this.userAgentPrefix, this.config); } @@ -224,11 +226,10 @@ export class ProxyNetworkProvider implements INetworkProvider { } private async doGet(resourceUrl: string): Promise { - const axios = await getAxios(); const url = `${this.url}/${resourceUrl}`; try { - const response = await axios.default.get(url, this.config); + const response = await this.axios.default.get(url, this.config); const payload = response.data.data; return payload; } catch (error) { @@ -237,11 +238,10 @@ export class ProxyNetworkProvider implements INetworkProvider { } private async doPost(resourceUrl: string, payload: any): Promise { - const axios = await getAxios(); const url = `${this.url}/${resourceUrl}`; try { - const response = await axios.default.post(url, payload, { + const response = await this.axios.default.post(url, payload, { ...this.config, headers: { "Content-Type": "application/json", diff --git a/src/utils.ts b/src/utils.ts index 9cc93522..c80c7c6d 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -57,11 +57,9 @@ export function isEmpty(value: { isEmpty?: () => boolean; length?: number }): bo return value.length === 0; } -export async function getAxios() { - let axios; +export function getAxios() { try { - axios = require("axios"); - return axios; + return require("axios"); } catch (error) { throw new Error("axios is required but not installed. Please install axios to make network requests."); }