Auto-generated Service Overview
Type Safety
Synced with backend Compile-time validation
Namespace Based
Static functions Concise calls
TanStack Query
Auto-generated React Hooks Caching and revalidation
Subset Support
Only needed fields Performance optimization
Why Auto-generation?
Problem: Limitations of Manual API Clients
In traditional frontend development, you manually write client code to call backend APIs. Manual client example:- Lack of type safety: Overuse of
anytype, runtime errors occur - Cannot track backend changes: Frontend doesnβt know when API changes
- Duplicate code: Similar code repeated for every API
- Prone to mistakes: URL typos, wrong parameters, etc.
- Hard to maintain: Need to modify all call sites when API changes
Solution: Benefits of Auto-generation
Sonamu analyzes the@api decorator from the backend to automatically generate type-safe clients.
Auto-generated client example:
- β¨ Complete type safety: Backend types are directly reflected in frontend
- β¨ Immediate error detection: API changes detected at compile time
- β¨ Auto-completion: IDE automatically suggests API parameters and response types
- β¨ Namespace based: Clean structure, easy imports
- β¨ Single source of truth: Backend is the only source for API specs
Single Source of Truth is the principle where all information in the system derives from one
source. In Sonamu, the
@api decorator in the backend is the only API specification, and the
frontend follows it.Service Generation Process
Step 1: Backend API Definition
Define APIs with the@api decorator in the backend.
Step 2: TypeScript AST Parsing
Sonamu uses the TypeScript compiler API to analyze the code. Analysis process:- AST (Abstract Syntax Tree): Represents code as a tree structure
- Type extraction: Obtains accurate type information from TypeScriptβs type system
- Metadata collection: Collects all information including decorator options, Guards, comments
Step 3: Namespace Service Generation
Based on collected information, Namespace-based Services are generated. Generated code (services.generated.ts):- Simplicity: Simpler than classes (no
newrequired) - Static methods: No state management needed
- Tree-shaking: Unused functions excluded from bundle
- Easy import:
import { UserService } from "./services.generated"
Step 4: TanStack Query Hook Generation
Hooks that can be used directly in React are also auto-generated.- Auto caching
- Auto revalidation
- Auto loading/error state management
- Conditional fetching support
- Optimistic updates support
Structure of Generated Services
fetch Utility Function
Common fetch function used by all Services.- Wraps Axios calls: Passes
optionsto Axios - Auto response extraction: Returns
res.datadirectly - Error conversion: Axios error β
SonamuError - Zod issue handling: Type-safe handling of validation errors
Subset System
Sonamuβs unique Subset system. What is Subset? A system that defines multiple variants (subsets) of an entity to query only needed fields.- Performance: Query only needed fields to reduce network cost
- Type safety: Returns accurate type for each Subset
- Explicitness: Clearly express what data is needed in code
- Database optimization: Include only necessary columns in SELECT clause
- A: Basic fields (id, core info)
- B: Intermediate fields (A + additional info)
- C: All fields (all columns, including timestamps)
Type Safety in Practice
Compile-time Validation
When backend API changes, errors occur immediately at compile time. Backend change:IDE Auto-completion
Thanks to type information, IDE provides powerful auto-completion.Development Workflow
Backend-First Development
Sonamuβs auto-generation encourages Backend-First development. Typical workflow: Benefits:- Clear contract between backend and frontend
- Prevents bugs from type mismatch at source
- Auto-generated API documentation (Service is the documentation)
- Improved collaboration efficiency
Regeneration During Development
Services must be regenerated whenever API changes.Practical Usage Examples
Basic Usage
Usage in React (TanStack Query Hook)
Conditional Fetching
Advanced Features
qs.stringify Usage
Usesqs library to serialize query parameters for GET requests.
- Nested object support (
filters[status]=active) - Array serialization support (
ids[]=1&ids[]=2) - Matches backendβs parsing method
Error Handling
Handle SonamuError in a type-safe manner.Query Options Reuse
Prefetching
Cautions
Next Steps
Using Services
How to use generated Services
TanStack Query Hook
Using in React
Creating APIs
@api decorator
Subset System
Subset system