Skip to content

Commit

Permalink
better types, small fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
mlibre committed May 25, 2024
1 parent 8011106 commit 61a778b
Show file tree
Hide file tree
Showing 11 changed files with 15 additions and 28 deletions.
7 changes: 4 additions & 3 deletions dist/API/app.js

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

2 changes: 1 addition & 1 deletion dist/API/app.js.map

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

2 changes: 1 addition & 1 deletion dist/API/blockchain.js

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

2 changes: 1 addition & 1 deletion dist/API/blockchain.js.map

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

5 changes: 0 additions & 5 deletions dist/API/routes/block.js

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

2 changes: 1 addition & 1 deletion dist/API/routes/block.js.map

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

9 changes: 5 additions & 4 deletions src/API/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,22 @@ function onError ( error: NodeJS.ErrnoException )
{
throw error;
}
// handle specific listen errors with friendly messages
switch ( error.code )
{
case "EACCES":
console.error( `${hostPort} requires elevated privileges` );
break;
process.exit( 1 );
// eslint-disable-next-line no-fallthrough
case "EADDRINUSE":
console.error( `${hostPort} is already in use` );
break;
process.exit( 1 );
// eslint-disable-next-line no-fallthrough
default:
throw error;
}
}

function errorHandler ( err: CustomError, req: express.Request, res: express.Response, next: express.NextFunction )
function errorHandler ( err: SimpleError, req: express.Request, res: express.Response, next: express.NextFunction )
{
if ( res.headersSent )
{
Expand Down
2 changes: 1 addition & 1 deletion src/API/blockchain.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import Blockchain from "../library/main.js";
import Consensus from "../library/pow-consensus.js";
const consensus = new Consensus();
import { initJsonFile } from "../library/utils.js";
import Wallet from "../library/wallet.js";
import { name, dbPath, minerKeysFile, hostUrl, nodesList } from "./config.js";
const consensus = new Consensus();

const minerKeys = initJsonFile( minerKeysFile, Wallet.generateKeyPair() );
export default new Blockchain({
Expand Down
5 changes: 0 additions & 5 deletions src/API/routes/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,18 @@ router.get( "/", function ( req, res )
if ( !index && !from && !to && !list && !firstAndLast )
{
res.json( blockchain.chain.latestBlock );
return;
}
else if ( index )
{
const blockIndex = toNum( index );
res.json( blockchain.chain.get( blockIndex ) );
return;
}
else if ( from || to )
{
const blockFrom = toNum( from );
const blockTo = toNum( to );
const blocks = blockchain.getBlocks( blockFrom, blockTo );
res.json( blocks );
return;
}
else if ( list )
{
Expand All @@ -37,15 +34,13 @@ router.get( "/", function ( req, res )
blocks.push( blockchain.chain.get( blcokIndex ) );
}
res.json( blocks );
return;
}
else if ( firstAndLast )
{
const blocks = [];
blocks.push( blockchain.chain.get( 0 ) );
blocks.push( blockchain.chain.latestBlock );
res.json( blocks );
return;
}
});

Expand Down
2 changes: 1 addition & 1 deletion src/API/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export function toNum ( value: unknown ): number
return Number( value );
}

export function convertErrorToSimpleObj ( err: CustomError )
export function convertErrorToSimpleObj ( err: SimpleError )
{
if ( err.isAxiosError )
{
Expand Down
5 changes: 0 additions & 5 deletions src/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,6 @@ interface ErrorWithStdsOutErr extends Error
status?: number;
}

interface CustomError extends Error
{
[x: string]: CustomError;
}

interface SimpleError
{
message?: string;
Expand Down

0 comments on commit 61a778b

Please sign in to comment.