@happy-ts/fetch-t

中文文档
1.10.0发布于 2026/05/25MITTypeScriptESM + CJS构建来源已验证近 30 天下载 4791

Type-safe Fetch API wrapper with abortable requests, timeout support, progress tracking, automatic retry, and Rust-like Result error handling.

原始使用文档
文档目录11 节
  1. Features
  2. Installation
  3. Quick Start
  4. Basic Usage
  5. Abortable Request
  6. Automatic Retry
  7. Examples
  8. Error Handling Design
  9. Programming Errors (Synchronous)
  10. Runtime Errors (Result Type)
  11. License

fetchT

License Build Status codecov NPM version NPM downloads JSR Version JSR Score

Type-safe Fetch API wrapper with abortable requests, timeout support, progress tracking, automatic retry, and Rust-like Result error handling.


中文 | API Documentation


Features

  • Abortable Requests - Cancel requests anytime via FetchTask.abort()
  • Type-safe Responses - Specify return type with responseType parameter (text, json, arraybuffer, bytes, blob, stream)
  • Timeout Support - Auto-abort requests after specified milliseconds
  • Progress Tracking - Monitor download progress with onProgress callback
  • Chunk Streaming - Access raw data chunks via onChunk callback
  • Automatic Retry - Configurable retry strategies with retry option
  • Result Error Handling - Rust-like Result type for explicit error handling
  • Cross-platform - Works with Deno, Node.js, Bun, and browsers

Installation

# npm
npm install @happy-ts/fetch-t

# yarn
yarn add @happy-ts/fetch-t

# pnpm
pnpm add @happy-ts/fetch-t

# JSR (Deno)
deno add @happy-ts/fetch-t

# JSR (Bun)
bunx jsr add @happy-ts/fetch-t

Quick Start

Basic Usage

import { fetchT } from '@happy-ts/fetch-t';

// GET JSON data
const result = await fetchT<{ id: number; title: string }>('https://api.example.com/data', {
    responseType: 'json',
});

result.inspect(data => {
    console.log(data.title);
}).inspectErr(err => {
    console.error('Request failed:', err.message);
});

Abortable Request

const task = fetchT('https://api.example.com/large-file', {
    abortable: true,
    responseType: 'arraybuffer',
});

// Abort after 5 seconds
setTimeout(() => {
    task.abort('User cancelled');
}, 5000);

const result = await task.result;

Automatic Retry

const result = await fetchT('https://api.example.com/data', {
    retry: {
        retries: 3,
        delay: (attempt) => Math.min(1000 * Math.pow(2, attempt - 1), 10000),
        when: [500, 502, 503, 504],
        onRetry: (error, attempt) => console.log(`Retry ${attempt}: ${error.message}`),
    },
    responseType: 'json',
});

Examples

Error Handling Design

fetchT distinguishes between two types of errors:

Programming Errors (Synchronous)

Invalid parameters throw immediately for fail-fast behavior:

// These throw synchronously - no try/catch around await needed
fetchT('https://example.com', { timeout: -1 });     // Error: timeout must be > 0
fetchT('https://example.com', { timeout: 'bad' });  // TypeError: timeout must be a number
fetchT('not-a-url');                                // TypeError: Invalid URL

This differs from native fetch, which returns rejected Promises for parameter errors. Synchronous throws provide clearer stack traces and catch bugs during development.

Runtime Errors (Result Type)

Network failures and HTTP errors are wrapped in Result type:

const result = await fetchT('https://api.example.com/data', { responseType: 'json' });

if (result.isErr()) {
    const error = result.unwrapErr();
    // FetchError with status code, or network Error
}

License

MIT

版本与发布记录

@happy-ts/fetch-t 共发布 37 个版本,首次发布于 2024/05/09;过去 12 个月发布了 12 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
1.10.0latest2026/05/255 个月前
1.9.12026/01/1610 个月前
1.9.02026/01/1310 个月前
1.8.12026/01/1310 个月前
1.8.02026/01/1210 个月前
1.7.02026/01/0810 个月前

关于 @happy-ts/fetch-t 的常见问题

@happy-ts/fetch-t 是什么?

Type-safe Fetch API wrapper with abortable requests, timeout support, progress tracking, automatic retry, and Rust-like Result error handling.

如何安装 @happy-ts/fetch-t?

在项目目录运行 npm install @happy-ts/fetch-t,或使用 pnpm add / yarn add / bun add。

@happy-ts/fetch-t 支持 TypeScript 吗?

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

@happy-ts/fetch-t 支持 ESM 还是 CommonJS?

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

@happy-ts/fetch-t 可以商用吗?

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

@happy-ts/fetch-t 的下载量有多少?

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

@happy-ts/fetch-t 还在维护吗?

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