@h-ai/capacitor 0.1.0-alpha9 → 1.0.0-beta.2

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/dist/index.d.ts CHANGED
@@ -2,6 +2,17 @@ import * as _h_ai_core from '@h-ai/core';
2
2
  import { HaiResult } from '@h-ai/core';
3
3
  import { TokenStorage } from '@h-ai/api-client';
4
4
 
5
+ /**
6
+ * Capacitor 模块的公共错误定义;业务操作通过 HaiResult 返回。
7
+ * @example
8
+ * ```ts
9
+ * import { HaiCapacitorError } from '@h-ai/capacitor'
10
+ * import { err } from '@h-ai/core'
11
+ *
12
+ * const result = err(HaiCapacitorError.NOT_AVAILABLE, 'Native bridge required')
13
+ * if (!result.success) void result.error.code
14
+ * ```
15
+ */
5
16
  declare const HaiCapacitorError: {
6
17
  readonly INIT_FAILED: _h_ai_core.HaiErrorDef;
7
18
  readonly NOT_AVAILABLE: _h_ai_core.HaiErrorDef;
@@ -11,12 +22,27 @@ declare const HaiCapacitorError: {
11
22
  readonly PREFERENCES_SET_FAILED: _h_ai_core.HaiErrorDef;
12
23
  readonly PREFERENCES_REMOVE_FAILED: _h_ai_core.HaiErrorDef;
13
24
  readonly DEVICE_INFO_FAILED: _h_ai_core.HaiErrorDef;
25
+ readonly APP_VERSION_FAILED: _h_ai_core.HaiErrorDef;
14
26
  readonly PUSH_REGISTER_FAILED: _h_ai_core.HaiErrorDef;
15
27
  readonly PUSH_LISTEN_FAILED: _h_ai_core.HaiErrorDef;
28
+ readonly PUSH_CLEANUP_FAILED: _h_ai_core.HaiErrorDef;
16
29
  readonly CAMERA_FAILED: _h_ai_core.HaiErrorDef;
17
30
  readonly STATUS_BAR_FAILED: _h_ai_core.HaiErrorDef;
18
31
  };
19
- /** 设备信息操作 */
32
+ /**
33
+ * 设备信息操作
34
+ * @example
35
+ * ```ts
36
+ * import type { DeviceOperations } from '@h-ai/capacitor'
37
+ *
38
+ * async function use(operations: DeviceOperations) {
39
+ * const result = await operations.getInfo()
40
+ * if (!result.success) return result.error.message
41
+ * return result.data
42
+ * }
43
+ * // 由已初始化的 capacitor.device 提供这些操作。
44
+ * ```
45
+ */
20
46
  interface DeviceOperations {
21
47
  /** 获取设备信息(平台、型号、系统版本等) */
22
48
  getInfo: () => Promise<HaiResult<DeviceInfo>>;
@@ -26,19 +52,58 @@ interface DeviceOperations {
26
52
  build: string;
27
53
  }>>;
28
54
  }
29
- /** 相机操作 */
55
+ /**
56
+ * 相机操作
57
+ * @example
58
+ * ```ts
59
+ * import type { CameraOperations } from '@h-ai/capacitor'
60
+ *
61
+ * async function use(operations: CameraOperations) {
62
+ * const result = await operations.takePhoto({ source: 'photos' })
63
+ * if (!result.success) return result.error.message
64
+ * return result.data
65
+ * }
66
+ * // 由已初始化的 capacitor.camera 提供这些操作。
67
+ * ```
68
+ */
30
69
  interface CameraOperations {
31
70
  /** 拍照或选取相册图片 */
32
71
  takePhoto: (options?: PhotoOptions) => Promise<HaiResult<PhotoResult>>;
33
72
  }
34
- /** 推送通知操作 */
73
+ /**
74
+ * 推送通知操作
75
+ * @example
76
+ * ```ts
77
+ * import type { PushOperations } from '@h-ai/capacitor'
78
+ *
79
+ * async function use(operations: PushOperations) {
80
+ * const result = await operations.register()
81
+ * if (!result.success) return result.error.message
82
+ * return result.data
83
+ * }
84
+ * // 由已初始化的 capacitor.push 提供这些操作。
85
+ * ```
86
+ */
35
87
  interface PushOperations {
36
- /** 注册推送通知,返回设备 Token */
37
- register: () => Promise<HaiResult<PushRegistration>>;
38
- /** 监听推送通知事件,返回清理函数 */
39
- listen: (callbacks: PushNotificationCallbacks) => Promise<HaiResult<() => Promise<void>>>;
88
+ /** 注册原生 Token;30 秒总超时,可取消,结束时回收监听器。 */
89
+ register: (options?: PushOptions) => Promise<HaiResult<PushRegistration>>;
90
+ /** 监听原生事件;设置阶段限时 30 秒,成功后由 cleanup/信号/模块关闭释放。 */
91
+ listen: (callbacks: PushNotificationCallbacks, options?: PushOptions) => Promise<HaiResult<PushCleanup>>;
40
92
  }
41
- /** 状态栏操作 */
93
+ /**
94
+ * 状态栏操作
95
+ * @example
96
+ * ```ts
97
+ * import type { StatusBarOperations } from '@h-ai/capacitor'
98
+ *
99
+ * async function use(operations: StatusBarOperations) {
100
+ * const result = await operations.configure({ style: 'dark' })
101
+ * if (!result.success) return result.error.message
102
+ * return result.data
103
+ * }
104
+ * // 由已初始化的 capacitor.statusBar 提供这些操作。
105
+ * ```
106
+ */
42
107
  interface StatusBarOperations {
43
108
  /** 配置状态栏(样式、背景色、沉浸式) */
44
109
  configure: (config: StatusBarConfig) => Promise<HaiResult<void>>;
@@ -47,7 +112,20 @@ interface StatusBarOperations {
47
112
  /** 隐藏状态栏 */
48
113
  hide: () => Promise<HaiResult<void>>;
49
114
  }
50
- /** Preferences 操作(安全读写,返回 HaiResult) */
115
+ /**
116
+ * Preferences 操作(安全读写,返回 HaiResult)
117
+ * @example
118
+ * ```ts
119
+ * import type { PreferencesOperations } from '@h-ai/capacitor'
120
+ *
121
+ * async function use(operations: PreferencesOperations) {
122
+ * const result = await operations.get('theme')
123
+ * if (!result.success) return result.error.message
124
+ * return result.data
125
+ * }
126
+ * // 由已初始化的 capacitor.preferences 提供这些操作。
127
+ * ```
128
+ */
51
129
  interface PreferencesOperations {
52
130
  /** 安全读取 Preference 值 */
53
131
  get: (key: string) => Promise<HaiResult<string | null>>;
@@ -56,12 +134,29 @@ interface PreferencesOperations {
56
134
  /** 安全删除 Preference 值 */
57
135
  remove: (key: string) => Promise<HaiResult<void>>;
58
136
  }
59
- /** Capacitor 模块服务对象接口 */
137
+ /**
138
+ * Capacitor 模块服务对象接口
139
+ * @example
140
+ * ```ts
141
+ * import { capacitor } from '@h-ai/capacitor'
142
+ *
143
+ * const initialized = await capacitor.init()
144
+ * if (!initialized.success) throw new Error(initialized.error.message)
145
+ * try {
146
+ * const result = await capacitor.device.getInfo()
147
+ * if (!result.success) throw new Error(result.error.message)
148
+ * void result.data
149
+ * } finally {
150
+ * const closed = await capacitor.close()
151
+ * if (!closed.success) throw new Error(closed.error.message)
152
+ * }
153
+ * ```
154
+ */
60
155
  interface CapacitorFunctions {
61
156
  /** 初始化 Capacitor 模块 */
62
157
  init: () => Promise<HaiResult<void>>;
63
- /** 关闭模块,重置状态 */
64
- close: () => Promise<void>;
158
+ /** 关闭模块并回收推送监听;失败时保持不可用,保留句柄供再次 close 重试。 */
159
+ close: () => Promise<HaiResult<void>>;
65
160
  /** 获取当前平台 */
66
161
  getPlatform: () => string;
67
162
  /** 是否运行在原生 App 中 */
@@ -79,9 +174,27 @@ interface CapacitorFunctions {
79
174
  /** Preferences 安全读写操作 */
80
175
  readonly preferences: PreferencesOperations;
81
176
  }
82
- /** 设备平台 */
177
+ /**
178
+ * 设备平台
179
+ * @example
180
+ * ```ts
181
+ * import type { DevicePlatform } from '@h-ai/capacitor'
182
+ *
183
+ * const value: DevicePlatform = 'android'
184
+ * void value
185
+ * ```
186
+ */
83
187
  type DevicePlatform = 'ios' | 'android' | 'web';
84
- /** 设备信息 */
188
+ /**
189
+ * 设备信息
190
+ * @example
191
+ * ```ts
192
+ * import type { DeviceInfo } from '@h-ai/capacitor'
193
+ *
194
+ * const value: DeviceInfo = { platform: 'android', osVersion: '15', model: 'Example device', manufacturer: 'Example', isVirtual: false }
195
+ * void value
196
+ * ```
197
+ */
85
198
  interface DeviceInfo {
86
199
  /** 平台(ios / android / web) */
87
200
  platform: DevicePlatform;
@@ -98,7 +211,44 @@ interface DeviceInfo {
98
211
  /** 应用 Build 号 */
99
212
  appBuild?: string;
100
213
  }
101
- /** 推送通知数据 */
214
+ /**
215
+ * 推送流程的取消选项;listen 成功后信号仍可终止监听。
216
+ * @example
217
+ * ```ts
218
+ * import type { PushOptions } from '@h-ai/capacitor'
219
+ *
220
+ * const value: PushOptions = { signal: new AbortController().signal }
221
+ * void value
222
+ * ```
223
+ */
224
+ interface PushOptions {
225
+ /** 取消当前注册或监听,不影响其他消费者拥有的句柄。 */
226
+ signal?: AbortSignal;
227
+ }
228
+ /**
229
+ * 幂等推送清理函数;原生 remove 失败返回错误,调用同一函数可重试。
230
+ * @example
231
+ * ```ts
232
+ * import type { PushCleanup } from '@h-ai/capacitor'
233
+ *
234
+ * async function release(cleanup: PushCleanup) {
235
+ * const result = await cleanup()
236
+ * if (!result.success) return cleanup() // 同一所有权句柄允许重试清理。
237
+ * return result
238
+ * }
239
+ * ```
240
+ */
241
+ type PushCleanup = () => Promise<HaiResult<void>>;
242
+ /**
243
+ * 推送通知数据
244
+ * @example
245
+ * ```ts
246
+ * import type { PushNotification } from '@h-ai/capacitor'
247
+ *
248
+ * const value: PushNotification = { id: 'notice_1', title: 'Order ready', data: { orderId: 'order_1' } }
249
+ * void value
250
+ * ```
251
+ */
102
252
  interface PushNotification {
103
253
  /** 通知 ID */
104
254
  id: string;
@@ -109,21 +259,57 @@ interface PushNotification {
109
259
  /** 附加数据 */
110
260
  data?: Record<string, unknown>;
111
261
  }
112
- /** 推送注册结果 */
262
+ /**
263
+ * 推送注册结果
264
+ * @example
265
+ * ```ts
266
+ * import type { PushRegistration } from '@h-ai/capacitor'
267
+ *
268
+ * const value: PushRegistration = { token: 'example-only-native-token' }
269
+ * void value
270
+ * ```
271
+ */
113
272
  interface PushRegistration {
114
273
  /** 设备推送 Token */
115
274
  token: string;
116
275
  }
117
- /** 推送通知回调 */
276
+ /**
277
+ * 推送通知回调
278
+ * @example
279
+ * ```ts
280
+ * import type { PushNotificationCallbacks } from '@h-ai/capacitor'
281
+ *
282
+ * const value: PushNotificationCallbacks = { onReceived: notification => { document.title = notification.title ?? 'Notice' } }
283
+ * void value
284
+ * ```
285
+ */
118
286
  interface PushNotificationCallbacks {
119
287
  /** 收到推送时调用 */
120
288
  onReceived?: (notification: PushNotification) => void;
121
289
  /** 用户点击推送时调用 */
122
290
  onActionPerformed?: (notification: PushNotification) => void;
123
291
  }
124
- /** 照片来源 */
292
+ /**
293
+ * 照片来源
294
+ * @example
295
+ * ```ts
296
+ * import type { PhotoSource } from '@h-ai/capacitor'
297
+ *
298
+ * const value: PhotoSource = 'photos'
299
+ * void value
300
+ * ```
301
+ */
125
302
  type PhotoSource = 'camera' | 'photos' | 'prompt';
126
- /** 照片选项 */
303
+ /**
304
+ * 照片选项
305
+ * @example
306
+ * ```ts
307
+ * import type { PhotoOptions } from '@h-ai/capacitor'
308
+ *
309
+ * const value: PhotoOptions = { source: 'photos', resultType: 'uri', quality: 90, width: 1200 }
310
+ * void value
311
+ * ```
312
+ */
127
313
  interface PhotoOptions {
128
314
  /** 来源(默认 'prompt') */
129
315
  source?: PhotoSource;
@@ -136,16 +322,43 @@ interface PhotoOptions {
136
322
  /** 返回格式 */
137
323
  resultType?: 'uri' | 'base64' | 'dataUrl';
138
324
  }
139
- /** 照片结果 */
325
+ /**
326
+ * 照片结果
327
+ * @example
328
+ * ```ts
329
+ * import type { PhotoResult } from '@h-ai/capacitor'
330
+ *
331
+ * const value: PhotoResult = { data: 'file:///example/photo.jpeg', format: 'jpeg' }
332
+ * void value
333
+ * ```
334
+ */
140
335
  interface PhotoResult {
141
336
  /** 图片路径或 base64 数据 */
142
337
  data: string;
143
- /** MIME 类型 */
338
+ /** 原生插件返回的格式扩展名(如 jpeg),不是 MIME 类型 */
144
339
  format: string;
145
340
  }
146
- /** 状态栏样式 */
341
+ /**
342
+ * 状态栏样式
343
+ * @example
344
+ * ```ts
345
+ * import type { StatusBarStyle } from '@h-ai/capacitor'
346
+ *
347
+ * const value: StatusBarStyle = 'dark'
348
+ * void value
349
+ * ```
350
+ */
147
351
  type StatusBarStyle = 'dark' | 'light' | 'default';
148
- /** 状态栏配置 */
352
+ /**
353
+ * 状态栏配置
354
+ * @example
355
+ * ```ts
356
+ * import type { StatusBarConfig } from '@h-ai/capacitor'
357
+ *
358
+ * const value: StatusBarConfig = { style: 'dark', overlay: false }
359
+ * void value
360
+ * ```
361
+ */
149
362
  interface StatusBarConfig {
150
363
  /** 状态栏文字样式 */
151
364
  style?: StatusBarStyle;
@@ -166,15 +379,19 @@ interface StatusBarConfig {
166
379
 
167
380
  /**
168
381
  * Capacitor 模块服务对象
169
- *
170
382
  * @example
171
383
  * ```ts
172
384
  * import { capacitor } from '@h-ai/capacitor'
173
385
  *
174
- * const result = await capacitor.init()
175
- * if (result.success) {
176
- * const info = await capacitor.device.getInfo()
177
- * await capacitor.statusBar.configure({ style: 'dark', overlay: true })
386
+ * const initialized = await capacitor.init()
387
+ * if (!initialized.success) throw new Error(initialized.error.message)
388
+ * try {
389
+ * const result = await capacitor.device.getInfo()
390
+ * if (!result.success) throw new Error(result.error.message)
391
+ * void result.data
392
+ * } finally {
393
+ * const closed = await capacitor.close()
394
+ * if (!closed.success) throw new Error(closed.error.message)
178
395
  * }
179
396
  * ```
180
397
  */
@@ -183,33 +400,36 @@ declare const capacitor: CapacitorFunctions;
183
400
  /**
184
401
  * @h-ai/capacitor — Token 存储
185
402
  *
186
- * 基于 Capacitor Preferences 的 TokenStorage 实现,
187
- * 比浏览器 localStorage 更安全,适用于原生 App 场景。
403
+ * 基于原生安全存储插件的 TokenStorage 实现。
404
+ * 仅在 Capacitor 原生环境启用,拒绝回退到不安全的 Web 存储。
188
405
  *
189
406
  * @module capacitor-token-storage
190
407
  */
191
408
 
192
409
  /**
193
- * 创建基于 Capacitor Preferences 的 TokenStorage
410
+ * 创建基于原生安全存储的 TokenStorage
194
411
  *
195
- * 使用 `@capacitor/preferences` 插件进行持久化存储,
196
- * 在 Android 使用 SharedPreferences,iOS 使用 UserDefaults,
197
- * 比 localStorage 有更好的安全性。
412
+ * 在原生 Android/iOS 环境下使用 `@aparajita/capacitor-secure-storage`
413
+ * 保存 access token / refresh token。所有方法的存储/平台错误均通过 Promise 拒绝传播,
414
+ * Web 调用明确失败,不做存储回退或伪成功。
415
+ * `clear()` 一开始即屏蔽本实例旧凭据,删除失败时仍保持屏蔽;相应 key 成功写入后恢复读取。
416
+ * 任一凭据写入失败也会屏蔽整对凭据,避免 access 已保存、refresh 失败时暴露半完成登录。
417
+ * 本实例屏蔽不代表原生持久数据已删除;跨重启撤销仍依赖服务端退出/撤销会话。
198
418
  *
199
419
  * @returns TokenStorage 实例
200
- *
420
+ * @throws 返回适配器的方法在插件不可用或原生存储失败时拒绝 Promise。
201
421
  * @example
202
422
  * ```ts
203
423
  * import { createCapacitorTokenStorage } from '@h-ai/capacitor'
204
- * import { api } from '@h-ai/api-client'
205
- *
206
- * await api.init({
207
- * baseUrl: 'https://api.example.com/v1',
208
- * auth: {
209
- * storage: createCapacitorTokenStorage(),
210
- * refreshUrl: '/auth/refresh',
211
- * },
212
- * })
424
+ *
425
+ * const storage = createCapacitorTokenStorage()
426
+ * try {
427
+ * const token = await storage.getAccessToken()
428
+ * void token // 仅原生安全存储;平台或存储失败会拒绝 Promise。
429
+ * } catch (cause) {
430
+ * // 应阻止认证流程继续并呈现本地化错误;不回退到普通 Preferences。
431
+ * void cause
432
+ * }
213
433
  * ```
214
434
  */
215
435
  declare function createCapacitorTokenStorage(): TokenStorage;
@@ -218,12 +438,19 @@ declare function createCapacitorTokenStorage(): TokenStorage;
218
438
  *
219
439
  * @param key - Preference Key
220
440
  * @returns HaiResult 包裹的值
221
- *
222
441
  * @example
223
442
  * ```ts
224
- * const result = await capacitor.preferences.get('my_key')
225
- * if (result.success) {
226
- * result.data // 值或 null
443
+ * import { capacitor } from '@h-ai/capacitor'
444
+ *
445
+ * const initialized = await capacitor.init()
446
+ * if (!initialized.success) throw new Error(initialized.error.message)
447
+ * try {
448
+ * const result = await capacitor.preferences.get('theme')
449
+ * if (!result.success) throw new Error(result.error.message)
450
+ * void result.data
451
+ * } finally {
452
+ * const closed = await capacitor.close()
453
+ * if (!closed.success) throw new Error(closed.error.message)
227
454
  * }
228
455
  * ```
229
456
  */
@@ -234,10 +461,20 @@ declare function safeGetPreference(key: string): Promise<HaiResult<string | null
234
461
  * @param key - Preference Key
235
462
  * @param value - 要写入的值
236
463
  * @returns HaiResult
237
- *
238
464
  * @example
239
465
  * ```ts
240
- * await capacitor.preferences.set('my_key', 'value')
466
+ * import { capacitor } from '@h-ai/capacitor'
467
+ *
468
+ * const initialized = await capacitor.init()
469
+ * if (!initialized.success) throw new Error(initialized.error.message)
470
+ * try {
471
+ * const result = await capacitor.preferences.set('theme', 'dark')
472
+ * if (!result.success) throw new Error(result.error.message)
473
+ * void result.data
474
+ * } finally {
475
+ * const closed = await capacitor.close()
476
+ * if (!closed.success) throw new Error(closed.error.message)
477
+ * }
241
478
  * ```
242
479
  */
243
480
  declare function safeSetPreference(key: string, value: string): Promise<HaiResult<void>>;
@@ -246,12 +483,22 @@ declare function safeSetPreference(key: string, value: string): Promise<HaiResul
246
483
  *
247
484
  * @param key - Preference Key
248
485
  * @returns HaiResult
249
- *
250
486
  * @example
251
487
  * ```ts
252
- * await capacitor.preferences.remove('my_key')
488
+ * import { capacitor } from '@h-ai/capacitor'
489
+ *
490
+ * const initialized = await capacitor.init()
491
+ * if (!initialized.success) throw new Error(initialized.error.message)
492
+ * try {
493
+ * const result = await capacitor.preferences.remove('theme')
494
+ * if (!result.success) throw new Error(result.error.message)
495
+ * void result.data
496
+ * } finally {
497
+ * const closed = await capacitor.close()
498
+ * if (!closed.success) throw new Error(closed.error.message)
499
+ * }
253
500
  * ```
254
501
  */
255
502
  declare function safeRemovePreference(key: string): Promise<HaiResult<void>>;
256
503
 
257
- export { type CameraOperations, type CapacitorFunctions, type DeviceInfo, type DeviceOperations, type DevicePlatform, HaiCapacitorError, type PhotoOptions, type PhotoResult, type PhotoSource, type PreferencesOperations, type PushNotification, type PushNotificationCallbacks, type PushOperations, type PushRegistration, type StatusBarConfig, type StatusBarOperations, type StatusBarStyle, capacitor, createCapacitorTokenStorage, safeGetPreference, safeRemovePreference, safeSetPreference };
504
+ export { type CameraOperations, type CapacitorFunctions, type DeviceInfo, type DeviceOperations, type DevicePlatform, HaiCapacitorError, type PhotoOptions, type PhotoResult, type PhotoSource, type PreferencesOperations, type PushCleanup, type PushNotification, type PushNotificationCallbacks, type PushOperations, type PushOptions, type PushRegistration, type StatusBarConfig, type StatusBarOperations, type StatusBarStyle, capacitor, createCapacitorTokenStorage, safeGetPreference, safeRemovePreference, safeSetPreference };