@cloudbase/js-sdk 3.1.8 → 3.1.9

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/CHANGELOG.md CHANGED
@@ -1,5 +1,8 @@
1
1
  # 更新日志
2
2
 
3
+ ## [3.1.9] 2026-03-13
4
+ - [Added] 增强类型声明以及错误提示
5
+
3
6
  ## [3.1.8] 2026-03-12
4
7
  - [Fixed] 增加 storage 类型定义
5
8
 
package/dist/index.cjs.js CHANGED
@@ -14,7 +14,7 @@ var cloudrun_1 = require("@cloudbase/cloudrun");
14
14
  var mysql_1 = require("@cloudbase/mysql");
15
15
  var apis_1 = require("@cloudbase/apis");
16
16
  var database_1 = require("./../database");
17
- var version = "3.1.8";
17
+ var version = "3.1.9";
18
18
  app_1.default.registerVersion(version);
19
19
  try {
20
20
  (0, auth_1.registerAuth)(app_1.default);
package/dist/index.esm.js CHANGED
@@ -10,7 +10,7 @@ import { registerCloudrun } from '@cloudbase/cloudrun';
10
10
  import { registerMySQL } from '@cloudbase/mysql';
11
11
  import { registerApis } from '@cloudbase/apis';
12
12
  import { registerDatabase } from './../database';
13
- var version = "3.1.8";
13
+ var version = "3.1.9";
14
14
  cloudbase.registerVersion(version);
15
15
  try {
16
16
  registerAuth(cloudbase);
package/index.d.ts CHANGED
@@ -78,11 +78,19 @@ declare namespace cloudbase {
78
78
  }
79
79
 
80
80
  interface ICloudbaseConfig {
81
+ /** 环境 ID,在腾讯云开发控制台 → 环境 → 环境总览中获取 */
81
82
  env: string
83
+ /**
84
+ * 地域信息
85
+ * @example 'ap-shanghai'
86
+ */
82
87
  region?: string
88
+ /** 网络请求超时上限,单位 ms,默认 15000 */
83
89
  timeout?: number
90
+ /** 本地登录态保留期限 */
84
91
  persistence?: Persistence
85
92
  oauthClient?: any
93
+ /** 是否开启调试模式 */
86
94
  debug?: boolean
87
95
  _fromApp?: ICloudbase
88
96
  clientId?: string
@@ -95,7 +103,31 @@ declare namespace cloudbase {
95
103
  }
96
104
  accessKey?: string
97
105
  endPointMode?: EndPointKey
98
- lang?: LANGS
106
+ lang?: LANGS
107
+ /**
108
+ * 认证相关配置
109
+ * @example
110
+ * ```typescript
111
+ * cloudbase.init({
112
+ * env: 'your-env-id',
113
+ * auth: {
114
+ * detectSessionInUrl: true // 自动检测 URL 中的 OAuth 回调参数
115
+ * }
116
+ * })
117
+ * ```
118
+ */
119
+ auth?: {
120
+ /** 是否自动检测 URL 中的 OAuth 回调参数,默认 false */
121
+ detectSessionInUrl?: boolean
122
+ /** 密钥 ID(Node.js 端使用) */
123
+ secretId?: string
124
+ /** 密钥(Node.js 端使用) */
125
+ secretKey?: string
126
+ /** 临时会话 Token(Node.js 端使用) */
127
+ sessionToken?: string
128
+ /** 密钥类型 */
129
+ secretType?: 'SESSION_SECRET' | 'SECRET'
130
+ }
99
131
  }
100
132
 
101
133
  interface ICloudbaseExtension {
@@ -148,20 +180,49 @@ declare namespace cloudbase {
148
180
  *
149
181
  * @example
150
182
  * ```javascript
183
+ * // 基本用法
151
184
  * const app = cloudbase.init({
152
185
  * env: 'your-envid',
153
186
  * timeout: 15000
154
187
  * });
188
+ *
189
+ * // 推荐:从环境变量读取环境 ID
190
+ * const app = cloudbase.init({
191
+ * env: process.env.CLOUDBASE_ENV || import.meta.env.VITE_CLOUDBASE_ENV || 'your-envid'
192
+ * });
193
+ *
194
+ * // 使用认证配置
195
+ * const app = cloudbase.init({
196
+ * env: 'your-envid',
197
+ * auth: {
198
+ * detectSessionInUrl: true // 自动检测 URL 中的 OAuth 回调参数
199
+ * }
200
+ * });
155
201
  * ```
156
202
  *
157
203
  * @param config 初始化配置
158
- * @param config.env 环境ID
204
+ * @param config.env 环境ID,在腾讯云开发控制台 → 环境 → 环境总览中获取
159
205
  * @param config.timeout 【可选】网络请求超时上限,单位`ms`,默认值`15000`
206
+ * @param config.auth 【可选】认证相关配置
160
207
  *
161
208
  * @return {!cloudbase.app.App} 初始化成功的Cloudbase实例
162
209
  */
163
210
  function init(config: ICloudbaseConfig): cloudbase.app.App
164
211
 
212
+ /**
213
+ * 检查 Cloudbase 实例是否已完成初始化
214
+ *
215
+ * @example
216
+ * ```javascript
217
+ * if (!cloudbase.isInitialized()) {
218
+ * cloudbase.init({ env: 'your-envid' });
219
+ * }
220
+ * ```
221
+ *
222
+ * @returns 是否已初始化
223
+ */
224
+ function isInitialized(): boolean
225
+
165
226
  function updateConfig(config: ICloudbaseUpgradedConfig): void
166
227
 
167
228
  function updateLang(lang: LANGS): void
@@ -1318,6 +1379,296 @@ declare namespace cloudbase.auth {
1318
1379
  * @param params
1319
1380
  */
1320
1381
  signInWithPhoneAuth(params: { phoneCode: string }): Promise<SignInRes>
1382
+
1383
+ // ========== v1 兼容 API ==========
1384
+
1385
+ /**
1386
+ * v1 API: 获取用于微信登录的 WeixinAuthProvider
1387
+ *
1388
+ * @deprecated 建议使用 auth.signInWithOAuth({ provider: appid }) 替代。
1389
+ *
1390
+ * @example
1391
+ * ```javascript
1392
+ * const provider = auth.weixinAuthProvider({ appid: 'wx-appid', scope: 'snsapi_login' });
1393
+ * provider.signInWithRedirect();
1394
+ * ```
1395
+ *
1396
+ * @param options
1397
+ * @param options.appid 微信 AppID
1398
+ * @param options.scope 微信授权范围
1399
+ */
1400
+ weixinAuthProvider(options: { appid: string; scope: string }): cloudbase.auth.WeixinAuthProvider
1401
+
1402
+ /**
1403
+ * v1 API: 获取用于自定义登录的 CustomAuthProvider
1404
+ *
1405
+ * @deprecated 建议使用 auth.signInWithCustomTicket(() => Promise.resolve(ticket)) 替代。
1406
+ *
1407
+ * @example
1408
+ * ```javascript
1409
+ * const provider = auth.customAuthProvider();
1410
+ * await provider.signIn(ticket);
1411
+ * ```
1412
+ */
1413
+ customAuthProvider(): cloudbase.auth.CustomAuthProvider
1414
+
1415
+ /**
1416
+ * v1 API: 获取用于匿名登录的 AnonymousAuthProvider
1417
+ *
1418
+ * @deprecated 建议使用 auth.signInAnonymously({}) 替代。
1419
+ *
1420
+ * @example
1421
+ * ```javascript
1422
+ * const provider = auth.anonymousAuthProvider();
1423
+ * await provider.signIn();
1424
+ * ```
1425
+ */
1426
+ anonymousAuthProvider(): cloudbase.auth.AnonymousAuthProvider
1427
+
1428
+ /**
1429
+ * v1 API: 使用邮箱和密码注册云开发账户
1430
+ *
1431
+ * @deprecated 建议使用 auth.signUp({ email, password }) 替代。
1432
+ *
1433
+ * @example
1434
+ * ```javascript
1435
+ * await auth.signUpWithEmailAndPassword('user@example.com', 'password123');
1436
+ * ```
1437
+ *
1438
+ * @param email 邮箱
1439
+ * @param password 密码
1440
+ */
1441
+ signUpWithEmailAndPassword(email: string, password: string): Promise<SignUpRes>
1442
+
1443
+ /**
1444
+ * v1 API: 使用邮箱和密码登录云开发(支持两种调用方式)
1445
+ *
1446
+ * @deprecated 建议使用 auth.signInWithPassword({ email, password }) 替代。
1447
+ *
1448
+ * @example
1449
+ * ```javascript
1450
+ * // 方式一(v1 风格):
1451
+ * await auth.signInWithEmailAndPassword('user@example.com', 'password123');
1452
+ *
1453
+ * // 方式二(v3 风格):
1454
+ * const { data, error } = await auth.signInWithEmailAndPassword({
1455
+ * email: 'user@example.com',
1456
+ * password: 'password123'
1457
+ * });
1458
+ * ```
1459
+ *
1460
+ * @param emailOrParams - 邮箱字符串(v1)或包含 email 和 password 的对象(v3)
1461
+ * @param password - 密码(仅 v1 风格时使用)
1462
+ */
1463
+ signInWithEmailAndPassword(emailOrParams: string | { email: string; password: string; is_encrypt?: boolean }, password?: string): Promise<SignInRes | ILoginState>
1464
+
1465
+ /**
1466
+ * v1 API: 发送重置密码的邮件
1467
+ *
1468
+ * @deprecated 建议使用 auth.resetPasswordForEmail(email) 替代。
1469
+ *
1470
+ * @example
1471
+ * ```javascript
1472
+ * await auth.sendPasswordResetEmail('user@example.com');
1473
+ * ```
1474
+ *
1475
+ * @param email 邮箱
1476
+ */
1477
+ sendPasswordResetEmail(email: string): Promise<void>
1478
+
1479
+ /**
1480
+ * v1 API: 使用用户名和密码登录云开发
1481
+ *
1482
+ * @deprecated 建议使用 auth.signInWithPassword({ username, password }) 替代。
1483
+ *
1484
+ * @example
1485
+ * ```javascript
1486
+ * const loginState = await auth.signInWithUsernameAndPassword('username', 'password123');
1487
+ * ```
1488
+ *
1489
+ * @param username 用户名
1490
+ * @param password 密码
1491
+ */
1492
+ signInWithUsernameAndPassword(username: string, password: string): Promise<ILoginState>
1493
+
1494
+ /**
1495
+ * v1 API: 发送手机验证码
1496
+ *
1497
+ * @deprecated 建议使用 auth.signInWithOtp({ phone }) 或 auth.getVerification({ phone_number }) 替代。
1498
+ *
1499
+ * @example
1500
+ * ```javascript
1501
+ * const success = await auth.sendPhoneCode('+8613800138000');
1502
+ * ```
1503
+ *
1504
+ * @param phoneNumber 手机号
1505
+ * @returns 是否发送成功
1506
+ */
1507
+ sendPhoneCode(phoneNumber: string): Promise<boolean>
1508
+
1509
+ /**
1510
+ * v1 API: 手机号注册(支持短信验证码+密码方式)
1511
+ *
1512
+ * @deprecated 建议使用 auth.signUp({ phone_number, verification_code, password? }) 替代。
1513
+ *
1514
+ * @example
1515
+ * ```javascript
1516
+ * const loginState = await auth.signUpWithPhoneCode('+8613800138000', '123456', 'password');
1517
+ * ```
1518
+ *
1519
+ * @param phoneNumber 手机号
1520
+ * @param phoneCode 验证码
1521
+ * @param password 可选密码
1522
+ */
1523
+ signUpWithPhoneCode(phoneNumber: string, phoneCode: string, password?: string): Promise<ILoginState>
1524
+
1525
+ /**
1526
+ * v1 API: 手机号登录(支持短信验证码 or 密码方式)
1527
+ *
1528
+ * @deprecated 密码方式建议使用 auth.signInWithPassword({ phone, password }) 替代;
1529
+ * 验证码方式建议使用 auth.signInWithOtp({ phone }) 替代。
1530
+ *
1531
+ * @example
1532
+ * ```javascript
1533
+ * // 验证码登录
1534
+ * const loginState = await auth.signInWithPhoneCodeOrPassword({
1535
+ * phoneNumber: '+8613800138000',
1536
+ * phoneCode: '123456'
1537
+ * });
1538
+ *
1539
+ * // 密码登录
1540
+ * const loginState = await auth.signInWithPhoneCodeOrPassword({
1541
+ * phoneNumber: '+8613800138000',
1542
+ * password: 'password123'
1543
+ * });
1544
+ * ```
1545
+ *
1546
+ * @param params
1547
+ */
1548
+ signInWithPhoneCodeOrPassword(params: {
1549
+ phoneNumber: string
1550
+ phoneCode?: string
1551
+ password?: string
1552
+ }): Promise<ILoginState>
1553
+
1554
+ /**
1555
+ * v1 API: 手机号强制重置密码
1556
+ *
1557
+ * @deprecated 建议使用 auth.resetPasswordForEmail(phoneNumber) 替代。
1558
+ *
1559
+ * @example
1560
+ * ```javascript
1561
+ * const loginState = await auth.forceResetPwdByPhoneCode({
1562
+ * phoneNumber: '+8613800138000',
1563
+ * phoneCode: '123456',
1564
+ * password: 'newPassword'
1565
+ * });
1566
+ * ```
1567
+ *
1568
+ * @param params
1569
+ */
1570
+ forceResetPwdByPhoneCode(params: {
1571
+ phoneNumber: string
1572
+ phoneCode: string
1573
+ password: string
1574
+ }): Promise<ILoginState>
1575
+
1576
+ /**
1577
+ * v1 API: 接收一个回调函数,在刷新短期访问令牌前调用,根据返回值决定是否刷新
1578
+ *
1579
+ * @deprecated 建议使用 auth.onAuthStateChange(callback) 监听 TOKEN_REFRESHED 事件替代。
1580
+ *
1581
+ * @param callback 回调函数,返回 true 则刷新,返回 false 则不刷新
1582
+ */
1583
+ shouldRefreshAccessToken(callback: () => boolean): void
1584
+
1585
+ /**
1586
+ * v1 API: 接收一个回调函数,在登录状态过期时调用
1587
+ *
1588
+ * @deprecated 建议使用 auth.onAuthStateChange(callback) 替代,监听 SIGNED_OUT 事件。
1589
+ *
1590
+ * @param callback 登录态过期回调
1591
+ */
1592
+ onLoginStateExpired(callback: Function): void
1593
+
1594
+ /**
1595
+ * v1 API: 接收一个回调函数,在短期访问令牌刷新后调用
1596
+ *
1597
+ * @deprecated 建议使用 auth.onAuthStateChange(callback) 替代,监听 TOKEN_REFRESHED 事件。
1598
+ *
1599
+ * @param callback 令牌刷新回调
1600
+ */
1601
+ onAccessTokenRefreshed(callback: Function): void
1602
+
1603
+ /**
1604
+ * v1 API: 接收一个回调函数,在匿名登录状态被转换后调用
1605
+ *
1606
+ * @deprecated 建议使用 auth.onAuthStateChange(callback) 替代,监听 SIGNED_IN 事件。
1607
+ *
1608
+ * @param callback 匿名转正回调
1609
+ */
1610
+ onAnonymousConverted(callback: Function): void
1611
+
1612
+ /**
1613
+ * v1 API: 接收一个回调函数,在登录类型发生变化后调用
1614
+ *
1615
+ * @deprecated 建议使用 auth.onAuthStateChange(callback) 替代,监听 SIGNED_IN / SIGNED_OUT 事件。
1616
+ *
1617
+ * @param callback 登录类型变化回调
1618
+ */
1619
+ onLoginTypeChanged(callback: Function): void
1620
+ }
1621
+
1622
+ // ========== v1 兼容 Provider 类 ==========
1623
+
1624
+ /**
1625
+ * v1 微信登录 Provider
1626
+ *
1627
+ * @deprecated 建议使用 auth.signInWithOAuth({ provider }) 替代。
1628
+ */
1629
+ interface WeixinAuthProvider {
1630
+ /**
1631
+ * 跳转微信授权页面进行登录
1632
+ * @deprecated 建议使用 auth.signInWithOAuth({ provider: 'providerId' }) 替代。
1633
+ */
1634
+ signInWithRedirect(): void
1635
+ /**
1636
+ * 获取微信授权重定向结果
1637
+ * @deprecated 建议使用 auth.verifyOAuth({ code, state, provider }) 替代。
1638
+ */
1639
+ getRedirectResult(options?: { createUser?: boolean; syncUserInfo?: boolean }): Promise<any>
1640
+ /**
1641
+ * 获取微信绑定重定向结果
1642
+ * @deprecated 建议使用 auth.linkIdentity({ provider: 'providerId' }) 替代。
1643
+ */
1644
+ getLinkRedirectResult(options?: { withUnionId?: boolean }): Promise<void>
1645
+ }
1646
+
1647
+ /**
1648
+ * v1 自定义登录 Provider
1649
+ *
1650
+ * @deprecated 建议使用 auth.signInWithCustomTicket(() => Promise.resolve(ticket)) 替代。
1651
+ */
1652
+ interface CustomAuthProvider {
1653
+ /**
1654
+ * 使用自定义登录凭据 ticket 登录云开发
1655
+ * @param ticket 自定义登录 ticket
1656
+ * @deprecated 建议使用 auth.signInWithCustomTicket(() => Promise.resolve(ticket)) 替代。
1657
+ */
1658
+ signIn(ticket: string): Promise<void>
1659
+ }
1660
+
1661
+ /**
1662
+ * v1 匿名登录 Provider
1663
+ *
1664
+ * @deprecated 建议使用 auth.signInAnonymously({}) 替代。
1665
+ */
1666
+ interface AnonymousAuthProvider {
1667
+ /**
1668
+ * 匿名登录云开发
1669
+ * @deprecated 建议使用 auth.signInAnonymously({}) 替代。
1670
+ */
1671
+ signIn(): Promise<void>
1321
1672
  }
1322
1673
  }
1323
1674
  /**
@@ -3009,10 +3360,19 @@ declare namespace cloudbase.database {
3009
3360
  /**
3010
3361
  * 创建集合
3011
3362
  *
3363
+ * **⚠️ 权限说明**:此方法通常需要管理员权限,客户端 SDK(Web/小程序)在默认安全规则下
3364
+ * 可能无权限调用此方法。如果调用失败,请通过以下方式创建集合:
3365
+ * 1. 登录[腾讯云开发控制台](https://console.cloud.tencent.com/tcb/database)手动创建
3366
+ * 2. 在云函数中使用管理端 SDK 创建
3367
+ * 3. 使用 CLI 工具创建:`tcb service:collection-create <collName>`
3368
+ *
3012
3369
  * {@link https://docs.cloudbase.net/api-reference/webv3-next/database#createcollection}
3013
3370
  *
3014
3371
  * @example
3372
+ * ```javascript
3373
+ * // 管理端 SDK 或具有管理员权限时可用
3015
3374
  * const result = await db.createCollection('new-collection')
3375
+ * ```
3016
3376
  *
3017
3377
  * @param collName 集合名称
3018
3378
  *
@@ -3085,5 +3445,5 @@ declare namespace cloudbase.database {
3085
3445
  }
3086
3446
  }
3087
3447
 
3088
- export default cloudbase
3448
+ export = cloudbase
3089
3449
  export as namespace cloudbase