# @faremeter/fetch 中文文档

规范地址：https://npmcn.com/package/%40faremeter/fetch/

版本：0\.22\.0
npm 元数据更新时间：2026-05-27T14:24:34.506Z
中文译文更新时间：2026-11-14T10:17:01.625Z
软件包许可：LGPL-3\.0-only

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40faremeter/fetch
原文对照：https://npmcn.com/package/%40faremeter/fetch/?lang=original

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：158 kB，22 个文件
- 模块格式：ESM + CJS
- TypeScript 类型：包内自带
- 运行依赖：3 个
- 首次发布：2025-07-15T20:50:26.328Z
- 最新版本发布：2026-05-27T14:24:34.359Z
- 近 30 天下载量：3976（2026-10-15 至 2026-11-13，npm 官方下载统计）
- 已发布版本数：33（近 12 个月 12 个）

## 中文简介

HTTP 获取自动处理 402 的包装器

## 安装

```sh
npm install @faremeter/fetch
```

也可以使用：`pnpm add @faremeter/fetch`、`yarn add @faremeter/fetch`、`bun add @faremeter/fetch`

## README

# @ faremeter/ fatch (缩写)

HTTP获取可自动处理402 Pay required response with the x402 protocol.

## 安装

```bash
pnpm install @faremeter/fetch
```

## 特征

-   402 自动处理 - 透明支付和重试
-   粘接支付处理器 - 支持任何块链
-   多链支持 - 同时使用多个处理器
-   智能支付者选择 - 基于平衡,成本等选择.
-   重试逻辑 - 可配置的指数后退
-   类型安全 - 全类型脚本支持

## API 参考

## 职能

