@foundatiofx/fetchclient

中文文档
1.3.4发布于 2026/07/29MITTypeScriptES Module零依赖构建来源已验证近 30 天下载 2888
原始使用文档
文档目录11 节
  1. Features
  2. Install
  3. Quick Example
  4. Functional API
  5. Class-Based API
  6. Caching
  7. Middleware
  8. Rate Limiting
  9. Circuit Breaker
  10. Testing
  11. Documentation

Foundatio Foundatio

NPM JSR Build status Discord

FetchClient is a tiny, typed wrapper around fetch with JSON helpers, caching, middleware, rate limiting, circuit breaker, timeouts, and friendly error handling.

Features

  • Typed JSON helpers - getJSON, queryJSON, postJSON, putJSON, patchJSON, deleteJSON
  • Two API styles - Functional or class-based - your choice
  • Response caching - TTL-based caching with tags for grouped invalidation
  • Middleware - Intercept requests/responses for logging, auth, transforms
  • Rate limiting - Per-domain rate limits with automatic header detection
  • Circuit breaker - Prevent cascading failures when services go down
  • Timeouts - Request timeouts with AbortSignal support
  • Error handling - RFC 7807 Problem Details support
  • Testing - MockRegistry for mocking HTTP in tests

Install

npm install @foundatiofx/fetchclient

Quick Example

FetchClient works two ways - pick whichever style you prefer:

Functional API

import { getJSON, postJSON, setBaseUrl } from "@foundatiofx/fetchclient";

setBaseUrl("https://api.example.com");

const { data: users } = await getJSON<User[]>("/users");
const { data: created } = await postJSON<User>("/users", { name: "Alice" });

Or use getFetchClient() to avoid multiple imports:

import { getFetchClient, setBaseUrl } from "@foundatiofx/fetchclient";

setBaseUrl("https://api.example.com");

const client = getFetchClient();
const { data: users } = await client.getJSON<User[]>("/users");
const { data: created } = await client.postJSON<User>("/users", {
  name: "Alice",
});

Class-Based API

import { FetchClient } from "@foundatiofx/fetchclient";

const client = new FetchClient({ baseUrl: "https://api.example.com" });
const { data } = await client.getJSON<User[]>("/users");

Caching

const response = await client.getJSON<User>("/api/users/1", {
  cacheKey: ["users", "1"],
  cacheDuration: 60000, // 1 minute
  cacheTags: ["users"],
});

// Invalidate by tag
client.cache.deleteByTag("users");

Middleware

import { useMiddleware } from "@foundatiofx/fetchclient";

useMiddleware(async (ctx, next) => {
  console.log("Request:", ctx.request.url);
  await next();
  console.log("Response:", ctx.response?.status);
});

Rate Limiting

import { usePerDomainRateLimit } from "@foundatiofx/fetchclient";

usePerDomainRateLimit({
  maxRequests: 100,
  windowSeconds: 60,
  updateFromHeaders: true, // Respect API rate limit headers
});

Circuit Breaker

import { useCircuitBreaker } from "@foundatiofx/fetchclient";

useCircuitBreaker({
  failureThreshold: 5,
  openDurationMs: 30000,
});

// When API fails repeatedly, circuit opens
// Requests return 503 immediately without hitting the API

Testing

import { FetchClientProvider } from "@foundatiofx/fetchclient";
import { MockRegistry } from "@foundatiofx/fetchclient/mocks";

const mocks = new MockRegistry();
mocks.onGet("/api/users").reply(200, [{ id: 1, name: "Alice" }]);

const client = new FetchClient();
mocks.install(client);

const { data } = await client.getJSON("/api/users");
// data = [{ id: 1, name: "Alice" }]

Documentation


MIT © Foundatio

版本与发布记录

@foundatiofx/fetchclient 共发布 13 个版本,首次发布于 2025/08/28;过去 12 个月发布了 10 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
1.3.4latest2026/07/293 个月前
1.3.32026/03/028 个月前
1.3.22026/03/028 个月前
1.3.12026/02/228 个月前
1.3.02026/02/228 个月前
1.2.22026/02/228 个月前

关于 @foundatiofx/fetchclient 的常见问题

@foundatiofx/fetchclient 是什么?

![Foundatio](https://raw.githubusercontent.com/foundatiofx/foundatio/master/media/foundatio.svg#gh-light-mode-only "Foun

如何安装 @foundatiofx/fetchclient?

在项目目录运行 npm install @foundatiofx/fetchclient,或使用 pnpm add / yarn add / bun add。

@foundatiofx/fetchclient 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@foundatiofx/fetchclient 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@foundatiofx/fetchclient 可以商用吗?

@foundatiofx/fetchclient 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@foundatiofx/fetchclient 的下载量有多少?

根据 npm 官方下载统计,@foundatiofx/fetchclient 在 2026/10/16 – 2026/11/14 这 30 天内被下载 2,888 次,日均约 96 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@foundatiofx/fetchclient 还在维护吗?

最新版本 1.3.4 发布于 2026/07/29(3 个月前);至今共发布 13 个版本,其中过去 12 个月发布了 10 个;首次发布于 2025/08/28。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。