Upload progress
An upload form with a progress bar that moves as the bytes go out, plus the speed and the time left. Pick a file up to 1 MB or press Upload a 1 MB test file, and press Cancel to stop halfway. The file goes to httpbin, which echoes what it received.
Opens the example in StackBlitz in a new tab. The first start installs packages and takes a few seconds.
import { AxiosAdapter } from "@hyper-fetch/axios";
import { createClient } from "@hyper-fetch/core";
import type { AxiosError } from "axios";
/**
* The Axios adapter sends requests with XMLHttpRequest in the browser, which reports upload progress.
* Everything else (requests, hooks, cache) stays the same as with the default fetch adapter.
*/
export const client = createClient<{ error: AxiosError }>({ url: "https://httpbin.org" }).setAdapter(AxiosAdapter);
/** httpbin rejects bodies above about 1 MB with 413, so the example keeps files under that. */
export const maxFileSize = 1024 * 1024;
/** httpbin echoes the request. `headers["Content-Length"]` tells how many bytes it received. */
export const uploadFileRequest = client.createRequest<{
response: { headers: Record<string, string>; files: Record<string, string> };
payload: FormData;
}>()({
endpoint: "/post",
method: "POST",
});
npx tiged BetterTyped/hyper-fetch/examples/react/upload-progress hyper-fetch-react-upload-progress && cd hyper-fetch-react-upload-progress && npm install && npm run dev- The Axios adapter swaps the transport with
setAdapter(AxiosAdapter)and reports upload progress. onSubmitUploadProgressreceivesprogress,loaded,total,timeLeftandstartTimestampfor every chunk.abort()fromuseSubmitcancels the upload and firesonSubmitAbort.- No other change: requests, hooks and callbacks are the same as with the default adapter.
How it works
-
api.tscreates the client with the Axios adapter. The error type is set toAxiosError, soresponse.erroris typed:createClient<{ error: AxiosError }>({ url: "https://httpbin.org" }).setAdapter(AxiosAdapter); -
uploadFileRequesttakes aFormDatapayload. The browser sets the multipartContent-Typewith its boundary. -
App.tsxregistersonSubmitUploadProgress. In the browser, axios sends withXMLHttpRequest, whoseupload.onprogressevent fires as chunks leave. The adapter passes each event to HyperFetch, which addsprogressin percent andtimeLeftin milliseconds. The speed isloadeddivided by the time sincestartTimestamp. -
At 100% the body is sent, but the server has not answered yet. The card says so until
onSubmitSuccessarrives with the echo, whereheaders["Content-Length"]shows how many bytes httpbin received. -
Cancel calls
abort(). Axios stops the upload, the request resolves as aborted,onSubmitAbortfires, and the response is not stored. -
httpbin answers
413for bodies above about 1 MB, so files are checked before they are sent. With your own server, drop that check.
Why the default adapter cannot do this
The default HTTP adapter uses fetch. fetch has no upload progress events: the browser sends the body without telling
the page how much is gone. So with fetch HyperFetch can only report the start and the end of the upload. Download
progress works with both adapters, because a fetch response body can be read chunk by chunk. If you only need to show
that an upload is running, the file upload example does it with the default adapter.
- Uploads of large files where users need to see that something is happening and how long it takes.
- Set the Axios adapter on a separate client for uploads if the rest of the app should keep using
fetch.
Related
setAdapter(AxiosAdapter) gives HyperFetch real upload events, and onSubmitUploadProgress turns them into a
percentage, speed and time left. fetch cannot report upload progress, so use Axios when the bar has to move.
