Skip to main content

Axios

A product list where axios performs the HTTP calls. The requests and the hook are the same as in any HyperFetch app; the adapter, the request options and the error type come from axios.

beginneraxiosuseFetcherrors
import { useFetch } from "@hyper-fetch/react";
import { useState } from "react";

import { getProducts } from "./api";

export const App = () => {
const [delay, setDelay] = useState(500);

// A regular HyperFetch hook. Cache, retries and dedupe work the same; axios only sends the HTTP request.
const { data, error, loading, extra, status, refetch } = useFetch(
// `select` and `delay` are dummyjson query params: return only these fields, answer after `delay` ms.
getProducts.setQueryParams({ limit: 6, select: "title,price,thumbnail", delay }),
);

return (
<main className="container">
<hgroup>
<h1>Products</h1>
<p>
HyperFetch requests sent by axios. Raise the server delay above the 2s axios timeout to see an AxiosError.
</p>
</hgroup>
<article>
<label>
Server delay: {delay} ms {delay > 2000 && <mark>above the axios timeout</mark>}
<input
type="range"
min={0}
max={4000}
step={250}
value={delay}
onChange={(event) => setDelay(Number(event.target.value))}
/>
</label>
<div className="toolbar muted">
<span>
status {status ?? "-"} · content-type {String(extra?.headers["content-type"] ?? "-")}
</span>
<button type="button" className="outline secondary" onClick={refetch} disabled={loading}>
Reload
</button>
</div>
</article>
{loading && <progress />}
{error && (
<article>
<header>
<strong className="error">{error.name}</strong>
</header>
{/* The error is the AxiosError itself, with its code, config and response. */}
<p>
<code>{error.code}</code> {error.message}
</p>
<small className="muted">
{error.config?.method?.toUpperCase()} {error.config?.url}
</small>
</article>
)}
{!error && data && (
<div className="products">
{data.products.map((product) => (
<article key={product.id}>
<img src={product.thumbnail} alt="" width={64} height={64} />
<div>
<strong>{product.title}</strong>
<p className="muted">${product.price}</p>
</div>
</article>
))}
</div>
)}
</main>
);
};
Run locallynpx tiged BetterTyped/hyper-fetch/examples/react/axios hyper-fetch-react-axios && cd hyper-fetch-react-axios && npm install && npm run dev
What it shows
  1. setAdapter(AxiosAdapter) makes axios the transport for every request of the client.
  2. Axios options go in the request options, typed as the axios request config.
  3. AxiosError is the error type of the client, with code, config and response.
  4. Response headers from axios are in extra.headers.

How it works​

  1. api.ts creates the client with createClient<{ error: AxiosError }>() and calls setAdapter(AxiosAdapter). The generic types error in every hook and send() result.

  2. getProducts sets axios options in options. They are passed to the axios() call as they are:

    options: {
    timeout: 2000,
    transitional: { clarifyTimeoutError: true },
    },

    The adapter owns url, baseURL, method, data, signal and the progress callbacks, so those are left out of the type.

  3. App calls useFetch with a delay query param. DummyJSON waits that long before it answers.

  4. When the delay goes over 2 seconds, axios aborts the request. The response resolves with success: false and the AxiosError as error, with the ETIMEDOUT code from clarifyTimeoutError and the request config.

  5. On success, extra.headers holds the axios response headers, and status the HTTP status.

When to use it
  • Projects that already configure axios and want HyperFetch caching, queues and hooks on top.
  • Code that relies on axios features such as its timeout handling, validateStatus or transformResponse.
  • Node.js environments where you prefer axios over fetch.

Summary

One setAdapter(AxiosAdapter) call moves the client to axios. Axios options go in options, and failures arrive as a typed AxiosError in the response.