@workfly/wf-types 0.1.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.
Files changed (2) hide show
  1. package/package.json +19 -0
  2. package/wf.d.ts +474 -0
package/package.json ADDED
@@ -0,0 +1,19 @@
1
+ {
2
+ "name": "@workfly/wf-types",
3
+ "version": "0.1.0",
4
+ "description": "WorkFly 小程序全局 wf SDK 的 TypeScript 类型(由 src/shared/wf-api.ts 经 scripts/gen-wf-types.mjs 生成)。模板工程 devDependency 引入后 tsconfig types 即可拿到全局 wf。",
5
+ "types": "wf.d.ts",
6
+ "files": [
7
+ "wf.d.ts"
8
+ ],
9
+ "keywords": [
10
+ "workfly",
11
+ "miniapp",
12
+ "wf",
13
+ "types"
14
+ ],
15
+ "publishConfig": {
16
+ "access": "public"
17
+ },
18
+ "license": "MIT"
19
+ }
package/wf.d.ts ADDED
@@ -0,0 +1,474 @@
1
+ // 【自动生成,请勿手改】WorkFly 小程序 SDK 类型声明。
2
+ // 源:src/shared/wf-api.ts;重新生成:node scripts/gen-wf-types.mjs
3
+ // bundle(专业版)小程序把本文件放进工程即可全量 wf 类型;sandbox 直接用全局 wf。
4
+ /** 异步 API 的 wx 风格回调(与 Promise 双模并存)。`R` 为成功结果业务字段。 */
5
+ export interface WxCallbacks<R> {
6
+ /** 成功回调,入参为业务结果 + `errMsg: '<api>:ok'`。 */
7
+ success?: (res: R & { errMsg: string }) => void
8
+ /** 失败回调,入参为 `{ errMsg: '<api>:fail <reason>' }`。 */
9
+ fail?: (err: { errMsg: string }) => void
10
+ /** 结束回调,成功失败都触发。 */
11
+ complete?: (res: { errMsg: string }) => void
12
+ }
13
+
14
+ /** 异步 API 的入参 = 业务参数 + 回调。 */
15
+ export type WxAsync<A, R> = A & WxCallbacks<R>
16
+
17
+ // ── 网络 ──────────────────────────────────────────────────────────────────────
18
+
19
+ /** {@link WF.request} 入参。字段对齐 wx:用 header / data / method(非 headers / body)。 */
20
+ export interface RequestOptions {
21
+ /** 目标 URL。 */
22
+ url: string
23
+ /** HTTP 方法,默认 GET。 */
24
+ method?: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'HEAD' | 'OPTIONS'
25
+ /** 请求头。 */
26
+ header?: Record<string, string>
27
+ /** 请求体;对象会自动 JSON 序列化并补 Content-Type: application/json。 */
28
+ data?: string | Record<string, unknown>
29
+ /** 响应体解析方式,默认 'json'(自动 JSON.parse)。 */
30
+ dataType?: 'json' | 'text'
31
+ /** 超时毫秒数。 */
32
+ timeout?: number
33
+ }
34
+
35
+ /** {@link WF.request} 返回。字段对齐 wx:data / statusCode / header。 */
36
+ export interface RequestResult {
37
+ /** 响应体(dataType:'json' 时为解析后的对象)。 */
38
+ data: unknown
39
+ /** HTTP 状态码。 */
40
+ statusCode: number
41
+ /** 响应头。 */
42
+ header: Record<string, string>
43
+ }
44
+
45
+ // ── 存储 ──────────────────────────────────────────────────────────────────────
46
+
47
+ /** {@link WF.setStorage} 入参。 */
48
+ export interface SetStorageOptions {
49
+ /** 键名。 */
50
+ key: string
51
+ /** 值,任意可 JSON 序列化的数据。 */
52
+ data: unknown
53
+ /** 为 true 时加密落盘(走宿主 Vault),需声明 `vault` 权限。默认 false(明文)。 */
54
+ encrypt?: boolean
55
+ }
56
+
57
+ /** {@link WF.getStorage} 入参。 */
58
+ export interface GetStorageOptions {
59
+ /** 键名。 */
60
+ key: string
61
+ /** 与写入时一致:true 从加密存储读。默认 false。 */
62
+ encrypt?: boolean
63
+ }
64
+
65
+ /** 存储概况。 */
66
+ export interface StorageInfo {
67
+ /** 当前所有键名。 */
68
+ keys: string[]
69
+ /** 当前占用(KB,估算)。 */
70
+ currentSize: number
71
+ /** 上限(KB)。 */
72
+ limitSize: number
73
+ }
74
+
75
+ // ── 剪贴板 ────────────────────────────────────────────────────────────────────
76
+
77
+ /** {@link WF.setClipboardData} 入参。 */
78
+ export interface SetClipboardDataOptions {
79
+ /** 写入剪贴板的文本。 */
80
+ data: string
81
+ }
82
+
83
+ // ── UI 交互 ───────────────────────────────────────────────────────────────────
84
+
85
+ /** {@link WF.showToast} 入参。 */
86
+ export interface ToastOptions {
87
+ /** 提示文案。 */
88
+ title: string
89
+ /** 图标,默认 success。 */
90
+ icon?: 'success' | 'error' | 'loading' | 'none'
91
+ /** 停留毫秒数,默认 1500。 */
92
+ duration?: number
93
+ /** 是否显示透明蒙层防穿透,默认 false。 */
94
+ mask?: boolean
95
+ }
96
+
97
+ /** {@link WF.showLoading} 入参。 */
98
+ export interface LoadingOptions {
99
+ /** 加载文案。 */
100
+ title: string
101
+ /** 是否显示蒙层,默认 false。 */
102
+ mask?: boolean
103
+ }
104
+
105
+ /** {@link WF.showModal} 入参。 */
106
+ export interface ModalOptions {
107
+ /** 标题。 */
108
+ title?: string
109
+ /** 正文内容。 */
110
+ content?: string
111
+ /** 是否显示取消按钮,默认 true。 */
112
+ showCancel?: boolean
113
+ /** 取消按钮文案,默认「取消」。 */
114
+ cancelText?: string
115
+ /** 确认按钮文案,默认「确定」。 */
116
+ confirmText?: string
117
+ }
118
+
119
+ /** {@link WF.showModal} 返回。 */
120
+ export interface ModalResult {
121
+ /** 用户点了确认。 */
122
+ confirm: boolean
123
+ /** 用户点了取消(或蒙层关闭)。 */
124
+ cancel: boolean
125
+ }
126
+
127
+ /** {@link WF.showActionSheet} 入参。 */
128
+ export interface ActionSheetOptions {
129
+ /** 选项文案列表。 */
130
+ itemList: string[]
131
+ /** 选项文字颜色。 */
132
+ itemColor?: string
133
+ }
134
+
135
+ /** {@link WF.previewImage} 入参。 */
136
+ export interface PreviewImageOptions {
137
+ /** 要预览的图片地址列表。 */
138
+ urls: string[]
139
+ /** 当前显示的图片地址,默认 urls[0]。 */
140
+ current?: string
141
+ }
142
+
143
+ // ── 启动 / 系统 ───────────────────────────────────────────────────────────────
144
+
145
+ /** {@link WF.getLaunchOptionsSync} 返回。 */
146
+ export interface LaunchOptions {
147
+ /** 启动路径(小程序内部路由,预留)。 */
148
+ path: string
149
+ /** 启动携带的参数(deeplink / 剪贴板提示 / IM 内容匹配带入,如 { curl })。 */
150
+ query: Record<string, unknown>
151
+ /** 启动场景(panel / spotlight / deeplink / im)。 */
152
+ scene: string
153
+ }
154
+
155
+ /** {@link WF.getSystemInfoSync} 返回。 */
156
+ export interface SystemInfo {
157
+ /** 宿主平台标识,固定 'workfly'。 */
158
+ platform: 'workfly'
159
+ /** 当前主题,跟随宿主暗 / 亮。 */
160
+ theme: 'dark' | 'light'
161
+ /** 宿主应用版本。 */
162
+ version: string
163
+ /** 界面语言(如 'zh-CN')。 */
164
+ language: string
165
+ }
166
+
167
+ // ── BIP 生态扩展 ──────────────────────────────────────────────────────────────
168
+
169
+ /** {@link BipNamespace.rsaEncrypt} 入参。 */
170
+ export interface RsaEncryptOptions {
171
+ /** PEM 格式公钥(调用方自带)。 */
172
+ publicKey: string
173
+ /** 待加密明文。 */
174
+ data: string
175
+ /** 填充模式,默认 pkcs1(对齐 Java Cipher.getInstance("RSA"))。 */
176
+ padding?: 'pkcs1' | 'oaep'
177
+ }
178
+
179
+ /** {@link BipNamespace.rsaEncrypt} 返回。 */
180
+ export interface RsaEncryptResult {
181
+ /** base64 编码的密文。 */
182
+ data: string
183
+ }
184
+
185
+ /**
186
+ * `wf.bip.*` —— BIP 生态扩展区。
187
+ *
188
+ * `wf.*` 是对标 wx 的可移植标准面(与 BIP 无关);本命名空间是 WorkFly 作为用友 BIP
189
+ * 客户端特有的、wx 没有的业务能力,统一围进此处,不污染标准面(先例:wx.cloud.*)。
190
+ */
191
+ export interface BipNamespace {
192
+ /**
193
+ * RSA 公钥加密(wx 无等价的 BIP 生态扩展)。
194
+ *
195
+ * 当前服务 BIP 系 OAuth 握手:包内拼 `clientSecret:秒级时间戳` 后调用本方法得到
196
+ * base64 密文,再走授权码链路。公钥由调用方传入,宿主不知用途。
197
+ *
198
+ * @status stable
199
+ * @example
200
+ * const { data } = await wf.bip.rsaEncrypt({
201
+ * publicKey,
202
+ * data: 'secret:' + Math.floor(Date.now() / 1000)
203
+ * })
204
+ */
205
+ rsaEncrypt(opts: WxAsync<RsaEncryptOptions, RsaEncryptResult>): Promise<RsaEncryptResult>
206
+ }
207
+
208
+ // ── 顶层 WF 接口 ──────────────────────────────────────────────────────────────
209
+
210
+ /**
211
+ * WorkFly 小程序 SDK 全局对象 `wf`(对标微信 `wx`)。
212
+ *
213
+ * - sandbox 形态:`window.wf`
214
+ * - bundle 形态:全局 `wf`,且挂载函数 `mount(el, ctx)` 的 `ctx` 指向同一对象。
215
+ */
216
+ export interface WF {
217
+ // —— 网络 ——
218
+ /**
219
+ * 发起 HTTP 请求(经主进程代理,绕 CORS、可直发任意头)。
220
+ * @status stable
221
+ * @example
222
+ * const res = await wf.request({
223
+ * url: 'https://api.example.com/items',
224
+ * method: 'GET'
225
+ * })
226
+ * console.log(res.statusCode, res.data)
227
+ */
228
+ request(opts: WxAsync<RequestOptions, RequestResult>): Promise<RequestResult>
229
+
230
+ // —— 存储(异步) ——
231
+ /**
232
+ * 写入本地存储。`encrypt:true` 时加密落盘(Vault)。
233
+ * @status stable
234
+ * @example
235
+ * await wf.setStorage({
236
+ * key: 'items',
237
+ * data: list
238
+ * })
239
+ * // encrypt:true 走加密存储(需 vault 权限)
240
+ * await wf.setStorage({
241
+ * key: 'token',
242
+ * data: token,
243
+ * encrypt: true
244
+ * })
245
+ */
246
+ setStorage(opts: WxAsync<SetStorageOptions, void>): Promise<void>
247
+ /**
248
+ * 读取本地存储。
249
+ * @status stable
250
+ * @example
251
+ * const { data } = await wf.getStorage({
252
+ * key: 'items'
253
+ * })
254
+ */
255
+ getStorage(opts: WxAsync<GetStorageOptions, { data: unknown }>): Promise<{ data: unknown }>
256
+ /**
257
+ * 删除指定键。
258
+ * @status stable
259
+ * @example
260
+ * await wf.removeStorage({
261
+ * key: 'items'
262
+ * })
263
+ */
264
+ removeStorage(opts: WxAsync<{ key: string }, void>): Promise<void>
265
+ /**
266
+ * 清空本小程序的全部存储。
267
+ * @status stable
268
+ * @example
269
+ * await wf.clearStorage()
270
+ */
271
+ clearStorage(opts?: WxCallbacks<void>): Promise<void>
272
+ /**
273
+ * 获取存储概况。
274
+ * @status stable
275
+ * @example
276
+ * const { keys, currentSize, limitSize } = await wf.getStorageInfo()
277
+ */
278
+ getStorageInfo(opts?: WxCallbacks<StorageInfo>): Promise<StorageInfo>
279
+
280
+ // —— 存储(同步) ——
281
+ /**
282
+ * 同步写入本地存储。
283
+ * @status stable
284
+ * @example
285
+ * wf.setStorageSync('count', 1)
286
+ * // 第三参数 encrypt:true 加密落盘
287
+ * wf.setStorageSync('token', token, true)
288
+ */
289
+ setStorageSync(key: string, data: unknown, encrypt?: boolean): void
290
+ /**
291
+ * 同步读取本地存储。
292
+ * @status stable
293
+ * @example
294
+ * const count = wf.getStorageSync('count')
295
+ */
296
+ getStorageSync(key: string, encrypt?: boolean): unknown
297
+ /**
298
+ * 同步删除指定键。
299
+ * @status stable
300
+ * @example
301
+ * wf.removeStorageSync('count')
302
+ */
303
+ removeStorageSync(key: string): void
304
+ /**
305
+ * 同步清空全部存储。
306
+ * @status stable
307
+ * @example
308
+ * wf.clearStorageSync()
309
+ */
310
+ clearStorageSync(): void
311
+ /**
312
+ * 同步获取存储概况。
313
+ * @status stable
314
+ * @example
315
+ * const { keys, currentSize, limitSize } = wf.getStorageInfoSync()
316
+ */
317
+ getStorageInfoSync(): StorageInfo
318
+
319
+ // —— 剪贴板 ——
320
+ /**
321
+ * 写入系统剪贴板。
322
+ * @status stable
323
+ * @example
324
+ * await wf.setClipboardData({
325
+ * data: 'Hello WorkFly'
326
+ * })
327
+ */
328
+ setClipboardData(opts: WxAsync<SetClipboardDataOptions, void>): Promise<void>
329
+ /**
330
+ * 读取系统剪贴板文本。
331
+ * @status stable
332
+ * @example
333
+ * const { data } = await wf.getClipboardData()
334
+ */
335
+ getClipboardData(opts?: WxCallbacks<{ data: string }>): Promise<{ data: string }>
336
+
337
+ // —— UI 交互(宿主渲染) ——
338
+ /**
339
+ * 显示消息提示框。
340
+ * @status stable
341
+ * @example
342
+ * await wf.showToast({
343
+ * title: '已保存',
344
+ * icon: 'success'
345
+ * })
346
+ */
347
+ showToast(opts: WxAsync<ToastOptions, void>): Promise<void>
348
+ /**
349
+ * 隐藏消息提示框。
350
+ * @status stable
351
+ * @example
352
+ * await wf.hideToast()
353
+ */
354
+ hideToast(opts?: WxCallbacks<void>): Promise<void>
355
+ /**
356
+ * 显示加载提示框(需配对 hideLoading)。
357
+ * @status stable
358
+ * @example
359
+ * await wf.showLoading({
360
+ * title: '加载中…'
361
+ * })
362
+ * // 完成后务必配对 hideLoading
363
+ * await wf.hideLoading()
364
+ */
365
+ showLoading(opts: WxAsync<LoadingOptions, void>): Promise<void>
366
+ /**
367
+ * 隐藏加载提示框。
368
+ * @status stable
369
+ * @example
370
+ * await wf.hideLoading()
371
+ */
372
+ hideLoading(opts?: WxCallbacks<void>): Promise<void>
373
+ /**
374
+ * 显示模态对话框。
375
+ * @status stable
376
+ * @example
377
+ * const { confirm } = await wf.showModal({
378
+ * title: '删除',
379
+ * content: '确定删除?'
380
+ * })
381
+ * if (confirm) {
382
+ * // 执行删除
383
+ * }
384
+ */
385
+ showModal(opts: WxAsync<ModalOptions, ModalResult>): Promise<ModalResult>
386
+ /**
387
+ * 显示底部操作菜单。
388
+ * @status stable
389
+ * @example
390
+ * const { tapIndex } = await wf.showActionSheet({
391
+ * itemList: ['编辑', '删除']
392
+ * })
393
+ */
394
+ showActionSheet(
395
+ opts: WxAsync<ActionSheetOptions, { tapIndex: number }>
396
+ ): Promise<{ tapIndex: number }>
397
+ /**
398
+ * 全屏预览图片(复用宿主看图窗口)。
399
+ * @status stable
400
+ * @example
401
+ * await wf.previewImage({
402
+ * urls: ['https://example.com/a.png', 'https://example.com/b.png']
403
+ * })
404
+ */
405
+ previewImage(opts: WxAsync<PreviewImageOptions, void>): Promise<void>
406
+
407
+ // —— 启动 / 系统 ——
408
+ /**
409
+ * 同步获取小程序启动参数(场景、携带 query)。
410
+ * @status stable
411
+ * @example
412
+ * const { path, query, scene } = wf.getLaunchOptionsSync()
413
+ * // query 带入 deeplink / 剪贴板 / IM 内容匹配的参数
414
+ */
415
+ getLaunchOptionsSync(): LaunchOptions
416
+ /**
417
+ * 同步获取本次再进入的参数(deeplink / IM 二次唤起)。
418
+ * @status stable
419
+ * @example
420
+ * const opts = wf.getEnterOptionsSync()
421
+ */
422
+ getEnterOptionsSync(): LaunchOptions
423
+ /**
424
+ * 同步获取宿主系统信息(平台、主题、版本、语言)。
425
+ * @status stable
426
+ * @example
427
+ * const { platform, theme, version, language } = wf.getSystemInfoSync()
428
+ */
429
+ getSystemInfoSync(): SystemInfo
430
+
431
+ // —— BIP 生态扩展 ——
432
+ /** BIP 生态扩展命名空间,详见 {@link BipNamespace}。 */
433
+ bip: BipNamespace
434
+ }
435
+
436
+ // ── bundle 形态挂载契约(与宿主 BundleHost 对齐)──────────────────────────────────
437
+
438
+ /** 小程序清单的最小可见面(bundle 运行时拿得到的 app 元信息)。 */
439
+ export interface MiniAppManifest {
440
+ id: string
441
+ name: string
442
+ version: string
443
+ ui: {
444
+ standalone: {
445
+ enabled: true
446
+ entry: string
447
+ render?: 'webview' | 'sandbox' | 'bundle'
448
+ layout?: 'full' | 'split'
449
+ }
450
+ }
451
+ permissions?: ({ id: string; reason: string } | string)[]
452
+ [k: string]: unknown
453
+ }
454
+
455
+ /** 宿主传给挂载函数的上下文;`ctx.wf` 与全局 `wf` 是同一对象。 */
456
+ export interface BundleCtx {
457
+ app: MiniAppManifest
458
+ wf: WF
459
+ /** 返回上一层(关闭本小程序视图)。 */
460
+ onBack: () => void
461
+ /** deeplink / 剪贴板 / IM 内容带入的启动 payload(如 { curl }),可据此预填。 */
462
+ launchPayload?: Record<string, unknown> | null
463
+ }
464
+
465
+ /** bundle 入口默认导出的「挂载函数」:在宿主给的 el 内自建一棵树,返回 unmount。 */
466
+ export type BundleMount = (el: HTMLElement, ctx: BundleCtx) => () => void
467
+
468
+ declare global {
469
+ /** 宿主注入的小程序 SDK(sandbox: window.wf;bundle: 全局 wf 与 ctx.wf 同一对象)。 */
470
+ const wf: WF
471
+ interface Window {
472
+ wf: WF
473
+ }
474
+ }