API Request CLI
This example is a command-line application that fetches data from PokéAPI.
list prints a page of Pokémon names, and describe <name> prints the details of a single Pokémon.
It shows how to build a robust HTTP client where every failure is represented in the type.
What This Example Shows
- Typed HTTP errors: The
fetchwrapper distinguishes network errors, timeouts, non-OK HTTP statuses, and invalid JSON, each as its own error class in aFetchErrorunion. - Timeouts: A timeout that resolves to a
Failureraces against the request withPromise.race. - Rejecting responses:
Result.andThenturns a response with a non-OK status into aFailure, as described in Chaining Results. - Reusable wrappers:
retryandcacheare higher-order functions that wrap any function returning aResultAsync, and are applied withResult.pipe. - Response validation: API responses are validated with a Valibot schema and
Result.parse. - Input validation: The
--pageargument is converted to an integer with a function that returns aResult.
