# trpc/trpc-openapi architecture

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

- Interactive diagram: https://gitdiagram.com/trpc/trpc-openapi
- Repository: https://github.com/trpc/trpc-openapi

## Overview

##### Main Components

a. Core Components:
- tRPC Router Integration Layer
- OpenAPI Generator
- HTTP Adapters Layer
- Type System (Zod Integration)

b. Supported Framework Adapters:
- Express Adapter
- Next.js Adapter
- Fastify Adapter
- AWS Lambda Adapter
- Nuxt Adapter
- Node HTTP Adapter
- Standalone Adapter

**┌─────────────────────────────────────────────────────┐**
- Client Applications                 │
- ┘
- ↓ ↑
**┌─────────────────────────────────────────────────────┐**
- Framework Layer                    │
- ┌─────┐  ┌────────┐  ┌───────┐  ┌──────────────┐  │
- Express│  │Next.js │  │Fastify│  │Other Adapters│  │
- ┘  └────────┘  └───────┘  └──────────────┘  │
- ┘
- ↓ ↑
**┌─────────────────────────────────────────────────────┐**
- trpc-openapi Core Layer               │
- ┌─────────────────┐      ┌────────────────────┐   │
- HTTP Handler   │←────→│  OpenAPI Generator │   │
- ┘      └────────────────────┘   │
- ┌─────────────────┐      ┌────────────────────┐   │
- Request Parser  │←────→│  Response Formatter │   │
- ┘      └────────────────────┘   │
- ┘
- ↓ ↑
**┌─────────────────────────────────────────────────────┐**
- tRPC Layer                       │
- ┌─────────────┐  ┌──────────────┐  ┌──────────┐   │
- tRPC Router  │←→│ Procedures   │←→│  Context │   │
- ┘  └──────────────┘  └──────────┘   │
- ┘
- ↓ ↑
**┌─────────────────────────────────────────────────────┐**
- Validation Layer (Zod)                │
- ┘

##### Important Elements to Highlight

a. Request Flow:
- How requests flow from clients through the adapters
- Transformation from REST to tRPC procedures
- Demonstrate validation and context handling

b. OpenAPI Generation:
- Generation of OpenAPI documentation
- Metadata processing
- Schema generation from Zod types

c. Security Layer:
- Authentication/authorization handling
- How protected routes are managed
- Security scheme implementation

##### Additional Details

- Component types and their purposes
- Security flow indicators

##### Implementation Notes

- The pluggable nature of the framework adapters
- The transformation between REST and tRPC
- The OpenAPI document generation process
- Error handling and response formatting
- Type safety and validation

##### Key Relationships

Clear connections between:
- Framework adapters and the core handler
- OpenAPI generator and tRPC procedures
- Request/response transformation flow
- Security middleware integration

Remember to include version information (supporting tRPC v10) and note any deprecated or archived functionality as mentioned in the README.

## Diagram (Mermaid)

```mermaid
flowchart TB
    subgraph Clients[Client Applications]
        direction TB
        REST[REST API Clients]
        SDK[SDK Clients]
    end

    subgraph Adapters[Framework Adapters Layer]
        direction TB
        Express["Express Adapter"]:::adapter
        Next["Next.js Adapter"]:::adapter
        Fastify["Fastify Adapter"]:::adapter
        Lambda["AWS Lambda"]:::adapter
        Nuxt["Nuxt Adapter"]:::adapter
        NodeHTTP["Node HTTP"]:::adapter
        Standalone["Standalone"]:::adapter
    end

    subgraph Core[trpc-openapi Core Layer]
        direction TB
        Handler["HTTP Handler"]:::core
        Generator["OpenAPI Generator"]:::core
        Parser["Request Parser"]:::core
        Formatter["Response Formatter"]:::core
    end

    subgraph TRPC[tRPC Layer]
        direction TB
        Router["tRPC Router"]:::trpc
        Procedures["Procedures"]:::trpc
        Context["Context"]:::trpc
    end

    subgraph Validation[Validation Layer]
        direction TB
        Zod["Zod Schema Validation"]:::validation
    end

    %% Connections
    Clients --> Adapters
    Adapters --> Core
    Core --> TRPC
    TRPC --> Validation

    %% Detailed Core Connections
    Handler <--> Generator
    Handler <--> Parser
    Handler <--> Formatter

    %% TRPC Connections
    Router <--> Procedures
    Procedures <--> Context

    %% Styling
    classDef adapter fill:#90EE90,stroke:#333,stroke-width:2px
    classDef core fill:#87CEEB,stroke:#333,stroke-width:2px
    classDef trpc fill:#FFA500,stroke:#333,stroke-width:2px
    classDef validation fill:#DDA0DD,stroke:#333,stroke-width:2px

    %% Click Events
    click Express "https://github.com/trpc/trpc-openapi/blob/master/src/adapters/express.ts"
    click Next "https://github.com/trpc/trpc-openapi/blob/master/src/adapters/next.ts"
    click Fastify "https://github.com/trpc/trpc-openapi/blob/master/src/adapters/fastify.ts"
    click Lambda "https://github.com/trpc/trpc-openapi/blob/master/src/adapters/aws-lambda.ts"
    click Nuxt "https://github.com/trpc/trpc-openapi/blob/master/src/adapters/nuxt.ts"
    click NodeHTTP "https://github.com/trpc/trpc-openapi/tree/master/src/adapters/node-http/"
    click Standalone "https://github.com/trpc/trpc-openapi/blob/master/src/adapters/standalone.ts"
    click Generator "https://github.com/trpc/trpc-openapi/tree/master/src/generator/"
    click Handler "https://github.com/trpc/trpc-openapi/blob/master/src/adapters/node-http/core.ts"
    click Parser "https://github.com/trpc/trpc-openapi/blob/master/src/adapters/node-http/input.ts"
    click Procedures "https://github.com/trpc/trpc-openapi/blob/master/src/adapters/node-http/procedures.ts"
    click Zod "https://github.com/trpc/trpc-openapi/blob/master/src/utils/zod.ts"
```
