Skip to content

Commit

Permalink
refactor: migrate axios to fetch (#564)
Browse files Browse the repository at this point in the history
  • Loading branch information
homura committed Nov 9, 2023
1 parent a243f11 commit 93ff1b4
Show file tree
Hide file tree
Showing 12 changed files with 251 additions and 179 deletions.
5 changes: 5 additions & 0 deletions .changeset/red-rings-approve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ckb-lumos/rpc": minor
---

Replace the HTTP client `axios` with the native supported `fetch` to avoid the modifying the default adapter of axios
90 changes: 50 additions & 40 deletions packages/rpc/__tests__/ckb-rpc.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
/* eslint-disable max-len */
/** mock template start **/
jest.mock("cross-fetch");
const axiosMock = require("cross-fetch").default;

beforeAll(() => {
const originalResolve = axiosMock.mockResolvedValue;
axiosMock.mockResolvedValue = (value) =>
originalResolve({
json: () => Promise.resolve(value.data),
});
});

jest.mock("axios");
/** mock template end **/

const axiosMock = require("axios");
const { CKBRPC, ResultFormatter } = require("../lib");

describe("Test with mock", () => {
Expand Down Expand Up @@ -94,7 +104,7 @@ describe("Test with mock", () => {
},
});
const res = await rpc.getBlockByNumber(BLOCK_NUMBER);
expect(axiosMock.mock.calls[0][0].data).toEqual({
expect(JSON.parse(axiosMock.mock.calls[0][1].body)).toEqual({
id,
jsonrpc: "2.0",
method: "get_block_by_number",
Expand Down Expand Up @@ -175,7 +185,7 @@ describe("Test with mock", () => {
},
});
const res = await rpc.txPoolInfo();
expect(axiosMock.mock.calls[0][0].data).toEqual({
expect(JSON.parse(axiosMock.mock.calls[0][1].body)).toEqual({
id,
jsonrpc: "2.0",
method: "tx_pool_info",
Expand All @@ -202,7 +212,7 @@ describe("Test with mock", () => {
});

const res = await rpc.clearTxPool();
expect(axiosMock.mock.calls[0][0].data).toEqual({
expect(JSON.parse(axiosMock.mock.calls[0][1].body)).toEqual({
id,
jsonrpc: "2.0",
method: "clear_tx_pool",
Expand Down Expand Up @@ -245,7 +255,7 @@ describe("Test with mock", () => {
});

const res = await rpc.getRawTxPool(true);
expect(axiosMock.mock.calls[0][0].data).toEqual({
expect(JSON.parse(axiosMock.mock.calls[0][1].body)).toEqual({
id,
jsonrpc: "2.0",
method: "get_raw_tx_pool",
Expand Down Expand Up @@ -292,7 +302,7 @@ describe("Test with mock", () => {
});

const res = await rpc.getRawTxPool(false);
expect(axiosMock.mock.calls[0][0].data).toEqual({
expect(JSON.parse(axiosMock.mock.calls[0][1].body)).toEqual({
id,
jsonrpc: "2.0",
method: "get_raw_tx_pool",
Expand Down Expand Up @@ -321,7 +331,7 @@ describe("Test with mock", () => {
});

const res = await rpc.getRawTxPool(null);
expect(axiosMock.mock.calls[0][0].data).toEqual({
expect(JSON.parse(axiosMock.mock.calls[0][1].body)).toEqual({
id,
jsonrpc: "2.0",
method: "get_raw_tx_pool",
Expand Down Expand Up @@ -350,7 +360,7 @@ describe("Test with mock", () => {
});

const res = await rpc.getRawTxPool();
expect(axiosMock.mock.calls[0][0].data).toEqual({
expect(JSON.parse(axiosMock.mock.calls[0][1].body)).toEqual({
id,
jsonrpc: "2.0",
method: "get_raw_tx_pool",
Expand Down Expand Up @@ -379,7 +389,7 @@ describe("Test with mock", () => {
},
});
const res = await rpc.getCurrentEpoch();
expect(axiosMock.mock.calls[0][0].data).toEqual({
expect(JSON.parse(axiosMock.mock.calls[0][1].body)).toEqual({
id,
jsonrpc: "2.0",
method: "get_current_epoch",
Expand Down Expand Up @@ -407,7 +417,7 @@ describe("Test with mock", () => {
},
});
const res = await rpc.getEpochByNumber(BLOCK_NUMBER);
expect(axiosMock.mock.calls[0][0].data).toEqual({
expect(JSON.parse(axiosMock.mock.calls[0][1].body)).toEqual({
id,
jsonrpc: "2.0",
method: "get_epoch_by_number",
Expand Down Expand Up @@ -438,7 +448,7 @@ describe("Test with mock", () => {
},
});
const res = await rpc.getCellbaseOutputCapacityDetails(BLOCK_HASH);
expect(axiosMock.mock.calls[0][0].data).toEqual({
expect(JSON.parse(axiosMock.mock.calls[0][1].body)).toEqual({
id,
jsonrpc: "2.0",
method: "get_cellbase_output_capacity_details",
Expand Down Expand Up @@ -469,7 +479,7 @@ describe("Test with mock", () => {
},
});
const res = await rpc.calculateDaoMaximumWithdraw(...PARAMS);
expect(axiosMock.mock.calls[0][0].data).toEqual({
expect(JSON.parse(axiosMock.mock.calls[0][1].body)).toEqual({
id,
jsonrpc: "2.0",
method: "calculate_dao_maximum_withdraw",
Expand Down Expand Up @@ -510,7 +520,7 @@ describe("Test with mock", () => {
},
});
const res = await rpc.getBlockEconomicState(BLOCK_HASH);
expect(axiosMock.mock.calls[0][0].data).toEqual({
expect(JSON.parse(axiosMock.mock.calls[0][1].body)).toEqual({
id,
jsonrpc: "2.0",
method: "get_block_economic_state",
Expand Down Expand Up @@ -564,7 +574,7 @@ describe("Test with mock", () => {
TRANSACTION_HASHES,
BLOCK_HASH
);
expect(axiosMock.mock.calls[0][0].data).toEqual({
expect(JSON.parse(axiosMock.mock.calls[0][1].body)).toEqual({
id,
jsonrpc: "2.0",
method: "get_transaction_proof",
Expand Down Expand Up @@ -609,7 +619,7 @@ describe("Test with mock", () => {
},
});
const res = await rpc.getTransactionProof(TRANSACTION_HASHES);
expect(axiosMock.mock.calls[0][0].data).toEqual({
expect(JSON.parse(axiosMock.mock.calls[0][1].body)).toEqual({
id,
jsonrpc: "2.0",
method: "get_transaction_proof",
Expand Down Expand Up @@ -652,7 +662,7 @@ describe("Test with mock", () => {
},
});
const res = await rpc.verifyTransactionProof(TRANSACTION_PROOF);
expect(axiosMock.mock.calls[0][0].data).toEqual({
expect(JSON.parse(axiosMock.mock.calls[0][1].body)).toEqual({
id,
jsonrpc: "2.0",
method: "verify_transaction_proof",
Expand Down Expand Up @@ -716,7 +726,7 @@ describe("Test with mock", () => {
});

const res = await rpc.getConsensus();
expect(axiosMock.mock.calls[0][0].data).toEqual({
expect(JSON.parse(axiosMock.mock.calls[0][1].body)).toEqual({
id,
jsonrpc: "2.0",
method: "get_consensus",
Expand Down Expand Up @@ -778,7 +788,7 @@ describe("Test with mock", () => {
},
});
const res = await rpc.getBlockchainInfo();
expect(axiosMock.mock.calls[0][0].data).toEqual({
expect(JSON.parse(axiosMock.mock.calls[0][1].body)).toEqual({
id,
jsonrpc: "2.0",
method: "get_blockchain_info",
Expand Down Expand Up @@ -822,7 +832,7 @@ describe("Test with mock", () => {
},
});
const res = await rpc.localNodeInfo();
expect(axiosMock.mock.calls[0][0].data).toEqual({
expect(JSON.parse(axiosMock.mock.calls[0][1].body)).toEqual({
id,
jsonrpc: "2.0",
method: "local_node_info",
Expand Down Expand Up @@ -859,7 +869,7 @@ describe("Test with mock", () => {
},
});
const res = await rpc.getPeers();
expect(axiosMock.mock.calls[0][0].data).toEqual({
expect(JSON.parse(axiosMock.mock.calls[0][1].body)).toEqual({
id,
jsonrpc: "2.0",
method: "get_peers",
Expand All @@ -877,7 +887,7 @@ describe("Test with mock", () => {
},
});
const res = await rpc.getTipBlockNumber();
expect(axiosMock.mock.calls[0][0].data).toEqual({
expect(JSON.parse(axiosMock.mock.calls[0][1].body)).toEqual({
id,
jsonrpc: "2.0",
method: "get_tip_block_number",
Expand All @@ -896,7 +906,7 @@ describe("Test with mock", () => {
},
});
const res = await rpc.getBlockHash("0x0");
expect(axiosMock.mock.calls[0][0].data).toEqual({
expect(JSON.parse(axiosMock.mock.calls[0][1].body)).toEqual({
id,
jsonrpc: "2.0",
method: "get_block_hash",
Expand Down Expand Up @@ -974,7 +984,7 @@ describe("Test with mock", () => {
});

const res = await rpc.getBlock(BLOCK_HASH);
expect(axiosMock.mock.calls[0][0].data).toEqual({
expect(JSON.parse(axiosMock.mock.calls[0][1].body)).toEqual({
id,
jsonrpc: "2.0",
method: "get_block",
Expand Down Expand Up @@ -1064,7 +1074,7 @@ describe("Test with mock", () => {
},
});
const res = await rpc.getTipHeader();
expect(axiosMock.mock.calls[0][0].data).toEqual({
expect(JSON.parse(axiosMock.mock.calls[0][1].body)).toEqual({
id,
jsonrpc: "2.0",
method: "get_tip_header",
Expand Down Expand Up @@ -1139,7 +1149,7 @@ describe("Test with mock", () => {
},
});
const res = await rpc.getTransaction(TX_HASH);
expect(axiosMock.mock.calls[0][0].data).toEqual({
expect(JSON.parse(axiosMock.mock.calls[0][1].body)).toEqual({
id,
jsonrpc: "2.0",
method: "get_transaction",
Expand Down Expand Up @@ -1218,7 +1228,7 @@ describe("Test with mock", () => {
},
});
const res = await rpc.getLiveCell(OUT_POINT, true);
expect(axiosMock.mock.calls[0][0].data).toEqual({
expect(JSON.parse(axiosMock.mock.calls[0][1].body)).toEqual({
id,
jsonrpc: "2.0",
method: "get_live_cell",
Expand Down Expand Up @@ -1261,7 +1271,7 @@ describe("Test with mock", () => {
},
});
const res = await rpc.getBannedAddresses();
expect(axiosMock.mock.calls[0][0].data).toEqual({
expect(JSON.parse(axiosMock.mock.calls[0][1].body)).toEqual({
id,
jsonrpc: "2.0",
method: "get_banned_addresses",
Expand All @@ -1279,7 +1289,7 @@ describe("Test with mock", () => {
},
});
const res = await rpc.clearBannedAddresses();
expect(axiosMock.mock.calls[0][0].data).toEqual({
expect(JSON.parse(axiosMock.mock.calls[0][1].body)).toEqual({
id,
jsonrpc: "2.0",
method: "clear_banned_addresses",
Expand All @@ -1298,7 +1308,7 @@ describe("Test with mock", () => {
},
});
const res = await rpc.setBan(...PARAMS);
expect(axiosMock.mock.calls[0][0].data).toEqual({
expect(JSON.parse(axiosMock.mock.calls[0][1].body)).toEqual({
id,
jsonrpc: "2.0",
method: "set_ban",
Expand All @@ -1325,7 +1335,7 @@ describe("Test with mock", () => {
},
});
const res = await rpc.syncState();
expect(axiosMock.mock.calls[0][0].data).toEqual({
expect(JSON.parse(axiosMock.mock.calls[0][1].body)).toEqual({
id,
jsonrpc: "2.0",
method: "sync_state",
Expand All @@ -1352,7 +1362,7 @@ describe("Test with mock", () => {
},
});
const res = await rpc.setNetworkActive(false);
expect(axiosMock.mock.calls[0][0].data).toEqual({
expect(JSON.parse(axiosMock.mock.calls[0][1].body)).toEqual({
id,
jsonrpc: "2.0",
method: "set_network_active",
Expand All @@ -1372,7 +1382,7 @@ describe("Test with mock", () => {
const PEER_ID = "peer id";
const ADDRESS = "address";
const res = await rpc.addNode(PEER_ID, ADDRESS);
expect(axiosMock.mock.calls[0][0].data).toEqual({
expect(JSON.parse(axiosMock.mock.calls[0][1].body)).toEqual({
id,
jsonrpc: "2.0",
method: "add_node",
Expand All @@ -1391,7 +1401,7 @@ describe("Test with mock", () => {
});
const PEER_ID = "peer id";
const res = await rpc.removeNode(PEER_ID);
expect(axiosMock.mock.calls[0][0].data).toEqual({
expect(JSON.parse(axiosMock.mock.calls[0][1].body)).toEqual({
id,
jsonrpc: "2.0",
method: "remove_node",
Expand All @@ -1409,7 +1419,7 @@ describe("Test with mock", () => {
},
});
const res = await rpc.pingPeers();
expect(axiosMock.mock.calls[0][0].data).toEqual({
expect(JSON.parse(axiosMock.mock.calls[0][1].body)).toEqual({
id,
jsonrpc: "2.0",
method: "ping_peers",
Expand Down Expand Up @@ -1446,7 +1456,7 @@ describe("Test with mock", () => {
},
});
const res = await rpc.getHeader(BLOCK_HASH);
expect(axiosMock.mock.calls[0][0].data).toEqual({
expect(JSON.parse(axiosMock.mock.calls[0][1].body)).toEqual({
id,
jsonrpc: "2.0",
method: "get_header",
Expand Down Expand Up @@ -1499,7 +1509,7 @@ describe("Test with mock", () => {
},
});
const res = await rpc.getHeaderByNumber(BLOCK_NUMBER);
expect(axiosMock.mock.calls[0][0].data).toEqual({
expect(JSON.parse(axiosMock.mock.calls[0][1].body)).toEqual({
id,
jsonrpc: "2.0",
method: "get_header_by_number",
Expand Down Expand Up @@ -1573,7 +1583,7 @@ describe("Test with mock", () => {
},
});
const res = await rpc.sendTransaction(tx, "passthrough");
expect(axiosMock.mock.calls[0][0].data).toEqual({
expect(JSON.parse(axiosMock.mock.calls[0][1].body)).toEqual({
id,
jsonrpc: "2.0",
method: "send_transaction",
Expand Down Expand Up @@ -2035,7 +2045,7 @@ describe("Test with mock", () => {
"\n",
JSON.stringify({ id, jsonrpc: "2.0", ...expectedParams })
);
expect(axiosMock.mock.calls[0][0].data).toEqual({
expect(JSON.parse(axiosMock.mock.calls[0][1].body)).toEqual({
id,
jsonrpc: "2.0",
...expectedParams,
Expand Down Expand Up @@ -2192,7 +2202,7 @@ describe("Test with mock", () => {
],
});
const res = await batch.exec();
expect(axiosMock.mock.calls[0][0].data).toEqual([
expect(JSON.parse(axiosMock.mock.calls[0][1].body)).toEqual([
{
id,
jsonrpc: "2.0",
Expand Down
25 changes: 0 additions & 25 deletions packages/rpc/__tests__/initAxiosWebworkerAdapter.test.js

This file was deleted.

Loading

2 comments on commit 93ff1b4

@vercel
Copy link

@vercel vercel bot commented on 93ff1b4 Nov 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀 New canary release: 0.0.0-canary-93ff1b4-20231109020124

npm install @ckb-lumos/lumos@0.0.0-canary-93ff1b4-20231109020124

Please sign in to comment.