Skip to content

Commit

Permalink
Add prettier, lint, workflows and fixes for API
Browse files Browse the repository at this point in the history
  • Loading branch information
ArturDolzan committed Jan 3, 2025
1 parent 1aa7b1d commit 4fcf15e
Show file tree
Hide file tree
Showing 27 changed files with 548 additions and 428 deletions.
15 changes: 15 additions & 0 deletions .github/workflows/js-checks.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: JS Checks

on:
pull_request:
push:

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}-${{ github.event_name }}
cancel-in-progress: true

jobs:
js-checks:
uses: hemilabs/actions/.github/workflows/js-checks.yml@main
with:
node-versions: '["16", "18", "20", "22"]'
10 changes: 0 additions & 10 deletions apps/api/.eslintrc.js

This file was deleted.

26 changes: 26 additions & 0 deletions apps/api/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"extends": ["bloq", "prettier"],
"ignorePatterns": ["dist", "_esm/*", "_cjs/*", "_types/*"],
"overrides": [
{
"extends": ["bloq/typescript", "prettier"],
"files": ["src/**/*.ts"]
},
{
"extends": ["bloq/markdown"],
"files": ["*.md"]
},
{
"extends": ["bloq/vitest", "prettier"],
"files": ["*.spec.{js,ts}"]
}
],
"parserOptions": {
"sourceType": "module"
},
"root": true,
"rules": {
"arrow-body-style": "off",
"no-console": "off"
}
}
7 changes: 7 additions & 0 deletions apps/api/.prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"arrowParens": "avoid",
"quoteProps": "consistent",
"semi": false,
"singleQuote": true,
"tabWidth": 2
}
3 changes: 2 additions & 1 deletion apps/api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ The environment variables are defined in the `.env` file. The following variable

- `ENABLE_MAINNET`: A boolean that enables mainnet.[Hemi Docs](https://github.com/hemilabs/infrastructure/blob/main/NETWORK_INFO.md).


Example of the .env file

```
Expand All @@ -99,8 +98,10 @@ ENABLE_MAINNET=false
```

## Contribution

If you want to contribute to this project and make it better, your help is very welcome.
You can find more information about how to contribute in the [`CONTRIBUTING.md`](../../CONTRIBUTING.md) file.

## License

This project is licensed under the MIT License - see the [LICENSE](../../LICENSE) file for details.
11 changes: 8 additions & 3 deletions apps/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@
"test:cov": "vitest run src --coverage",
"test:watch": "vitest watch src",
"dev": "nodemon",
"format:check": "prettier --check .",
"format:fix": "npx prettier --write .",
"build": "tsc -p tsconfig.build.json",
"lint": "eslint . --ext ts --report-unused-disable-directives --max-warnings 0",
"lint": "eslint . --ext ts",
"start:api": "node dist/server.js"
},
"author": "",
"license": "MIT",
"dependencies": {
"@cryptochords/shared": "*",
"@typescript-eslint/eslint-plugin": "8.18.0",
"dotenv": "16.4.7",
"express": "4.21.2",
"web3": "4.16.0"
Expand All @@ -27,8 +28,12 @@
"@types/node": "22.10.2",
"@types/ws": "8.5.13",
"@vitest/coverage-v8": "2.1.8",
"eslint": "8.56.0",
"@typescript-eslint/eslint-plugin": "^8.18.2",
"@typescript-eslint/parser": "^8.18.2",
"eslint": "^8.57.1",
"eslint-config-bloq": "^4.4.1",
"nodemon": "3.1.9",
"prettier": "^3.3.3",
"ts-node": "10.9.2",
"typescript": "5.3.3",
"vitest": "2.1.8"
Expand Down
14 changes: 7 additions & 7 deletions apps/api/src/application/helpers/BroadcastToClients.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import WebSocket, { WebSocketServer } from 'ws';
import { L2Block } from '@cryptochords/shared';
import WebSocket, { WebSocketServer } from 'ws'
import { L2Block } from '@cryptochords/shared'

const BroadcastToClients = (wss: WebSocketServer, l2Block: L2Block): void => {
const broadcastToClients = (wss: WebSocketServer, l2Block: L2Block): void => {
wss.clients.forEach((client: WebSocket) => {
if (client.readyState === WebSocket.OPEN) {
client.send(JSON.stringify(l2Block.toJSON()));
client.send(JSON.stringify(l2Block.toJSON()))
}
});
};
})
}

