npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@tni/http

v2.0.1

Published

基于 Axios 的 HTTP 客户端:拦截器、上传下载、SSE、预设响应处理

Downloads

40

Readme

@tni/http

基于 axios 的模块化 HTTP 客户端封装,面向前端应用的 API 层统一收敛请求实例、拦截器、上传下载、SSE、业务响应解包和结构化错误。

设计目标

  • 保持 axios 原生配置兼容:HttpClientOptions 继承 CreateAxiosDefaults,单次请求使用 HttpClientConfig
  • 让能力模块边界清晰:主客户端只负责编排,上传、下载、SSE、拦截器管理分别放在 modules/*
  • 保持可维护可扩展:默认配置、工厂、错误模型、header 合并、params 序列化工具均独立维护。
  • 不绑定 UI 框架:错误提示通过 makeErrorMessage 回调交给业务层实现 Toast、Message 或日志。
  • axios 作为 peer dependency:宿主项目统一控制 axios 版本,包本身只封装项目协议。

目录结构

src/
  index.ts                    # npm 包统一入口
  core/
    client-contract.ts        # 模块依赖的最小客户端契约
    defaults.ts               # DEFAULT_HTTP_CLIENT_OPTIONS
    factory.ts                # createHttpClient / defHttp
    http-client.ts            # 主客户端编排
  interceptors/
    interceptor-manager.ts    # 拦截器注册、移除、复用
    preset-interceptors.ts    # 常用响应拦截器预设
  modules/
    file-downloader.ts        # 下载能力
    file-uploader.ts          # 上传能力
    sse.ts                    # fetch 版 SSE
  shared/
    axios-augment.ts          # axios 模块扩展
    errors.ts                 # HttpResponseError
    types.ts                  # 对外类型
    utils/
      headers.ts              # header 归一化与合并
      params-serializer.ts    # qs 数组序列化策略
      merge-options.ts        # 默认配置合并

快速开始

import {
  createHttpClient,
  defaultResponseInterceptor,
  errorMessageResponseInterceptor,
} from "@tni/http";

export const http = createHttpClient(
  {
    baseURL: "/api",
    timeout: 10_000,
  },
  (client) => {
    client.addRequestInterceptor({
      fulfilled: (config) => {
        const token = localStorage.getItem("token");
        if (token) {
          config.headers.Authorization = `Bearer ${token}`;
        }
        return config;
      },
    });

    client.addResponseInterceptor(
      defaultResponseInterceptor({
        codeField: "code",
        dataField: "data",
        successCode: 0,
      }),
    );

    client.addResponseInterceptor(errorMessageResponseInterceptor((msg) => console.error(msg)));
  },
);
const user = await http.get<{ id: number; name: string }>("/users/me", {
  responseReturn: "data",
});

核心 API

  • createHttpClient(options?, setup?):推荐的实例创建方式,适合在应用 API 层统一注册 baseURL、鉴权和响应协议。
  • new HttpClient(options?):类方式创建实例,适合需要继承或测试时精细控制。
  • defHttp:默认实例,适合轻量场景;复杂应用建议自行创建实例。
  • client.instance:底层 axios 实例,保留原生扩展能力。
  • client.addRequestInterceptor(config) / client.addResponseInterceptor(config):注册拦截器并返回 id。
  • client.ejectRequestInterceptor(id) / client.ejectResponseInterceptor(id):按 id 移除拦截器。
  • client.clearRequestInterceptors() / client.clearResponseInterceptors():清空当前实例拦截器,主要用于测试或实例销毁。
  • client.get/post/put/delete/request:标准 HTTP 请求入口。
  • client.upload(url, data, config?):multipart 上传,内部构造 FormData
  • client.download(url, config?):Blob 下载,默认 responseType: "blob"
  • client.requestSSE(url, data?, options?) / client.postSSE(url, data?, options?):fetch 版流式读取,同时复用请求拦截器注入的 header。

响应处理

默认 HttpClient 不做业务解包,返回完整 AxiosResponse。如需按 { code, data, message } 协议返回业务数据,注册 defaultResponseInterceptor 并在单次请求里声明 responseReturn: "data"

client.addResponseInterceptor(
  defaultResponseInterceptor({
    codeField: "code",
    dataField: "data",
    successCode: 0,
  }),
);

const list = await client.get("/users", { responseReturn: "data" });

如果业务响应不满足成功协议,会抛出 HttpResponseError,可读取 codedataresponse

import { HttpResponseError } from "@tni/http";

try {
  await client.get("/users", { responseReturn: "data" });
} catch (error) {
  if (error instanceof HttpResponseError) {
    console.error(error.code, error.message, error.data);
  }
}

参数序列化

paramsSerializer 支持 axios 原生函数,也支持内置策略:

  • bracketsids[]=1&ids[]=2
  • indicesids[0]=1&ids[1]=2
  • repeatids=1&ids=2
  • commaids=1,2
await client.get("/users", {
  params: { ids: [1, 2, 3] },
  paramsSerializer: "brackets",
});

推荐接入方式

  1. 在业务应用的 src/api/client.ts 创建并导出唯一或少量 HttpClient 实例。
  2. 请求鉴权、业务码解包、错误提示统一放在实例拦截器,不分散写到页面组件。
  3. 页面和 store 只调用 src/api/* 的业务函数,不直接依赖 axios 或拼接基础 URL。
  4. 上传、下载、SSE 优先复用本包模块方法,确有特殊协议时再访问 client.instance

验证

vp run http#typecheck
vp test packages/http
vp check packages/http