# ptol/typed-json-rpc architecture

> Architecture diagram of the GitHub repository ptol/typed-json-rpc, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2024-12-28). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/ptol/typed-json-rpc
- Repository: https://github.com/ptol/typed-json-rpc

## Overview

##### Main Components

a) Core Library Components:
- JSON-RPC Client Factory
- Request Handler Factory
- Request Creator
- Type System Interface

b) Transport Layer Abstractions:
- HTTP Transport
- Worker Transport
- EventEmitter Transport

**┌─ Application Layer ─────────────────────┐**
- Client Applications                     │
- ┬─────────────────────────┘

**┌─ API Layer ────▼────────────────────────┐**
- TypeScript Interface Definitions        │
- ┬─────────────────────────┘

**┌─ Core Layer ───▼────────────────────────┐**
- ┌─────────────────────┐ ┌────────────┐  │
- JSON-RPC Client   │ │  Request   │  │
- Factory        │ │  Handler   │  │
- ┘ └────────────┘  │
- ┬─────────────────────────┘

**┌─ Transport ───▼────────────────────────┐**
- ┌─────┐  ┌──────┐  ┌───────────────┐  │
- HTTP │  │Worker│  │EventEmitter   │  │
- ┘  └──────┘  └───────────────┘  │
- ┘

##### Important Elements to Highlight

a) Type System:
- How TypeScript interfaces (`Api`) flow through the system
- Demonstrate type safety between client and server

b) Request/Response Flow:
- JSON-RPC message flow
- How requests are transformed and handled

##### Component Relationships

- How `createJsonRpcClient` connects to transport senders
- How `createRequestHandler` processes incoming requests
- How type definitions ensure end-to-end type safety

##### Implementation Details

Notes about:
- Type-safe request/response handling
- Transport layer abstraction
- Error handling flow

## Diagram (Mermaid)

```mermaid
flowchart TB
    subgraph "Application Layer"
        Client[Client Applications]
    end

    subgraph "API Layer"
        TypeDefs["TypeScript Interface Definitions"]:::types
        click TypeDefs "https://github.com/ptol/typed-json-rpc/blob/main/examples/src/api.ts"
    end

    subgraph "Core Layer"
        ClientFactory["JSON-RPC Client Factory"]:::core
        click ClientFactory "https://github.com/ptol/typed-json-rpc/blob/main/src/createJsonRpcClient.ts"
        
        RequestHandler["Request Handler Factory"]:::core
        click RequestHandler "https://github.com/ptol/typed-json-rpc/blob/main/src/createRequestHandler.ts"
        
        RequestCreator["Request Creator"]:::core
        click RequestCreator "https://github.com/ptol/typed-json-rpc/blob/main/src/createJsonRpcRequest.ts"
        
        TypeSystem["Type System Interface"]:::core
        click TypeSystem "https://github.com/ptol/typed-json-rpc/blob/main/src/types.ts"
        
        BasicClient["Basic JSON-RPC Client"]:::core
        click BasicClient "https://github.com/ptol/typed-json-rpc/blob/main/src/createBasicJsonRpcClient.ts"
    end

    subgraph "Transport Layer"
        direction TB
        
        subgraph "HTTP Transport"
            direction TB
            HttpSender["HTTP Sender"]:::transport
            click HttpSender "https://github.com/ptol/typed-json-rpc/blob/main/examples/src/http/httpSender.ts"
            HttpServer["HTTP Server"]:::transport
            click HttpServer "https://github.com/ptol/typed-json-rpc/blob/main/examples/src/http/httpServer.ts"
        end
        
        subgraph "Worker Transport"
            direction TB
            WorkerSender["Worker Sender"]:::transport
            click WorkerSender "https://github.com/ptol/typed-json-rpc/blob/main/examples/src/worker/workerSender.ts"
            WorkerImpl["Worker Implementation"]:::transport
            click WorkerImpl "https://github.com/ptol/typed-json-rpc/blob/main/examples/src/worker/worker.ts"
        end
        
        subgraph "EventEmitter Transport"
            EventSender["Event Sender"]:::transport
            click EventSender "https://github.com/ptol/typed-json-rpc/blob/main/examples/src/events/eventSender.ts"
        end
    end

    Client --> TypeDefs
    TypeDefs --> ClientFactory
    TypeDefs --> RequestHandler
    
    ClientFactory --> RequestCreator
    ClientFactory --> TypeSystem
    ClientFactory --> BasicClient
    
    RequestHandler --> TypeSystem
    
    BasicClient -.-> HttpSender
    BasicClient -.-> WorkerSender
    BasicClient -.-> EventSender
    
    HttpSender --> HttpServer
    WorkerSender --> WorkerImpl

    classDef core fill:#2196F3,stroke:#1565C0,color:white
    classDef transport fill:#4CAF50,stroke:#388E3C,color:white
    classDef types fill:#FFC107,stroke:#FFA000,color:black

    %% Legend
    subgraph Legend
        CoreComponent["Core Component"]:::core
        TransportComponent["Transport Component"]:::transport
        TypeComponent["Type Definition"]:::types
    end
```
