Composable middleware for the standard fetch API.
@grafctory/http is the HTTP client module of the Grafctory model-driven knowledge graph framework. It
layers recurring HTTP concerns over the standard fetch
function, without introducing a bespoke client API. A middleware wraps a fetch implementation, returning a new one with
the same signature that adjusts requests and responses as they flow through, and a chain of them is assembled into a
single client to be shared across an application:
fetch function, interchangeable with the platform primitivenpm install @grafctory/http
TypeScript consumers must use "moduleResolution": "nodenext"/"node16"/"bundler" in tsconfig.json.
The legacy "node" resolver is not supported.
A client is assembled once from the middlewares an application needs and shared across it, with the root module supplying the assembler and each subpath module a middleware:
import { createFetch } from "@grafctory/http";
import { bearer } from "@grafctory/http/bearer";
import { headers } from "@grafctory/http/headers";
import { reject } from "@grafctory/http/reject";
import { resolve } from "@grafctory/http/resolve";
import { timeout } from "@grafctory/http/timeout";
const client = createFetch( // shared across the application
reject(), // rejects unless response.ok
bearer(() => session.token), // Authorization header, resolved per request
headers({ Accept: "application/json" }), // stated on every request
timeout(5000), // gives up on a server that never replies
resolve() // answers transport failures as responses, rejected above
);
const response = await client("https://api.example.com/data");
Middlewares are layered in declaration order: requests are processed by the first middleware first and reach the
standard fetch function last, while responses travel back through the chain in reverse. The resulting client is itself
a standard fetch function, so it is handed to anything expecting the platform primitive and is itself wrapped again by
a further chain.
This section introduces the overall organisation; each module documents its own concern in detail in the API reference:
| Module | Description |
|---|---|
| @grafctory/http | Client assembly and HTTP utilities |
| Request Authentication | |
| @grafctory/http/basic | Basic authentication |
| @grafctory/http/bearer | Bearer authentication |
| Exchange Control | |
| @grafctory/http/headers | Default header fields |
| @grafctory/http/xsrf | XSRF protection |
| @grafctory/http/throttle | Adaptive pacing and retries |
| @grafctory/http/timeout | Bounded response wait |
| Exchange Resolution | |
| @grafctory/http/cache | HTTP response caching |
| @grafctory/http/protocol | Custom protocol handlers |
| @grafctory/http/transport | Custom fetch transport |
| @grafctory/http/mock | Mock fetch transport |
| Exchange Settlement | |
| @grafctory/http/monitor | Exchange logging |
| @grafctory/http/reject | Failures as rejections |
| @grafctory/http/resolve | Failures as responses |
This project is licensed under the Apache 2.0 License – see LICENSE file for details.
Grafctory™ is a trademark of Metreeca srl.