@heybox/hb-sdk 0.1.2 → 0.2.0-alpha.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 (49) hide show
  1. package/README.md +172 -39
  2. package/bin/hb-sdk.cjs +3 -0
  3. package/dist/cli.cjs +9639 -0
  4. package/dist/devtools/mock-host/index.html +626 -0
  5. package/dist/index.cjs.js +513 -83
  6. package/dist/index.esm.js +499 -78
  7. package/dist/protocol.cjs.js +104 -0
  8. package/dist/protocol.esm.js +90 -0
  9. package/dist/templates/vue3-vite-ts/.gitignore.ejs +5 -0
  10. package/dist/templates/vue3-vite-ts/README.md.ejs +46 -0
  11. package/dist/templates/vue3-vite-ts/index.html.ejs +12 -0
  12. package/dist/templates/vue3-vite-ts/package.json.ejs +29 -0
  13. package/dist/templates/vue3-vite-ts/src/App.vue +63 -0
  14. package/dist/templates/vue3-vite-ts/src/__tests__/App.spec.ts +67 -0
  15. package/dist/templates/vue3-vite-ts/src/main.ts +5 -0
  16. package/dist/templates/vue3-vite-ts/src/styles.css +60 -0
  17. package/dist/templates/vue3-vite-ts/src/vite-env.d.ts +1 -0
  18. package/dist/templates/vue3-vite-ts/tsconfig.app.json +17 -0
  19. package/dist/templates/vue3-vite-ts/tsconfig.json +11 -0
  20. package/dist/templates/vue3-vite-ts/tsconfig.node.json +11 -0
  21. package/dist/templates/vue3-vite-ts/vite.config.ts +6 -0
  22. package/dist/templates/vue3-vite-ts/vitest.config.ts +10 -0
  23. package/package.json +28 -5
  24. package/types/core/client.d.ts +27 -4
  25. package/types/core/errors.d.ts +45 -2
  26. package/types/core/sdk.d.ts +78 -10
  27. package/types/core/singleton.d.ts +33 -7
  28. package/types/core/utils.d.ts +2 -0
  29. package/types/index.d.ts +10 -4
  30. package/types/modules/auth/index.d.ts +42 -0
  31. package/types/modules/network/index.d.ts +125 -0
  32. package/types/modules/share/index.d.ts +12 -2
  33. package/types/modules/share/screenshot.d.ts +14 -2
  34. package/types/modules/share/show-share-menu.d.ts +14 -2
  35. package/types/modules/share/types.d.ts +24 -4
  36. package/types/modules/storage/index.d.ts +70 -0
  37. package/types/modules/user/get-info.d.ts +11 -1
  38. package/types/modules/user/index.d.ts +13 -9
  39. package/types/modules/user/types.d.ts +1 -0
  40. package/types/modules/viewport/index.d.ts +78 -0
  41. package/types/protocol/guards.d.ts +6 -1
  42. package/types/protocol/types.d.ts +19 -4
  43. package/types/protocol.d.ts +18 -0
  44. package/types/modules/system/get-storage.d.ts +0 -15
  45. package/types/modules/system/get-window-info.d.ts +0 -16
  46. package/types/modules/system/index.d.ts +0 -23
  47. package/types/modules/system/set-storage.d.ts +0 -12
  48. package/types/modules/system/types.d.ts +0 -34
  49. package/types/modules/user/login.d.ts +0 -18
@@ -1,5 +1,10 @@
1
1
  import type { MiniProgramEventHandler, MiniProgramEventName } from '../protocol/types';
