Core overview
Define each endpoint once as a typed request, then send it from anywhere. The client behind it caches, dedupes, retries, queues and waits out offline periods. It has no framework dependency.
import { createClient } from "@hyper-fetch/core";
// One client per backend
const client = createClient({ url: "https://api.example.com" });
// One request per endpoint, typed once
export const getUser = client.createRequest<{ response: User }>()({
endpoint: "/users/:userId",
});
// `:userId` is a required, typed param. send() never throws.
const { data, error, status } = await getUser.send({ params: { userId: 1 } });
HTTP errors, timeouts, aborts and network failures all resolve with error set. You branch on the result instead of
wrapping calls in try/catch.
- Request
Typed endpoint, defined once
- Dispatcher
Dedupes, queues, retries
- Adapter
fetch, Axios, GraphQL, Firebase
- Cache
Stored by cache key
- Response
data, error, status
Building blocks
Base URL, adapter, cache and interceptors for one backend.
RequestOne endpoint with its params, payload and options.
DispatcherDeduplication, cancelation, queues, offline and retries.
CacheResponses by key, staleTime, cacheTime and invalidation.
AdapterThe transport: fetch by default, or your own.
ManagersFocus and online state, lifecycle events, aborting.
SDKEvery endpoint in one typed tree with createSdk.
PluginsLogging, metrics and error reporting for a client.
Data flowThe full path of a request, step by step.
Requests are templates
A request is plain, immutable data. Every setter, such as setParams or setQueryParams, returns a new request and
leaves the original untouched. So one request serves a component, a server loader, a test and a story, with no custom
hook per endpoint. In tests and stories, setMock answers it without a server.
Common tasks
Add tokens, refresh on 401 and resend.
Error handlingType errors and branch on the result.
RetriesRetry failed requests with a delay.
Cache revalidationShow cached data, refresh in the background.
QueuesSend writes one at a time, in order.
OfflineWait for the network instead of failing.
Next
Core runs alone in backends and scripts and in shared API packages. In a UI, add a binding on top.
Install core, create a client, send a typed request.
React hooks@hyper-fetch/reactuseFetch and useSubmit on the same requests and cache.
Realtime@hyper-fetch/socketsTyped listeners and emitters over WebSocket and SSE.
Other backendsAxios, GraphQL, Firebase, tRPC and oRPC adapters.
Vanilla TypeScriptexampleCore in the browser, without a framework.
HyperFlow devtoolsWatch requests, cache and queues while the app runs.