Skip to content

Commit fe5f1b4

Browse files
authored
Merge pull request #505 from multiversx/TOOL-264-bring-sdk-wallet-into-sdk-core-optional-dependencies
Make axios and bls optional
2 parents 10c93ed + df8d1ad commit fe5f1b4

File tree

9 files changed

+92
-44
lines changed

9 files changed

+92
-44
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,24 @@ npm install --global browserify
3232
npm install esmify --no-save
3333
```
3434

35+
## Optional Dependencies
36+
37+
### axios
38+
39+
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`:
40+
41+
```bash
42+
npm install axios
43+
```
44+
45+
### @multiversx/sdk-bls-wasm
46+
47+
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:
48+
49+
```bash
50+
npm install @multiversx/sdk-bls-wasm
51+
```
52+
3553
### Building the library
3654

3755
In order to compile the library, run the following:

package-lock.json

Lines changed: 25 additions & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
},
3838
"dependencies": {
3939
"@multiversx/sdk-transaction-decoder": "1.0.2",
40-
"@multiversx/sdk-bls-wasm": "0.3.5",
4140
"json-bigint": "1.0.0",
4241
"bech32": "1.1.4",
4342
"bip39": "3.1.0",
@@ -75,7 +74,10 @@
7574
},
7675
"peerDependencies": {
7776
"bignumber.js": "^9.0.1",
78-
"protobufjs": "^7.2.6",
79-
"axios": "^1.7.4"
77+
"protobufjs": "^7.2.6"
78+
},
79+
"optionalDependencies": {
80+
"axios": "^1.7.4",
81+
"@multiversx/sdk-bls-wasm": "0.3.5"
8082
}
8183
}