export default BroadcastToClients
export default broadcastToClients
24 changes: 12 additions & 12 deletions apps/api/src/application/helpers/broadcastToClients.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { describe, it, expect, vi } from 'vitest';
import WebSocket, { WebSocketServer } from 'ws';
import BroadcastToClients from './BroadcastToClients';
import { describe, it, expect, vi } from 'vitest'
import WebSocket, { WebSocketServer } from 'ws'
import broadcastToClients from './broadcastToClients'

describe('BroadcastToClients', () => {
it('should send a message to all connected clients', () => {
const mockSend = vi.fn();
const wss: any = new WebSocketServer({ noServer: true });
const mockSend = vi.fn()
const wss: any = new WebSocketServer({ noServer: true })
wss.clients = new Set([
{
readyState: WebSocket.OPEN,
Expand All @@ -16,15 +16,15 @@ describe('BroadcastToClients', () => {
readyState: WebSocket.CLOSED,
send: mockSend,
},
]);
])

const l2Block = {
toJSON: () => ({ key: 'value' }),
};
}

BroadcastToClients(wss, l2Block as any);
broadcastToClients(wss, l2Block as any)

expect(mockSend).toHaveBeenCalledTimes(1);
expect(mockSend).toHaveBeenCalledWith(JSON.stringify({ key: 'value' }));
});
});
expect(mockSend).toHaveBeenCalledTimes(1)
expect(mockSend).toHaveBeenCalledWith(JSON.stringify({ key: 'value' }))
})
})
62 changes: 31 additions & 31 deletions apps/api/src/application/polling/pollingService.spec.ts
Original file line number Diff line number Diff line change
@@ -1,49 +1,49 @@
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { WebSocketServer } from 'ws';
import { EventEmitter } from 'events';
import BroadcastToClients from '../helpers/BroadcastToClients';
import { TxTypesEnum } from '@cryptochords/shared';
import { PollingService } from './pollingService';

vi.mock('ws');
import { describe, it, expect, vi, beforeEach } from 'vitest'
import { WebSocketServer } from 'ws'
import { EventEmitter } from 'events'
import broadcastToClients from '../helpers/broadcastToClients'
import { TxTypesEnum } from '@cryptochords/shared'
import { PollingService } from './pollingService'

vi.mock('ws')
vi.mock('../helpers/BroadcastToClients', () => ({
__esModule: true,
default: vi.fn(),
}));
}))

class MockBlockRepository extends EventEmitter {
execute = vi.fn();
stop = vi.fn();
execute = vi.fn()
stop = vi.fn()
}

describe('PollingService', () => {
let pollingService: PollingService;
let mockBlockRepository: MockBlockRepository;
let mockWss: WebSocketServer;
let pollingService: PollingService
let mockBlockRepository: MockBlockRepository
let mockWss: WebSocketServer

beforeEach(() => {
mockBlockRepository = new MockBlockRepository();
mockWss = new WebSocketServer({ noServer: true });
pollingService = new PollingService(mockBlockRepository);
});
mockBlockRepository = new MockBlockRepository()
mockWss = new WebSocketServer({ noServer: true })
pollingService = new PollingService(mockBlockRepository)
})

