Skip to content

Commit

Permalink
Update readme and move axios to constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
danielailie committed Oct 15, 2024
1 parent 6f260e4 commit df8d1ad
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 15 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ 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
```

### @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
Expand Down
9 changes: 4 additions & 5 deletions src/networkProviders/apiNetworkProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -202,24 +204,21 @@ export class ApiNetworkProvider implements INetworkProvider {
}

private async doGet(resourceUrl: string): Promise<any> {
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);
}
}

private async doPost(resourceUrl: string, payload: any): Promise<any> {
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",
Expand Down
8 changes: 4 additions & 4 deletions src/networkProviders/proxyNetworkProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -224,11 +226,10 @@ export class ProxyNetworkProvider implements INetworkProvider {
}

private async doGet(resourceUrl: string): Promise<any> {
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) {
Expand All @@ -237,11 +238,10 @@ export class ProxyNetworkProvider implements INetworkProvider {
}

private async doPost(resourceUrl: string, payload: any): Promise<any> {
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",
Expand Down
6 changes: 2 additions & 4 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
}
Expand Down

0 comments on commit df8d1ad

Please sign in to comment.