src/networkProviders/apiNetworkProvider.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import axios from "axios";
21
import { ErrContractQuery, ErrNetworkProvider } from "../errors";
2+
import { getAxios } from "../utils";
33
import { numberToPaddedHex } from "../utils.codec";
44
import { AccountOnNetwork, GuardianData } from "./accounts";
55
import { defaultAxiosConfig, defaultPagination } from "./config";
@@ -26,12 +26,14 @@ export class ApiNetworkProvider implements INetworkProvider {
2626
private config: NetworkProviderConfig;
2727
private backingProxyNetworkProvider;
2828
private userAgentPrefix = `${BaseUserAgent}/api`;
29+
private axios: any;
2930

3031
constructor(url: string, config?: NetworkProviderConfig) {
3132
this.url = url;
3233
const proxyConfig = this.getProxyConfig(config);
3334
this.config = { ...defaultAxiosConfig, ...config };
3435
this.backingProxyNetworkProvider = new ProxyNetworkProvider(url, proxyConfig);
36+
this.axios = getAxios();
3537
extendUserAgent(this.userAgentPrefix, this.config);
3638
}
3739

@@ -205,7 +207,7 @@ export class ApiNetworkProvider implements INetworkProvider {
205207
const url = `${this.url}/${resourceUrl}`;
206208

207209
try {
208-
const response = await axios.get(url, this.config);
210+
const response = await this.axios.default.get(url, this.config);
209211
return response.data;
210212
} catch (error) {
211213
this.handleApiError(error, resourceUrl);
@@ -216,7 +218,7 @@ export class ApiNetworkProvider implements INetworkProvider {
216218
const url = `${this.url}/${resourceUrl}`;
217219

218220
try {
219-
const response = await axios.post(url, payload, {
221+
const response = await this.axios.default.post(url, payload, {
220222
...this.config,
221223
headers: {
222224
"Content-Type": "application/json",

src/networkProviders/proxyNetworkProvider.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import axios from "axios";
21
import { ErrContractQuery, ErrNetworkProvider } from "../errors";
2+
import { getAxios } from "../utils";
33
import { AccountOnNetwork, GuardianData } from "./accounts";
44
import { defaultAxiosConfig } from "./config";
55
import { BaseUserAgent, EsdtContractAddress } from "./constants";
@@ -22,10 +22,12 @@ export class ProxyNetworkProvider implements INetworkProvider {
2222
private url: string;
2323
private config: NetworkProviderConfig;
2424
private userAgentPrefix = `${BaseUserAgent}/proxy`;
25+
private axios: any;
2526

2627
constructor(url: string, config?: NetworkProviderConfig) {
2728
this.url = url;
2829
this.config = { ...defaultAxiosConfig, ...config };
30+
this.axios = getAxios();
2931
extendUserAgent(this.userAgentPrefix, this.config);
3032
}
3133

@@ -227,7 +229,7 @@ export class ProxyNetworkProvider implements INetworkProvider {
227229
const url = `${this.url}/${resourceUrl}`;
228230

229231
try {
230-
const response = await axios.get(url, this.config);
232+
const response = await this.axios.default.get(url, this.config);
231233
const payload = response.data.data;
232234
return payload;
233235
} catch (error) {
@@ -239,7 +241,7 @@ export class ProxyNetworkProvider implements INetworkProvider {
239241
const url = `${this.url}/${resourceUrl}`;
240242

241243
try {
242-
const response = await axios.post(url, payload, {
244+
const response = await this.axios.default.post(url, payload, {
243245
...this.config,
244246
headers: {
245247
"Content-Type": "application/json",

src/testutils/utils.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
import { PathLike } from "fs";
1+
import BigNumber from "bignumber.js";
22
import * as fs from "fs";
3-
import { SmartContract } from "../smartcontracts/smartContract";
3+
import { PathLike } from "fs";
4+
import { IChainID, IGasLimit } from "../interface";
45
import { Code } from "../smartcontracts/code";
6+
import { SmartContract } from "../smartcontracts/smartContract";
57
import { AbiRegistry, TypedValue } from "../smartcontracts/typesystem";
68
import { Transaction } from "../transaction";
79
import { TransactionWatcher } from "../transactionWatcher";
8-
import { IChainID, IGasLimit } from "../interface";
10+
import { getAxios } from "../utils";
911
import { TestWallet } from "./wallets";
10-
import axios, { AxiosResponse } from "axios";
11-
import BigNumber from "bignumber.js";
1212

1313
export async function prepareDeployment(obj: {
1414
deployer: TestWallet;
@@ -41,7 +41,8 @@ export async function prepareDeployment(obj: {
4141

4242
export async function loadContractCode(path: PathLike): Promise<Code> {
4343
if (isOnBrowserTests()) {
44-
let response: AxiosResponse<ArrayBuffer> = await axios.get(path.toString(), {
44+
const axios = await getAxios();
45+
let response: any = await axios.default.get(path.toString(), {
4546
responseType: "arraybuffer",
4647
transformResponse: [],
4748
headers: {
@@ -60,7 +61,8 @@ export async function loadContractCode(path: PathLike): Promise<Code> {
6061

6162
export async function loadAbiRegistry(path: PathLike): Promise<AbiRegistry> {
6263
if (isOnBrowserTests()) {
63-
let response: AxiosResponse = await axios.get(path.toString());
64+
const axios = await getAxios();
65+
let response: any = await axios.default.get(path.toString());
6466
return AbiRegistry.create(response.data);
6567
}
6668

src/testutils/wallets.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import axios from "axios";
21
import * as fs from "fs";
32
import * as path from "path";
43
import { Account } from "../account";
54
import { Address } from "../address";
65
import { IAddress } from "../interface";
76
import { IAccountOnNetwork } from "../interfaceOfNetwork";
7+
import { getAxios } from "../utils";
88
import { UserSecretKey, UserSigner } from "./../wallet";
99
import { readTestFile } from "./files";
1010
import { isOnBrowserTests } from "./utils";
@@ -82,7 +82,8 @@ async function readTestWalletFileContents(name: string): Promise<string> {
8282
}
8383

8484
async function downloadTextFile(url: string) {
85-
let response = await axios.get(url, { responseType: "text", transformResponse: [] });
85+
const axios = await getAxios();
86+
let response = await axios.default.get(url, { responseType: "text", transformResponse: [] });
8687
let text = response.data.toString();
8788
return text;
8889
}

src/utils.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,11 @@ export function isEmpty(value: { isEmpty?: () => boolean; length?: number }): bo
5656

5757
return value.length === 0;
5858
}
59+
60+
export function getAxios() {
61+
try {
62+
return require("axios");
63+
} catch (error) {
64+
throw new Error("axios is required but not installed. Please install axios to make network requests.");
65+
}
66+
}

0 commit comments

Comments
 (0)