Skip to content

Commit

Permalink
refactor: remove the need for axios
Browse files Browse the repository at this point in the history
  • Loading branch information
thepiwo committed Jan 9, 2023
1 parent e88b1df commit ed6cea0
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 84 deletions.
55 changes: 0 additions & 55 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
"license": "ISC",
"dependencies": {
"@aeternity/aepp-sdk": "^12.1.3",
"axios": "^1.2.2",
"commander": "^9.5.0",
"promisify-child-process": "^4.1.1",
"prompts": "^2.4.2"
Expand Down
26 changes: 1 addition & 25 deletions src/lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const fs = require('fs');
const path = require('path');
const http = require('http');
const { AeSdk, MemoryAccount, Node } = require('@aeternity/aepp-sdk');

const networks = require('./networks.json');
const wallets = require('./wallets.json');
const { get } = require('../utils/utils');

const getContractContent = (contractPath) => fs.readFileSync(contractPath, 'utf8');

Expand Down Expand Up @@ -49,30 +49,6 @@ const getFilesystem = (contractPath) => {
return filesystem;
};

async function get(url) {
return new Promise((resolve, reject) => {
// eslint-disable-next-line consistent-return
const req = http.request(url, { method: 'GET' }, (res) => {
if (res.statusCode < 200 || res.statusCode > 299) {
return reject(new Error(`HTTP status code ${res.statusCode}`));
}

const body = [];
res.on('data', (chunk) => body.push(chunk));
res.on('end', () => resolve(Buffer.concat(body).toString()));
});

req.on('error', (err) => reject(err));

req.on('timeout', () => {
req.destroy();
reject(new Error('Request time out'));
});

req.end();
});
}

const getDefaultAccounts = () => wallets.map((keypair) => new MemoryAccount({ keypair }));

const getSdk = async () => {
Expand Down
28 changes: 25 additions & 3 deletions src/utils/utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const axios = require('axios');

const http = require('http');
const config = require('../config/config.json');

// eslint-disable-next-line no-promise-executor-return
Expand All @@ -12,12 +11,34 @@ const awaitNodeAvailable = async (interval = 200, attempts = 100) => {
// eslint-disable-next-line no-await-in-loop
if (i) await pause(interval);
// eslint-disable-next-line no-await-in-loop
result = await axios.get('http://localhost:3001/v3/status').catch(() => null);
result = await get('http://localhost:3001/v3/status').catch(() => null);
if (result) return;
}
throw new Error('timed out waiting for node to come up');
};

const get = async (url) => new Promise((resolve, reject) => {
// eslint-disable-next-line consistent-return
const req = http.request(url, { method: 'GET' }, (res) => {
if (res.statusCode < 200 || res.statusCode > 299) {
return reject(new Error(`HTTP status code ${res.statusCode}`));
}

const body = [];
res.on('data', (chunk) => body.push(chunk));
res.on('end', () => resolve(Buffer.concat(body).toString()));
});

req.on('error', (err) => reject(err));

req.on('timeout', () => {
req.destroy();
reject(new Error('Request time out'));
});

req.end();
});

const getNetwork = (network) => {
const networks = {
local: {
Expand Down Expand Up @@ -45,4 +66,5 @@ module.exports = {
config,
getNetwork,
awaitNodeAvailable,
get,
};

0 comments on commit ed6cea0

Please sign in to comment.