# coobaha/typed-fastify architecture

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

- Interactive diagram: https://gitdiagram.com/coobaha/typed-fastify
- Repository: https://github.com/coobaha/typed-fastify

## Overview

##### Main Components

a) Core Components:
- Schema Definition Layer
- Type Inference Engine
- JSON Schema Generator
- Runtime Validation Layer
- Fastify Integration Layer

b) Supporting Components:
- CLI Tool (tfs)
- Watch Mode System
- Documentation Generator

**┌─ Application Layer ─────────────────┐**
- User's Fastify Application         │
- ┬───────────────────┘
- ↓
**┌─ Typed Fastify Layer ──────────────┐**
- ┌─────────────┐    ┌────────────┐  │
- Schema    │←→│    Type     │  │
- Definition  │    │  Inference │  │
- ┘    └────────────┘  │
- ↓               ↓          │
- ┌─────────────┐    ┌────────────┐  │
- JSON Schema  │←→│  Runtime    │  │
- Generator   │    │ Validation │  │
- ┘    └────────────┘  │
- ┬───────────────────┘
- ↓
**┌─ Fastify Layer ──────────────────┐**
- Fastify Framework               │
- ┘

##### Key Interactions to Highlight

a) Development Time Flow:
- TypeScript Schema Definition → Type Inference
- TypeScript Schema → JSON Schema Generation
- CLI Tool interaction with Schema Generation

b) Runtime Flow:
- Request → Schema Validation
- Response → Type Checking
- Runtime Validation → Fastify Integration

##### Important Details

a) Type System Components:
- Request handling (body, headers, querystring, params)
- Response handling (status-based typing)
- Path parameter inference

b) Schema Generation:
- typescript-json-schema integration
- Custom transforms
- @tjs annotations support

**┌─ CLI Tools ──────────────┐**
- ┌─────────┐ ┌─────────┐ │
- tfs   │ │ Watch  │ │
- gen   │ │  Mode  │ │
- ┘ └─────────┘ │
- ┘

##### Implementation Notes

- Small annotations explaining key transformations
- Version information and compatibility notes

## Diagram (Mermaid)

```mermaid
flowchart TB
    subgraph "Application Layer"
        UserApp["User's Fastify Application"]
    end

    subgraph "Typed Fastify Layer"
        direction TB
        SchemaDefinition["Schema Definition"]:::typescript
        TypeInference["Type Inference Engine"]:::typescript
        JSONSchemaGen["JSON Schema Generator"]:::runtime
        RuntimeValidation["Runtime Validation"]:::runtime
        
        SchemaDefinition <--> TypeInference
        SchemaDefinition --> JSONSchemaGen
        JSONSchemaGen <--> RuntimeValidation
    end

    subgraph "CLI Tools"
        direction TB
        TFSGen["TFS Generator"]:::tool
        WatchMode["Watch Mode"]:::tool
    end

    subgraph "Testing & Validation"
        direction TB
        SchemaExamples["Schema Examples"]:::test
        TypeValidation["Type System Validation"]:::test
        IntegrationTests["Integration Tests"]:::test
    end

    subgraph "Documentation"
        Docs["Documentation"]:::docs
    end

    subgraph "Fastify Layer"
        FastifyFramework["Fastify Framework"]:::framework
    end

    UserApp --> SchemaDefinition
    TypeInference --> RuntimeValidation
    RuntimeValidation --> FastifyFramework
    TFSGen --> JSONSchemaGen
    WatchMode --> JSONSchemaGen

    %% Click events for component mapping
    click SchemaDefinition "https://github.com/coobaha/typed-fastify/blob/main/src/schema.ts"
    click TypeInference "https://github.com/coobaha/typed-fastify/blob/main/src/type-utils.ts"
    click JSONSchemaGen "https://github.com/coobaha/typed-fastify/blob/main/generator/gen.ts"
    click RuntimeValidation "https://github.com/coobaha/typed-fastify/blob/main/test/integration.test.ts"
    click WatchMode "https://github.com/coobaha/typed-fastify/blob/main/generator/watcher.ts"
    click TFSGen "https://github.com/coobaha/typed-fastify/blob/main/generator/gen.bin.ts"
    click Docs "https://github.com/coobaha/typed-fastify/tree/main/docs/"
    click SchemaExamples "https://github.com/coobaha/typed-fastify/blob/main/test/test_schema.ts"
    click TypeValidation "https://github.com/coobaha/typed-fastify/blob/main/test/typed-fastify.test-d.ts"
    click IntegrationTests "https://github.com/coobaha/typed-fastify/blob/main/test/integration.test.ts"

    %% Styling
    classDef typescript fill:#3178c6,color:white
    classDef runtime fill:#4caf50,color:white
    classDef framework fill:#ff9800,color:white
    classDef tool fill:#607d8b,color:white
    classDef test fill:#9c27b0,color:white
    classDef docs fill:#795548,color:white

    %% Legend
    subgraph Legend
        L1["TypeScript Components"]:::typescript
        L2["Runtime Components"]:::runtime
        L3["Framework Integration"]:::framework
        L4["Development Tools"]:::tool
        L5["Testing Components"]:::test
        L6["Documentation"]:::docs
    end
```
