@ faremeter/ fatch (缩写)
HTTP获取可自动处理402 Pay required response with the x402 protocol.
安装
pnpm install @faremeter/fetch
特征
- 402 自动处理 - 透明支付和重试
- 粘接支付处理器 - 支持任何块链
- 多链支持 - 同时使用多个处理器
- 智能支付者选择 - 基于平衡,成本等选择.
- 重试逻辑 - 可配置的指数后退
- 类型安全 - 全类型脚本支持
API 参考
职能
选择第一个可用
选择第一个可用支付执行者的默认支付者选择者 .
| 函数 | 类型 |
|---|---|
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项检查答复.
类型
检测到的图像
| 类型 | 类型 |
|---|---|
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 |
实例
参见 票价计算仓库数字 :
相关软件包
- @faremeter/ 骑行 - 高级SDK
- @faremeter/ pay-solana (英语) - 索拉纳付款管理人
- @faremeter/ pay-evm (英语) - EVM付款管理人
- @ faremeter/ 中间软件 - 服务器端中间软件
许可证
LGPL-3.0 仅限