@quantabit/sdk-config 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,184 @@
1
+ # @quantabit/sdk-config
2
+
3
+ Qbit_DID SDK 统一配置管理模块。所有 SDK 共享此配置,**修改一处即可全局生效**。
4
+
5
+ ## 功能特性
6
+
7
+ - 🔧 **统一配置管理** - 所有 SDK 共享 API URL、超时等配置
8
+ - 🌍 **环境预设** - 开发、测试、生产等环境一键切换
9
+ - 🔄 **动态更新** - 支持运行时修改配置
10
+ - ⚛️ **React Hook** - 提供 `useSDKConfig` Hook 便捷使用
11
+ - 🔐 **Token 管理** - 统一的认证 Token 存储和管理
12
+ - 📝 **日志系统** - 可配置的日志级别
13
+
14
+ ## 安装
15
+
16
+ ```bash
17
+ npm install @quantabit/sdk-config
18
+ # 或
19
+ yarn add @quantabit/sdk-config
20
+ ```
21
+
22
+ ## 快速开始
23
+
24
+ ### 1. 应用启动时初始化配置
25
+
26
+ ```javascript
27
+ // app/layout.tsx 或 main.jsx
28
+ import { initConfig } from "@quantabit/sdk-config";
29
+
30
+ // 使用自定义配置
31
+ initConfig({
32
+ apiBaseUrl: "https://api.yoursite.com/api/v1",
33
+ timeout: 15000,
34
+ debug: process.env.NODE_ENV !== "production",
35
+ });
36
+
37
+ // 或使用环境预设
38
+ import { initWithEnvironment } from "@quantabit/sdk-config";
39
+ initWithEnvironment("production");
40
+ ```
41
+
42
+ ### 2. 在组件中使用 Hook
43
+
44
+ ```jsx
45
+ import { useSDKConfig } from "@quantabit/sdk-config";
46
+
47
+ function SettingsPanel() {
48
+ const { config, setApiBaseUrl } = useSDKConfig();
49
+
50
+ return (
51
+ <div>
52
+ <p>当前 API: {config.apiBaseUrl}</p>
53
+ <button onClick={() => setApiBaseUrl("https://new-api.com")}>
54
+ 切换 API
55
+ </button>
56
+ </div>
57
+ );
58
+ }
59
+ ```
60
+
61
+ ### 3. 在其他 SDK 中使用
62
+
63
+ ```javascript
64
+ // 其他 SDK 内部使用
65
+ import { getConfig, buildApiUrl, getToken } from "@quantabit/sdk-config";
66
+
67
+ async function fetchData() {
68
+ const config = getConfig();
69
+ const url = buildApiUrl("/users/me");
70
+ const token = getToken();
71
+
72
+ const response = await fetch(url, {
73
+ headers: {
74
+ Authorization: `Bearer ${token}`,
75
+ },
76
+ timeout: config.timeout,
77
+ });
78
+
79
+ return response.json();
80
+ }
81
+ ```
82
+
83
+ ## API 参考
84
+
85
+ ### 配置函数
86
+
87
+ | 函数 | 说明 |
88
+ | ------------------------------------- | ---------------------------- |
89
+ | `getConfig()` | 获取当前配置对象 |
90
+ | `initConfig(options)` | 初始化配置(应用启动时调用) |
91
+ | `setApiBaseUrl(url)` | 设置 API 基础 URL |
92
+ | `setWsBaseUrl(url)` | 设置 WebSocket URL |
93
+ | `setDebug(debug)` | 设置调试模式 |
94
+ | `resetConfig()` | 重置为默认配置 |
95
+ | `subscribeConfig(listener)` | 订阅配置变更 |
96
+ | `initWithEnvironment(env, overrides)` | 使用环境预设初始化 |
97
+
98
+ ### Token 管理
99
+
100
+ | 函数 | 说明 |
101
+ | ----------------------------- | ------------------ |
102
+ | `getToken()` | 获取 Access Token |
103
+ | `getRefreshToken()` | 获取 Refresh Token |
104
+ | `saveTokens(access, refresh)` | 保存 Token |
105
+ | `clearTokens()` | 清除所有 Token |
106
+ | `isAuthenticated()` | 检查是否已认证 |
107
+
108
+ ### 工具函数
109
+
110
+ | 函数 | 说明 |
111
+ | ----------------------------------- | ---------------- |
112
+ | `buildApiUrl(endpoint, useFullUrl)` | 构建完整 API URL |
113
+ | `logger.debug/info/warn/error` | 日志输出 |
114
+
115
+ ## 配置项
116
+
117
+ | 配置项 | 类型 | 默认值 | 说明 |
118
+ | ------------ | ------- | -------------- | ---------------- |
119
+ | `apiBaseUrl` | string | `/api/v1` | API 基础路径 |
120
+ | `apiFullUrl` | string | `''`(需配置) | 完整 API URL |
121
+ | `timeout` | number | `30000` | 请求超时(毫秒) |
122
+ | `retryCount` | number | `3` | 重试次数 |
123
+ | `debug` | boolean | `false` | 调试模式 |
124
+ | `logLevel` | string | `info` | 日志级别 |
125
+ | `wsBaseUrl` | string | `''`(需配置) | WebSocket URL |
126
+
127
+ ## 环境预设
128
+
129
+ ```javascript
130
+ import { initWithEnvironment } from "@quantabit/sdk-config";
131
+
132
+ // 开发环境
133
+ initWithEnvironment("development");
134
+
135
+ // 测试环境
136
+ initWithEnvironment("staging");
137
+
138
+ // 生产环境
139
+ initWithEnvironment("production");
140
+
141
+ // 带覆盖配置
142
+ initWithEnvironment("production", {
143
+ timeout: 10000,
144
+ });
145
+ ```
146
+
147
+ ## 与其他 SDK 集成
148
+
149
+ 所有 Qbit_DID SDK 都应从此包读取配置:
150
+
151
+ ```javascript
152
+ // 在 auth-sdk、wallet-sdk 等中
153
+ import {
154
+ getConfig,
155
+ buildApiUrl,
156
+ getToken,
157
+ logger,
158
+ } from "@quantabit/sdk-config";
159
+
160
+ class ApiClient {
161
+ async request(endpoint, options = {}) {
162
+ const config = getConfig();
163
+ const url = buildApiUrl(endpoint);
164
+ const token = getToken();
165
+
166
+ logger.debug("Requesting:", url);
167
+
168
+ const response = await fetch(url, {
169
+ ...options,
170
+ headers: {
171
+ "Content-Type": "application/json",
172
+ Authorization: token ? `Bearer ${token}` : undefined,
173
+ ...options.headers,
174
+ },
175
+ });
176
+
177
+ return response.json();
178
+ }
179
+ }
180
+ ```
181
+
182
+ ## License
183
+
184
+ MIT © QuantaBit Team