Skip to main content

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.

beginnerdevtoolsuseFetchsend
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>
);
};
Run locallynpx tiged BetterTyped/hyper-fetch/examples/react/devtools hyper-fetch-react-devtools && cd hyper-fetch-react-devtools && npm install && npm run dev
What it shows
  1. DevtoolsPlugin connects a client to HyperFlow with one addPlugin() call.
  2. Development only: the plugin is added when import.meta.env.DEV is true, so production builds do not connect.
  3. Network: every request with its payload, headers, response, status and timings.
  4. Cache and queues: the cached responses and the requests waiting or in flight, updated live.

How it works​

  1. Download HyperFlow and start it. It listens on ws://localhost:2137.

  2. api.ts adds the plugin to the client:

    client.addPlugin(DevtoolsPlugin({ appName: "HyperFetch devtools example", environment: import.meta.env.MODE }));

    appName is the name HyperFlow lists the app under, and environment tells apart several builds of the same app. Set url only if you changed the HyperFlow port.

  3. Run the example with npm run dev and 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.

  4. What to look at in HyperFlow:

    • Refetch in the users list: a GET in the network view, and its entry in the cache view.
    • Add a user: a POST with its JSON payload and the 201 response.
    • 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.
When to use it
  • 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.

Summary

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.