
Security News
GitHub Actions Checkout Now Blocks Risky pull_request_target Checkouts
GitHub Actions checkout now blocks risky pull_request_target checkouts by default to help prevent pwn request supply chain attacks.
@huaiyou/api-client
Advanced tools
基于 Axios 的企业级网络请求库,专为 Monorepo 架构设计。采用分层架构与插件化设计,提供高可扩展性、强类型支持和多场景预设实例。
# 在 Monorepo 根目录
pnpm add @huaiyou/api-client --filter <your-package-name>
项目内置了三种常用的预设实例,开箱即用:
import { adminClient, webClient, thirdPartyClient } from '@huaiyou/api-client';
// 后台管理场景(自动重试、强错误提示)
const getUserList = () => {
return adminClient.get('/users', {
params: { page: 1 },
});
};
// 前台 C 端场景(GET 请求自动缓存、性能优先)
const getNews = () => {
return webClient.get('/news/latest');
};
// 第三方接口(无默认插件干扰)
const getGithubStars = () => {
return thirdPartyClient.get('https://api.github.com/repos/vuejs/core');
};
你可以在发起请求时,临时修改插件行为:
// 强制开启重试,即使是 webClient
webClient.get('/important-data', {
retry: {
retries: 3,
retryDelay: 1000,
},
});
// 禁用缓存,强制获取最新数据
webClient.get('/news/latest', {
cache: {
enable: false,
},
});
// 开启 Mock 数据(开发调试用)
adminClient.get('/users/1', {
mock: {
enable: true,
data: { id: 1, name: 'Mock User' },
delay: 500,
},
});
api-client 的核心能力通过插件实现,所有插件均支持在创建实例时配置,或在请求时动态覆盖。
防止短时间内发送相同的请求(如用户频繁点击按钮)。
{
requestKey?: string; // 手动指定请求唯一标识
}
网络不稳定或服务器临时错误时自动重试。
{
retries?: number; // 重试次数,默认 3
retryDelay?: number; // 重试间隔(ms),默认 1000
shouldRetry?: (error: any) => boolean; // 自定义重试条件
}
缓存 GET 请求的响应结果,减少网络请求。
{
enable?: boolean; // 是否开启
ttl?: number; // 缓存时间(ms),默认 5 * 60 * 1000
storage?: Storage; // 存储介质,默认 localStorage
}
拦截请求并返回模拟数据,无需后端接口。
{
enable?: boolean; // 是否开启
data?: any; // Mock 数据内容
delay?: number; // 模拟延迟(ms),默认 200
}
在控制台输出详细的请求和响应日志。
development) 开启。当接口返回 401 时,自动挂起并发请求,刷新 Token 后重试。
{
refreshToken: () => Promise<void>; // 刷新 Token 的具体实现
shouldRefresh?: (error: AxiosError) => boolean; // 自定义触发条件,默认 status === 401
}
自动为每个请求生成 UUID 并添加到 Header 中,方便全链路追踪。
{
headerName?: string; // Header 键名,默认 'X-Request-ID'
}
如果内置实例无法满足需求,你可以使用 createAxios 工厂函数创建完全自定义的实例。
import { createAxios, CancelRequestPlugin, RetryPlugin } from '@huaiyou/api-client';
const myClient = createAxios({
// Axios 基础配置
baseURL: 'https://api.myservice.com',
timeout: 10000,
headers: { 'X-App-Version': '1.0.0' },
// 插件列表(按需加载)
plugins: [new CancelRequestPlugin(), new RetryPlugin()],
// 拦截器
interceptors: {
request: (config) => {
// 自定义 Token 逻辑
config.headers['Authorization'] = 'Bearer my-token';
return config;
},
response: (res) => {
return res.data; // 直接返回 data 字段
},
error: (error) => {
// 自定义错误上报
reportError(error);
return Promise.reject(error);
},
},
});
graph TD
A[业务代码] --> B(Api Client Instances)
B --> C{Core Layer}
C --> D[Interceptors]
C --> E[Plugins]
subgraph Plugins [插件层]
E1[CancelRequest]
E2[Retry]
E3[Cache]
E4[Mock]
E5[Logger]
end
subgraph Core [核心层]
C1[createAxios Factory]
C2[Axios Instance]
end
subgraph Instances [预设实例]
I1[Admin Client]
I2[Web Client]
I3[ThirdParty Client]
end
adminClient 和 webClient 已经足够使用。config 覆盖即可。interface User {
id: number;
name: string;
}
// response 类型自动推导为 User
const user = await adminClient.get<User>('/users/1');
env.ts 中的配置,确保开发、测试、生产环境自动切换 baseURL。src/plugins 目录下实现 ApiPlugin 接口。src/config 目录下调整默认值。FAQs
Unified API client with axios and TypeScript
The npm package @huaiyou/api-client receives a total of 1 weekly downloads. As such, @huaiyou/api-client popularity was classified as not popular.
We found that @huaiyou/api-client demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
GitHub Actions checkout now blocks risky pull_request_target checkouts by default to help prevent pwn request supply chain attacks.

Product
Socket now supports Custom Roles and Repository Access Permissions so organizations can control who can access specific repositories and actions.

Product
Socket MCP now lets AI assistants review org alerts, investigate threats using the Socket threat feed, and inspect package files in addition to dependency scoring.