@quantabit/sdk-config
v1.0.9
Published
QuantaBit SDK 统一配置管理 - 所有SDK共享的API配置
Readme
@quantabit/sdk-config
QuantaBit SDK 统一配置管理模块。所有 SDK 共享此配置,修改一处即可全局生效。
功能特性
- 🔧 统一配置管理 - 所有 SDK 共享 API URL、超时等配置
- 🌍 环境预设 - 开发、测试、生产等环境一键切换
- 🔄 动态更新 - 支持运行时修改配置
- ⚛️ React Hook - 提供
useSDKConfigHook 便捷使用 - 🔐 Token 管理 - 统一的认证 Token 存储和管理
- 📝 日志系统 - 可配置的日志级别
安装
npm install @quantabit/sdk-config
# 或
yarn add @quantabit/sdk-config快速开始
1. 应用启动时初始化配置
// app/layout.tsx 或 main.jsx
import { initConfig } from "@quantabit/sdk-config";
// 使用自定义配置
initConfig({
apiBaseUrl: "https://api.yoursite.com/api/v1",
timeout: 15000,
debug: process.env.NODE_ENV !== "production",
});
// 或使用环境预设
import { initWithEnvironment } from "@quantabit/sdk-config";
initWithEnvironment("production");2. 在组件中使用 Hook
import { useSDKConfig } from "@quantabit/sdk-config";
function SettingsPanel() {
const { config, setApiBaseUrl } = useSDKConfig();
return (
<div>
<p>当前 API: {config.apiBaseUrl}</p>
<button onClick={() => setApiBaseUrl("https://new-api.com")}>
切换 API
</button>
</div>
);
}3. 在其他 SDK 中使用
// 其他 SDK 内部使用
import { getConfig, buildApiUrl, getToken } from "@quantabit/sdk-config";
async function fetchData() {
const config = getConfig();
const url = buildApiUrl("/users/me");
const token = getToken();
const response = await fetch(url, {
headers: {
Authorization: `Bearer ${token}`,
},
timeout: config.timeout,
});
return response.json();
}API 参考
配置函数
| 函数 | 说明 |
| ------------------------------------- | ---------------------------- |
| getConfig() | 获取当前配置对象 |
| initConfig(options) | 初始化配置(应用启动时调用) |
| setApiBaseUrl(url) | 设置 API 基础 URL |
| setWsBaseUrl(url) | 设置 WebSocket URL |
| setDebug(debug) | 设置调试模式 |
| resetConfig() | 重置为默认配置 |
| subscribeConfig(listener) | 订阅配置变更 |
| initWithEnvironment(env, overrides) | 使用环境预设初始化 |
Token 管理
| 函数 | 说明 |
| ----------------------------- | ------------------ |
| getToken() | 获取 Access Token |
| getRefreshToken() | 获取 Refresh Token |
| saveTokens(access, refresh) | 保存 Token |
| clearTokens() | 清除所有 Token |
| isAuthenticated() | 检查是否已认证 |
工具函数
| 函数 | 说明 |
| ----------------------------------- | ---------------- |
| buildApiUrl(endpoint, useFullUrl) | 构建完整 API URL |
| logger.debug/info/warn/error | 日志输出 |
配置项
| 配置项 | 类型 | 默认值 | 说明 |
| ------------ | ------- | -------------- | ---------------- |
| apiBaseUrl | string | /api/v1 | API 基础路径 |
| apiFullUrl | string | ''(需配置) | 完整 API URL |
| timeout | number | 30000 | 请求超时(毫秒) |
| retryCount | number | 3 | 重试次数 |
| debug | boolean | false | 调试模式 |
| logLevel | string | info | 日志级别 |
| wsBaseUrl | string | ''(需配置) | WebSocket URL |
环境预设
import { initWithEnvironment } from "@quantabit/sdk-config";
// 开发环境
initWithEnvironment("development");
// 测试环境
initWithEnvironment("staging");
// 生产环境
initWithEnvironment("production");
// 带覆盖配置
initWithEnvironment("production", {
timeout: 10000,
});与其他 SDK 集成
所有 QuantaBit SDK 都应从此包读取配置:
// 在 auth-sdk、wallet-sdk 等中
import {
getConfig,
buildApiUrl,
getToken,
logger,
} from "@quantabit/sdk-config";
class ApiClient {
async request(endpoint, options = {}) {
const config = getConfig();
const url = buildApiUrl(endpoint);
const token = getToken();
logger.debug("Requesting:", url);
const response = await fetch(url, {
...options,
headers: {
"Content-Type": "application/json",
Authorization: token ? `Bearer ${token}` : undefined,
...options.headers,
},
});
return response.json();
}
}License
MIT © QuantaBit Team
