Skip to content

Architecture Overview

TypoKit is a schema-first, type-driven framework where TypeScript types are the single source of truth for the entire stack. Types define your API contracts, database schemas, validation rules, client SDKs, and tests — all generated from one place.

graph TD
subgraph "Schema Layer"
SCHEMA["src/types.ts<br/>Pure TS Types + JSDoc"]
end
subgraph "Build Time (Rust)"
TRANSFORM["Transform Pipeline<br/>Parse ASTs → Extract metadata"]
GENERATE["Code Generation<br/>Validators, Routes, OpenAPI,<br/>Tests, Client"]
end
subgraph "Runtime (TypeScript)"
SERVER["@app/server<br/>TypoKit Core + Server Adapter"]
DB["@app/db<br/>Database Adapter + Migrations"]
CLIENT["@app/client<br/>Generated Type-Safe API Client"]
end
SCHEMA --> TRANSFORM
TRANSFORM --> GENERATE
GENERATE -->|".typokit/"| SERVER
GENERATE -->|".typokit/"| DB
GENERATE -->|".typokit/"| CLIENT

The system separates into three tiers:

  1. Schema layer — Pure TypeScript types with JSDoc metadata. Zero runtime dependencies. Everything downstream derives from here.
  2. Build time — A Rust-powered transform pipeline (via napi-rs) parses type ASTs, extracts metadata, and generates TypeScript artifacts into .typokit/.
  3. Runtime — Standard TypeScript code consumes the generated artifacts for HTTP serving, database access, and API client usage.

TypoKit is a monorepo with focused, single-responsibility packages:

PackageDescription
@typokit/coreCentral runtime — routing, middleware pipeline, handler execution, plugin system, error handling
@typokit/typesShared TypeScript type definitions used across all packages
@typokit/errorsStructured error classes with machine-readable context for AI agents
@typokit/cliCLI tool — typokit init, build, dev, generate, migrate, inspect
PackageDescription
@typokit/transform-nativeRust-based AST parser and code generator (napi-rs). 20–70× faster than ts-morph
@typokit/transform-typiaAlternative transform using Typia for validator generation
PackageDescription
@typokit/server-nativeZero-dependency adapter using compiled radix tree directly. O(k) route lookup
@typokit/server-fastifyAdapter for Fastify — translates route table to Fastify-native registrations
@typokit/server-honoAdapter for Hono — lightweight, works natively on Node, Bun, and Deno
@typokit/server-expressAdapter for Express — enables incremental migration of existing Express apps
PackageDescription
@typokit/platform-nodeNode.js-specific bindings (HTTP, fs, crypto)
@typokit/platform-bunBun-native bindings for optimal performance on Bun runtime
@typokit/platform-denoDeno-native bindings with permission-aware APIs
PackageDescription
@typokit/db-drizzleGenerates Drizzle ORM schema files (per-table .ts files)
@typokit/db-kyselyGenerates Kysely type definitions (single database.ts)
@typokit/db-prismaGenerates Prisma schema (schema.prisma)
@typokit/db-rawGenerates raw SQL DDL + TypeScript types
PackageDescription
@typokit/testingTest harness — createTestClient, createIntegrationSuite, createFactory, toMatchSchema
@typokit/otelOpenTelemetry integration — auto-instrumented spans, structured logging, log-to-span bridging
PackageDescription
@typokit/plugin-debugDebug sidecar exposing inspection endpoints on a separate port
@typokit/plugin-wsWebSocket support — build-time codegen + runtime connection management
@typokit/plugin-axumRust/Axum server code generation from TypeScript schemas
PackageDescription
@typokit/clientCore generated type-safe API client
@typokit/client-react-queryReact Query wrapper around the generated client
@typokit/client-swrSWR wrapper around the generated client
PackageDescription
@typokit/nxNx plugin for monorepo build orchestration
@typokit/turboTurborepo plugin (alternative to Nx)

The build pipeline is the core of TypoKit’s architecture. It transforms plain TypeScript types into optimized runtime artifacts:

flowchart LR
A["Schema Types<br/>(*.ts with JSDoc)"] --> B["Rust Transform<br/>(AST parsing)"]
B --> C["Extract Type<br/>Metadata"]
C --> D["Generate<br/>Validators"]
C --> E["Generate<br/>Route Tree"]
C --> F["Generate<br/>OpenAPI Spec"]
C --> G["Generate<br/>Test Stubs"]
C --> H["Generate<br/>Client SDK"]
C --> I["Schema Diff<br/>(Migrations)"]
D --> J[".typokit/"]
E --> J
F --> J
G --> J
H --> J
I --> J
J --> |"compile hook"| K2["Compile<br/>(tsc or cargo)"]
K2 --> L["dist/ or target/"]

The build pipeline uses AsyncSeriesHook (tapable pattern) with seven phases that plugins can hook into:

  1. beforeTransform — Pre-processing, setup
  2. afterTypeParse — Type metadata extracted, available for inspection
  3. afterValidators — Compiled validators generated
  4. afterRouteTable — Radix tree route table compiled
  5. emit — All artifacts written to .typokit/
  6. compile — Compilation step (default: tsc, overridable by plugins like plugin-axum)
  7. done — Build complete, cleanup

See the Build Pipeline deep dive for implementation details.

When an HTTP request arrives, it flows through three ordered layers with ten discrete steps:

sequenceDiagram
participant Client
participant Adapter as Server Adapter
participant Core as TypoKit Core
participant Handler
Client->>Adapter: HTTP Request
Note over Adapter: 1. HTTP parsing (platform-native)
Note over Adapter: 2. Framework-native middleware<br/>(CORS, compression, helmet)
Note over Adapter: 3. Normalize → TypoKitRequest
Adapter->>Core: TypoKitRequest
Note over Core: 4. TypoKit middleware chain<br/>(auth, logging — type-aware)
Note over Core: 5. Request validation<br/>(compiled validators)
Core->>Handler: 6. Handler execution
Handler-->>Core: Return value
Note over Core: 7. Response serialization
Note over Core: 8. Error handling (if thrown)
Note over Core: 9. OTel span completion
Core-->>Adapter: TypoKitResponse
Note over Adapter: 10. Write HTTP response
Adapter-->>Client: HTTP Response

Key ordering principle: Framework-native middleware (CORS, compression) runs before TypoKit normalization for HTTP-level concerns. TypoKit’s typed middleware runs after normalization to ensure type-aware context transformation.

See the Compiled Router deep dive for route-matching internals.

DecisionRationaleTrade-off
Schema-first, plain TS typesSingle source of truth; Typia-inspired approach avoids runtime schema librariesRequires build step; cannot be purely dynamic
Rust build pipeline20–70× faster than ts-morph; avoids TS compiler plugin fragilitynapi-rs binding maintenance; platform-specific binaries
Thrown errors over Result typesBetter AI ergonomics; centralized error middleware enriches all errorsLess functional purity
Pluggable server adaptersReuse existing frameworks (Fastify, Hono, Express); bring your own HTTP layerMore abstraction layers
Explicit route registrationTraceable dependency graph; clearer for AI agents (no magic file-based routing)More boilerplate in app.ts
Generate types, not queries (no ORM)Works with Drizzle, Kysely, Prisma, or raw SQL; no lock-inDevelopers handle their own queries
Migrations never auto-applySafety for destructive changes; AI and human review before applyExtra manual step
Typed middleware as context narrowingType system communicates exactly what’s available in each handlerRequires careful context type design

For detailed implementation documentation, see:

  • Build Pipeline — Rust transform internals, tapable hooks, plugin integration
  • Compiled Router — Radix tree construction, route matching, parameter extraction