# kwhitley/itty-router architecture

> Architecture diagram of the GitHub repository kwhitley/itty-router, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-01-18). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/kwhitley/itty-router
- Repository: https://github.com/kwhitley/itty-router

## Overview

##### Main Components

a) Core Router Components:
- IttyRouter (Base Router)
- AutoRouter (Enhanced Router)
- SharedRouter (Common Router Functionality)
- Router (Standard Router Implementation)

b) Middleware Layer:
- CORS Handler
- Error Handler
- Content Handlers (JSON, HTML, Text, Images)
- Cookie Handler
- Parameter Parser

c) Type System:
- Request/Response Types
- Router Types
- Handler Types
- Custom Route Types

**┌─ Application Layer ─────────────────────┐**
- Examples & Implementation               │
- ┬─────────────────────────┘
- ↓
**┌─ Router Layer ──────────────────────────┐**
- ┌─────────────┐  ┌─────────────┐       │
- IttyRouter  │  │ AutoRouter  │       │
- ┬──────┘  └──────┬──────┘       │
- ┬────┘               │
- ↓                    │
- ┌─────────────────────┐        │
- SharedRouter     │        │
- ┘        │
- ┬─────────────────────────┘
- ↓
**┌─ Middleware Layer ───────────────────────┐**
- ┌─────────┐ ┌───────┐ ┌──────────────┐  │
- CORS   │ │ Error │ │   Content    │  │
- ┘ └───────┘ └──────────────┘  │
- ┌─────────┐ ┌───────────────────────┐   │
- Cookies │ │    Parameter Parser    │   │
- ┘ └───────────────────────┘   │
- ┬─────────────────────────┘
- ↓
**┌─ Type System Layer ─────────────────────┐**
- Request/Response Types                  │
- Router Types                           │
- Handler Types                          │
- Custom Route Types                     │
- ┘

##### Important Elements to Highlight

a) Size Optimization:
- Minimal footprint (450-970 bytes) as a key feature
- Modular nature allowing for tree-shaking

b) Runtime Compatibility:
- Connections to different runtimes (Bun, Node.js, Cloudflare Workers)
- Serverless-first design

c) Type System Integration:
- How TypeScript types flow through the system
- Generic type constraints and customization points

##### Notes for Implementation

- Keep the core router implementation visually simple
- Middleware chain architecture
- How route matching flows through the system
- Where type checking occurs

Remember to maintain the balance between showing the technical sophistication of the system while emphasizing its minimal footprint and efficient design, as these are key selling points of the project.

## Diagram (Mermaid)

```mermaid
flowchart TB
    subgraph "Application Layer"
        APP[Application Implementation]
    end

    subgraph "Router Layer"
        IR["IttyRouter"]
        AR["AutoRouter"]
        SR["SharedRouter"]
        R["Router"]
        IR --> SR
        AR --> SR
        SR --> R
    end

    subgraph "Middleware Layer"
        CH["CORS Handler"]
        EH["Error Handler"]
        COH["Content Handlers"]
        CKH["Cookie Handler"]
        PP["Parameter Parser"]
        
        subgraph "Content Handler Types"
            JSON["JSON Handler"]
            HTML["HTML Handler"]
            TEXT["Text Handler"]
            
            subgraph "Image Handlers"
                JPEG["JPEG Handler"]
                PNG["PNG Handler"]
                WEBP["WebP Handler"]
            end
        end
    end

    subgraph "Type System Layer"
        subgraph "Request Types"
            IReq["IRequest"]
            IReqS["IRequestStrict"]
            ReqL["RequestLike"]
        end
        
        subgraph "Router Types"
            RT["RouterType"]
            IRT["IttyRouterType"]
            ART["AutoRouterType"]
        end
        
        subgraph "Handler Types"
            RH["RequestHandler"]
            ResH["ResponseHandler"]
        end
        
        subgraph "Route Types"
            Route["Route"]
            RE["RouteEntry"]
            CR["CustomRoutes"]
        end
    end

    subgraph "Additional Components"
        SE["Status Error"]
        WS["WebSocket Support"]
        RC["Response Creation"]
    end

    APP --> IR
    APP --> AR
    
    %% Component Mappings
    click IR "https://github.com/kwhitley/itty-router/blob/v5.x/src/IttyRouter.ts"
    click AR "https://github.com/kwhitley/itty-router/blob/v5.x/src/AutoRouter.ts"
    click R "https://github.com/kwhitley/itty-router/blob/v5.x/src/Router.ts"
    click CH "https://github.com/kwhitley/itty-router/blob/v5.x/src/cors.ts"
    click EH "https://github.com/kwhitley/itty-router/blob/v5.x/src/error.ts"
    click JSON "https://github.com/kwhitley/itty-router/blob/v5.x/src/json.ts"
    click HTML "https://github.com/kwhitley/itty-router/blob/v5.x/src/html.ts"
    click TEXT "https://github.com/kwhitley/itty-router/blob/v5.x/src/text.ts"
    click JPEG "https://github.com/kwhitley/itty-router/blob/v5.x/src/jpeg.ts"
    click PNG "https://github.com/kwhitley/itty-router/blob/v5.x/src/png.ts"
    click WEBP "https://github.com/kwhitley/itty-router/blob/v5.x/src/webp.ts"
    click CKH "https://github.com/kwhitley/itty-router/blob/v5.x/src/withCookies.ts"
    click PP "https://github.com/kwhitley/itty-router/blob/v5.x/src/withParams.ts"
    click IReq "https://github.com/kwhitley/itty-router/blob/v5.x/src/types/IRequest.ts"
    click IReqS "https://github.com/kwhitley/itty-router/blob/v5.x/src/types/IRequestStrict.ts"
    click ReqL "https://github.com/kwhitley/itty-router/blob/v5.x/src/types/RequestLike.ts"
    click RT "https://github.com/kwhitley/itty-router/blob/v5.x/src/types/RouterType.ts"
    click IRT "https://github.com/kwhitley/itty-router/blob/v5.x/src/types/IttyRouterType.ts"
    click ART "https://github.com/kwhitley/itty-router/blob/v5.x/src/types/AutoRouterType.ts"
    click RH "https://github.com/kwhitley/itty-router/blob/v5.x/src/types/RequestHandler.ts"
    click ResH "https://github.com/kwhitley/itty-router/blob/v5.x/src/types/ResponseHandler.ts"
    click Route "https://github.com/kwhitley/itty-router/blob/v5.x/src/types/Route.ts"
    click RE "https://github.com/kwhitley/itty-router/blob/v5.x/src/types/RouteEntry.ts"
    click CR "https://github.com/kwhitley/itty-router/blob/v5.x/src/types/CustomRoutes.ts"
    click SE "https://github.com/kwhitley/itty-router/blob/v5.x/src/StatusError.ts"
    click WS "https://github.com/kwhitley/itty-router/blob/v5.x/src/websocket.ts"
    click RC "https://github.com/kwhitley/itty-router/blob/v5.x/src/createResponse.ts"

    classDef default fill:#f9f9f9,stroke:#333,stroke-width:1px
    classDef router fill:#d4e6f1,stroke:#2874a6
    classDef middleware fill:#d4efdf,stroke:#27ae60
    classDef types fill:#fcf3cf,stroke:#d4ac0d
    classDef additional fill:#e8daef,stroke:#8e44ad

    class IR,AR,SR,R router
    class CH,EH,COH,CKH,PP,JSON,HTML,TEXT,JPEG,PNG,WEBP middleware
    class IReq,IReqS,ReqL,RT,IRT,ART,RH,ResH,Route,RE,CR types
    class SE,WS,RC additional
```
