A lightweight, type-safe API library for TypeScript with Zod validation.
After building several full-stack applications, I discovered that Large Language Models (LLMs) face significant challenges when implementing features that span both backend and frontend components, particularly around API interfaces.
The core issues I observed:
- API Contract Drift: LLMs struggle to maintain consistency when defining an API endpoint and then implementing its usage in the frontend
- Context Loss: Without a clear, shared contract, LLMs lack the contextual assistance needed to ensure proper integration between client and server
- Integration Errors: The disconnect between backend definitions and frontend consumption leads to runtime errors that could be prevented
The Solution: Leverage TypeScript's powerful type system to provide real-time feedback and compile-time validation for both LLMs and developers. By creating a shared contract that enforces consistency across the entire stack, we eliminate the guesswork and reduce integration issues.
This module is specifically designed to make coding with Large Language Models (LLMs) easier and more efficient. When working on bigger applications with extensive APIs, maintaining context becomes challenging for both developers and AI assistants. ts-typed-api solves this by:
- Centralized Type Definitions: Keep all API contracts in one place, making it easier for LLMs to understand your entire API surface
- Automatic Type Synchronization: The type system ensures both client and server stay perfectly in sync, preventing the drift that commonly occurs in large codebases. Compile-time checks prevent the common client-server mismatches that occur in AI-assisted development
- Context-Friendly Structure: Organized domain-based API definitions that LLMs can easily parse and understand
- Compile-Time Validation: Catch integration issues before runtime, reducing the debugging cycles when working with AI-generated code
- Self-Documenting: Type definitions serve as living documentation that LLMs can easily parse
- Define your API in a file that will be shared by both the server and the client
- Implement handlers in the server, leveraging the type system and request/response validation
- Implement the client based on the contract from #1 leveraging type system
Check out the examples/ directory:
- simple/ - Basic usage with ping endpoints and middleware
- advanced/ - Complex schemas with authentication, CRUD operations, and file uploads
Create your API definitions organized by logical domains:
Register handlers with full type safety and middleware support:
Make API calls with full type safety and response handling:
Now both server and client are type safe and in sync! The moment you change the definition of the API, type system will let you know about potential changes you need to handle (like additional response code or a change request body schema).
Create custom HTTP client adapters by implementing the HttpClientAdapter interface:
Add cross-cutting concerns like authentication, logging, and validation:
- OpenAPI generation with dynamic documentation based on Swagger
Contributions are welcome! Please feel free to submit a Pull Request.
Apache 2.0 License