The Fetch API with a power pack ⚡️
Fetchfully is a small, typed layer over the standard Fetch API. It builds URLs, sends bodies, parses responses, and returns every outcome as a value, so requests never need try/catch. Live request status, deduplication, caching, retries, and interrupts are built in. It has no runtime dependencies.
import { Fetchfully } from "fetchfully";
const api = Fetchfully.create({ baseURL: "https://api.example.com" });
const res = await api.get("/users", { page: 1 });
if (res.isSuccess) {
console.log(res.data); // the parsed JSON
} else {
console.error(res.error); // an HttpError, NetworkError, TimeoutError, …
}- Features
- Installation
- Quick start
- Key behaviours
- Making requests
- Responses
- Errors
- Timeouts, cancellation, and retries
- Refetching
- Interrupts
- Live request status
- Running requests together
- Deduplicating requests
- Caching
- Configuration reference
- API reference
- TypeScript
- What Fetchfully doesn't do
- Development
- License
- Errors as values. Every request resolves to a success or an error response. Check
isSuccessinstead of catching. - One config object. Each request is described by a single object, with instance-wide and global defaults.
- URL building. Path segments and query objects, with a choice of array formats.
- Bodies and parsing. Objects are sent as JSON, and responses are parsed by their
Content-Type. - Shortcut methods.
get,post,put,patch, anddelete. - Timeouts, cancellation, and retries, with a typed error class for each kind of failure.
- Interrupts. Change every request or response of an instance. Every handler can be removed.
- Live status for any UI framework.
createQuery()is an observable that plugs into React, Vue, Svelte, Solid, and Angular. - Several requests at once.
combineQueries()gives several queries one status, anddedupeshares identical GET requests that are in flight together. - Caching. An opt-in, per-instance cache with freshness, a size limit, and invalidation that refreshes the queries showing the changed data.
- Typed. Written in TypeScript, with every public type exported.
npm install fetchfullyFetchfully works wherever the standard Fetch API exists: modern browsers and Node.js 18 or later.
// ES modules
import { Fetchfully } from "fetchfully";
// CommonJS
const { Fetchfully } = require("fetchfully");Http is the same object under another name: import { Http } from "fetchfully".
import { Fetchfully, HttpError } from "fetchfully";
// 1. Create an instance for your API
const api = Fetchfully.create({
baseURL: "https://api.example.com",
headers: { Authorization: `Bearer ${token}` },
timeout: 10_000,
});
// 2. Read
const users = await api.get("/users", { page: 2 });
// GET https://api.example.com/users?page=2
if (users.isSuccess) render(users.data);
// 3. Write
const created = await api.post("/users", { name: "Ada" });
// POST https://api.example.com/users with the JSON body {"name":"Ada"}
if (created.error instanceof HttpError && created.error.status === 409) {
showMessage("That user already exists");
}These rules cover most of what you need to use Fetchfully correctly.
- Requests resolve; they don't reject. Every outcome is a response with
status: "success"orstatus: "error". Only programming errors reject the promise: setting bothbaseURLandurl, or an interrupt that throws. - Check before reading. Test
res.isSuccess(orres.status) first. On an error,res.dataisnullandres.erroris anError. - Shortcut arguments:
get(path, query?, options?),post(path, body?, query?, options?)(the same forputandpatch), anddelete(path, query?, options?). - Config merges from general to specific:
Fetchfully.defaults, thenFetchfully.create(config), then the call's own config, then request interrupts. Objects such asheadersandcachemerge key by key, and other values are replaced. Header names match case-insensitively, and a header set toundefinedisn't sent. - Bodies: objects and arrays are sent as JSON. Strings,
FormData,Blob, binary data, andURLSearchParamsare sent as they are. - Responses are parsed by
Content-Type: JSON becomes a value, and text becomes a string. See How the body is parsed. - Off by default: retries, deduplication, and caching. Retries only cover network errors and timeouts. Deduplication and caching only apply to GET.
- Live status comes from
createQuery(). An awaited response has always finished. A query sends nothing until you callrun().
The examples in this section use an api instance:
const api = Fetchfully.create({ baseURL: "https://api.example.com" });Every instance, including Fetchfully itself, is a function that takes one config object:
const res = await api({
method: "PATCH",
path: ["users", "42"],
query: { notify: true },
body: { status: "active" },
headers: { "X-Request-Id": requestId },
});
// PATCH https://api.example.com/users/42?notify=trueThe Configuration reference lists every option.
| Method | Signature |
|---|---|
| GET | api.get(path, query?, options?) |
| POST | api.post(path, body?, query?, options?) |
| PUT | api.put(path, body?, query?, options?) |
| PATCH | api.patch(path, body?, query?, options?) |
| DELETE | api.delete(path, query?, options?) |
options accepts any config option except path, method, query, and body, which come from the other arguments.
await api.get("/users", { active: true });
await api.post("/users", { name: "Ada" });
await api.put("/users/42", { name: "Ada Lovelace" });
await api.patch("/users/42", { status: "active" });
await api.delete("/users/42");
// Options go last. Pass undefined to skip the arguments before them.
await api.get("/users", undefined, { timeout: 3_000 });
await api.post("/reports", report, undefined, { retries: 2 });baseURLis an instance's base URL, andpathis joined onto it.urlis a full URL for a single request.pathis joined onto it too.- Set one or the other. Setting both rejects the request, and that includes a
baseURLset onFetchfully.defaults.
await api.get("/users/42/posts"); // https://api.example.com/users/42/posts
await api({ path: ["users", "42", "posts"] }); // the same URL
await Fetchfully({ url: "https://status.example.com/v1", path: "health" });
// https://status.example.com/v1/healthSlashes at the edges of path segments are trimmed, so "/users" and "users" give the same URL, and a trailing slash on baseURL never doubles up. A trailing slash at the end of a string path is kept ("users/" becomes …/users/), because some APIs treat the two differently.
Pass an object as query, or as a shortcut's query argument. Values are URL-encoded, null and undefined values are skipped, and nested objects are sent as JSON.
queryArrayFormat decides how arrays are written. For { colors: ["red", "blue"] }, before URL encoding:
queryArrayFormat |
Result |
|---|---|
"comma" (default) |
colors=red,blue |
"repeat" |
colors=red&colors=blue |
"brackets" |
colors[]=red&colors[]=blue |
"none" |
colors=red blue |
await api.get("/posts", { page: 1, tags: ["js", "web"], author: null });
// https://api.example.com/posts?page=1&tags=js%2Cweb (author is skipped)body (or a shortcut's body argument) is ignored for GET. For the other methods:
| Body | Sent as | Content-Type |
|---|---|---|
| Object or array | JSON | application/json, from the default headers |
| String | as is | application/json, from the default headers, unless you set another |
FormData |
as is | set by fetch, with the multipart boundary |
Blob or File |
as is | the blob's own type, unless you set another |
ArrayBuffer, typed array, or Node Buffer |
as is | none, unless you set one |
URLSearchParams |
as is | application/x-www-form-urlencoded, unless you set another |
For a body that isn't a string or a JSON value, Fetchfully leaves out its default Content-Type: application/json so that fetch can choose. On FormData, any Content-Type is left out, because only fetch knows the boundary.
await api.post("/avatars", fileInput.files[0]); // sent with the file's own type
const form = new FormData();
form.append("title", "Q3 report");
form.append("file", file);
await api.post("/uploads", form);
await api.post("/import", csvText, undefined, {
headers: { "Content-Type": "text/csv" },
});Every request starts with these default headers:
Content-Type: application/json
Cache-Control: no-cacheHeaders you pass are merged over them. Header names match case-insensitively, so "content-type" replaces the default "Content-Type". This also applies to a header an interrupt sets.
To drop a header, set it to undefined. On an instance, that drops it for every request the instance sends.
await api.get("/users", undefined, { headers: { "Cache-Control": undefined } }); // one call
const lean = Fetchfully.create({
baseURL: "https://api.example.com",
headers: { "Cache-Control": undefined }, // every request from this instance
});Cross-origin requests and preflights. Browsers send a cross-origin request straight away only when its headers are on a short safe list. Cache-Control isn't on it, and neither is Content-Type: application/json. So with the default headers, every cross-origin request is preceded by an OPTIONS preflight request, and the server must allow both headers in Access-Control-Allow-Headers. If that extra round trip matters, set both to undefined on the instance. Requests that send JSON then need Content-Type: application/json on the call.
| Field | Type | Meaning |
|---|---|---|
status |
"success" | "error" |
The outcome. |
isSuccess, isError |
boolean |
Shorthand for the two statuses. |
data |
parsed body, or null |
The parsed body on success. Always null on error. |
error |
Error, or null |
One of the error classes on error. Always null on success. |
statusCode |
number, or undefined |
The HTTP status. undefined when no response arrived. |
headers |
Headers, or undefined |
The response headers, for example res.headers?.get("etag"). |
requestUrl |
string |
The URL Fetchfully requested, after path and query building. |
refetch |
(override?) => Promise<response> |
Sends the same request again. See Refetching. |
isLoading and isIdle are deprecated. On an awaited response they are always false and true. Use createQuery() for live status. Both will be removed in 2.0.
| Method | Succeeds when the status is |
|---|---|
| GET | any 2xx |
| POST, PUT, PATCH, DELETE | any 2xx, or 304 |
Any other status is an HttpError. fetch follows redirects before Fetchfully sees the response.
Fetchfully reads the response's Content-Type:
Content-Type |
data is |
|---|---|
application/json |
the parsed JSON |
application/octet-stream |
an ArrayBuffer |
multipart/form-data |
a FormData |
image/*, video/*, audio/* |
a Blob |
| anything else, or none | a string |
A response labelled application/json needs a valid JSON body. An empty or invalid one gives an error response with a SyntaxError. A 204 No Content without a Content-Type succeeds with data: "".
Checking isSuccess (or status) narrows the response:
interface User {
id: number;
name: string;
}
const res = await api.get<User>("/users/1");
if (res.isSuccess) {
res.data.name; // User, not User | null
} else {
res.error.message; // Error, not Error | null
}Failures come back on res.error. Each kind has its own class, exported for instanceof checks:
| Class | When | Retried with retries |
|---|---|---|
HttpError |
The server answered with a status that isn't a success. Has status, statusText, and URL, the final URL after redirects. |
No |
NetworkError |
No response arrived: offline, DNS failure, connection refused. Browsers also report blocked cross-origin requests this way, because they don't tell scripts why a request failed. | Yes |
TimeoutError |
The timeout elapsed first. |
Yes |
CancelError |
Your signal aborted the request. |
No |
CorsError |
The runtime's error message says a cross-origin request was blocked. Browsers rarely say so, so expect NetworkError there. |
No |
import { HttpError, NetworkError, TimeoutError } from "fetchfully";
const res = await api.get("/users/42");
if (res.isError) {
if (res.error instanceof HttpError && res.error.status === 404) showNotFound();
else if (res.error instanceof NetworkError) showOffline();
else if (res.error instanceof TimeoutError) showRetryButton();
}- Error bodies aren't read. On an
HttpError,res.dataisnull. Useres.statusCodeandres.headers. - Invalid input is never sent. A malformed URL or header comes back as the runtime's own
TypeError, and no network request is made. - Other failures keep their own type, such as the
SyntaxErrorfrom a response labelled JSON that isn't. - Programming errors reject. Setting both
baseURLandurl, or an interrupt that throws, rejects the promise with that error. A query'srun()catches these too and puts them instate.error.
timeout aborts a request that takes longer than the given number of milliseconds, with a TimeoutError. The default, 0, means no timeout. Each retry gets its own timeout.
const res = await api.get("/reports", undefined, { timeout: 5_000 });
if (res.error instanceof TimeoutError) showRetryButton();Pass an AbortSignal as signal. Aborting it ends the request with a CancelError, and a signal that is already aborted sends nothing. One signal can be shared by many requests.
const controller = new AbortController();
const pending = api.get("/search", { q: "ada" }, { signal: controller.signal });
controller.abort(); // for example, the user typed again
const res = await pending; // res.error is a CancelErrortimeout and signal work together. Whichever fires first ends the request.
retries is the number of extra attempts after a NetworkError or TimeoutError. retryDelay is the fixed wait between attempts, in milliseconds (default 1000). HTTP errors, cancellations, and invalid input are never retried.
const api = Fetchfully.create({
baseURL: "https://api.example.com",
retries: 2, // up to 3 attempts in total
retryDelay: 500,
});Every response has refetch(), which sends the same request again. An override replaces the options you give for that run. It replaces whole top-level keys, so a new query replaces the old one instead of merging with it.
const page1 = await api.get("/users", { page: 1, limit: 20 });
const again = await page1.refetch(); // the same request
const page2 = await page1.refetch({ query: { page: 2, limit: 20 } });With caching on, refetch() always goes to the network and updates the cached response.
Interrupts run on every request or response of one instance, in the order they were added. Each use() returns a function that removes the handler. A new instance from Fetchfully.create() starts with no interrupts.
A request interrupt receives the merged config and returns the config to send. It can be async.
const eject = api.interrupts.request.use(async (config) => {
config.headers["Authorization"] = `Bearer ${await getToken()}`;
return config;
});
eject(); // removes the handleruse(onFulfilled?, onRejected?) takes two optional handlers. onFulfilled receives success responses and onRejected receives error responses. Each returns the response to pass on, which can be a different one. They run for every response, including ones served from the cache.
api.interrupts.response.use(
(response) => response,
(response) => {
if (response.statusCode === 401) redirectToLogin();
return response;
},
);useEffect(() => {
const eject = api.interrupts.request.use((config) => {
config.headers["Authorization"] = `Bearer ${token}`;
return config;
});
return eject; // removes the handler on unmount, or when the token changes
}, [token]);createQuery(fetcher, config?) wraps a request in a small observable that reports its state as it changes, from idle to loading to success or error. It has no framework dependency: it exposes subscribe and getSnapshot, which every major UI framework can consume.
import { createQuery } from "fetchfully";
const users = createQuery(api, { path: "/users" });
const unsubscribe = users.subscribe((state) => render(state));
await users.run(); // sends the request
await users.run({ query: { page: 2 } }); // overrides config for this run
users.abort(); // cancels; state.error is a CancelErrorstate.status |
Meaning | state.data |
state.error |
|---|---|---|---|
"idle" |
run() hasn't been called yet |
null |
null |
"loading" |
a request is in flight | the previous data, or null |
null |
"success" |
the latest request succeeded | the new data | null |
"error" |
the latest request failed | the previous data, or null |
the Error |
Each state also has isIdle, isLoading, isSuccess, and isError.
- Nothing runs on creation. Call
run()when the request should go out. This keeps server rendering and conditional fetching in your hands. - The newest run wins. Calling
run()while a request is in flight aborts the older one and drops its result. run()never rejects. It resolves with the final state. Failures, including programming errors, end up instate.error.- Overrides are shallow.
run(override)mergesoverrideover the query's config, replacing whole top-level keys. subscribe()calls the listener straight away with the current state, then on every change. It returns an unsubscribe function.- Snapshots are stable.
getSnapshot()returns the same object until the state changes. - One query, many subscribers. Create a query once, for example at module level, and subscribe to it from as many components as you like. They all share one state, and at most one request is in flight.
- It refreshes after invalidation. While it has subscribers, a query runs again when
api.cache.invalidate()matches its last request. See Invalidation.
import { useEffect, useState, useSyncExternalStore } from "react";
function Users() {
// Create the query once per component, not on every render
const [users] = useState(() => createQuery(api, { path: "/users" }));
const state = useSyncExternalStore(users.subscribe, users.getSnapshot, users.getSnapshot);
useEffect(() => {
users.run();
return () => users.abort();
}, [users]);
if (state.isError) return <p>{state.error.message}</p>;
if (!state.data) return <p>Loading…</p>;
return <UserList users={state.data} />;
}A query already satisfies Svelte's store contract, so $ works directly:
<script>
const users = createQuery(api, { path: "/users" });
users.run();
</script>
{#if $users.isLoading}Loading…{:else if $users.isError}{$users.error.message}{/if}import { shallowRef, onScopeDispose } from "vue";
const users = createQuery(api, { path: "/users" });
const state = shallowRef(users.getSnapshot());
onScopeDispose(users.subscribe((s) => (state.value = s)));
onScopeDispose(() => users.abort());
users.run();import { from } from "solid-js";
const state = from(users); // state()?.isLoadingconst state = signal(users.getSnapshot());
inject(DestroyRef).onDestroy(users.subscribe((s) => state.set(s)));
users.run();Responses never reject, so Promise.all waits for every request instead of stopping at the first failure:
const [users, posts] = await Promise.all([api.get("/users"), api.get("/posts")]);
if (users.isError) showError(users.error);For live status across several requests, combine queries into a group. A group has the same subscribe, getSnapshot, run, and abort as a single query, so every framework recipe above works unchanged.
import { createQuery, combineQueries } from "fetchfully";
const users = createQuery(api, { path: "/users" });
const posts = createQuery(api, { path: "/posts" });
const page = combineQueries([users, posts]);
page.subscribe((state) => {
state.status; // combined: "idle" | "loading" | "success" | "error"
state.data; // [usersData, postsData]; both set when status is "success"
state.error; // the first failure, if any
state.errors; // each query's error, or null
state.states; // each query's own state
});
await page.run(); // runs both in parallel; never rejects
page.abort(); // cancels bothThe combined status is loading if any query is in flight. Otherwise it is error if any query failed, success if all succeeded, and idle if some query hasn't run yet. Groups take existing queries, so a query shared across components can also belong to a group. In TypeScript, combineQueries([users, posts]) infers the data as a tuple, such as [User[], Post[]].
With dedupe on, identical GET requests that are in flight at the same time share one network call. Identical means the same URL, query, headers, credentials, and mode.
const api = Fetchfully.create({ baseURL: "https://api.example.com", dedupe: true });
// Three components ask for the current user at once: one request
const [a, b, c] = await Promise.all([api.get("/me"), api.get("/me"), api.get("/me")]);dedupe is off by default. It can be set on Fetchfully.defaults, on an instance, or on one call (api.get("/me", undefined, { dedupe: true })).
- Each caller stays independent. Every caller gets its own response object, runs the response interrupts, and keeps its own
timeoutandsignal. A caller that cancels leaves on its own. The network request is aborted only once every caller has cancelled. - The data is shared. Callers receive the same parsed
dataobject, so treat it as read-only. - It's not a cache. Requests that don't overlap in time each go to the network. For that, see Caching, which deduplicates as well.
- GET only. Other methods are never deduplicated.
Each instance has its own in-memory response cache. Turn it on with cache, and successful GET responses are served from memory while they're fresh.
const api = Fetchfully.create({
baseURL: "https://api.example.com",
cache: { staleTime: 30_000 }, // serve stored responses for 30 seconds
});
await api.get("/users"); // from the network, then stored
await api.get("/users"); // from the cache, with no request| Option | Meaning |
|---|---|
staleTime |
How long, in milliseconds, a stored response is served without a request. 0 always goes to the network but still stores the result. Infinity keeps it until it's invalidated. |
maxEntries |
The most responses kept per instance (default 100). The least recently used goes first. Set it on the instance. |
Like dedupe, cache can be set on Fetchfully.defaults, on an instance, or on one call. Caching also shares requests that are in flight, so you don't need dedupe as well.
await api.get("/users", undefined, { cache: false }); // skip the cache for this call
await api.get("/users", undefined, { cache: { staleTime: 5_000 } }); // stricter freshness for this call
const res = await api.get("/users");
await res.refetch(); // always a fresh request; updates the cache
await usersQuery.run({ cache: { staleTime: 0 } }); // the same, for a queryAfter a change on the server, invalidate what it affected. A path matches itself and everything below it:
await api.post("/users", newUser);
api.cache.invalidate("/users"); // users, users/42, users?page=2, but not users-admin
api.cache.invalidate("https://other.example.com/feed"); // a full URL
api.cache.invalidate((url) => url.includes("/admin/")); // or any rule
api.cache.clear(); // everythingQueries showing invalidated data refresh themselves. Any createQuery() (or combineQueries() group) that has subscribers, and whose last request matches, runs again with its last options. Queries without subscribers are left alone. This works with caching off too, so invalidate() doubles as a "this data changed" signal:
const users = createQuery(api, { path: "/users" });
users.subscribe(render);
await users.run();
await api.post("/users", newUser);
api.cache.invalidate("/users"); // `users` reloads; subscribers see loading, then successInvalidation matches against each response's requestUrl.
- GET only, successes only. Errors and other methods are never stored.
- The data is shared. Every caller served from the cache gets the same
dataobject, so treat it as read-only. - Headers are part of the key. A different
Authorizationheader makes a different entry, so one user's responses aren't served to another. - Invalidation wins over requests in flight. A request that started before an
invalidate()still returns its result to its callers, but the result isn't stored, and later requests don't join it. - On a server, use one instance per incoming request. A cache on a long-lived shared instance would be shared by every user of the server.
- It's not the browser's HTTP cache. This is Fetchfully's own memory cache, and
Cache-Controlheaders don't affect it.
| Option | Type | Default | Description |
|---|---|---|---|
baseURL |
string |
none | Base URL that path is joined onto. Can't be combined with url. |
url |
string |
none | Full URL for one request. Can't be combined with baseURL. |
path |
string | string[] |
none | Path joined onto baseURL or url. |
query |
Record<string, any> |
none | Query parameters. null and undefined values are skipped. |
queryArrayFormat |
"comma" | "repeat" | "brackets" | "none" |
"comma" |
How arrays in query are written. |
method |
"GET" | "POST" | "PUT" | "PATCH" | "DELETE" |
"GET" |
The HTTP method. |
body |
RequestBody |
none | The request body, ignored for GET. See Request bodies. |
headers |
RequestHeaders |
Content-Type: application/json, Cache-Control: no-cache |
Request headers, merged over the defaults. |
credentials |
"same-origin" | "omit" | "include" |
"same-origin" |
Whether cookies are sent, as in fetch. |
mode |
"cors" | "same-origin" | "no-cors" |
"cors" |
The cross-origin mode, as in fetch. |
keepalive |
boolean |
false |
Lets the request outlive the page, as in fetch. |
timeout |
number (ms) |
0 (none) |
Aborts each attempt after this long, with a TimeoutError. |
signal |
AbortSignal |
none | Cancels the request, with a CancelError. |
retries |
number |
0 |
Extra attempts after a NetworkError or TimeoutError. |
retryDelay |
number (ms) |
1000 |
The fixed wait between attempts. |
dedupe |
boolean |
false |
Shares one network call between identical GET requests in flight. |
cache |
{ staleTime, maxEntries? } | false |
off | The per-instance response cache, for GET. See Caching. |
Config can be set at four levels. Each one merges over the one before it.
// 1. Global: the default instance, and instances created after this line
Fetchfully.defaults.timeout = 5_000;
// 2. Instance
const api = Fetchfully.create({ baseURL: "https://api.example.com", retries: 1 });
api.defaults.timeout = 8_000; // changes this instance's config for later requests
// 3. One call
await api.get("/exports", undefined, { timeout: 30_000 });
// 4. A request interrupt, which sees the merged config last
api.interrupts.request.use((config) => ({ ...config, timeout: config.timeout * 2 }));Fetchfully.create() copies Fetchfully.defaults when it runs, so set global defaults before creating instances.
import {
Fetchfully, // the default instance
Http, // the same object as Fetchfully
createQuery, // a request with live status
combineQueries, // several queries as one
HttpError,
NetworkError,
TimeoutError,
CancelError,
CorsError,
} from "fetchfully";Fetchfully, and every instance from Fetchfully.create(), has these members:
| Member | Description |
|---|---|
instance(config) |
Sends a request and resolves to a response. |
instance.get, .post, .put, .patch, .delete |
Shortcut methods. See Shortcut methods. |
instance.defaults |
The instance's config. Changes apply to later requests. |
instance.interrupts.request.use(handler) |
Adds a request interrupt and returns a function that removes it. |
instance.interrupts.response.use(onFulfilled?, onRejected?) |
Adds a response interrupt and returns a function that removes it. |
instance.cache.invalidate(target) |
Drops matching cached responses and re-runs matching subscribed queries. target is a path, a full URL, or (requestUrl) => boolean. |
instance.cache.clear() |
Drops every cached response and re-runs every subscribed query. |
instance.cache.onInvalidate(listener) |
Calls listener(matches) on every invalidation, where matches(requestUrl) returns a boolean. Returns an unsubscribe function. |
Fetchfully.create(config?) creates a new instance with its own config, interrupts, and cache. It exists only on Fetchfully itself, not on the instances it creates.
fetcher is an instance, or any function with the same call signature. Without a cache property, a plain function works but never re-runs after an invalidation. createQuery() returns a query:
| Member | Description |
|---|---|
getSnapshot() |
The current state. The same object is returned until the state changes. |
subscribe(listener) |
Calls listener(state) straight away, then on every change. Returns an unsubscribe function. |
run(override?) |
Sends the request with override merged over the config. Aborts any request already in flight. Resolves with the final state and never rejects. |
abort(reason?) |
Aborts the request in flight, if any. The state becomes an error with a CancelError. |
Takes an array of queries and returns a group with the same members as a query. Its run() takes no override and runs every query in parallel. Its state also has data as a tuple, errors, and states. See Running requests together.
Every request method and createQuery() takes a type for the response data:
import { Fetchfully, createQuery, combineQueries, type FetchfullyResponse } from "fetchfully";
interface User { id: number; name: string }
interface Post { id: number; title: string }
const api = Fetchfully.create({ baseURL: "https://api.example.com" });
const res: FetchfullyResponse<User[]> = await api.get<User[]>("/users");
const users = createQuery<User[]>(api, { path: "/users" });
const posts = createQuery<Post[]>(api, { path: "/posts" });
const page = combineQueries([users, posts]); // data: [User[], Post[]]The type isn't checked at runtime: get<User>() trusts the server to send a User.
These types are exported: FetchfullyConfig, FetchfullyInstance (an instance from create()), FetchfullyDefaultInstance (Fetchfully itself, which also has create()), FetchfullyResponse, FetchfullySuccessResponse, FetchfullyErrorResponse, RequestOptions, RequestBody, RequestHeaders, RequestStatus, Query, QueryState, QueryListener, QueryFetcher, QueryGroup, QueryGroupState, QueryGroupData, QueryGroupListener, CacheOptions, FetchfullyCache, and InvalidateTarget.
-
Refresh on focus or reconnect. Nothing runs on its own. Call
query.run()orapi.cache.invalidate()from your own listener:document.addEventListener("visibilitychange", () => { if (document.visibilityState === "visible") api.cache.invalidate("/users"); });
-
Optimistic updates. Query state is read-only, and there's no way to set data locally.
-
Read error bodies. On an
HttpError,dataisnull. -
Retry HTTP errors, or back off. Retries cover network errors and timeouts only, with a fixed delay.
-
Report upload or download progress.
-
Stream request bodies.
ReadableStreambodies aren't supported. -
Persist or share the cache. The cache lives in memory, in one instance.
npm install
npm run build # writes dist/fetchfully.mjs, dist/fetchfully.cjs, and dist/index.d.ts
node test/core.smoke.mjs # run one test fileThe tests run against dist, so build first. test/core, test/query, test/multi, and test/cache (each *.smoke.mjs) use a mocked fetch and run offline. test/interrupts.smoke.mjs and test/test-smoke.mjs call live APIs and need a network connection.
npm publish runs the build first (prepublishOnly), so a release always ships a fresh dist. Each release's changes are listed in CHANGELOG.md.
Report bugs at github.com/romeopeter/fetchfully/issues.
MIT. See LICENSE.md.