-   [选择第一个可用](https://npmcn.com/package/%40faremeter/fetch/#readme-choosefirstavailable)
-   [所需付款](https://npmcn.com/package/%40faremeter/fetch/#readme-processpaymentrequiredresponse)
-   [所需付款](https://npmcn.com/package/%40faremeter/fetch/#readme-processpaymentrequiredresponsempp)
-   [关闭](https://npmcn.com/package/%40faremeter/fetch/#readme-wrap)
-   [响应Feeder](https://npmcn.com/package/%40faremeter/fetch/#readme-responsefeeder)

### 选择第一个可用

选择第一个可用支付执行者的默认支付者选择者 .

| 函数 | 类型 |
| --- | --- |
| `chooseFirstAvailable` | `(possiblePayers: PaymentExecer[]) => PaymentExecer` |

参数:

-   `possiblePayers`: - 能够处理要求的付款执行人分组

回返情况:

数组中的第一个执行者

### 所需付款

处理一个402 付款要求响应,自动检测v1或v2协议.

| 函数 | 类型 |
| --- | --- |
| `processPaymentRequiredResponse` | `(ctx: RequestContext, response: Response, options: ProcessPaymentRequiredResponseOpts) => Promise<ProcessPaymentRequiredResponseResult>` |

参数:

-   `ctx`: - 请求上下文
-   `response`: - 402 反应对象(不一定被消耗)
-   `options`: - 处理选项,包括支付处理器

回返情况:

付款信息,包括信头和检测到的版本

### 所需付款

| 函数 | 类型 |
| --- | --- |
| `processPaymentRequiredResponseMPP` | `(response: Response, handlers: MPPPaymentHandler[], opts?: ProcessMPPOpts or undefined) => Promise<string or undefined>` |

### 关闭

用自动的 x402 支付处理来包裹获取函数 .

当收到402 付款要求回复时, 包装器会自动 处理付款要求,通过配置的处理器执行付款, 并用所附的付款信头重复请求.

| 函数 | 类型 |
| --- | --- |
| `wrap` | `(phase2Fetch: (input: RequestInfo or URL, init?: RequestInit or undefined) => Promise<Response>, options: WrapOpts) => (input: RequestInfo or URL, init?: RequestInit) => Promise<...>` |

参数:

-   `phase2Fetch`: - 用于付费请求的获取功能(第二阶段)
-   `options`: - 配置,包括支付处理器和重试设置

回返情况:

与本地抓取相同的签名所包裹的抓取函数

### 响应Feeder

创建从队列中返回响应的模拟获取.

每次调用返回的取回函数都会将下一个响应从数组中移出 . 反应可以是响应对象,也可以是获取动态行为功能.

| 函数 | 类型 |
| --- | --- |
| `responseFeeder` | `(responses: (Response or ((input: RequestInfo or URL, init?: RequestInit or undefined) => Promise<Response>))[]) => (input: RequestInfo or URL, init?: RequestInit or undefined) => Promise<...>` |

参数:

-   `responses`: - 响应矩阵或获取函数返回顺序

回返情况:

从队列中获取响应的函数

## 已包装的抓取错误

在用尽所有尝试后, 付款失败时发生错误 . 包含最后402项检查答复.

## 类型

-   [检测到的图像](https://npmcn.com/package/%40faremeter/fetch/#readme-detectedversion)
-   [进程付款要求](https://npmcn.com/package/%40faremeter/fetch/#readme-processpaymentrequiredresponseopts)
-   [所需付款](https://npmcn.com/package/%40faremeter/fetch/#readme-processpaymentrequiredresponseresult)
-   [进程预览](https://npmcn.com/package/%40faremeter/fetch/#readme-processmppopts)
-   [包装](https://npmcn.com/package/%40faremeter/fetch/#readme-wrapopts)
-   [摩克纹章](https://npmcn.com/package/%40faremeter/fetch/#readme-mockfetchtype)
-   [缩写回复](https://npmcn.com/package/%40faremeter/fetch/#readme-mockresponse)

### 检测到的图像

| 类型 | 类型 |
| --- | --- |
| `DetectedVersion` | `1 or 2` |

### 进程付款要求

处理402 付款要求答复的备选方案.

| 类型 | 类型 |
| --- | --- |
| `ProcessPaymentRequiredResponseOpts` | `{ /** Payment handlers that produce execers for payment requirements. */ handlers: PaymentHandler[]; /** Optional function to select among multiple possible payers. Defaults to chooseFirstAvailable. */ payerChooser?: (execer: PaymentExecer[]) => Promise<PaymentExecer>; }` |

### 所需付款

402 所需付款的处理结果.

| 类型 | 类型 |
| --- | --- |
| `ProcessPaymentRequiredResponseResult` | `{ /** The selected payment execer. */ payer: PaymentExecer; /** The result from executing the payment. */ payerResult: { payload: object }; /** The payment payload in the detected protocol version format. */ paymentPayload: x402PaymentPayload or x402PaymentPayloadV1; /** Base64-encoded payment header ready to attach to the retry request. */ paymentHeader: string; /** The detected protocol version (1 or 2). */ detectedVersion: DetectedVersion; }` |

### 进程预览

试图处理402个响应,作为MPP的挑战.

检查有支付挑战的 WWW- Authenticate 头条, 然后 要求处理者找到匹配的 返回授权 成功时头值, 如果没有 MPP 挑战, 则未定义 或者没有处理器匹配(允许掉入到x402).

不消耗反应体.

| 类型 | 类型 |
| --- | --- |
| `ProcessMPPOpts` | `{ bodyDigest?: string; }` |

### 包装

以 x402 支付处理来包裹抓取函数的配置选项 .

| 类型 | 类型 |
| --- | --- |
| `WrapOpts` | `ProcessPaymentRequiredResponseOpts and { /** MPP payment handlers for Authorization: Payment flow. */ mppHandlers?: MPPPaymentHandler[]; /** Optional fetch function for the initial request (phase 1). Defaults to phase2Fetch. */ phase1Fetch?: typeof fetch; /** Number of retry attempts after initial failure. Defaults to 2. */ retryCount?: number; /** Initial delay between retries in milliseconds. Doubles after each attempt. Defaults to 100. */ initialRetryDelay?: number; /** If true, returns the 402 response instead of throwing on payment failure. */ returnPaymentFailure?: boolean; }` |

### 摩克纹章

| 类型 | 类型 |
| --- | --- |
| `MockFetchType` | `typeof fetch` |

### 缩写回复

| 类型 | 类型 |
| --- | --- |
| `MockResponse` | `Response` |

## 实例

参见 [票价计算仓库](https://github.com/faremeter/faremeter/tree/main/scripts)数字 :

-   [索拉纳付款实例](https://github.com/faremeter/faremeter/blob/main/scripts/solana-example/sol-payment.ts)
-   [EVM 支付实例](https://github.com/faremeter/faremeter/blob/main/scripts/evm-example/base-sepolia-payment.ts)

## 相关软件包

-   [@faremeter/ 骑行](https://www.npmjs.com/package/@faremeter/rides) - 高级SDK
-   [@faremeter/ pay-solana (英语)](https://www.npmjs.com/package/@faremeter/payment-solana) - 索拉纳付款管理人
-   [@faremeter/ pay-evm (英语)](https://www.npmjs.com/package/@faremeter/payment-evm) - EVM付款管理人
-   [@ faremeter/ 中间软件](https://www.npmjs.com/package/@faremeter/middleware) - 服务器端中间软件

## 许可证

LGPL-3.0 仅限

## 常见问题

### @faremeter/fetch 是什么？

HTTP 获取自动处理 402 的包装器

### 如何安装 @faremeter/fetch？

在项目目录运行 npm install @faremeter/fetch，或使用 pnpm add / yarn add / bun add。

### @faremeter/fetch 支持 TypeScript 吗？

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

### @faremeter/fetch 支持 ESM 还是 CommonJS？

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

### @faremeter/fetch 可以商用吗？

@faremeter/fetch 使用 LGPL-3\.0-only 许可。弱 Copyleft：作为库被调用时通常不要求开源你自己的代码，但修改库本身需以 LGPL 开源；前端打包合并后的适用边界存在不同理解。以上为概括，不构成法律意见，请以许可原文为准。

### @faremeter/fetch 的下载量有多少？

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

### @faremeter/fetch 还在维护吗？

最新版本 0\.22\.0 发布于 2026/05/27（5 个月前）；至今共发布 33 个版本，其中过去 12 个月发布了 12 个；首次发布于 2025/07/16。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

