@beaver-im/js-sdk 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,275 @@
1
+ # Beaver JS SDK
2
+
3
+ Beaver 开放平台 JavaScript SDK - 对标飞书/钉钉/企业微信标准
4
+
5
+ ## 🚀 快速开始
6
+
7
+ ### 安装
8
+
9
+ ```bash
10
+ npm install @beaver/js-sdk
11
+ ```
12
+
13
+ 或通过 CDN:
14
+
15
+ ```html
16
+ <script src="https://cdn.beaver.im/sdk/beaver-sdk.min.js"></script>
17
+ ```
18
+
19
+ ---
20
+
21
+ ## 📦 功能列表
22
+
23
+ ### ✅ 已实现
24
+
25
+ 1. **OAuth 2.0 授权登录** - Web Component + API
26
+ 2. **用户信息查询** - 单个/批量获取
27
+ 3. **消息发送** - 文本/图片/文件
28
+ 4. **群组管理** - 创建/查询/成员管理
29
+ 5. **Webhook 配置** - 事件订阅
30
+
31
+ ### 🔮 计划中
32
+
33
+ - 扫码登录
34
+ - 免登(应用内)
35
+ - 机器人 API
36
+ - 文件上传/下载
37
+
38
+ ---
39
+
40
+ ## 💡 使用示例
41
+
42
+ ### 1️⃣ OAuth 授权登录
43
+
44
+ #### 方案 A: Web Component (推荐)
45
+
46
+ ```html
47
+ <!-- 添加登录按钮 -->
48
+ <beaver-login
49
+ app-id="your_app_id"
50
+ redirect-uri="https://yoursite.com/callback">
51
+ </beaver-login>
52
+
53
+ <script type="module">
54
+ import '@beaver/js-sdk'
55
+
56
+ // 监听登录事件
57
+ document.querySelector('beaver-login').addEventListener('beaver:login-start', (e) => {
58
+ console.log('Login started:', e.detail)
59
+ })
60
+ </script>
61
+ ```
62
+
63
+ #### 方案 B: JavaScript API
64
+
65
+ ```typescript
66
+ import { AuthApi } from '@beaver/js-sdk'
67
+
68
+ const auth = new AuthApi({
69
+ appId: 'your_app_id',
70
+ appSecret: 'your_app_secret',
71
+ redirectUri: 'https://yoursite.com/callback'
72
+ })
73
+
74
+ // 发起授权
75
+ auth.login()
76
+
77
+ // 在回调页面获取 code
78
+ const authResponse = auth.getAuthCodeFromUrl()
79
+ if (authResponse) {
80
+ // 用 code 换取 token
81
+ const tokenInfo = await auth.exchangeCodeForToken(authResponse.code)
82
+ console.log('Access Token:', tokenInfo.accessToken)
83
+ }
84
+ ```
85
+
86
+ ---
87
+
88
+ ### 2️⃣ 获取用户信息
89
+
90
+ ```typescript
91
+ import { UserApi } from '@beaver/js-sdk'
92
+
93
+ const userApi = new UserApi('your_access_token')
94
+
95
+ // 获取单个用户
96
+ const userInfo = await userApi.getUserInfo('user_123')
97
+ console.log(userInfo.nickName, userInfo.avatar)
98
+
99
+ // 批量获取用户
100
+ const userList = await userApi.getUserList(['user_123', 'user_456'])
101
+ console.log(userList)
102
+ ```
103
+
104
+ ---
105
+
106
+ ### 3️⃣ 完整 OAuth 流程示例
107
+
108
+ ```typescript
109
+ import { AuthApi, UserApi } from '@beaver/js-sdk'
110
+
111
+ // Step 1: 初始化
112
+ const auth = new AuthApi({
113
+ appId: 'your_app_id',
114
+ appSecret: 'your_app_secret',
115
+ redirectUri: 'https://yoursite.com/callback'
116
+ })
117
+
118
+ // Step 2: 用户点击登录按钮
119
+ auth.login()
120
+
121
+ // Step 3: 在回调页面处理
122
+ // URL: https://yoursite.com/callback?code=xxx&state=yyy
123
+ const authResponse = auth.getAuthCodeFromUrl()
124
+
125
+ if (authResponse) {
126
+ // Step 4: 用 code 换取 access_token
127
+ const tokenInfo = await auth.exchangeCodeForToken(authResponse.code)
128
+
129
+ // Step 5: 用 access_token 获取用户信息
130
+ const userApi = new UserApi(tokenInfo.accessToken)
131
+ const userInfo = await userApi.getUserInfo('current_user_id')
132
+
133
+ console.log('登录成功!', userInfo)
134
+ }
135
+ ```
136
+
137
+ ---
138
+
139
+ ## 🏗️ API 参考
140
+
141
+ ### AuthApi
142
+
143
+ OAuth 2.0 认证相关 API
144
+
145
+ #### 构造函数
146
+
147
+ ```typescript
148
+ new AuthApi(config: IAuthConfig)
149
+ ```
150
+
151
+ **参数:**
152
+ - `appId` - 应用 ID
153
+ - `appSecret` - 应用密钥(换取 token 时需要)
154
+ - `redirectUri` - 回调地址
155
+ - `scope` - 授权范围(可选)
156
+ - `state` - CSRF 防护(可选)
157
+ - `baseUrl` - 开放平台地址(默认: https://open.beaver.im)
158
+
159
+ #### 方法
160
+
161
+ | 方法 | 说明 | 返回值 |
162
+ |------|------|--------|
163
+ | `buildAuthUrl()` | 构建授权 URL | `string` |
164
+ | `login()` | 发起授权跳转 | `void` |
165
+ | `getAuthCodeFromUrl()` | 从 URL 获取授权码 | `IAuthResponse \| null` |
166
+ | `exchangeCodeForToken(code)` | 用授权码换取 Token | `Promise<ITokenInfo>` |
167
+
168
+ ---
169
+
170
+ ### UserApi
171
+
172
+ 用户信息查询 API
173
+
174
+ #### 构造函数
175
+
176
+ ```typescript
177
+ new UserApi(accessToken: string, baseUrl?: string)
178
+ ```
179
+
180
+ **参数:**
181
+ - `accessToken` - 访问令牌
182
+ - `baseUrl` - 开放平台地址(可选)
183
+
184
+ #### 方法
185
+
186
+ | 方法 | 说明 | 返回值 |
187
+ |------|------|--------|
188
+ | `getUserInfo(userId)` | 获取用户信息 | `Promise<IUserInfo>` |
189
+ | `getUserList(userIds)` | 批量获取用户 | `Promise<IUserInfo[]>` |
190
+
191
+ ---
192
+
193
+ ## 📝 类型定义
194
+
195
+ ```typescript
196
+ interface IAuthConfig {
197
+ appId: string
198
+ appSecret?: string
199
+ redirectUri: string
200
+ scope?: string
201
+ state?: string
202
+ baseUrl?: string
203
+ }
204
+
205
+ interface ITokenInfo {
206
+ accessToken: string
207
+ refreshToken?: string
208
+ expiresIn: number
209
+ tokenType: string
210
+ }
211
+
212
+ interface IUserInfo {
213
+ userId: string
214
+ nickName: string
215
+ avatar?: string
216
+ phone?: string
217
+ email?: string
218
+ }
219
+ ```
220
+
221
+ ---
222
+
223
+ ## 🔧 开发
224
+
225
+ ### 环境配置
226
+
227
+ SDK 支持多环境配置,通过 `.env` 文件管理不同环境的 API 地址:
228
+
229
+ - `.env.dev` - 开发环境(默认)
230
+ - `.env.test` - 测试环境
231
+ - `.env.prod` - 生产环境
232
+
233
+ ### 构建命令
234
+
235
+ ```bash
236
+ # 安装依赖
237
+ pnpm install
238
+
239
+ # 开发模式(使用 .env.dev 配置)
240
+ pnpm dev
241
+
242
+ # 构建 - 开发环境
243
+ pnpm build:dev
244
+
245
+ # 构建 - 测试环境
246
+ pnpm build:test
247
+
248
+ # 构建 - 生产环境
249
+ pnpm build:prod
250
+
251
+ # 默认构建(等同于 build:dev)
252
+ pnpm build
253
+ ```
254
+
255
+ ### 环境变量说明
256
+
257
+ | 变量名 | 说明 | 示例 |
258
+ |--------|------|------|
259
+ | `VITE_API_ENV` | 当前环境标识 | dev/test/prod |
260
+ | `VITE_API_BASE` | API 基础地址 | http://127.0.0.1:22000 |
261
+
262
+ ### 在代码中使用
263
+
264
+ ```typescript
265
+ import { API_ENV, API_BASE } from '@beaver/js-sdk/config/env'
266
+
267
+ console.log('当前环境:', API_ENV) // 'dev' | 'test' | 'prod'
268
+ console.log('API 地址:', API_BASE)
269
+ ```
270
+
271
+ ---
272
+
273
+ ## 📄 License
274
+
275
+ MIT
@@ -0,0 +1,4 @@
1
+ import { IPasswordLoginReq, IPasswordLoginRes } from '../types/api/oauth';
2
+
3
+ /** 账号密码登录 */
4
+ export declare const passwordLoginApi: (data: IPasswordLoginReq) => Promise<import('../utils/request').IResponseSuccessData<IPasswordLoginRes>>;
@@ -0,0 +1,4 @@
1
+ import { IQuickSignRes } from '../types/api/localClient';
2
+
3
+ /** 本机客户端快捷登录(海狸 PC 已登录时) */
4
+ export declare const quickSignApi: (appId: string) => Promise<import('../utils/request').IResponseSuccessData<IQuickSignRes>>;
@@ -0,0 +1,8 @@
1
+ import { IGenerateQrCodeReq, IGenerateQrCodeRes, ICheckQrCodeStatusReq, ICheckQrCodeStatusRes } from '../types/api/qrcode';
2
+
3
+ /** 扫码页地址(写入二维码,供手机 App 打开) */
4
+ export declare function buildScanQrUrl(sceneId: string): string;
5
+ /** 生成扫码登录会话 */
6
+ export declare const generateQrCodeApi: (data: IGenerateQrCodeReq) => Promise<import('../utils/request').IResponseSuccessData<IGenerateQrCodeRes>>;
7
+ /** 查询扫码状态 */
8
+ export declare const checkQrCodeStatusApi: (data: ICheckQrCodeStatusReq) => Promise<import('../utils/request').IResponseSuccessData<ICheckQrCodeStatusRes>>;