@alt-stack/http-client-fetch

中文文档
1.8.2发布于 2026/10/20MITTypeScriptESM + CJS近 30 天下载 5991

Type-safe HTTP client using native fetch API

原始使用文档
文档目录4 节
  1. Install
  2. Quick use
  3. Custom fetch and auth headers
  4. Documentation

@alt-stack/http-client-fetch

Typed Zod-backed HTTP client using the standard Fetch API. Successful and documented error responses include the native Response as raw.

Install

pnpm add @alt-stack/http-client-fetch zod

Use Zod 4 and a runtime with fetch, Response, Headers, and AbortController (Node.js 18+, modern browsers, or Bun).

Quick use

import { createApiClient } from "@alt-stack/http-client-fetch";
import { Request, Response } from "./generated-api.js";

const api = createApiClient({
  baseUrl: "http://127.0.0.1:3000",
  Request,
  Response,
  fetchOptions: { credentials: "include" },
});

const result = await api.get("/users/{id}", { params: { id: "u_1" } });
if (result.success) console.log(result.body, result.raw.status);

Request and Response can be generated by @alt-stack/zod-openapi. Fetch options cannot override the executor-owned method, headers, body, or signal.

Custom fetch and auth headers

import { customFetch, getToken } from "./runtime.js";

const api = createApiClient({
  baseUrl: "https://api.example.test",
  Request,
  Response,
  fetch: customFetch,
  headers: async () => ({ Authorization: `Bearer ${await getToken()}` }),
});

await api.get("/users/{id}", {
  params: { id: "u_1" },
  headers: { "X-Request-Id": "request-123" },
});

Provide your own customFetch: typeof globalThis.fetch for proxies, platform polyfills, instrumentation, or tests. Both createApiClient and new FetchExecutor({ fetch: customFetch }) accept it. Without it, the executor reads globalThis.fetch at each attempt. The selected function is called without binding a receiver.

Client headers may be a static record or a sync/async provider. Providers run once per attempt, including retries, before request headers overlay them. Nullish values are omitted. Provider failures throw a sanitized UnexpectedApiClientError without retaining the original message or cause. See the core API for merge, retry, and timeout details.

Documentation

版本与发布记录

@alt-stack/http-client-fetch 共发布 13 个版本,首次发布于 2025/12/05;过去 12 个月发布了 13 个版本。

版本发布日期距今
1.8.2latest2026/10/2018 天前
1.8.12026/10/2019 天前
1.8.02026/10/2019 天前
1.6.22026/08/202 个月前
1.6.12026/07/283 个月前
1.6.02026/07/223 个月前

关于 @alt-stack/http-client-fetch 的常见问题

@alt-stack/http-client-fetch 是什么?

Type-safe HTTP client using native fetch API

如何安装 @alt-stack/http-client-fetch?

在项目目录运行 npm install @alt-stack/http-client-fetch,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:zod(^4.1.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@alt-stack/http-client-fetch 支持 TypeScript 吗?

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

@alt-stack/http-client-fetch 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@alt-stack/http-client-fetch 可以商用吗?

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

@alt-stack/http-client-fetch 的下载量有多少?

根据 npm 官方下载统计,@alt-stack/http-client-fetch 在 2026/10/06 – 2026/11/04 这 30 天内被下载 5,991 次,日均约 200 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@alt-stack/http-client-fetch 还在维护吗?

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