Devtools
A small app with a few buttons that each produce something worth inspecting: a cached list, a mutation, a failed request, a slow request you can cancel. The devtools plugin streams all of it to HyperFlow, the HyperFetch desktop devtools.
Opens the example in StackBlitz in a new tab. The first start installs packages and takes a few seconds.
import { useFetch } from "@hyper-fetch/react";
import { useState } from "react";
import { addUser, client, getSlowUsers, getStatus, getUsers } from "./api";
const slowRequest = getSlowUsers.setQueryParams({ limit: 3, delay: 4000 });
const Users = () => {
const { data, error, loading, refetch } = useFetch(
// `select` is a dummyjson query param: the API returns only these fields.
getUsers.setQueryParams({ limit: 5, select: "firstName,lastName,email" }),
);
return (
<article>
<header className="toolbar">
<strong>Users</strong>
<button type="button" className="outline secondary" onClick={refetch} disabled={loading}>
Refetch
</button>
</header>
{loading && !data && <p aria-busy="true">Loading users...</p>}
{error && <p className="error">Could not load users: {error.message}</p>}
<ul>
{data?.users.map((user) => (
<li key={user.id}>
{user.firstName} {user.lastName} <small className="muted">{user.email}</small>
</li>
))}
</ul>
</article>
);
};
export const App = () => {
const [log, setLog] = useState<string[]>([]);
const addLog = (entry: string) => setLog((prev) => [entry, ...prev].slice(0, 6));
// Each action produces something different to look at in HyperFlow.
const actions = {
add: async () => {
const { data, status } = await addUser.send({
payload: { firstName: "Ada", lastName: "Lovelace", email: "ada@example.com" },
});
addLog(`POST /users/add -> ${status}, created user ${data?.id}`);
},
fail: async () => {
const { error, status } = await getStatus.setParams({ status: 500 }).send();
addLog(`GET /http/500 -> ${status}, ${error?.message}`);
},
slow: async () => {
addLog("GET /users?delay=4000 sent");
const { status, error } = await slowRequest.send();
addLog(`GET /users?delay=4000 -> ${error ? error.message : status}`);
},
cancel: () => slowRequest.abort(),
clearCache: async () => {
await client.cache.clear();
addLog("Cache cleared");
},
};
return (
<main className="container">
<hgroup>
<h1>Devtools</h1>
<p>Open HyperFlow, then use the app. Every request, response and cache change shows up there live.</p>
</hgroup>
<div className="grid-2">
<Users />
<article>
<header>
<strong>Things to inspect</strong>
</header>
<div className="actions">
<button type="button" onClick={actions.add}>
Add a user
</button>
<button type="button" className="secondary" onClick={actions.fail}>
Fail with 500
</button>
<button type="button" className="secondary" onClick={actions.slow}>
Slow request
</button>
<button type="button" className="outline secondary" onClick={actions.cancel}>
Cancel it
</button>
<button type="button" className="outline" onClick={actions.clearCache}>
Clear cache
</button>
</div>
<ol className="log">
{log.map((entry, index) => (
<li key={log.length - index}>{entry}</li>
))}
</ol>
</article>
</div>
</main>
);
};
npx tiged BetterTyped/hyper-fetch/examples/react/devtools hyper-fetch-react-devtools && cd hyper-fetch-react-devtools && npm install && npm run devDevtoolsPluginconnects a client to HyperFlow with oneaddPlugin()call.- Development only: the plugin is added when
import.meta.env.DEVis true, so production builds do not connect. - Network: every request with its payload, headers, response, status and timings.
- Cache and queues: the cached responses and the requests waiting or in flight, updated live.
How it works
-
Download HyperFlow and start it. It listens on
ws://localhost:2137. -
api.tsadds the plugin to the client:client.addPlugin(DevtoolsPlugin({ appName: "HyperFetch devtools example", environment: import.meta.env.MODE }));appNameis the name HyperFlow lists the app under, andenvironmenttells apart several builds of the same app. Seturlonly if you changed the HyperFlow port. -
Run the example with
npm run devand open it. The app appears in HyperFlow and the plugin starts sending events. Without HyperFlow running, the plugin keeps trying to connect in the background and the browser console shows failed WebSocket connections; requests are not affected. -
What to look at in HyperFlow:
- Refetch in the users list: a
GETin the network view, and its entry in the cache view. - Add a user: a
POSTwith its JSON payload and the201response. - Fail with 500: a failed request with the error body.
- Slow request and Cancel it: the request sits in its queue for four seconds; cancelling ends it with an abort error.
- Clear cache: the cache view empties.
- Refetch in the users list: a
- Debugging why a request was sent twice, not sent at all or served from the cache.
- Checking payloads, headers and timings without the browser network tab.
- Watching queues in offline-first apps and cancelling or retrying requests by hand.
Related
client.addPlugin(DevtoolsPlugin({ appName })) in development mode is all the setup. HyperFlow then shows every
request, cache entry and queue of the client while you use the app.
