Contract-First
Guide to implementing a contract-first approach with oRPC
Introduction
The contract-first approach is a powerful methodology for API development. It offers several key advantages:
- Enables parallel development of client and server components
- Facilitates clear API definition before implementation
- Simplifies publishing TypeScript clients to npm registry
- Reduces type conflicts between client and server
Installation
Define Your Contract
Start by defining your API contract:
Implement Your Server
With your contract defined, implement the server logic:
That's it! The contract definition and implementation are now completely separated, allowing for clean, maintainable code.
Client Usage
Create a fully typed client using just the contract definition:
The client is type-safe and can be used in any JavaScript environment.
Benefits of Contract-First Development
- Type Safety: Ensures consistency between client and server
- Clear API Documentation: The contract serves as living documentation
- Parallel Development: Teams can work independently on client and server
- Easy Testing: Mock implementations can be created from the contract
- Version Control: API changes are tracked through contract changes
By following this approach, you create a robust foundation for your API development process.