Downloads and streams
When an endpoint returns something other than JSON, set its response kind. The call then resolves to a Blob, a string or a ReadableStream instead of schema output. The kinds are listed in Responses; this page shows them in use.
Inputs and errors work exactly as for JSON endpoints. Only the success value changes, and no responseSchema is involved.
Downloading a file
response: 'blob' resolves to a Blob.
In a browser, saving it means pointing a temporary link at an object URL:
To use the filename the server suggests, call .safe() and read the Content-Disposition header:
A download that takes a body is declared the same way, with a requestSchema:
Reading text
response: 'text' resolves to a string. Use it for CSV, plain text, HTML or any format you parse yourself.
Consuming a stream
response: 'stream' resolves to a ReadableStream<Uint8Array> as soon as the headers arrive, so you can process a large or slow body as it comes in.
Pass a signal to stop reading early. Aborting it cancels the request. With @navios/http, a timeout covers the time until the headers arrive, not the reading of the stream. Read a stream to its end or cancel it, so that its connection and its listener on your signal are released.
For a stream of discrete server events, the Server-Sent Events builder gives you typed, validated events instead of raw bytes.
When the download fails
A non-2xx reply produces the same EndpointError as on any endpoint, so error.kind and error.status work as usual.
The error body needs care, because its form depends on the HTTP client. @navios/http reads an error body as text and parses it when the server sent JSON, application/problem+json included, whatever the endpoint asked for. A client that reads it in the format the endpoint asked for, as axios does, returns a Blob on a 'blob' endpoint even if the server sent JSON. Branch on the status, and decode the body yourself when it is a Blob and you need its content:
An errorSchema entry is validated against whatever the client returned, so with a client that returns a Blob a schema that expects an object will not match.
Client requirements
Builder sets responseType on the request ('blob', 'text' or 'stream'), and the HTTP client has to honor it. @navios/http and axios do. A hand-written fetch wrapper must branch on config.responseType and return response.blob(), response.text() or response.body as data, as this one does.
Using downloads from React components is covered in Uploads and downloads.