Skip to content
This repository was archived by the owner on Dec 14, 2022. It is now read-only.

Commit 74810d3

Browse files
committed
migrate from fetch to axios
1 parent 4c1e87d commit 74810d3

File tree

3 files changed

+21
-30
lines changed

3 files changed

+21
-30
lines changed

jest.config.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module.exports = {
2+
// ...
3+
testTimeout: 60000
4+
}

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@oraichain/cosmosjs",
33
"licenses": [],
4-
"version": "0.1.4",
4+
"version": "0.1.5",
55
"description": "A JavasSript Open Source Library for Oraichain and possibly many other Cosmos network blockchains as well",
66
"main": "dist/index.js",
77
"repository": {
@@ -41,22 +41,22 @@
4141
"author": "Oraichain",
4242
"license": "MIT",
4343
"dependencies": {
44+
"axios": "^0.27.2",
4445
"bech32": "^1.1.3",
4546
"bip32": "^2.0.5",
4647
"bip39": "^3.0.3",
47-
"dotenv": "^8.2.0",
4848
"google-protobuf": "^3.14.0",
49-
"isomorphic-fetch": "^3.0.0",
5049
"protobufjs": "^6.10.2",
5150
"secp256k1": "^4.0.2"
5251
},
5352
"devDependencies": {
53+
"dotenv": "^8.2.0",
5454
"@babel/core": "^7.13.8",
5555
"@babel/plugin-transform-runtime": "^7.16.10",
5656
"@babel/preset-env": "^7.13.8",
5757
"@babel/register": "^7.13.8",
58-
"@cosmjs/proto-signing": "^0.28.10",
5958
"@cosmjs/amino": "^0.28.10",
59+
"@cosmjs/proto-signing": "^0.28.10",
6060
"@cosmjs/stargate": "^0.28.10",
6161
"babel-loader": "^8.1.0",
6262
"babel-preset-es2015": "^6.24.1",

src/index.js

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
/*
2-
Developed / Developing by Cosmostation
3-
[WARNING] CosmosJS is under ACTIVE DEVELOPMENT and should be treated as alpha version. We will remove this warning when we have a release that is stable, secure, and propoerly tested.
4-
*/
5-
import 'isomorphic-fetch';
61
import * as bip32 from 'bip32';
72
import * as bip39 from 'bip39';
83
import bech32 from 'bech32';
@@ -13,6 +8,9 @@ import { trimBuffer, hash160 } from './utils';
138
import WalletFactory from './wallet/walletFactory';
149
import WalletSigner from './wallet/walletSigner';
1510
import AminoTypes from './messages/amino';
11+
import Axios from 'axios';
12+
13+
const TIMEOUT = 30000;
1614

1715
export default class Cosmos {
1816
constructor(url, chainId, bech32MainPrefix = "orai", hdPath = "m/44'/118'/0'/0/0") {
@@ -21,6 +19,13 @@ export default class Cosmos {
2119
this.chainId = chainId;
2220
this.path = hdPath;
2321
this.bech32MainPrefix = bech32MainPrefix;
22+
this.axios = Axios.create({
23+
baseURL: this.url,
24+
headers: {
25+
Accept: 'application/json',
26+
},
27+
timeout: TIMEOUT,
28+
});
2429
}
2530

2631
setBech32MainPrefix(value) {
@@ -192,26 +197,12 @@ export default class Cosmos {
192197
return secp256k1.ecdsaSign(message, privKey).signature;
193198
}
194199

195-
async handleFetchResponse(response) {
196-
const contentType = response.headers.get("content-type");
197-
if (contentType && contentType.indexOf("application/json") !== -1) {
198-
return response.json();
199-
} else {
200-
let responseText = await response.text();
201-
throw { status: CONSTANTS.STATUS_CODE.GENERIC_ERROR, message: responseText }
202-
}
203-
}
204-
205200
get(path) {
206-
return fetch(`${this.url}${path}`).then((res) => this.handleFetchResponse(res));
201+
return this.axios.get(path).then((res) => res.data).catch(err => err.response.data);
207202
}
208203

209204
post(path, data) {
210-
return fetch(`${this.url}${path}`, {
211-
method: 'POST',
212-
headers: { 'Content-Type': 'application/json' },
213-
body: JSON.stringify(data)
214-
}).then((res) => this.handleFetchResponse(res));
205+
return this.axios.post(path, data).then((res) => res.data).catch(err => err.response.data);
215206
}
216207

217208
// "BROADCAST_MODE_UNSPECIFIED", "BROADCAST_MODE_BLOCK", "BROADCAST_MODE_SYNC", "BROADCAST_MODE_ASYNC"
@@ -360,11 +351,7 @@ export default class Cosmos {
360351
}
361352
}
362353

363-
return fetch(`${this.url}/cosmos/tx/v1beta1/simulate`, {
364-
method: 'POST',
365-
headers: { 'Content-Type': 'application/json' },
366-
body: JSON.stringify(simulateTx)
367-
}).then((res) => this.handleFetchResponse(res));
354+
return this.axios.post(`/cosmos/tx/v1beta1/simulate`, simulateTx).then((res) => res.data);
368355
}
369356
}
370357

0 commit comments

Comments
 (0)