Cloudflare Workers
Cloudflare Workers, Vercel Edge Functions and the other edge runtimes run web standard APIs instead of Node.js. HyperFetch
only needs one of them, fetch, so it runs there unchanged: typed requests, retries and response mapping included.
Purpose
- Web standards only: The default adapter uses
fetchand nothing from Node.js. - Typed calls at the edge: Call upstream APIs with typed requests and a typed SDK.
- Safe for shared isolates: A client per incoming request keeps users' data apart.
Getting started
npm install @hyper-fetch/core
A Worker isolate serves many requests, so create the client inside fetch. Its cache and deduplication then belong to
that one request:
src/index.ts
import { createClient } from "@hyper-fetch/core";
type Env = { API_URL: string };
export default {
async fetch(request: Request, env: Env): Promise<Response> {
const client = createClient({ url: env.API_URL });
const getProduct = client.createRequest<{
response: { id: string; price: number };
}>()({
endpoint: "/products/:id",
});
const id = new URL(request.url).searchParams.get("id") ?? "";
const { data, error } = await getProduct
.setParams({ id })
.setRetry(1)
.send();
return Response.json(error ?? data, { status: error ? 502 : 200 });
},
};
The same handler shape works in Vercel Edge Functions and Next.js route handlers on the edge runtime.
A typed SDK at the edge
Generate an SDK from the OpenAPI document of the API you call, and create it inside the handler like the client:
npx @hyper-fetch/cli@latest generate --template openapi --url https://api.example.com/openapi.json --fileName upstream.sdk.ts
const sdk = createSdk(createClient({ url: env.API_URL }));
const { data } = await sdk.products.$id.$get.setParams({ id }).send();
OpenAPI TypeScript client
Generate a typed SDK from an OpenAPI document and call it with send().
When it's helpful
- Edge proxies and backends-for-frontends that combine several upstream APIs.
- Workers that need retries and typed responses without pulling in Node.js APIs.
- Calling the same API from the edge and the browser with the same request definitions.
Summary
- HyperFetch only needs
fetch, so it runs on Workers and other edge runtimes unchanged. - Create the client inside the handler so each incoming request gets its own cache.
- Generate a typed SDK with the CLI for the APIs your Worker calls.
