@heybox/hb-sdk 0.1.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.
@@ -0,0 +1,18 @@
1
+ import type { MiniProgramRequester } from '../../core/client';
2
+ import { SHARE_SCREENSHOT_METHOD, type ScreenshotResult } from './screenshot';
3
+ import { SHARE_SHOW_SHARE_MENU_METHOD, type ShowShareMenuPayload, type ShowShareMenuResult } from './show-share-menu';
4
+ import type { MiniProgramScreenshotOptions } from './types';
5
+ export * from './screenshot';
6
+ export * from './show-share-menu';
7
+ export * from './types';
8
+ /** 分享模块开放的方法名。 */
9
+ export type MiniProgramShareMethod = typeof SHARE_SHOW_SHARE_MENU_METHOD | typeof SHARE_SCREENSHOT_METHOD;
10
+ /** 外部小程序可调用的分享模块。 */
11
+ export interface MiniProgramShareModule {
12
+ /** 展示基础分享面板。 */
13
+ showShareMenu(options: ShowShareMenuPayload): Promise<ShowShareMenuResult>;
14
+ /** 截图并唤起分享。 */
15
+ screenshot(options?: MiniProgramScreenshotOptions): Promise<ScreenshotResult>;
16
+ }
17
+ /** 创建分享模块。 */
18
+ export declare function createShareModule(requester: MiniProgramRequester): MiniProgramShareModule;
@@ -0,0 +1,10 @@
1
+ import type { MiniProgramRequester } from '../../core/client';
2
+ import type { MiniProgramScreenshotOptions } from './types';
3
+ /** 截图分享能力方法名。 */
4
+ export declare const SHARE_SCREENSHOT_METHOD: "share.screenshot";
5
+ /** `share.screenshot` 入参。 */
6
+ export type ScreenshotPayload = MiniProgramScreenshotOptions | undefined;
7
+ /** `share.screenshot` 返回值由客户端协议决定。 */
8
+ export type ScreenshotResult = unknown;
9
+ /** 截图并唤起分享。 */
10
+ export declare function screenshot(requester: MiniProgramRequester, options?: MiniProgramScreenshotOptions): Promise<ScreenshotResult>;
@@ -0,0 +1,10 @@
1
+ import type { MiniProgramRequester } from '../../core/client';
2
+ import type { MiniProgramShowShareMenuOptions } from './types';
3
+ /** 展示分享面板能力方法名。 */
4
+ export declare const SHARE_SHOW_SHARE_MENU_METHOD: "share.showShareMenu";
5
+ /** `share.showShareMenu` 入参。 */
6
+ export type ShowShareMenuPayload = MiniProgramShowShareMenuOptions;
7
+ /** `share.showShareMenu` 返回值由客户端协议决定。 */
8
+ export type ShowShareMenuResult = unknown;
9
+ /** 展示基础分享面板。 */
10
+ export declare function showShareMenu(requester: MiniProgramRequester, options: ShowShareMenuPayload): Promise<ShowShareMenuResult>;
@@ -0,0 +1,35 @@
1
+ /** 小程序可选分享渠道。 */
2
+ export type MiniProgramShareChannel = 'wechatSession' | 'wechatTimeline' | 'qqFriend' | 'qzone' | 'weibo';
3
+ /** 展示基础分享面板的配置。 */
4
+ export interface MiniProgramShowShareMenuOptions {
5
+ /** 分享标题。 */
6
+ title: string;
7
+ /** 分享描述。 */
8
+ desc: string;
9
+ /** 分享落地页 URL。 */
10
+ url: string;
11
+ /** 分享缩略图 URL。 */
12
+ imageUrl?: string;
13
+ /** 指定分享渠道;不传则由客户端展示默认分享面板。 */
14
+ channel?: MiniProgramShareChannel;
15
+ }
16
+ /** 截图区域。 */
17
+ export interface MiniProgramScreenshotRect {
18
+ /** 截图区域左边距。 */
19
+ left: number;
20
+ /** 截图区域上边距。 */
21
+ top: number;
22
+ /** 截图区域宽度。 */
23
+ width: number;
24
+ /** 截图区域高度。 */
25
+ height: number;
26
+ }
27
+ /** 截图分享配置。 */
28
+ export interface MiniProgramScreenshotOptions {
29
+ /** 指定截图区域;不传则默认截当前可见窗口。 */
30
+ rect?: MiniProgramScreenshotRect;
31
+ /** 截图延迟,单位毫秒。 */
32
+ delay?: number;
33
+ /** 是否展示保存到相册能力。 */
34
+ saveToAlbum?: boolean;
35
+ }
@@ -0,0 +1,15 @@
1
+ import type { MiniProgramRequester } from '../../core/client';
2
+ /** 获取小程序隔离 storage 能力方法名。 */
3
+ export declare const SYSTEM_GET_STORAGE_METHOD: "system.getStorage";
4
+ /** `system.getStorage` 入参。 */
5
+ export interface GetStoragePayload {
6
+ /** storage key。 */
7
+ key: string;
8
+ }
9
+ /** `system.getStorage` 返回值。 */
10
+ export interface GetStorageResult<T = unknown> {
11
+ /** key 对应的数据;不存在时为 undefined。 */
12
+ data: T | undefined;
13
+ }
14
+ /** 获取小程序隔离 storage。 */
15
+ export declare function getStorage<T = unknown>(requester: MiniProgramRequester, options: GetStoragePayload): Promise<GetStorageResult<T>>;
@@ -0,0 +1,16 @@
1
+ import type { MiniProgramRequester } from '../../core/client';
2
+ import type { MiniProgramWindowInfoResult } from './types';
3
+ /** 系统窗口信息能力方法名。 */
4
+ export declare const SYSTEM_GET_WINDOW_INFO_METHOD: "system.getWindowInfo";
5
+ /**
6
+ * `system.getWindowInfo` 不需要入参。
7
+ */
8
+ export type GetWindowInfoPayload = void;
9
+ /**
10
+ * `system.getWindowInfo` 返回当前小程序可用窗口信息。
11
+ */
12
+ export type GetWindowInfoResult = MiniProgramWindowInfoResult;
13
+ /**
14
+ * 获取当前小程序窗口信息。
15
+ */
16
+ export declare function getWindowInfo(requester: MiniProgramRequester): Promise<GetWindowInfoResult>;
@@ -0,0 +1,23 @@
1
+ import type { MiniProgramRequester } from '../../core/client';
2
+ import { SYSTEM_GET_WINDOW_INFO_METHOD, type GetWindowInfoResult } from './get-window-info';
3
+ import { SYSTEM_GET_STORAGE_METHOD, type GetStoragePayload, type GetStorageResult } from './get-storage';
4
+ import { SYSTEM_SET_STORAGE_METHOD, type SetStoragePayload } from './set-storage';
5
+ export * from './get-window-info';
6
+ export * from './get-storage';
7
+ export * from './set-storage';
8
+ export * from './types';
9
+ /** 系统模块开放的方法名。 */
10
+ export type MiniProgramSystemMethod = typeof SYSTEM_GET_WINDOW_INFO_METHOD | typeof SYSTEM_GET_STORAGE_METHOD | typeof SYSTEM_SET_STORAGE_METHOD;
11
+ /** 外部小程序可调用的系统模块。 */
12
+ export interface MiniProgramSystemModule {
13
+ /**
14
+ * 获取当前小程序窗口信息。
15
+ */
16
+ getWindowInfo(): Promise<GetWindowInfoResult>;
17
+ /** 获取小程序隔离 storage。 */
18
+ getStorage<T = unknown>(options: GetStoragePayload): Promise<GetStorageResult<T>>;
19
+ /** 写入小程序隔离 storage。 */
20
+ setStorage(options: SetStoragePayload): Promise<void>;
21
+ }
22
+ /** 创建系统模块。 */
23
+ export declare function createSystemModule(requester: MiniProgramRequester): MiniProgramSystemModule;
@@ -0,0 +1,12 @@
1
+ import type { MiniProgramRequester } from '../../core/client';
2
+ /** 写入小程序隔离 storage 能力方法名。 */
3
+ export declare const SYSTEM_SET_STORAGE_METHOD: "system.setStorage";
4
+ /** `system.setStorage` 入参。 */
5
+ export interface SetStoragePayload {
6
+ /** storage key。 */
7
+ key: string;
8
+ /** 需要写入的数据。 */
9
+ data: unknown;
10
+ }
11
+ /** 写入小程序隔离 storage。 */
12
+ export declare function setStorage(requester: MiniProgramRequester, options: SetStoragePayload): Promise<void>;
@@ -0,0 +1,34 @@
1
+ /** 小程序安全区域信息。 */
2
+ export interface MiniProgramSafeArea {
3
+ /** 安全区域左上角横坐标。 */
4
+ left: number;
5
+ /** 安全区域右下角横坐标。 */
6
+ right: number;
7
+ /** 安全区域左上角纵坐标。 */
8
+ top: number;
9
+ /** 安全区域右下角纵坐标。 */
10
+ bottom: number;
11
+ /** 安全区域宽度,单位 px。 */
12
+ width: number;
13
+ /** 安全区域高度,单位 px。 */
14
+ height: number;
15
+ }
16
+ /** 小程序窗口信息。 */
17
+ export interface MiniProgramWindowInfoResult {
18
+ /** 设备像素比。 */
19
+ pixelRatio: number;
20
+ /** 屏幕宽度,单位 px。 */
21
+ screenWidth: number;
22
+ /** 屏幕高度,单位 px。 */
23
+ screenHeight: number;
24
+ /** 可使用窗口宽度,单位 px。 */
25
+ windowWidth: number;
26
+ /** 可使用窗口高度,单位 px。 */
27
+ windowHeight: number;
28
+ /** 状态栏高度;黑盒环境按顶部可用偏移兼容。 */
29
+ statusBarHeight: number;
30
+ /** 安全区域。 */
31
+ safeArea: MiniProgramSafeArea;
32
+ /** 窗口上边缘的 y 值。 */
33
+ screenTop: number;
34
+ }
@@ -0,0 +1,19 @@
1
+ import type { MiniProgramRequester } from '../../core/client';
2
+ import type { MiniProgramUserInfoResult } from './types';
3
+ /** 用户信息能力方法名。 */
4
+ export declare const USER_GET_INFO_METHOD: "user.getInfo";
5
+ /**
6
+ * `user.getInfo` 不需要入参。
7
+ */
8
+ export type GetUserInfoPayload = void;
9
+ /**
10
+ * `user.getInfo` 返回当前用户登录态与公开基础资料。
11
+ */
12
+ export type GetUserInfoResult = MiniProgramUserInfoResult;
13
+ /**
14
+ * 获取当前访问小程序用户的公开基础资料。
15
+ *
16
+ * 该能力只返回 `heybox_id`、`nickname`、`avatar` 三个公开字段。
17
+ * 未登录时不会触发登录流程,直接返回 `{ isLogin: false, userInfo: null }`。
18
+ */
19
+ export declare function getInfo(requester: MiniProgramRequester): Promise<GetUserInfoResult>;
@@ -0,0 +1,21 @@
1
+ import type { MiniProgramRequester } from '../../core/client';
2
+ import { USER_GET_INFO_METHOD, type GetUserInfoResult } from './get-info';
3
+ import { USER_LOGIN_METHOD, type LoginResult } from './login';
4
+ export * from './get-info';
5
+ export * from './login';
6
+ export * from './types';
7
+ /** 用户模块开放的方法名。 */
8
+ export type MiniProgramUserMethod = typeof USER_GET_INFO_METHOD | typeof USER_LOGIN_METHOD;
9
+ /** 外部小程序可调用的用户模块。 */
10
+ export interface MiniProgramUserModule {
11
+ /**
12
+ * 获取当前用户登录态与公开基础资料。
13
+ */
14
+ getInfo(): Promise<GetUserInfoResult>;
15
+ /**
16
+ * 唤起登录,并返回登录后的最新用户信息。
17
+ */
18
+ login(): Promise<LoginResult>;
19
+ }
20
+ /** 创建用户模块。 */
21
+ export declare function createUserModule(requester: MiniProgramRequester): MiniProgramUserModule;
@@ -0,0 +1,18 @@
1
+ import type { MiniProgramRequester } from '../../core/client';
2
+ import type { MiniProgramUserInfoResult } from './types';
3
+ /** 用户登录能力方法名。 */
4
+ export declare const USER_LOGIN_METHOD: "user.login";
5
+ /**
6
+ * `user.login` 不需要入参。
7
+ */
8
+ export type LoginPayload = void;
9
+ /**
10
+ * `user.login` 返回登录流程结束后的最新用户登录态与公开基础资料。
11
+ */
12
+ export type LoginResult = MiniProgramUserInfoResult;
13
+ /**
14
+ * 唤起黑盒登录流程,并在流程返回后刷新用户公开基础资料。
15
+ *
16
+ * 该能力不会向小程序暴露 token、cookie 或任何登录凭据。
17
+ */
18
+ export declare function login(requester: MiniProgramRequester): Promise<LoginResult>;
@@ -0,0 +1,26 @@
1
+ /**
2
+ * 小程序用户公开基础资料。
3
+ *
4
+ * 这里只包含允许暴露给外部小程序的公开字段,不包含 token、手机号、
5
+ * cookie 或任何可用于直接调用主站私有接口的凭据。
6
+ */
7
+ export interface MiniProgramUserInfo {
8
+ /** 黑盒用户 ID。 */
9
+ heybox_id: string;
10
+ /** 用户昵称。 */
11
+ nickname: string;
12
+ /** 用户头像 URL。 */
13
+ avatar: string;
14
+ }
15
+ /**
16
+ * 用户能力统一返回结构。
17
+ *
18
+ * 未登录时 `isLogin` 为 `false`,且 `userInfo` 固定为 `null`。
19
+ * 已登录时 `isLogin` 为 `true`,`userInfo` 为公开基础资料。
20
+ */
21
+ export interface MiniProgramUserInfoResult {
22
+ /** 当前访问小程序的用户是否已登录黑盒账号。 */
23
+ isLogin: boolean;
24
+ /** 当前用户公开基础资料;未登录时为 `null`。 */
25
+ userInfo: MiniProgramUserInfo | null;
26
+ }
@@ -0,0 +1,8 @@
1
+ /** 小程序沙盒通信命名空间,父容器与 iframe 内 SDK 必须保持一致。 */
2
+ export declare const MINI_PROGRAM_MESSAGE_NAMESPACE: "heybox:miniprogram";
3
+ /** 小程序沙盒通信协议版本。 */
4
+ export declare const MINI_PROGRAM_MESSAGE_VERSION: 1;
5
+ /** 父容器注入到小程序 URL 上的通信 nonce 参数名。 */
6
+ export declare const MINI_PROGRAM_BRIDGE_NONCE_PARAM: "hb_mini_bridge_nonce";
7
+ /** SDK 初始化时发给父容器的握手方法名。 */
8
+ export declare const SDK_HANDSHAKE_METHOD: "sdk.handshake";
@@ -0,0 +1,3 @@
1
+ import type { MiniProgramBridgeMessage } from './types';
2
+ /** 判断未知数据是否符合小程序 bridge 消息信封。 */
3
+ export declare function isMiniProgramBridgeMessage(value: unknown): value is MiniProgramBridgeMessage;
@@ -0,0 +1,87 @@
1
+ import type { MiniProgramUserInfoResult } from '../modules/user/types';
2
+ import type { MINI_PROGRAM_MESSAGE_NAMESPACE, MINI_PROGRAM_MESSAGE_VERSION } from './constants';
3
+ /** 小程序沙盒消息类型。 */
4
+ export type MiniProgramBridgeMessageType = 'handshake' | 'request' | 'response' | 'event';
5
+ /** 首版开放给外部小程序监听的生命周期与业务事件名。 */
6
+ export type MiniProgramEventName = 'launch' | 'ready' | 'show' | 'hide' | 'unload' | 'error' | 'authChange';
7
+ /**
8
+ * bridge 错误结构。
9
+ *
10
+ * 所有请求失败都会被规范化成该结构,SDK 侧再包装为 `HbMiniProgramSDKError`。
11
+ */
12
+ export interface MiniProgramBridgeError {
13
+ /** 稳定错误码,便于业务侧分支处理。 */
14
+ code: string;
15
+ /** 面向开发者的中文错误说明。 */
16
+ message: string;
17
+ /** 可选调试数据,不保证结构稳定。 */
18
+ data?: unknown;
19
+ }
20
+ /**
21
+ * SDK 与沙盒之间统一的 postMessage 信封。
22
+ *
23
+ * 由于 iframe sandbox 不启用 `allow-same-origin`,协议不依赖 origin 校验,
24
+ * 而是由父容器校验 `event.source === iframe.contentWindow` 与实例 nonce。
25
+ */
26
+ export interface MiniProgramBridgeMessage<TPayload = unknown> {
27
+ /** 固定命名空间,用于过滤非小程序消息。 */
28
+ namespace: typeof MINI_PROGRAM_MESSAGE_NAMESPACE;
29
+ /** 协议版本号。 */
30
+ version: typeof MINI_PROGRAM_MESSAGE_VERSION;
31
+ /** 当前 iframe 实例的通信随机串。 */
32
+ nonce: string;
33
+ /** 消息类型。 */
34
+ type: MiniProgramBridgeMessageType;
35
+ /** 请求/响应关联 ID。 */
36
+ id?: string;
37
+ /** 请求方法名或事件名。 */
38
+ method?: string;
39
+ /** 消息载荷。 */
40
+ payload?: TPayload;
41
+ /** 失败响应的错误信息。 */
42
+ error?: MiniProgramBridgeError;
43
+ }
44
+ /**
45
+ * 小程序事件载荷映射。
46
+ *
47
+ * `on/off` 会基于该映射推导 handler 参数类型。
48
+ */
49
+ export interface MiniProgramEventPayloadMap {
50
+ /** 小程序首次完成握手并启动。 */
51
+ launch: {
52
+ /** 事件时间戳。 */
53
+ timestamp: number;
54
+ /** 父容器最终加载到 iframe 的 URL。 */
55
+ url?: string;
56
+ };
57
+ /** SDK 已可安全调用开放能力。 */
58
+ ready: {
59
+ /** 事件时间戳。 */
60
+ timestamp: number;
61
+ };
62
+ /** 小程序页面展示。 */
63
+ show: {
64
+ /** 事件时间戳。 */
65
+ timestamp: number;
66
+ /** 触发展示的父容器来源。 */
67
+ source?: string;
68
+ };
69
+ /** 小程序页面隐藏。 */
70
+ hide: {
71
+ /** 事件时间戳。 */
72
+ timestamp: number;
73
+ /** 触发隐藏的父容器来源。 */
74
+ source?: string;
75
+ };
76
+ /** 小程序页面即将卸载。 */
77
+ unload: {
78
+ /** 事件时间戳。 */
79
+ timestamp: number;
80
+ };
81
+ /** 父容器或开放能力运行异常。 */
82
+ error: MiniProgramBridgeError;
83
+ /** 登录状态发生变化后的最新用户信息。 */
84
+ authChange: MiniProgramUserInfoResult;
85
+ }
86
+ /** 小程序事件监听函数。 */
87
+ export type MiniProgramEventHandler<T extends MiniProgramEventName = MiniProgramEventName> = (payload: MiniProgramEventPayloadMap[T]) => void;