Quick start
This page takes you from an empty project to a typed endpoint call with a handled failure. The small users API declared here is the example the rest of the Builder docs build on.
Install
2.0 is an alpha, published under the next tag. All @navios packages of a project must have the same alpha version, because an alpha may break between releases.
@navios/http is a small fetch-based client and zod is the schema library used in these docs. Both are replaceable: see HTTP client and Schemas.
Create a builder
A builder holds the HTTP client and hands out endpoint functions. Most apps have one per backend.
Declare endpoints
An endpoint is a method, a URL and the schemas that describe its data. $userId marks a URL parameter.
Call them
Each declaration is an async function. Its argument object has up to three parts, and TypeScript requires exactly the ones the declaration implies:
urlParams— values for the$nameplaceholdersparams— query string, when the endpoint has aquerySchemadata— request body, when it has arequestSchema
Both calls validate on the way out and on the way in. If email is not an email, nothing is sent. If the server replies with a body that does not match userSchema, the call fails instead of handing you bad data.
Handle a failure
A failed call throws an EndpointError. When you would rather branch than catch, call .safe(): it never throws and resolves to a result you narrow on ok.
error.kind tells you what went wrong: an HTTP status, invalid input, an invalid response, a network failure, a timeout and so on. Errors lists every kind and shows how to give error bodies a type.
Next
- How it works explains what happens during a call and when to use
.safe(). - Declaring endpoints starts the tour of everything a declaration can say.
- Using React? Continue with the React Query quick start.