@cloudbase/js-sdk 3.6.2 → 3.6.3-beta.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/types/auth.d.ts CHANGED
@@ -70,27 +70,94 @@ export interface IUser extends IUserInfo {
70
70
  export interface ILoginState {
71
71
  user: IUser
72
72
  }
73
- export interface ICloudbaseAuth {
73
+
74
+ /**
75
+ * v1 兼容事件回调收到的载荷。
76
+ *
77
+ * 运行时实际传入的是 `ILoginState` 与事件元数据(`eventType` 等)的混合对象;
78
+ * 在某些时机(如未登录、首次立即回调)可能为 `null`。这里用一个开放的载荷类型
79
+ * 精确表达其形态,取代原先的裸 `Function`,同时保持对既有回调写法的非破坏。
80
+ */
81
+ export type IAuthEventPayload =
82
+ | (Partial<ILoginState> & {
83
+ /** 事件类型标识(如 `SIGN_IN` / `SIGN_OUT` / `CREDENTIALS_ERROR` 等)。 */
84
+ eventType?: string
85
+ /** 事件携带的原始数据。 */
86
+ data?: KV<any>
87
+ [key: string]: any
88
+ })
89
+ | null
90
+
91
+ /**
92
+ * v1 兼容事件回调签名。
93
+ *
94
+ * @deprecated 推荐改用 `onAuthStateChange((event, session, info) => {})` 监听,
95
+ * 该回调仅为兼容旧代码保留。
96
+ */
97
+ export type IAuthEventCallback = (payload: IAuthEventPayload) => void
98
+
99
+ /**
100
+ * 认证核心能力:状态字段与核心方法,均为**非可选**(与 `ICloudbase` 拆分同款)。
101
+ */
102
+ export interface AuthCore {
74
103
  config: ICloudbaseConfig
75
104
  loginType: string
76
- weixinAuthProvider: any
77
- anonymousAuthProvider: any
78
- customAuthProvider: any
79
105
  getAccessToken: () => IAccessTokenInfo
80
106
  getLoginState: () => Promise<ILoginState | null>
81
107
  hasLoginState: () => Promise<ILoginState | null>
82
108
  getUserInfo: () => Promise<UserInfo & IUser>
83
109
  getAuthHeader: () => Promise<KV<string>>
84
- onLoginStateChanged: (callback: Function) => void
85
- onLoginStateExpired: (callback: Function) => void
86
- onAccessTokenRefreshed: (callback: Function) => void
87
- onAnonymousConverted: (callback: Function) => void
88
- onLoginTypeChanged: (callback: Function) => void
89
- shouldRefreshAccessToken: (hook: Function) => void
90
110
  /** 当前登录用户,未登录时为 null */
91
111
  currentUser: IUser | null
92
112
  }
93
113
 
114
+ /**
115
+ * 认证事件监听能力(v1 兼容)。
116
+ *
117
+ * @deprecated 这些监听器均为兼容旧代码保留,推荐统一改用
118
+ * `onAuthStateChange((event, session, info) => {})`。回调签名已从裸 `Function`
119
+ * 收紧为精确类型 {@link IAuthEventCallback},对既有 `(state) => {}` 写法非破坏。
120
+ */
121
+ export interface AuthEventCapability {
122
+ onLoginStateChanged: (callback: IAuthEventCallback) => void
123
+ onLoginStateExpired: (callback: IAuthEventCallback) => void
124
+ onAccessTokenRefreshed: (callback: IAuthEventCallback) => void
125
+ onAnonymousConverted: (callback: IAuthEventCallback) => void
126
+ onLoginTypeChanged: (callback: IAuthEventCallback) => void
127
+ /** 决定是否刷新 accessToken 的钩子,返回 `true` 表示需要刷新。 */
128
+ shouldRefreshAccessToken: (hook: () => boolean) => void
129
+ }
130
+
131
+ /**
132
+ * 认证身份源提供方能力。三个 provider 由 `registerProvider` 动态注册,
133
+ * 类型收敛为 {@link IAuthProvider}(取代原先的 `any`)。
134
+ */
135
+ export interface AuthProviderCapability {
136
+ weixinAuthProvider: IAuthProvider
137
+ anonymousAuthProvider: IAuthProvider
138
+ customAuthProvider: IAuthProvider
139
+ }
140
+
141
+ /**
142
+ * 认证客户端总类型。
143
+ *
144
+ * 由 {@link AuthCore} + {@link AuthEventCapability} + {@link AuthProviderCapability}
145
+ * 交叉组合而成,与原先的单一大接口**结构等价**(字段无增删,仅按能力分层 +
146
+ * 收紧回调/provider 类型),因此对既有代码非破坏。
147
+ */
148
+ export type ICloudbaseAuth = AuthCore & AuthEventCapability & AuthProviderCapability
149
+
150
+ /**
151
+ * 按需组合的认证客户端泛型。
152
+ *
153
+ * 默认等价于完整的 {@link ICloudbaseAuth};也可只声明需要的能力,例如
154
+ * `CloudbaseAuthClient<AuthEventCapability>` 只暴露事件监听,为 tree-shaking
155
+ * 与精确 API 面铺路。
156
+ */
157
+ export type CloudbaseAuthClient<
158
+ Caps = AuthEventCapability & AuthProviderCapability,
159
+ > = AuthCore & Caps
160
+
94
161
  type IProvider = new (...args: any[]) => any
95
162
 
96
163
  export interface ICloudbaseAuthModule {
@@ -0,0 +1,40 @@
1
+ /**
2
+ * Cloudbase 统一错误结构契约。
3
+ *
4
+ * 这是一个**结构化契约**(structural contract),而非要求所有错误都继承同一个 class。
5
+ * 各模块的具体错误类(如 auth 的 `AuthError`、storage 的 `StorageError`)只要满足此结构,
6
+ * 即可作为 `Result<T, E>` / `SafeResult<T, E>` 的错误分支类型 `E`。
7
+ *
8
+ * 运行时判别推荐使用各模块的类型守卫(如 `isStorageError`),
9
+ * 或检查隐藏标记字段(如 `__isStorageError`),而非 `instanceof`
10
+ * (`instanceof` 在跨 bundle / 跨 realm 场景会失效)。
11
+ */
12
+ export interface ICloudbaseError extends Error {
13
+ /** 错误名称,用于二级判别(如 `'StorageApiError'`)。 */
14
+ name: string
15
+ /** 人类可读的错误描述。 */
16
+ message: string
17
+ /** 便于 `JSON.stringify` 时不丢失关键字段(原生 Error 序列化会丢 name/message)。 */
18
+ toJSON?: () => Record<string, unknown>
19
+ }
20
+
21
+ /**
22
+ * Cloudbase 体系内错误的隐藏标记字段集合。
23
+ *
24
+ * 各模块的错误类都携带其中之一(在其构造函数中设为 `true`):
25
+ * - `__isAuthError` —— {@link !AuthError}(`@cloudbase/oauth`)
26
+ * - `__isStorageError` —— `StorageError`(`@cloudbase/storage`)
27
+ *
28
+ * 之所以用隐藏标记而非 `instanceof`,是因为多个 SDK 副本共存(跨 bundle / 跨 realm)
29
+ * 时 `instanceof` 会失效,而标记字段始终可靠。
30
+ */
31
+ export type CloudbaseErrorBrand = '__isAuthError' | '__isStorageError'
32
+
33
+ /**
34
+ * 统一的跨模块错误判别类型。
35
+ *
36
+ * 任何携带 {@link CloudbaseErrorBrand} 标记之一的对象,都被视为 Cloudbase 体系内的错误,
37
+ * 满足 {@link ICloudbaseError} 契约。各模块另提供具体的运行时守卫(`isAuthError` /
38
+ * `isStorageError`),本类型用于在类型层统一表达「这是一个 Cloudbase 错误」。
39
+ */
40
+ export type CloudbaseErrorLike = ICloudbaseError & Partial<Record<CloudbaseErrorBrand, boolean>>
@@ -13,12 +13,21 @@ export interface ICallFunctionOptions {
13
13
  header?: KV<any>
14
14
  }
15
15
 
16
- export interface ICallFunctionResponse {
16
+ export interface ICallFunctionResponse<T = any> {
17
17
  requestId: string
18
- result: any
18
+ /**
19
+ * 云函数返回值。
20
+ *
21
+ * 默认 `any` 以保持对既有调用方(直接访问 `res.result.xxx`)的**非破坏**兼容;
22
+ * 推荐通过泛型参数指定精确类型以获得类型安全:`callFunction<{ n: number }>(...)`。
23
+ */
24
+ result: T
19
25
  }
20
26
 
21
- export type ICallFunction = (options: ICallFunctionOptions, callback?: Function) => Promise<ICallFunctionResponse>
27
+ export type ICallFunction = <T = any>(
28
+ options: ICallFunctionOptions,
29
+ callback?: (err: Error | null, res: ICallFunctionResponse<T> | null) => void,
30
+ ) => Promise<ICallFunctionResponse<T>>
22
31
 
23
32
  export interface IRetryOptions {
24
33
  retries?: number
@@ -29,7 +38,7 @@ export interface IRetryOptions {
29
38
  timeouts?: number[]
30
39
  timeoutOps?: {
31
40
  timeout: number
32
- cb: function
41
+ cb: (...args: any[]) => void
33
42
  }
34
43
  }
35
44
 
package/types/index.d.ts CHANGED
@@ -18,6 +18,10 @@ import {
18
18
  } from './storage'
19
19
  import { cloudbase } from '../cloudbase/index'
20
20
 
21
+ // 统一返回判别联合与错误契约(可辨识联合范式)
22
+ export { Result, SafeResult } from './result'
23
+ export { ICloudbaseError, CloudbaseErrorBrand, CloudbaseErrorLike } from './error'
24
+
21
25
  export enum LANGS {
22
26
  ZH = 'zh-CN',
23
27
  EN = 'en-US',
@@ -106,7 +110,14 @@ export interface ICloudbaseApis {
106
110
  }
107
111
  }
108
112
 
109
- export interface ICloudbase {
113
+ /**
114
+ * Cloudbase 核心能力(始终存在,与运行时是否注册某个业务组件无关)。
115
+ *
116
+ * 这些字段/方法由 SDK 初始化流程直接提供,因此**不是可选的**。
117
+ * 业务能力(auth / functions / storage / database / ai / models / mysql 等)
118
+ * 被拆分到独立的 `*Capability` 接口,通过下面的 `ICloudbase` 交叉类型组合。
119
+ */
120
+ export interface CloudbaseCore {
110
121
  config: ICloudbaseConfig
111
122
  platform: ICloudbasePlatformInfo
112
123
  cache: ICloudbaseCache
@@ -135,17 +146,30 @@ export interface ICloudbase {
135
146
  BASE_URL: string
136
147
  PROTOCOL: string
137
148
  }
149
+ }
150
+
151
+ /** 身份鉴权能力(`registerAuth` 注册后可用)。 */
152
+ export interface AuthCapability {
138
153
  auth?: (options?: { persistence: cloudbase.auth.Persistence }) => cloudbase.auth.App
154
+ }
139
155
 
140
- // ---- 云函数 ----
156
+ /** 云函数能力(`registerFunctions` 注册后可用)。 */
157
+ export interface FunctionsCapability {
141
158
  /** 调用云函数 */
142
- callFunction?: (options: ICallFunctionOptions, callback?: Function) => Promise<ICallFunctionResponse>
159
+ callFunction?: <T = any>(
160
+ options: ICallFunctionOptions,
161
+ callback?: (err: Error | null, res: ICallFunctionResponse<T> | null) => void,
162
+ ) => Promise<ICallFunctionResponse<T>>
163
+ }
143
164
 
144
- // ---- 数据库 ----
165
+ /** 数据库能力(`registerDatabase` 注册后可用)。 */
166
+ export interface DatabaseCapability {
145
167
  /** 获取数据库实例 */
146
168
  database?: (dbConfig?: { instance?: string; database?: string }) => cloudbase.database.App
169
+ }
147
170
 
148
- // ---- 云存储 ----
171
+ /** 云存储能力(`registerStorage` 注册后可用)。 */
172
+ export interface StorageCapability {
149
173
  /** Storage 命名空间实例(from 分流 + Bucket 管理) */
150
174
  storage?: cloudbase.storage.StorageClient
151
175
  /** 上传文件 */
@@ -160,6 +184,39 @@ export interface ICloudbase {
160
184
  copyFile?: (params: ICloudbaseCopyFileParams, callback?: Function) => Promise<ICloudbaseCopyFileResult>
161
185
  }
162
186
 
187
+ /**
188
+ * 完整的 Cloudbase 实例类型。
189
+ *
190
+ * = 核心能力 `CloudbaseCore` + 各业务能力 `*Capability` 的交叉组合。
191
+ * 结构上等价于旧的单一大接口,但拆分后可读性更好、可按需组合,
192
+ * 也为未来 tree-shaking 友好的模块化 client 打基础。
193
+ *
194
+ * 如需只声明「用到的能力子集」,可直接使用 {@link CloudbaseClient} 泛型组合,
195
+ * 例如 `CloudbaseClient<AuthCapability & FunctionsCapability>`。
196
+ */
197
+ export type ICloudbase = CloudbaseCore
198
+ & AuthCapability
199
+ & FunctionsCapability
200
+ & DatabaseCapability
201
+ & StorageCapability
202
+
203
+ /**
204
+ * 按需组合的 Cloudbase 客户端类型。
205
+ *
206
+ * 始终包含核心能力 `CloudbaseCore`,并叠加调用方声明的能力集合 `Caps`。
207
+ *
208
+ * @example
209
+ * ```ts
210
+ * // 只用到 auth 与云函数能力
211
+ * type MyClient = CloudbaseClient<AuthCapability & FunctionsCapability>
212
+ * ```
213
+ *
214
+ * @typeParam Caps - 需要叠加的能力集合,默认叠加全部内置能力(等价于 `ICloudbase`)
215
+ */
216
+ export type CloudbaseClient<
217
+ Caps = AuthCapability & FunctionsCapability & DatabaseCapability & StorageCapability,
218
+ > = CloudbaseCore & Caps
219
+
163
220
  /**
164
221
  * Node.js 端适配器扩展方法
165
222
  */
@@ -1,10 +1,12 @@
1
1
  {
2
2
  "name": "@cloudbase/types",
3
- "version": "3.6.2",
3
+ "version": "3.6.3-beta.0",
4
4
  "description": "cloudbase javascript sdk types",
5
5
  "files": [
6
6
  "index.js",
7
7
  "index.d.ts",
8
+ "result.d.ts",
9
+ "error.d.ts",
8
10
  "cache.d.ts",
9
11
  "events.d.ts",
10
12
  "request.d.ts",
@@ -12,6 +14,8 @@
12
14
  "component.d.ts",
13
15
  "functions.d.ts",
14
16
  "cloudrun.d.ts",
17
+ "container.d.ts",
18
+ "analytics.d.ts",
15
19
  "storage.d.ts",
16
20
  "realtime.d.ts",
17
21
  "database.d.ts"
@@ -0,0 +1,48 @@
1
+ import { ICloudbaseError } from './error'
2
+
3
+ /**
4
+ * `{ data, error }` 统一返回结构(可辨识联合 / Discriminated Union)。
5
+ *
6
+ * 判别字段为 `error` 是否为 `null`:
7
+ * - 成功:`{ data: T; error: null }`
8
+ * - 失败:`{ data: null; error: E }`
9
+ *
10
+ * @example
11
+ * ```ts
12
+ * const res = await sdk.callFunction<{ n: number }>({ name: 'foo' })
13
+ * if (res.error === null) {
14
+ * // 此处 res.data 被自动收窄为 { n: number }(非 null)
15
+ * console.log(res.data.n)
16
+ * } else {
17
+ * // 此处 res.data 被收窄为 null,res.error 为 E
18
+ * console.error(res.error.message)
19
+ * }
20
+ * ```
21
+ *
22
+ * @typeParam T - 成功时的数据类型
23
+ * @typeParam E - 失败时的错误类型,默认 `ICloudbaseError`
24
+ */
25
+ export type Result<T, E extends ICloudbaseError = ICloudbaseError> =
26
+ | { data: T; error: null }
27
+ | { data: null; error: E }
28
+
29
+ /**
30
+ * 安全解构变体:失败分支的 `data` 不是 `null`,而是把成功 shape 的每个字段映射成 `null`。
31
+ *
32
+ * 这样即使不先判空 `error`,也能安全解构成功结构里的字段(值可能为 `null`):
33
+ *
34
+ * @example
35
+ * ```ts
36
+ * // 无需先判 res.data === null 即可解构
37
+ * const { data: { user, session } } = await auth.signInWithPassword({ ... })
38
+ * ```
39
+ *
40
+ * 该类型用于「已有 `{ data: { ... } }` 嵌套结构、需要平滑升级为判别联合」的场景(如 auth)。
41
+ * 相比 `Result<T>`,它对既有解构代码是**非破坏性**的升级。
42
+ *
43
+ * @typeParam T - 成功时的数据结构(必须为对象)
44
+ * @typeParam E - 失败时的错误类型,默认 `ICloudbaseError`
45
+ */
46
+ export type SafeResult<T extends object, E extends ICloudbaseError = ICloudbaseError> =
47
+ | { data: T; error: null }
48
+ | { data: { [K in keyof T]: null }; error: E }
@@ -1,14 +1,31 @@
1
1
  import { KVstring } from '.'
2
2
  import { ICustomReqOpts } from './functions'
3
3
 
4
+ /**
5
+ * 上传/下载时的原始文件内容,覆盖浏览器与 Node 两端形态:
6
+ * - Node 端:`Buffer`(属于 `Uint8Array`)、可读流(含 `size`/`byteLength` 的对象)
7
+ * - 浏览器端:`Blob` / `File` / `ArrayBuffer` / TypedArray 等 `ArrayBufferView`
8
+ * - 纯文本内容:`string`
9
+ *
10
+ * 末尾的 `{ size?: number; byteLength?: number }` 兼容自定义流对象(实现层据此推算
11
+ * content-length)。这是一个开放联合:既约束常见形态,又不排除运行时特殊对象。
12
+ */
13
+ export type FileContent =
14
+ | Blob
15
+ | ArrayBuffer
16
+ | ArrayBufferView
17
+ | Uint8Array
18
+ | string
19
+ | { size?: number; byteLength?: number; [key: string]: any };
20
+
4
21
  export interface ICloudbaseUploadFileParams {
5
22
  cloudPath: string;
6
23
  filePath: string;
7
24
  method?: 'post' | 'put';
8
25
  headers?: KVstring;
9
26
  onUploadProgress?: Function;
10
- // 文件内容 Buffer 文件可读流, node端使用
11
- fileContent?: any;
27
+ /** 文件内容:Buffer / 可读流(Node 端)或 Blob / ArrayBuffer(浏览器端) */
28
+ fileContent?: FileContent;
12
29
  customReqOpts?: ICustomReqOpts;
13
30
  }
14
31
 
@@ -103,7 +120,8 @@ export interface ICloudbaseDownloadFileParams {
103
120
  export interface ICloudbaseDownloadFileResult {
104
121
  code?: string;
105
122
  message?: string;
106
- fileContent?: any;
123
+ /** 下载得到的文件内容:Buffer / 流(Node 端)或 Blob / ArrayBuffer(浏览器端) */
124
+ fileContent?: FileContent;
107
125
  requestId?: string;
108
126
  }
109
127
 
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cloudbase/utilities",
3
- "version": "3.6.2",
3
+ "version": "3.6.3-beta.0",
4
4
  "description": "cloudbase javascript sdk utilities",
5
5
  "main": "dist/cjs/index.js",
6
6
  "module": "dist/esm/index.js",
@@ -19,7 +19,7 @@
19
19
  "license": "Apache-2.0",
20
20
  "dependencies": {
21
21
  "@cloudbase/adapter-interface": "^0.7.1",
22
- "@cloudbase/types": "3.6.2",
22
+ "@cloudbase/types": "3.6.3-beta.0",
23
23
  "jwt-decode": "^3.1.2"
24
24
  },
25
25
  "gitHead": "859557576897d4778857c12fb8d7fe8ca1f3a052"