HTTP client
Builder does not make requests itself. It prepares a request, hands it to an HTTP client you provide, and interprets what comes back. This page covers what counts as a client, how to give one to a builder, and where cross-cutting concerns such as authentication belong.
What a client is
A client is any object with a request(config) method that resolves to { data, status, statusText, headers }.
The contract is deliberately loose:
requestmay resolve or reject for a non-2xx status. Builder checksstatusitself.- A rejection that carries a
responseproperty with a non-2xx status, as axios and@navios/httpproduce, is treated like a non-2xx response. - A rejection that carries a
responsewith a 2xx status means the client could not parse the body. It becomes kindresponse-validation, with the response'sdataas the error body. - A rejection without
responseis a transport failure and becomes kindnetwork,timeoutoraborted. That includes an error thrown by one of the client's own interceptors, which arrives asnetworkwith the error ascause.
config contains method, url (with URL parameters bound), params, data, headers, signal, timeout, responseType, and whatever the endpoint put in clientOptions.
Choosing a client
@navios/http is a small client built on fetch with an axios-like API. It is what these docs use.
The Navios HTTP client below describes it.
axios instances satisfy the contract as they are.
A fetch wrapper is enough when you want no dependency. Because Builder checks the status, the wrapper does not need to throw on errors. This one supports everything a declaration can ask for:
What a client receives
The wrapper above shows what a client of your own has to take care of:
paramsis a plain object. Leave outundefinedandnull, and repeat the key for each item of an array. A Navios server expects dates as ISO strings and objects as JSON.dataisundefined, aFormData(forbody: 'multipart') or the body itself. A string is always sent as it is: for a JSON endpoint Builder has encoded it. Everything else is JSON, also0,falseandnull.headershold acontent-typeonly when the body is a primitive or the declaration has one. Setapplication/jsonfor the rest, and nothing forFormData.responseTypeis'blob','stream'or'text', and absent for JSON. It is'text'forresponse: 'void'as well. An empty JSON body, such as a 204, must not fail the call.- Error bodies are JSON whatever
responseTypesays. A Navios server sends them asapplication/problem+json.
Giving a builder its client
Pass the client when you create the builder. Three methods cover the other cases:
provideClient exists for apps that only know their client at start-up, for example after reading configuration. Declarations can be made before a client exists; a call made without one fails with kind config.
withClient does not affect endpoints already declared on the original builder. Only endpoints declared on the returned builder use the new client.
A client for one call
The second argument of a call selects the client for that call only. The shared client is untouched.
This is the tool for server-side rendering, where each incoming request needs its own cookies or token and a shared mutable client would leak them between users. @navios/react-query builds on it: see SSR and prefetching.
Authentication
Credentials are a property of the connection, so they belong on the client rather than on each declaration. With @navios/http or axios, a request interceptor adds them:
Refreshing an expired token works the same way, in a response interceptor that retries the request after obtaining a new token. Builder sees only the final outcome, so the retry is invisible to your endpoint code and to onError.
For a header that only one endpoint needs, use the endpoint's headers option.
Several backends
Create one builder per backend. Each has its own client, and declarations import the builder they belong to.
The Navios HTTP client
create(config) from @navios/http returns a client with request, get, post, put, patch, delete, head and options, interceptors and defaults. It has no dependencies, runs wherever fetch does, and passes fetch options such as cache and Next.js' next through. The API follows axios; it is not a replacement for all of it. There are no transformRequest, auth, upload progress or proxy options.
A request also takes params, data, signal, timeout, credentials and withCredentials: true, which is short for credentials: 'include'. A string in data is sent as it is, as text/plain unless a content type is set; FormData, URLSearchParams, Blob, buffers and streams are sent as they are; everything else is JSON. null is no body unless the content type is JSON. timeout covers reading the body, except for a stream, where it ends with the headers; Infinity is no timeout.
Interceptors run in the order they were added. Request interceptors run before params and data are serialised, so they can change both, also in place: they get a shallow copy of a plain object or array. config.headers and client.defaults.headers list names in lower case, and take them in any. Response interceptors run after the body was read. What an interceptor returns takes the place of the config or the response, and returning nothing keeps it. A rejected handler receives every failure before it, including network errors and what a request interceptor threw. One of a request has to return a config or throw.
Every failure of the client itself is an HttpClientError with response (or null), status, code, cause and config. isHttpClientError(error) is the type guard. config can be sent again, which is how a token is refreshed:
error.toJSON() leaves out headers and bodies, so an error can be logged without leaking credentials.