@h-ai/capacitor 0.1.0-alpha.67 → 0.1.0-alpha.68

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.en.md CHANGED
@@ -47,7 +47,11 @@ await apiClient.init({
47
47
  })
48
48
  ```
49
49
 
50
- `createCapacitorTokenStorage()` persists tokens only on native Android/iOS and requires `@aparajita/capacitor-secure-storage`. On Web it does not fall back to `localStorage`: `get*()` returns `null`, and `set*()` / `clear()` are no-ops.
50
+ `createCapacitorTokenStorage()` persists tokens only on native Android/iOS and requires `@aparajita/capacitor-secure-storage`. Its methods return promises directly: Web calls, an unavailable plugin, and failed reads, writes, or removals reject instead of pretending credentials are absent or an operation succeeded. Web entry points should select api-client cookie or memory storage.
51
+
52
+ `clear()` immediately blocks both old credentials in this adapter instance and waits for all native removals before reporting the result. Failures keep them blocked; a successful write reopens only that key. Update credentials through `apiClient.auth` for serialized writes and handle rejected `setTokens()` / `clear()` calls. The UI must release its loading state, discard the logged-out identity, and display failures. Instance-local blocking does not prove persistent deletion; revocation across restarts still requires server logout/session revocation. Retry with the same instance after the plugin recovers.
53
+
54
+ A failed credential write blocks both credentials, including an access token already saved before a refresh-token write failed. After failed login or registration persistence, attempt `auth.clear()` to remove partially saved data. Report cleanup failures explicitly instead of claiming device credentials were deleted.
51
55
 
52
56
  #### Device information and push notifications
53
57
 
@@ -65,6 +69,8 @@ if (reg.success) {
65
69
  }
66
70
  ```
67
71
 
72
+ Push requires an actual native platform. The setup phase of `push.register({ signal? })` and `push.listen(callbacks, { signal? })` has a 30-second deadline including permission requests and listener attachment. Failure, timeout, cancellation and `capacitor.close()` stop callbacks and remove owned handles; late handles are removed when they arrive. Other consumers are not affected through removeAllListeners. After listen succeeds, its signal and returned cleanup can still stop it. Cleanup and close return `HaiResult<void>`; native removal failures return `PUSH_CLEANUP_FAILED` (`capacitor:032`) and retain failed handles for retry without repeating successful removals. Closing disables the module immediately, and initialization retries pending cleanup first. Controlled plugin timing tests do not replace repeated registration, permission and FCM/APNs checks on Android/iOS devices.
73
+
68
74
  #### Status bar
69
75
 
70
76
  ```ts
@@ -111,7 +117,7 @@ export const ssr = false
111
117
 
112
118
  ### Error handling
113
119
 
114
- All asynchronous APIs return `HaiResult<T>` instead of throwing directly:
120
+ Business operations on `capacitor.*` and `safe*Preference` return `HaiResult<T>`. TokenStorage adapter methods reject their promises on platform or storage errors:
115
121
 
116
122
  ```ts
117
123
  import { capacitor, HaiCapacitorError } from '@h-ai/capacitor'
@@ -150,6 +156,8 @@ pnpm --filter @h-ai/capacitor test
150
156
 
151
157
  > Tests must mock `@capacitor/*` modules because they depend on native plugins.
152
158
 