it('should execute and handle Block and Eth events', () => {
const websocketUrl = 'ws://test.url';
const mockL2Block = { key: 'Value' };
const websocketUrl = 'ws://test.url'
const mockL2Block = { key: 'Value' }

pollingService.execute(mockWss, websocketUrl);
pollingService.execute(mockWss, websocketUrl)

expect(mockBlockRepository.execute).toHaveBeenCalledWith(websocketUrl);
expect(mockBlockRepository.execute).toHaveBeenCalledWith(websocketUrl)

mockBlockRepository.emit(TxTypesEnum.Block, mockL2Block);
mockBlockRepository.emit(TxTypesEnum.Eth, mockL2Block);
mockBlockRepository.emit(TxTypesEnum.Block, mockL2Block)
mockBlockRepository.emit(TxTypesEnum.Eth, mockL2Block)

expect(BroadcastToClients).toHaveBeenCalledWith(mockWss, mockL2Block);
expect(BroadcastToClients).toHaveBeenCalledTimes(2);
});
expect(broadcastToClients).toHaveBeenCalledWith(mockWss, mockL2Block)
expect(broadcastToClients).toHaveBeenCalledTimes(2)
})

it('should stop the repository on stop', () => {
pollingService.stop();
expect(mockBlockRepository.stop).toHaveBeenCalled();
});
});
pollingService.stop()
expect(mockBlockRepository.stop).toHaveBeenCalled()
})
})
20 changes: 12 additions & 8 deletions apps/api/src/application/polling/pollingService.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
import { WebSocketServer } from "ws";
import { BlockRepository } from "../../domain/repositories/BlockRepository";
import BroadcastToClients from "../helpers/BroadcastToClients";
import { TxTypesEnum } from "@cryptochords/shared";
import { L2Block } from "@cryptochords/shared";
import { WebSocketServer } from 'ws'
import { BlockRepository } from '../../domain/repositories/BlockRepository'
import broadcastToClients from '../helpers/broadcastToClients'
import { TxTypesEnum } from '@cryptochords/shared'
import { L2Block } from '@cryptochords/shared'
export class PollingService {
constructor(private blockRepository: BlockRepository) {}

execute(wss: WebSocketServer, url: string): void {
this.blockRepository.execute(url)

this.blockRepository.on(TxTypesEnum.Block, (l2Block: L2Block) => BroadcastToClients(wss, l2Block));
this.blockRepository.on(TxTypesEnum.Eth, (l2Block: L2Block) => BroadcastToClients(wss, l2Block));
this.blockRepository.on(TxTypesEnum.Block, (l2Block: L2Block) =>
broadcastToClients(wss, l2Block),
)
this.blockRepository.on(TxTypesEnum.Eth, (l2Block: L2Block) =>
broadcastToClients(wss, l2Block),
)
}

stop() {
this.blockRepository.stop()
}
}
}
2 changes: 1 addition & 1 deletion apps/api/src/domain/base/Entity.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe('src/domain/base/Entity', () => {
})

it('should be an instance of ValueObject', () => {
const entity = TestEntity.create({ value: 'test'})
const entity = TestEntity.create({ value: 'test' })

expect(entity).toBeInstanceOf(ValueObject)
})
Expand Down
18 changes: 9 additions & 9 deletions apps/api/src/domain/enums/BlockTypesEnum.spec.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { describe, it, expect } from 'vitest';
import { BlockTypesEnum } from './BlockTypesEnum';
import { describe, it, expect } from 'vitest'
import { BlockTypesEnum } from './BlockTypesEnum'

describe('BlockTypesEnum', () => {
it('should have a LEGACY type with value "0"', () => {
expect(BlockTypesEnum.LEGACY).toBe('0');
});
expect(BlockTypesEnum.LEGACY).toBe('0')
})

it('should have an EIP2930 type with value "1"', () => {
expect(BlockTypesEnum.EIP2930).toBe('1');
});
expect(BlockTypesEnum.EIP2930).toBe('1')
})

it('should have an EIP1559 type with value "2"', () => {
expect(BlockTypesEnum.EIP1559).toBe('2');
});
});
expect(BlockTypesEnum.EIP1559).toBe('2')
})
})
2 changes: 1 addition & 1 deletion apps/api/src/domain/repositories/BlockRepository.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EventEmitter } from 'events';
import { EventEmitter } from 'events'

export interface BlockRepository extends EventEmitter {
execute(url: string): void
Expand Down
Loading

0 comments on commit 4fcf15e

Please sign in to comment.