2
- /** 创建 SDK bridge client 的配置项。 */
2
+ /**
3
+ * 创建 SDK bridge client 的配置项。
4
+ *
5
+ * @remarks
6
+ * 该配置主要用于独立实例、自定义宿主环境注入和测试场景。
7
+ */
3
8
  export interface MiniProgramSDKOptions {
4
9
  /** 请求与握手超时时间,单位毫秒。 */
5
10
  timeout?: number;
@@ -9,10 +14,24 @@ export interface MiniProgramSDKOptions {
9
14
  selfWindow?: Window;
10
15
  /** 父窗口,测试环境可注入;显式传 null 可模拟非 iframe 环境。 */
11
16
  targetWindow?: Window | null;
17
+ /** 精准 postMessage 目标 origin;未传时尝试推断,失败则回退为 `*`。 */
18
+ targetOrigin?: string;
12
19
  }
13
- /** 模块 API 发起请求所需的最小能力。 */
20
+ /**
21
+ * 模块 API 发起请求所需的最小能力。
22
+ *
23
+ * @remarks
24
+ * 各公开模块只依赖该最小接口发起 bridge 请求,以保持模块层与底层实现解耦。
25
+ */
14
26
  export interface MiniProgramRequester {
15
- /** 向父容器调用指定开放能力。 */
27
+ /**
28
+ * 向父容器调用指定开放能力。
29
+ *
30
+ * @typeParam T 标准化后的返回值类型。
31
+ * @param method bridge method 名称。
32
+ * @param payload 可序列化请求载荷。
33
+ * @returns 由父容器返回的标准化结果。
34
+ */
16
35
  request<T>(method: string, payload?: unknown): Promise<T>;
17
36
  }
18
37
  /** 底层 bridge client,负责握手、请求响应、事件分发与超时清理。 */
@@ -20,6 +39,7 @@ export declare class MiniProgramBridgeClient implements MiniProgramRequester {
20
39
  private readonly timeout;
21
40
  private readonly selfWindow?;
22
41
  private readonly targetWindow;
42
+ private readonly targetOrigin;
23
43
  private readonly nonce;
24
44
  private readonly pendingRequests;
25
45
  private readonly eventBus;
@@ -30,6 +50,8 @@ export declare class MiniProgramBridgeClient implements MiniProgramRequester {
30
50
  private resolveReady;
31
51
  private rejectReady;
32
52
  private readyTimer?;
53
+ private handshakeRetryTimer?;
54
+ private destroyed;
33
55
  constructor(options?: MiniProgramSDKOptions);
34
56
  /** 等待父容器握手完成。 */
35
57
  ready(): Promise<void>;
@@ -42,12 +64,13 @@ export declare class MiniProgramBridgeClient implements MiniProgramRequester {
42
64
  /** 销毁 SDK 实例,并拒绝尚未完成的请求。 */
43
65
  destroy(): void;
44
66
  private ensureStarted;
67
+ private postHandshake;
45
68
  private onMessage;
46
69
  private handleEvent;
47
70
  private handleResponse;
48
71
  private postMessage;
49
72
  private resolveReadyOnce;
50
73
  private failReady;
51
- private clearReadyTimer;
74
+ private clearReadyTimers;
52
75
  private rejectAllPending;
53
76
  }
@@ -1,5 +1,13 @@
1
1
  import type { MiniProgramBridgeError } from '../protocol/types';
2
- /** SDK 对外抛出的标准错误类型。 */
2
+ import type { MiniProgramNetworkRequestConfig, MiniProgramNetworkResponse } from '../modules/network';
3
+ /**
4
+ * SDK 对外抛出的标准 bridge / runtime 错误类型。
5
+ *
6
+ * @remarks
7
+ * 该错误用于表示握手失败、bridge 调用失败、父容器运行时错误等
8
+ * 非 HTTP 结果错误。其 `code` 字段可用于业务分支处理,
9
+ * `data` 仅作为可选调试信息,不保证结构稳定。
10
+ */
3
11
  export declare class HbMiniProgramSDKError extends Error {
4
12
  /** 稳定错误码。 */
5
13
  code: string;
@@ -7,5 +15,40 @@ export declare class HbMiniProgramSDKError extends Error {
7
15
  data?: unknown;
8
16
  constructor(error: MiniProgramBridgeError);
9
17
  }
10
- /** 创建 SDK 标准错误。 */
18
+ /**
19
+ * 创建 SDK 标准错误。
20
+ *
21
+ * @param code 稳定错误码。
22
+ * @param message 面向开发者的错误说明。
23
+ * @param data 可选调试数据。
24
+ * @returns 标准化 `HbMiniProgramSDKError` 实例。
25
+ *
26
+ * @remarks
27
+ * 仅供 SDK 内部或同仓运行时层统一构造 bridge / runtime 错误。
28
+ */
11
29
  export declare function createSDKError(code: string, message: string, data?: unknown): HbMiniProgramSDKError;
30
+ /**
31
+ * SDK 对外抛出的 HTTP 错误类型。
32
+ *
33
+ * @remarks
34
+ * 仅在请求已经成功走完 bridge / runtime / 宿主调用链、但最终 HTTP 状态不满足
35
+ * `validateStatus` 时抛出。它不表示握手失败或 bridge 级错误。
36
+ *
37
+ * @typeParam T 标准化响应数据的类型。
38
+ */
39
+ export declare class HbMiniProgramNetworkError<T = unknown> extends Error {
40
+ /** HTTP 状态码。 */
41
+ status: number;
42
+ /** 标准化响应数据。 */
43
+ data: T;
44
+ /** 标准化响应头。 */
45
+ headers: Record<string, string>;
46
+ /** SDK 公共请求配置快照。 */
47
+ config: MiniProgramNetworkRequestConfig;
48
+ /** 完整标准化响应。 */
49
+ response: MiniProgramNetworkResponse<T>;
50
+ /**
51
+ * @param response 已完成请求的标准化网络响应。
52
+ */
53
+ constructor(response: MiniProgramNetworkResponse<T>);
54
+ }
@@ -1,26 +1,94 @@
1
1
  import { type MiniProgramSDKOptions } from './client';
2
+ import { type MiniProgramAuthModule } from '../modules/auth';
2
3
  import { type MiniProgramShareModule } from '../modules/share';
4
+ import { type MiniProgramStorageModule } from '../modules/storage';
5
+ import { type MiniProgramNetworkModule } from '../modules/network';
6
+ import { type MiniProgramViewportModule } from '../modules/viewport';
3
7
  import { type MiniProgramUserModule } from '../modules/user';
4
- import { type MiniProgramSystemModule } from '../modules/system';
5
8
  import type { MiniProgramEventHandler, MiniProgramEventName } from '../protocol/types';
6
- /** 外部小程序 SDK 实例。 */
9
+ /**
10
+ * 外部小程序 SDK 实例。
11
+ *
12
+ * @remarks
13
+ * 该实例负责组织授权、用户、分享、容器、存储与网络等开放能力,并维护
14
+ * iframe 内小程序与父容器之间的 bridge 生命周期。
15
+ *
16
+ * 多数业务页直接使用默认单例即可;只有在测试、多实例或需要定制运行参数时,
17
+ * 才建议显式创建独立实例。
18
+ *
19
+ * @example
20
+ * ```ts
21
+ * import { createMiniProgramSDK } from '@heybox/hb-sdk'
22
+ *
23
+ * const sdk = createMiniProgramSDK({
24
+ * timeout: 15000,
25
+ * })
26
+ *
27
+ * await sdk.ready()
28
+ * ```
29
+ */
7
30
  export declare class MiniProgramSDK {
8
31
  private readonly client;
9
- /** 用户相关开放能力。 */
32
+ /** 授权相关开放能力。 */
33
+ readonly auth: MiniProgramAuthModule;
34
+ /** 用户资料相关开放能力。 */
10
35
  readonly user: MiniProgramUserModule;
11
36
  /** 分享相关开放能力。 */
12
37
  readonly share: MiniProgramShareModule;
13
- /** 系统相关开放能力。 */
14
- readonly system: MiniProgramSystemModule;
38
+ /** 视口相关开放能力。 */
39
+ readonly viewport: MiniProgramViewportModule;
40
+ /** Storage 相关开放能力。 */
41
+ readonly storage: MiniProgramStorageModule;
42
+ /** 网络请求相关开放能力。 */
43
+ readonly network: MiniProgramNetworkModule;
15
44
  constructor(options?: MiniProgramSDKOptions);
16
- /** 等待 SDK 与父容器完成握手。 */
45
+ /**
46
+ * 等待 SDK 与父容器完成握手。
47
+ *
48
+ * @returns 当 bridge 握手成功后 resolve。
49
+ * @throws {HbMiniProgramSDKError} 当当前页面不在 iframe 中、缺少 nonce 或握手超时时抛出。
50
+ */
17
51
  ready(): Promise<void>;
18
- /** 注册小程序生命周期或业务事件。 */
52
+ /**
53
+ * 注册小程序生命周期或业务事件。
54
+ *
55
+ * @param eventName 要监听的事件名。
56
+ * @param handler 事件处理函数。
57
+ * @returns 事件解绑函数。
58
+ */
19
59
  on<T extends MiniProgramEventName>(eventName: T, handler: MiniProgramEventHandler<T>): () => void;
20
- /** 移除小程序生命周期或业务事件。 */
60
+ /**
61
+ * 移除小程序生命周期或业务事件。
62
+ *
63
+ * @param eventName 要移除的事件名。
64
+ * @param handler 对应的事件处理函数。
65
+ */
21
66
  off<T extends MiniProgramEventName>(eventName: T, handler: MiniProgramEventHandler<T>): void;
22
- /** 销毁 SDK 实例。 */
67
+ /**
68
+ * 销毁 SDK 实例。
69
+ *
70
+ * @remarks
71
+ * 销毁后会移除 message 监听,并拒绝尚未完成的 bridge 请求。
72
+ * 已销毁实例不应继续复用。
73
+ */
23
74
  destroy(): void;
24
75
  }
25
- /** 创建独立 SDK 实例,主要用于测试或多实例场景。 */
76
+ /**
77
+ * 创建独立 SDK 实例。
78
+ *
79
+ * @param options SDK 运行配置,例如超时时间、nonce 或测试环境注入的 window。
80
+ * @returns 可独立管理生命周期的 `MiniProgramSDK` 实例。
81
+ *
82
+ * @remarks
83
+ * 适用于测试、多实例场景,或需要自定义 `timeout`、`targetWindow` 等运行参数的场景。
84
+ *
85
+ * @example
86
+ * ```ts
87
+ * import { createMiniProgramSDK } from '@heybox/hb-sdk'
88
+ *
89
+ * const sdk = createMiniProgramSDK({
90
+ * timeout: 15000,
91
+ * })
92
+ * ```
93
+ */
26
94
  export declare function createMiniProgramSDK(options?: MiniProgramSDKOptions): MiniProgramSDK;
@@ -1,18 +1,44 @@
1
+ import type { MiniProgramAuthModule } from '../modules/auth';
1
2
  import type { MiniProgramShareModule } from '../modules/share';
3
+ import type { MiniProgramStorageModule } from '../modules/storage';
4
+ import type { MiniProgramNetworkModule } from '../modules/network';
5
+ import type { MiniProgramViewportModule } from '../modules/viewport';
2
6
  import type { MiniProgramUserModule } from '../modules/user';
3
- import type { MiniProgramSystemModule } from '../modules/system';
4
7
  import type { MiniProgramEventHandler, MiniProgramEventName } from '../protocol/types';
5
- /** 等待默认 SDK 实例与父容器完成握手。 */
8
+ /**
9
+ * 等待默认 SDK 实例与父容器完成握手。
10
+ *
11
+ * @returns 当默认 SDK 单例与父容器握手成功后 resolve。
12
+ * @throws {HbMiniProgramSDKError} 当当前页面不在 iframe 中、缺少 nonce 或握手超时时抛出。
13
+ */
6
14
  export declare function ready(): Promise<void>;
7
- /** 注册默认 SDK 实例的事件监听。 */
15
+ /**
16
+ * 注册默认 SDK 实例的事件监听。
17
+ *
18
+ * @param eventName 要监听的事件名。
19
+ * @param handler 事件处理函数。
20
+ * @returns 事件解绑函数。
21
+ */
8
22
  export declare function on<T extends MiniProgramEventName>(eventName: T, handler: MiniProgramEventHandler<T>): () => void;
9
- /** 移除默认 SDK 实例的事件监听。 */
23
+ /**
24
+ * 移除默认 SDK 实例的事件监听。
25
+ *
26
+ * @param eventName 要移除的事件名。
27
+ * @param handler 对应的事件处理函数。
28
+ * @returns 无返回值。
29
+ */
10
30
  export declare function off<T extends MiniProgramEventName>(eventName: T, handler: MiniProgramEventHandler<T>): void;
11
- /** 默认 SDK 实例的用户模块。 */
31
+ /** 默认 SDK 实例的授权模块。 */
32
+ export declare const auth: MiniProgramAuthModule;
33
+ /** 默认 SDK 实例的用户资料模块。 */
12
34
  export declare const user: MiniProgramUserModule;
13
35
  /** 默认 SDK 实例的分享模块。 */
14
36
  export declare const share: MiniProgramShareModule;
15
- /** 默认 SDK 实例的系统模块。 */
16
- export declare const system: MiniProgramSystemModule;
37
+ /** 默认 SDK 实例的 Viewport 模块。 */
38
+ export declare const viewport: MiniProgramViewportModule;
39
+ /** 默认 SDK 实例的 storage 模块。 */
40
+ export declare const storage: MiniProgramStorageModule;
41
+ /** 默认 SDK 实例的 network 模块。 */
42
+ export declare const network: MiniProgramNetworkModule;
17
43
  /** 重置默认 SDK 实例,仅用于测试。 */
18
44
  export declare function resetDefaultSDKForTest(): void;
@@ -2,6 +2,8 @@
2
2
  export declare function getGlobalWindow(): Window | undefined;
3
3
  /** 获取父窗口;非 iframe 环境返回 null。 */
4
4
  export declare function getParentWindow(currentWindow?: Window): Window | null;
5
+ /** 尝试读取父窗口 origin;跨域读取失败时回退到 document.referrer。 */
6
+ export declare function readParentOrigin(currentWindow?: Window): string;
5
7
  /** 从父容器注入到 URL 的 query 中读取 bridge nonce。 */
6
8
  export declare function readBridgeNonce(currentWindow?: Window): string;
7
9
  /** 创建请求 ID。 */
package/types/index.d.ts CHANGED
@@ -1,20 +1,26 @@
1
1
  export { createMiniProgramSDK, MiniProgramSDK } from './core/sdk';
2
- export { HbMiniProgramSDKError } from './core/errors';
2
+ export { HbMiniProgramSDKError, HbMiniProgramNetworkError } from './core/errors';
3
3
  export type { MiniProgramRequester, MiniProgramSDKOptions } from './core/client';
4
- export { ready, on, off, user, share, system } from './core/singleton';
4
+ export { ready, on, off, auth, user, share, viewport, storage, network } from './core/singleton';
5
5
  export { MINI_PROGRAM_BRIDGE_NONCE_PARAM, MINI_PROGRAM_MESSAGE_NAMESPACE, MINI_PROGRAM_MESSAGE_VERSION, SDK_HANDSHAKE_METHOD, } from './protocol/constants';
6
6
  export { isMiniProgramBridgeMessage } from './protocol/guards';
7
7
  export type { MiniProgramBridgeError, MiniProgramBridgeMessage, MiniProgramBridgeMessageType, MiniProgramEventHandler, MiniProgramEventName, MiniProgramEventPayloadMap, } from './protocol/types';
8
+ export * from './modules/auth';
8
9
  export * from './modules/user';
9
10
  export * from './modules/share';
10
- export * from './modules/system';
11
+ export * from './modules/viewport';
12
+ export * from './modules/storage';
13
+ export * from './modules/network';
11
14
  import { off, on, ready } from './core/singleton';
12
15
  declare const hbSDK: {
13
16
  ready: typeof ready;
14
17
  on: typeof on;
15
18
  off: typeof off;
19
+ auth: import(".").MiniProgramAuthModule;
16
20
  user: import(".").MiniProgramUserModule;
17
21
  share: import(".").MiniProgramShareModule;
18
- system: import(".").MiniProgramSystemModule;
22
+ viewport: import(".").MiniProgramViewportModule;
23
+ storage: import(".").MiniProgramStorageModule;
24
+ network: import(".").MiniProgramNetworkModule;
19
25
  };
20
26
  export default hbSDK;
@@ -0,0 +1,42 @@
1
+ import type { MiniProgramRequester } from '../../core/client';
2
+ import type { MiniProgramUserInfoResult } from '../user';
3
+ /**
4
+ * 登录授权能力方法名。
5
+ *
6
+ * @remarks
7
+ * 供 SDK 与父容器 runtime 共享同一 bridge method 标识。
8
+ */
9
+ export declare const AUTH_LOGIN_METHOD: "auth.login";
10
+ /** `auth.login` 不需要入参。 */
11
+ export type LoginPayload = void;
12
+ /** `auth.login` 返回登录流程结束后的最新用户登录态与公开基础资料。 */
13
+ export type LoginResult = MiniProgramUserInfoResult;
14
+ /** 授权模块开放的方法名。 */
15
+ export type MiniProgramAuthMethod = typeof AUTH_LOGIN_METHOD;
16
+ /** 外部小程序可调用的授权模块。 */
17
+ export interface MiniProgramAuthModule {
18
+ /**
19
+ * 唤起登录授权,并返回登录后的最新用户信息。
20
+ *
21
+ * 该能力不会向小程序暴露 token、cookie 或任何登录凭据。
22
+ */
23
+ login(): Promise<LoginResult>;
24
+ }
25
+ /**
26
+ * 唤起登录授权,并在流程返回后刷新用户公开基础资料。
27
+ *
28
+ * @param requester 底层 bridge 请求能力。
29
+ * @returns 登录流程完成后的最新用户登录态与公开资料。
30
+ * @throws {HbMiniProgramSDKError} 当 bridge、父容器或宿主运行时调用失败时抛出。
31
+ *
32
+ * @remarks
33
+ * 该能力不会向小程序暴露 token、cookie 或其他登录凭据。
34
+ */
35
+ export declare function login(requester: MiniProgramRequester): Promise<LoginResult>;
36
+ /**
37
+ * 创建授权模块。
38
+ *
39
+ * @param requester 底层 bridge 请求能力。
40
+ * @returns 面向业务层的授权模块对象。
41
+ */
42
+ export declare function createAuthModule(requester: MiniProgramRequester): MiniProgramAuthModule;
@@ -0,0 +1,125 @@
1
+ import type { MiniProgramRequester } from '../../core/client';
2
+ /**
3
+ * 发起网络请求能力方法名。
4
+ *
5
+ * @remarks
6
+ * 供 SDK 与父容器 runtime 共享同一 bridge method 标识。
7
+ */
8
+ export declare const NETWORK_REQUEST_METHOD: "network.request";
9
+ /** `network.request` 请求头。 */
10
+ export type MiniProgramNetworkHeaders = Record<string, string>;
11
+ /** `network.request` 查询参数。 */
12
+ export type MiniProgramNetworkParams = Record<string, unknown>;
13
+ /**
14
+ * SDK 侧可配置的状态校验函数。
15
+ *
16
+ * @param status 标准化后的 HTTP 状态码。
17
+ * @returns 返回 `true` 时表示本次响应应视为成功。
18
+ *
19
+ * @remarks
20
+ * 该函数只在 SDK 侧执行,不会跨 bridge 传输到父容器 runtime。
21
+ */
22
+ export type MiniProgramNetworkValidateStatus = (status: number) => boolean;
23
+ /**
24
+ * 对外开放的网络请求配置。
25
+ *
26
+ * @remarks
27
+ * 该配置面向业务层,保持 axios-like 的窄接口,不暴露 `sendRequestV2`
28
+ * 等宿主协议的原始字段。
29
+ */
30
+ export interface MiniProgramNetworkRequestConfig {
31
+ /** 请求地址。 */
32
+ url: string;
33
+ /** HTTP 方法,默认 `GET`。 */
34
+ method?: string;
35
+ /** 查询参数,会被编码到请求 URL。 */
36
+ params?: MiniProgramNetworkParams;
37
+ /** 请求体。 */
38
+ data?: unknown;
39
+ /** 请求头。 */
40
+ headers?: MiniProgramNetworkHeaders;
41
+ /** 超时时间,单位毫秒。 */
42
+ timeout?: number;
43
+ /** 是否携带凭据意图;具体策略由宿主运行时决定。 */
44
+ withCredentials?: boolean;
45
+ /** SDK 侧状态校验;不会跨 bridge 传输函数值。 */
46
+ validateStatus?: MiniProgramNetworkValidateStatus;
47
+ }
48
+ /**
49
+ * bridge / runtime 可消费的网络请求 payload。
50
+ *
51
+ * @remarks
52
+ * 这是从公开配置中裁剪得到的可序列化请求载荷,不包含函数值或内部实现细节。
53
+ */
54
+ export interface NetworkRequestPayload {
55
+ url: string;
56
+ method?: string;
57
+ params?: MiniProgramNetworkParams;
58
+ data?: unknown;
59
+ headers?: MiniProgramNetworkHeaders;
60
+ timeout?: number;
61
+ withCredentials?: boolean;
62
+ }
63
+ /**
64
+ * bridge / runtime 返回的标准化 HTTP 响应。
65
+ *
66
+ * @typeParam T 标准化响应数据的类型。
67
+ */
68
+ export interface NetworkResponsePayload<T = unknown> {
69
+ data: T;
70
+ status: number;
71
+ statusText?: string;
72
+ headers: MiniProgramNetworkHeaders;
73
+ }
74
+ /**
75
+ * SDK 对外暴露的网络响应。
76
+ *
77
+ * @typeParam T 标准化响应数据的类型。
78
+ */
79
+ export interface MiniProgramNetworkResponse<T = unknown> extends NetworkResponsePayload<T> {
80
+ /** 发起请求时的 SDK 公共配置快照。 */
81
+ config: MiniProgramNetworkRequestConfig;
82
+ }
83
+ /** 网络模块开放的方法名。 */
84
+ export type MiniProgramNetworkMethod = typeof NETWORK_REQUEST_METHOD;
85
+ /** 外部小程序可调用的网络模块。 */
86
+ export interface MiniProgramNetworkModule {
87
+ /**
88
+ * 发起网络请求。
89
+ *
90
+ * @param config 面向业务层的网络请求配置。
91
+ * @returns 标准化网络响应;HTTP 2xx 默认返回 resolve。
92
+ * @throws {HbMiniProgramNetworkError} 当请求已完成但 HTTP 状态不满足 `validateStatus` 时抛出。
93
+ * @throws {HbMiniProgramSDKError} 当 bridge、运行时或宿主能力调用失败时抛出。
94
+ */
95
+ request<T = unknown>(config: MiniProgramNetworkRequestConfig): Promise<MiniProgramNetworkResponse<T>>;
96
+ }
97
+ /**
98
+ * 发起网络请求并返回标准化响应。
99
+ *
100
+ * @param requester 底层 bridge 请求能力。
101
+ * @param config 面向业务层的网络请求配置。
102
+ * @returns 标准化网络响应。HTTP 2xx 默认返回 resolve。
103
+ * @throws {HbMiniProgramNetworkError} 当请求已完成但 HTTP 状态不满足 `validateStatus` 时抛出。
104
+ * @throws {HbMiniProgramSDKError} 当 bridge、运行时或宿主能力调用失败时抛出。
105
+ *
106
+ * @remarks
107
+ * `validateStatus` 仅在 SDK 侧执行,不会跨 bridge 传输函数值。
108
+ * 公开配置与返回结构都保持 axios-like 的窄接口,不暴露宿主原始协议字段。
109
+ *
110
+ * @example
111
+ * ```ts
112
+ * const result = await request(requester, {
113
+ * url: 'https://api.xiaoheihe.cn/demo',
114
+ * method: 'GET',
115
+ * })
116
+ * ```
117
+ */
118
+ export declare function request<T = unknown>(requester: MiniProgramRequester, config: MiniProgramNetworkRequestConfig): Promise<MiniProgramNetworkResponse<T>>;
119
+ /**
120
+ * 创建 network 模块。
121
+ *
122
+ * @param requester 底层 bridge 请求能力。
123
+ * @returns 面向业务层的网络模块对象。
124
+ */
125
+ export declare function createNetworkModule(requester: MiniProgramRequester): MiniProgramNetworkModule;
@@ -5,7 +5,12 @@ import type { MiniProgramScreenshotOptions } from './types';
5
5
  export * from './screenshot';
6
6
  export * from './show-share-menu';
7
7
  export * from './types';
8
- /** 分享模块开放的方法名。 */
8
+ /**
9
+ * 分享模块开放的方法名。
10
+ *
11
+ * @remarks
12
+ * 该联合类型用于约束分享模块向父容器发起的 bridge method 集合。
13
+ */
9
14
  export type MiniProgramShareMethod = typeof SHARE_SHOW_SHARE_MENU_METHOD | typeof SHARE_SCREENSHOT_METHOD;
10
15
  /** 外部小程序可调用的分享模块。 */
11
16
  export interface MiniProgramShareModule {
@@ -14,5 +19,10 @@ export interface MiniProgramShareModule {
14
19
  /** 截图并唤起分享。 */
15
20
  screenshot(options?: MiniProgramScreenshotOptions): Promise<ScreenshotResult>;
16
21
  }
17
- /** 创建分享模块。 */
22
+ /**
23
+ * 创建分享模块。
24
+ *
25
+ * @param requester 底层 bridge 请求能力。
26
+ * @returns 面向业务层的分享模块对象。
27
+ */
18
28
  export declare function createShareModule(requester: MiniProgramRequester): MiniProgramShareModule;
@@ -1,10 +1,22 @@
1
1
  import type { MiniProgramRequester } from '../../core/client';
2
2
  import type { MiniProgramScreenshotOptions } from './types';
3
- /** 截图分享能力方法名。 */
3
+ /**
4
+ * 截图分享能力方法名。
5
+ *
6
+ * @remarks
7
+ * 供 SDK 与父容器 runtime 共享同一 bridge method 标识。
8
+ */
4
9
  export declare const SHARE_SCREENSHOT_METHOD: "share.screenshot";
5
10
  /** `share.screenshot` 入参。 */
6
11
  export type ScreenshotPayload = MiniProgramScreenshotOptions | undefined;
7
12
  /** `share.screenshot` 返回值由客户端协议决定。 */
8
13
  export type ScreenshotResult = unknown;
9
- /** 截图并唤起分享。 */
14
+ /**
15
+ * 截图并唤起分享。
16
+ *
17
+ * @param requester 底层 bridge 请求能力。
18
+ * @param options 截图区域、延迟与保存相册等配置。
19
+ * @returns 由宿主客户端协议决定的结果。
20
+ * @throws {HbMiniProgramSDKError} 当 bridge、父容器或宿主能力调用失败时抛出。
21
+ */
10
22
  export declare function screenshot(requester: MiniProgramRequester, options?: MiniProgramScreenshotOptions): Promise<ScreenshotResult>;
@@ -1,10 +1,22 @@
1
1
  import type { MiniProgramRequester } from '../../core/client';
2
2
  import type { MiniProgramShowShareMenuOptions } from './types';
3
- /** 展示分享面板能力方法名。 */
3
+ /**
4
+ * 展示分享面板能力方法名。
5
+ *
6
+ * @remarks
7
+ * 供 SDK 与父容器 runtime 共享同一 bridge method 标识。
8
+ */
4
9
  export declare const SHARE_SHOW_SHARE_MENU_METHOD: "share.showShareMenu";
5
10
  /** `share.showShareMenu` 入参。 */
6
11
  export type ShowShareMenuPayload = MiniProgramShowShareMenuOptions;
7
12
  /** `share.showShareMenu` 返回值由客户端协议决定。 */
8
13
  export type ShowShareMenuResult = unknown;
9
- /** 展示基础分享面板。 */
14
+ /**
15
+ * 展示基础分享面板。
16
+ *
17
+ * @param requester 底层 bridge 请求能力。
18
+ * @param options 基础分享参数。
19
+ * @returns 由宿主客户端协议决定的结果。
20
+ * @throws {HbMiniProgramSDKError} 当 bridge、父容器或宿主能力调用失败时抛出。
21
+ */
10
22
  export declare function showShareMenu(requester: MiniProgramRequester, options: ShowShareMenuPayload): Promise<ShowShareMenuResult>;
@@ -1,6 +1,16 @@
1
- /** 小程序可选分享渠道。 */
1
+ /**
2
+ * 小程序可选分享渠道。
3
+ *
4
+ * @remarks
5
+ * 不同宿主环境可支持的渠道集合可能不同;未传 `channel` 时由客户端自行展示默认分享面板。
6
+ */
2
7
  export type MiniProgramShareChannel = 'wechatSession' | 'wechatTimeline' | 'qqFriend' | 'qzone' | 'weibo';
3
- /** 展示基础分享面板的配置。 */
8
+ /**
9
+ * 展示基础分享面板的配置。
10
+ *
11
+ * @remarks
12
+ * 该配置只暴露基础分享字段,不暴露宿主内部上报、回调、自定义按钮等协议参数。
13
+ */
4
14
  export interface MiniProgramShowShareMenuOptions {
5
15
  /** 分享标题。 */
6
16
  title: string;
@@ -13,7 +23,12 @@ export interface MiniProgramShowShareMenuOptions {
13
23
  /** 指定分享渠道;不传则由客户端展示默认分享面板。 */
14
24
  channel?: MiniProgramShareChannel;
15
25
  }
16
- /** 截图区域。 */
26
+ /**
27
+ * 截图区域。
28
+ *
29
+ * @remarks
30
+ * 坐标与尺寸单位均为像素,基于当前小程序可见窗口坐标系。
31
+ */
17
32
  export interface MiniProgramScreenshotRect {
18
33
  /** 截图区域左边距。 */
19
34
  left: number;
@@ -24,7 +39,12 @@ export interface MiniProgramScreenshotRect {
24
39
  /** 截图区域高度。 */
25
40
  height: number;
26
41
  }
27
- /** 截图分享配置。 */
42
+ /**
43
+ * 截图分享配置。
44
+ *
45
+ * @remarks
46
+ * 该配置只暴露截图与保存相册相关基础字段,不暴露宿主内部上传、回调或样式扩展能力。
47
+ */
28
48
  export interface MiniProgramScreenshotOptions {
29
49
  /** 指定截图区域;不传则默认截当前可见窗口。 */
30
50
  rect?: MiniProgramScreenshotRect;