How nurekeshka/stock-market-api works
Project Type and Purpose
• This is a backend API service (“Stock Market API”) built with NestJS.
• It consumes real-time market data from Finnhub IO via WebSockets and provides REST endpoints for account and transaction management.
• It follows a modular, layered architecture using NestJS modules and several shared libraries.
File Structure Analysis
• Top-level folders:
• src/ – application entry point (main.ts) and domain modules (accounts, auth).
• libs/ – reusable NestJS libraries:
– common: error handlers, validation pipes, guards, interceptors
– configuration: centralized config loading (base.json, test.json)
– database: NestJS DatabaseModule with in-memory and (presumably) real DB adapters
– finnhub: connectors/gateway/service for external Finnhub IO integration
• Tests: unit tests located alongside code, end-to-end tests under test/.
README Insights
• Describes “Stock Market API” built on NestJS, outlines architecture overview in docs/systems.png.
• Uses JWT auth, Mongoose schemas for accounts, holdings, transactions.
• External dependency: Finnhub IO WebSocket API for live trade data.
Main Components
a. API Layer (NestJS App)
• Entry point: src/main.ts → boots AppModule.
• HTTP Server: handles REST controllers (AccountsController, TransactionsController, AuthController).
• WebSocket Gateway: FinnhubGateway listens for market data events and forwards to services.
b. Domain Modules
• AuthModule: JWT strategy, hash strategy, AuthService.
• AccountsModule: AccountsService, HoldingsService, TransactionsService.
c. Shared Libraries (NestJS Modules)
• CommonModule: validation pipes, error handlers, interceptors, guards.
• ConfigurationModule: loads environment/config JSONs.
• DatabaseModule: abstracts DB connectivity (MongoDB via Mongoose or in-memory for tests).
• FinnhubModule: FinnhubConnector (WS client), FinnhubService handles data normalization.
d. Persistence
• MongoDB (or other configured DB) with collections: accounts, holdings, transactions.
e. External Services
• Finnhub IO WebSocket API (live trades).
• (Potential) Third-party REST APIs if needed.
f. Clients
• Frontend or CLI (not shown in repo) consumes public API endpoints.
• E2E tests (test/auth.e2e-spec.ts) as a consumer.
Relationships and Interactions
• HTTP client → NestJS HTTP controllers → Domain services → DatabaseModule → MongoDB.
• Finnhub WebSocket → FinnhubConnector → FinnhubGateway events → FinnhubService → downstream modules (e.g., update holdings or broadcast).
• Auth flows: client logs in → AuthController → AuthService (validates, issues JWT) → subsequent requests go through JWT guard in CommonModule.
• ConfigurationModule provides config to all modules via DI (e.g., DB connection URI, Finnhub API key).
Architectural Patterns and Principles
• Modular Monorepo: shared libs + domain modules.
• Layered architecture: Controllers → Services → Repositories (DatabaseModule) → External Connectors.
• Dependency Injection (NestJS).
• Single Responsibility: each module/library has clear purpose (config, common utilities, external integration).
• Event-driven stream handling for market data via WebSocket Gateway.