How it works
Builder has a small model: a declaration is plain data, and declareEndpoint wraps it in a function. This page describes what that function does on every call, so the rest of the docs can refer to it instead of repeating it.
A declaration is data plus a function
Because the options stay readable on config, other tools can consume a declaration without calling it. That is how a Navios server controller knows which route to register and how @navios/react-query builds cache keys.
What happens during a call
Four things follow from this pipeline:
- Invalid input never leaves the process.
urlParams,paramsanddataare validated first. A failure there produces an error without touching the network. - What you pass is what is sent. The schemas decide whether a request leaves; they do not rewrite it. Defaults and transforms run where the request is parsed. What is sent explains this, and the option to send the schemas' output.
- Builder decides what is an error, not the HTTP client. Any status outside 200–299 is a failure, whether the client resolved or rejected. This is why a bare
fetchwrapper works as a client. - Every failure is the same type. Each rounded box in the diagram is an
EndpointError, and its label is the error'skind. Errors covers them in full.
Two ways to call
Both forms run the same pipeline and produce the same error for the same failure. They differ only in how the failure reaches you.
Use the throwing form when a failure should abort what you are doing and something above you handles it: a server request handler, a React Query hook, a script.
Use the safe form when a failure is one of the expected outcomes and you want the compiler to make you look at it: a form that shows a "name already taken" message, a lookup where 404 means "create it". Inside the else branch the error is fully typed, including the body of each status you declared. In a catch block the value is unknown and you need guards to get that back.
Where the types come from
Nothing is annotated by hand. declareEndpoint infers one type per part of the declaration:
Input and output differ when a schema transforms or applies defaults. You pass what the schema accepts and receive what it produces. Schemas has the details.
To name these types elsewhere in your code, use the type helpers rather than re-deriving them from the schemas.