What is a Fluent API?
A fluent API is a design pattern that allows you to chain methods together in a natural, readable way. Instead of nesting function calls or using multiple statements, you create a flowing sequence that reads almost like a sentence.Method Chaining Pattern
Every Univerto conversion follows the same three-step pattern:Step 1: from(value, unit)
Specify the starting value and its unit:
to() method, enforcing the correct order of operations.
Step 2: to(unit)
Specify the target unit for conversion:
convert(), convertToFraction(), and convertToRational().
Step 3: Choose Your Result Format
You have three options for getting the result:convert() - Standard Number Result
convertToFraction() - Fraction Object
convertToRational() - Rational Instance
Benefits of Fluent Design
Readability
The fluent API reads like natural language:Type Safety
The chaining pattern enables TypeScript to provide autocomplete and type checking at each step:Discoverability
IDE autocomplete naturally guides you through the conversion process. After typing.from(), your IDE will suggest .to(). After .to(), it suggests the conversion methods.
Enforced Correctness
The API structure prevents invalid operations. You cannot callconvert() without first specifying both source and target units:
Real-World Examples
Converting Data Sizes
Converting Speeds
Converting Volumes
Implementation Details
The fluent API is implemented using nested functions that return objects with specific methods. Here’s how the pattern works internally:createPreciseConverter function found in ~/workspace/source/src/features/core/utils/create-precise-converter/index.ts:3-47.
Each returned object exposes only the methods that make sense at that point in the chain, guiding you toward a complete conversion.