159
+ Public operations, internal resource scopes and types include typechecked examples; see the [JSDoc conventions (Chinese)](../../.agents/instructions/module-conventions.md#jsdoc-与示例验证). Typechecking does not prove device permissions or live FCM/APNs delivery.
160
+
153
161
  ## License
154
162
 
155
163
  Apache-2.0
package/README.ja.md CHANGED
@@ -47,7 +47,11 @@ await apiClient.init({
47
47
  })
48
48
  ```
49
49
 
50
- `createCapacitorTokenStorage()` は Android/iOS のネイティブ環境でのみ Token を永続化し、`@aparajita/capacitor-secure-storage` が必要です。Web では `localStorage` に切り替わらず、`get*()` は `null` を返し、`set*()` / `clear()` は何もしません。
50
+ `createCapacitorTokenStorage()` は Android/iOS のネイティブ環境でのみ Token を永続化し、`@aparajita/capacitor-secure-storage` が必要です。メソッドは Promise を直接返し、Web 呼び出し、プラグインの利用不可、読み書き・削除の失敗は reject されます。空の認証情報や操作成功として扱わないでください。Web では api-client の Cookie またはメモリーストレージを選びます。
51
+
52
+ `clear()` は開始時にこのインスタンスの古い認証情報を読み取り不可にし、すべてのネイティブ削除が完了してから結果を返します。失敗時は読み取りを禁止したままにし、各キーへの書き込み成功時のみそのキーを再開します。書き込みを直列化する `apiClient.auth` を使い、`setTokens()` / `clear()` の失敗を処理してください。UI は loading を解除し、ログアウトしたユーザー情報を消去して失敗を表示します。この制限はインスタンス内のみで、永続データの削除を保証しません。再起動後の失効にはサーバーでのセッション撤回が必要です。プラグイン復旧後は同じインスタンスで再試行できます。
53
+
54
+ 認証情報の一方の書き込みが失敗すると両方を読み取り不可にするため、access token 保存後に refresh token の保存が失敗しても不完全な認証状態は公開しません。ログイン・登録時の保存失敗後は `auth.clear()` で部分保存データの削除を試み、削除も失敗した場合は端末の認証情報を削除できたと表示しないでください。
51
55
 
52
56
  #### 端末情報とプッシュ通知
53
57
 
@@ -65,6 +69,8 @@ if (reg.success) {
65
69
  }
66
70
  ```
67
71
 
72
+ Push は実際のネイティブ環境が必要です。`push.register({ signal? })` と `push.listen(callbacks, { signal? })` の設定段階は権限要求とリスナー登録を含め30秒で終了します。失敗、タイムアウト、中止、`capacitor.close()` はコールバックを停止し、所有するハンドルを除去します。遅延したハンドルも到着時に除去し、removeAllListeners で他の利用者を巻き込みません。listen 成功後も signal と cleanup が有効です。cleanup と close は `HaiResult<void>` を返し、ネイティブ除去失敗は `PUSH_CLEANUP_FAILED`(`capacitor:032`)です。失敗したハンドルを保持して再試行し、成功済みの除去は繰り返しません。終了直後にモジュールを無効化し、再初期化前に残存ハンドルを処理します。制御したプラグイン試験は Android/iOS 実機の権限、FCM/APNs、反復登録試験を代替しません。
73
+
68
74
  #### ステータスバー
69
75
 
70
76
  ```ts
@@ -111,7 +117,7 @@ export const ssr = false
111
117
 
112
118
  ### エラー処理
113
119
 
114
- すべての非同期 API は直接 throw せず、`HaiResult<T>` を返します:
120
+ `capacitor.*` と `safe*Preference` は `HaiResult<T>` を返します。TokenStorage のメソッドはプラットフォームや保存エラーで Promise を reject します。
115
121
 
116
122
  ```ts
117
123
  import { capacitor, HaiCapacitorError } from '@h-ai/capacitor'
@@ -150,6 +156,8 @@ pnpm --filter @h-ai/capacitor test
150
156
 
151
157
  > ネイティブプラグインへの依存があるため、テストでは `@capacitor/*` モジュールを mock する必要があります。
152
158
 
159
+ 公開操作、内部リソースと型には型検査できる例があります。[JSDoc 規約(中国語)](../../.agents/instructions/module-conventions.md#jsdoc-与示例验证) を参照してください。型検査は端末権限や FCM/APNs の実配信を保証しません。
160
+
153
161
  ## ライセンス
154
162
 
155
163
  Apache-2.0
package/README.md CHANGED
@@ -47,7 +47,11 @@ await apiClient.init({
47
47
  })
48
48
  ```
49
49
 
50
- `createCapacitorTokenStorage()` 仅在 Android/iOS 原生环境中持久化 token,并要求已安装 `@aparajita/capacitor-secure-storage`。纯 Web 环境下不会退化到 `localStorage`;`get*()` 返回 `null`,`set*()` / `clear()` 为 no-op。
50
+ `createCapacitorTokenStorage()` 仅在 Android/iOS 原生环境中持久化 token,并要求已安装 `@aparajita/capacitor-secure-storage`。适配器的方法直接返回 Promise:Web 调用、插件不可用、读写或删除失败都会拒绝 Promise,不能当作空凭据或保存/退出成功。Web 入口应选择 api-client 的 Cookie/内存存储。
51
+
52
+ `clear()` 一开始就屏蔽本实例两类旧凭据,并等待所有原生删除结束后才报告结果;失败时保持屏蔽,相应 key 成功写入后才重新开放。通过 `apiClient.auth` 更新凭据以串行提交写入,并处理 `setTokens()` / `clear()` 的拒绝;界面仍须释放 loading、清理退出身份并提示失败。屏蔽只作用于当前适配器实例,不代表持久数据已删除,跨重启撤销依赖服务端退出/撤销会话。插件恢复后可以用同一实例重试,无需重建模块。
53
+
54
+ 任一凭据写入失败都会屏蔽整对凭据,因此 access 已保存而 refresh 保存失败时也不会留下可读的半完成会话。登录/注册保存失败后应尝试 `auth.clear()` 清理部分持久数据;清理再次失败须明确提示,不得声称设备凭据已删除。
51
55
 
52
56
  #### 设备信息与推送
53
57
 
@@ -65,6 +69,8 @@ if (reg.success) {
65
69
  }
66
70
  ```
67
71
 
72
+ 推送仅在实际原生平台调用插件。`push.register({ signal? })` 与 `push.listen(callbacks, { signal? })` 的设置阶段限时 30 秒(包含权限和监听器挂载);失败、超时、取消及 `capacitor.close()` 都会停止回调并移除本次拥有的句柄,迟到的句柄在完成后立即移除,不调用影响其他消费者的 removeAllListeners。listen 成功后信号仍有效,也可调用返回的 cleanup。cleanup 与 close 均返回 `HaiResult<void>`;原生 remove 失败返回 `PUSH_CLEANUP_FAILED`(`capacitor:032`),失败句柄保留供再次调用重试,成功移除的句柄不重复操作。模块关闭后立即不可用;重新初始化前先重试遗留清理。自动化验证控制插件时序,不能代替 Android/iOS 真机 FCM/APNs 权限、注册与反复挂载验收。
73
+
68
74
  #### 状态栏
69
75
 
70
76
  ```ts
@@ -111,7 +117,7 @@ export const ssr = false
111
117
 
112
118
  ### 错误处理
113
119
 
114
- 所有异步 API 返回 `HaiResult<T>`,不会直接 throw:
120
+ `capacitor.*` 业务操作与 `safe*Preference` 返回 `HaiResult<T>`;TokenStorage 适配器方法以 Promise 拒绝报告平台与存储错误:
115
121
 
116
122
  ```ts
117
123
  import { capacitor, HaiCapacitorError } from '@h-ai/capacitor'
@@ -150,6 +156,8 @@ pnpm --filter @h-ai/capacitor test
150
156
 
151
157
  > 由于原生插件依赖,测试中需 mock `@capacitor/*` 模块。
152
158
 
159
+ 公开操作、内部资源作用域与类型均提供可类型检查的示例;见[模块 JSDoc 约定](../../.agents/instructions/module-conventions.md#jsdoc-与示例验证)。推送示例的类型检查不证明设备权限或 FCM/APNs 实际可用。
160
+
153
161
  ## 许可证
154
162
 
155
163
  Apache-2.0
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;
@@ -14,10 +25,24 @@ declare const HaiCapacitorError: {
14
25
  readonly APP_VERSION_FAILED: _h_ai_core.HaiErrorDef;
15
26
  readonly PUSH_REGISTER_FAILED: _h_ai_core.HaiErrorDef;
16
27
  readonly PUSH_LISTEN_FAILED: _h_ai_core.HaiErrorDef;
28
+ readonly PUSH_CLEANUP_FAILED: _h_ai_core.HaiErrorDef;
17
29
  readonly CAMERA_FAILED: _h_ai_core.HaiErrorDef;
18
30
  readonly STATUS_BAR_FAILED: _h_ai_core.HaiErrorDef;
19
31
  };
20
- /** 设备信息操作 */
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
+ */
21
46
  interface DeviceOperations {
22
47
  /** 获取设备信息(平台、型号、系统版本等) */
23
48
  getInfo: () => Promise<HaiResult<DeviceInfo>>;
@@ -27,19 +52,58 @@ interface DeviceOperations {
27
52
  build: string;
28
53
  }>>;
29
54
  }
30
- /** 相机操作 */
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
+ */
31
69
  interface CameraOperations {
32
70
  /** 拍照或选取相册图片 */
33
71
  takePhoto: (options?: PhotoOptions) => Promise<HaiResult<PhotoResult>>;
34
72
  }
35
- /** 推送通知操作 */
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
+ */
36
87
  interface PushOperations {
37
- /** 注册推送通知,返回设备 Token */
38
- register: () => Promise<HaiResult<PushRegistration>>;
39
- /** 监听推送通知事件,返回清理函数 */
40
- 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>>;
41
92
  }
42
- /** 状态栏操作 */
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
+ */
43
107
  interface StatusBarOperations {
44
108
  /** 配置状态栏(样式、背景色、沉浸式) */
45
109
  configure: (config: StatusBarConfig) => Promise<HaiResult<void>>;
@@ -48,7 +112,20 @@ interface StatusBarOperations {
48
112
  /** 隐藏状态栏 */
49
113
  hide: () => Promise<HaiResult<void>>;
50
114
  }
51
- /** 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
+ */
52
129
  interface PreferencesOperations {
53
130
  /** 安全读取 Preference 值 */
54
131
  get: (key: string) => Promise<HaiResult<string | null>>;
@@ -57,12 +134,29 @@ interface PreferencesOperations {
57
134
  /** 安全删除 Preference 值 */
58
135
  remove: (key: string) => Promise<HaiResult<void>>;
59
136
  }
60
- /** 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
+ */
61
155
  interface CapacitorFunctions {
62
156
  /** 初始化 Capacitor 模块 */
63
157
  init: () => Promise<HaiResult<void>>;
64
- /** 关闭模块,重置状态 */
65
- close: () => Promise<void>;
158
+ /** 关闭模块并回收推送监听;失败时保持不可用,保留句柄供再次 close 重试。 */
159
+ close: () => Promise<HaiResult<void>>;
66
160
  /** 获取当前平台 */
67
161
  getPlatform: () => string;
68
162
  /** 是否运行在原生 App 中 */
@@ -80,9 +174,27 @@ interface CapacitorFunctions {
80
174
  /** Preferences 安全读写操作 */
81
175
  readonly preferences: PreferencesOperations;
82
176
  }
83
- /** 设备平台 */
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
+ */
84
187
  type DevicePlatform = 'ios' | 'android' | 'web';
85
- /** 设备信息 */
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
+ */
86
198
  interface DeviceInfo {
87
199
  /** 平台(ios / android / web) */
88
200
  platform: DevicePlatform;
@@ -99,7 +211,44 @@ interface DeviceInfo {
99
211
  /** 应用 Build 号 */
100
212
  appBuild?: string;
101
213
  }
102
- /** 推送通知数据 */
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
+ */
103
252
  interface PushNotification {
104
253
  /** 通知 ID */
105
254
  id: string;
@@ -110,21 +259,57 @@ interface PushNotification {
110
259
  /** 附加数据 */
111
260
  data?: Record<string, unknown>;
112
261
  }
113
- /** 推送注册结果 */
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
+ */
114
272
  interface PushRegistration {
115
273
  /** 设备推送 Token */
116
274
  token: string;
117
275
  }
118
- /** 推送通知回调 */
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
+ */
119
286
  interface PushNotificationCallbacks {
120
287
  /** 收到推送时调用 */
121
288
  onReceived?: (notification: PushNotification) => void;
122
289
  /** 用户点击推送时调用 */
123
290
  onActionPerformed?: (notification: PushNotification) => void;
124
291
  }
125
- /** 照片来源 */
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
+ */
126
302
  type PhotoSource = 'camera' | 'photos' | 'prompt';
127
- /** 照片选项 */
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
+ */
128
313
  interface PhotoOptions {
129
314
  /** 来源(默认 'prompt') */
130
315
  source?: PhotoSource;
@@ -137,16 +322,43 @@ interface PhotoOptions {
137
322
  /** 返回格式 */
138
323
  resultType?: 'uri' | 'base64' | 'dataUrl';
139
324
  }
140
- /** 照片结果 */
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
+ */
141
335
  interface PhotoResult {
142
336
  /** 图片路径或 base64 数据 */
143
337
  data: string;
144
- /** MIME 类型 */
338
+ /** 原生插件返回的格式扩展名(如 jpeg),不是 MIME 类型 */
145
339
  format: string;
146
340
  }
147
- /** 状态栏样式 */
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
+ */
148
351
  type StatusBarStyle = 'dark' | 'light' | 'default';
149
- /** 状态栏配置 */
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
+ */
150
362
  interface StatusBarConfig {
151
363
  /** 状态栏文字样式 */
152
364
  style?: StatusBarStyle;
@@ -167,15 +379,19 @@ interface StatusBarConfig {
167
379
 
168
380
  /**
169
381
  * Capacitor 模块服务对象
170
- *
171
382
  * @example
172
383
  * ```ts
173
384
  * import { capacitor } from '@h-ai/capacitor'
174
385
  *
175
- * const result = await capacitor.init()
176
- * if (result.success) {
177
- * const info = await capacitor.device.getInfo()
178
- * 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)
179
395
  * }
180
396
  * ```
181
397
  */
@@ -194,22 +410,26 @@ declare const capacitor: CapacitorFunctions;
194
410
  * 创建基于原生安全存储的 TokenStorage
195
411
  *
196
412
  * 在原生 Android/iOS 环境下使用 `@aparajita/capacitor-secure-storage`
197
- * 保存 access token / refresh token;Web 环境不做不安全回退。
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 { apiClient } from '@h-ai/api-client'
205
- *
206
- * await apiClient.init({
207
- * baseUrl: 'https://api.example.com/v1',
208
- * auth: {
209
- * storage: createCapacitorTokenStorage(),
210
- * refreshPath: '/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 };