@guohanmin/http
v1.0.0企业级 HTTP 请求库:统一响应结构、请求/响应/错误拦截器、超时与重试,支持 fetch/Axios/ky/Alova 传输适配器及 TanStack Query/SWR 集成
pnpm add @guohanmin/http23 public exports, grouped by kind.
Functions
- createHttpClientFunction
No description provided.
View details - createHttpErrorFunction
创建一个 {@link HttpError}
View details - createTimeoutSignalFunction
创建一个在指定毫秒数后自动以 `TimeoutError` abort 的 signal
View details - defaultRetryConditionFunction
默认重试条件:幂等方法(GET/HEAD/PUT/DELETE)+ 502/503/504/429 + 网络错误/超时。 用户主动通过 AbortSignal 取消(`code === 'ERR_CANCELED'`)永远不重试。
View details - defaultRetryDelayFunction
默认重试延迟:指数退避(200ms 基数)+ 0~100ms 随机抖动,避免重试风暴
View details - isHttpErrorFunction
判断一个值是否是本库抛出的 {@link HttpError}
View details - mergeSignalsFunction
合并多个 AbortSignal 为一个新的 AbortSignal:任意一个输入 signal 触发 abort, 合并后的 signal 也会以相同的 `reason` abort。 不依赖 `AbortSignal.any()`(兼容性风险),改为手写事件监听 + 清理。
View details - serializeBodyFunction
统一的请求体序列化逻辑(核心统一处理,不下放各适配器,避免出现细微不一致, 尤其影响文件上传场景)。 `FormData`/`Blob`/`ArrayBuffer`/`URLSearchParams`/`ReadableStream`/字符串原样传递, 不设置 `Content-Type: application/json`;其余情况使用 `JSON.stringify` 并设置该 header。
View details - sleepFunction
可被 AbortSignal 中断的 sleep,用于重试等待与超时倒计时
View details
Classes
Interfaces
- HttpAdapterInterface
适配器契约:把 {@link RequestConfig} 转换成对应底层库的调用方式。 约束(必须遵守,否则会和核心的重试/拦截器逻辑重复执行): - 不得启用底层库自带的重试/缓存/拦截器能力(Alova 的 `retry`/`cacheFor`、ky 的 `retry`/`hooks`、axios 的 `interceptors.*.use`),必须以“最裸”的方式调用底层库。 - 对于“确实收到 HTTP 响应但状态码非 2xx”的情况,必须返回正常的 {@link HttpResponse}, 而不是抛错——由核心的 `validateStatus` 统一判断成功/失败。 - 只有真正的网络错误才应该 reject;超时/主动 abort 由核心通过 signal 统一处理, 适配器只需要让传入的 `signal` 触发 abort 时原样把错误抛出即可。
View details - HttpClientInterface
`createHttpClient` 返回的客户端实例
View details - HttpClientOptionsInterface
{@link createHttpClient} 的配置项
View details - HttpErrorInterface
统一错误结构
View details - HttpResponseInterface
统一响应结构(传输层,始终生效,抹平 fetch/axios/ky/alova 之间的形状差异)
View details - ManagedSignalInterface
{@link mergeSignals}/{@link createTimeoutSignal} 的返回值
View details - RequestConfigInterface
请求配置
View details - RetryOptionsInterface
重试选项
View details - SerializedBodyInterface
{@link serializeBody} 的返回值
View details
Type Aliases
- FulfilledHandlerType
拦截器的“成功”处理器:接收上一步的值,返回新值(可以是异步的)
View details - HttpMethodType
HTTP 方法
View details - RejectedHandlerType
拦截器的“失败”处理器:接收 {@link HttpError},返回一个恢复值(可以是异步的)
View details - ResponseTypeType
响应体解析方式,默认 `'json'`。 为 `'stream'` 时核心不会消费 body,`HttpResponse.data` 直接是适配器给出的原始流/Response 对象,用于 SSE、下载进度等场景。 已知限制:axios 的 `responseType: 'stream'` 仅在 Node 环境下可用,浏览器端 axios 不支持, 这是 axios 适配器自身的限制,本库不会强行抹平。
View details