@heybox/hb-sdk 0.1.3 → 0.2.0-alpha.1
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.md +149 -345
- package/bin/hb-sdk.cjs +3 -0
- package/dist/cli.cjs +10117 -0
- package/dist/devtools/mock-host/index.html +252 -0
- package/dist/devtools/mock-host/main.js +975 -0
- package/dist/index.cjs.js +474 -85
- package/dist/index.esm.js +465 -71
- package/dist/protocol.cjs.js +163 -0
- package/dist/protocol.esm.js +148 -0
- package/dist/templates/vue3-vite-ts/.gitignore.ejs +5 -0
- package/dist/templates/vue3-vite-ts/README.md.ejs +42 -0
- package/dist/templates/vue3-vite-ts/index.html.ejs +12 -0
- package/dist/templates/vue3-vite-ts/package.json.ejs +28 -0
- package/dist/templates/vue3-vite-ts/src/App.vue +63 -0
- package/dist/templates/vue3-vite-ts/src/__tests__/App.spec.ts +67 -0
- package/dist/templates/vue3-vite-ts/src/main.ts +5 -0
- package/dist/templates/vue3-vite-ts/src/styles.css +60 -0
- package/dist/templates/vue3-vite-ts/src/vite-env.d.ts +1 -0
- package/dist/templates/vue3-vite-ts/tsconfig.app.json +17 -0
- package/dist/templates/vue3-vite-ts/tsconfig.json +11 -0
- package/dist/templates/vue3-vite-ts/tsconfig.node.json +11 -0
- package/dist/templates/vue3-vite-ts/vite.config.ts +6 -0
- package/dist/templates/vue3-vite-ts/vitest.config.ts +10 -0
- package/package.json +30 -5
- package/skill/SKILL.md +95 -0
- package/skill/references/api-protocol.md +135 -0
- package/skill/references/api-root.md +346 -0
- package/skill/references/cli.md +360 -0
- package/skill/references/examples.md +107 -0
- package/skill/references/llms-index.md +44 -0
- package/skill/references/recipes.md +374 -0
- package/skill/references/safety-boundaries.md +28 -0
- package/skill/references/smoke-evaluation.md +24 -0
- package/skill/scripts/check-references.mjs +14 -0
- package/skill/scripts/package-skill.mjs +60 -0
- package/skill/scripts/package-skill.sh +6 -0
- package/skill/scripts/skill-metadata.mjs +74 -0
- package/skill/scripts/sync-references.mjs +541 -0
- package/skill/scripts/validate-skill.mjs +233 -0
- package/skill/skill.json +11 -0
- package/types/core/client.d.ts +23 -3
- package/types/core/errors.d.ts +45 -2
- package/types/core/sdk.d.ts +78 -10
- package/types/core/singleton.d.ts +33 -7
- package/types/core/utils.d.ts +2 -0
- package/types/index.d.ts +14 -6
- package/types/modules/auth/index.d.ts +35 -0
- package/types/modules/network/index.d.ts +120 -0
- package/types/modules/share/index.d.ts +9 -5
- package/types/modules/share/screenshot.d.ts +9 -3
- package/types/modules/share/show-share-menu.d.ts +9 -3
- package/types/modules/share/types.d.ts +24 -4
- package/types/modules/storage/index.d.ts +56 -0
- package/types/modules/user/get-info.d.ts +6 -2
- package/types/modules/user/index.d.ts +8 -10
- package/types/modules/user/types.d.ts +1 -0
- package/types/modules/viewport/index.d.ts +71 -0
- package/types/protocol/capabilities.d.ts +180 -0
- package/types/protocol/guards.d.ts +6 -1
- package/types/protocol/types.d.ts +19 -4
- package/types/protocol.d.ts +13 -0
- package/types/modules/system/get-storage.d.ts +0 -15
- package/types/modules/system/get-window-info.d.ts +0 -16
- package/types/modules/system/index.d.ts +0 -23
- package/types/modules/system/set-storage.d.ts +0 -12
- package/types/modules/system/types.d.ts +0 -34
- package/types/modules/user/login.d.ts +0 -18
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { existsSync, readdirSync, readFileSync, statSync } from 'node:fs';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import { fileURLToPath } from 'node:url';
|
|
5
|
+
import process from 'node:process';
|
|
6
|
+
import {
|
|
7
|
+
HB_SDK_PACKAGE_NAME,
|
|
8
|
+
HB_SDK_SKILL_NAME,
|
|
9
|
+
validateSkillManifest,
|
|
10
|
+
} from './skill-metadata.mjs';
|
|
11
|
+
|
|
12
|
+
const scriptDir = path.dirname(fileURLToPath(import.meta.url));
|
|
13
|
+
const skillRoot = path.resolve(scriptDir, '..');
|
|
14
|
+
const packageRoot = path.resolve(skillRoot, '..');
|
|
15
|
+
const repoRoot = findRepoRoot(process.cwd()) ?? findRepoRoot(skillRoot) ?? process.cwd();
|
|
16
|
+
const errors = [];
|
|
17
|
+
const warnings = [];
|
|
18
|
+
|
|
19
|
+
function findRepoRoot(startDir) {
|
|
20
|
+
let dir = path.resolve(startDir);
|
|
21
|
+
while (true) {
|
|
22
|
+
if (existsSync(path.join(dir, 'package.json')) && existsSync(path.join(dir, 'packages/hb-sdk/package.json'))) {
|
|
23
|
+
return dir;
|
|
24
|
+
}
|
|
25
|
+
const parent = path.dirname(dir);
|
|
26
|
+
if (parent === dir) return null;
|
|
27
|
+
dir = parent;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function rel(filePath) {
|
|
32
|
+
return path.relative(repoRoot, filePath).split(path.sep).join('/');
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function skillRel(filePath) {
|
|
36
|
+
return path.relative(skillRoot, filePath).split(path.sep).join('/');
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function fail(message) {
|
|
40
|
+
errors.push(message);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function warn(message) {
|
|
44
|
+
warnings.push(message);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function read(filePath) {
|
|
48
|
+
return readFileSync(filePath, 'utf8');
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function walk(dir) {
|
|
52
|
+
const out = [];
|
|
53
|
+
for (const entry of readdirSync(dir)) {
|
|
54
|
+
const fullPath = path.join(dir, entry);
|
|
55
|
+
const stat = statSync(fullPath);
|
|
56
|
+
if (stat.isDirectory()) out.push(...walk(fullPath));
|
|
57
|
+
else out.push(fullPath);
|
|
58
|
+
}
|
|
59
|
+
return out;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function parseFrontmatter(markdown) {
|
|
63
|
+
const match = markdown.match(/^---\n([\s\S]*?)\n---\n/);
|
|
64
|
+
if (!match) return null;
|
|
65
|
+
const fields = new Map();
|
|
66
|
+
for (const line of match[1].split('\n')) {
|
|
67
|
+
const field = line.match(/^([A-Za-z0-9_-]+):\s*(.*)$/);
|
|
68
|
+
if (field) fields.set(field[1], field[2].replace(/^['"]|['"]$/g, ''));
|
|
69
|
+
}
|
|
70
|
+
return fields;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const requiredFiles = [
|
|
74
|
+
'SKILL.md',
|
|
75
|
+
'skill.json',
|
|
76
|
+
'references/api-root.md',
|
|
77
|
+
'references/api-protocol.md',
|
|
78
|
+
'references/cli.md',
|
|
79
|
+
'references/recipes.md',
|
|
80
|
+
'references/safety-boundaries.md',
|
|
81
|
+
'references/llms-index.md',
|
|
82
|
+
'references/examples.md',
|
|
83
|
+
'references/smoke-evaluation.md',
|
|
84
|
+
'scripts/sync-references.mjs',
|
|
85
|
+
'scripts/check-references.mjs',
|
|
86
|
+
'scripts/skill-metadata.mjs',
|
|
87
|
+
'scripts/validate-skill.mjs',
|
|
88
|
+
'scripts/package-skill.mjs',
|
|
89
|
+
'scripts/package-skill.sh',
|
|
90
|
+
];
|
|
91
|
+
|
|
92
|
+
for (const file of requiredFiles) {
|
|
93
|
+
const absolutePath = path.join(skillRoot, file);
|
|
94
|
+
if (!existsSync(absolutePath)) fail(`Missing required file: ${rel(absolutePath)}`);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const skillMdPath = path.join(skillRoot, 'SKILL.md');
|
|
98
|
+
const packageJsonPath = path.join(repoRoot, 'packages/hb-sdk/package.json');
|
|
99
|
+
const skillJsonPath = path.join(skillRoot, 'skill.json');
|
|
100
|
+
|
|
101
|
+
if (existsSync(skillJsonPath) && existsSync(packageJsonPath)) {
|
|
102
|
+
try {
|
|
103
|
+
const skillJson = JSON.parse(read(skillJsonPath));
|
|
104
|
+
const packageJson = JSON.parse(read(packageJsonPath));
|
|
105
|
+
|
|
106
|
+
for (const error of validateSkillManifest({ manifest: skillJson, sdkVersion: packageJson.version })) {
|
|
107
|
+
fail(error);
|
|
108
|
+
}
|
|
109
|
+
} catch (error) {
|
|
110
|
+
fail(`skill.json must be valid JSON: ${error instanceof Error ? error.message : String(error)}`);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
if (existsSync(skillMdPath)) {
|
|
115
|
+
const skillMd = read(skillMdPath);
|
|
116
|
+
const frontmatter = parseFrontmatter(skillMd);
|
|
117
|
+
if (!frontmatter) {
|
|
118
|
+
fail('SKILL.md must start with YAML frontmatter.');
|
|
119
|
+
} else {
|
|
120
|
+
const allowedFrontmatter = new Set(['name', 'description']);
|
|
121
|
+
for (const key of frontmatter.keys()) {
|
|
122
|
+
if (!allowedFrontmatter.has(key)) fail(`SKILL.md frontmatter must only include name and description; found ${key}.`);
|
|
123
|
+
}
|
|
124
|
+
const name = frontmatter.get('name');
|
|
125
|
+
const description = frontmatter.get('description');
|
|
126
|
+
if (name !== HB_SDK_SKILL_NAME) fail('SKILL.md frontmatter name must be `hb-sdk`.');
|
|
127
|
+
if (!description) fail('SKILL.md frontmatter description is required.');
|
|
128
|
+
if (description && description.length > 1024) fail('SKILL.md description must be <= 1024 characters.');
|
|
129
|
+
if (description && !description.includes("Don't use")) warn('Description should include negative triggers for discoverability.');
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
const lineCount = skillMd.split('\n').length;
|
|
133
|
+
if (lineCount > 500) fail(`SKILL.md must stay under 500 lines; found ${lineCount}.`);
|
|
134
|
+
if (!skillMd.includes('references/api-root.md')) fail('SKILL.md must point agents to references/api-root.md.');
|
|
135
|
+
if (!skillMd.includes('references/api-protocol.md')) fail('SKILL.md must point agents to references/api-protocol.md.');
|
|
136
|
+
if (!skillMd.includes('references/cli.md')) fail('SKILL.md must point agents to references/cli.md.');
|
|
137
|
+
if (!skillMd.includes('references/smoke-evaluation.md')) fail('SKILL.md must point evaluators to references/smoke-evaluation.md.');
|
|
138
|
+
if (!skillMd.includes('host/runtime') && !skillMd.includes('Host/runtime')) fail('SKILL.md must gate protocol usage to host/runtime tasks.');
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
const nestedReferenceFiles = existsSync(path.join(skillRoot, 'references'))
|
|
142
|
+
? walk(path.join(skillRoot, 'references')).filter(file => path.dirname(path.relative(path.join(skillRoot, 'references'), file)).includes(path.sep))
|
|
143
|
+
: [];
|
|
144
|
+
for (const file of nestedReferenceFiles) fail(`References must be one level deep: ${skillRel(file)}`);
|
|
145
|
+
|
|
146
|
+
if (existsSync(path.join(skillRoot, 'README.md'))) fail('Do not create README.md inside hb-sdk skill; keep human distribution docs outside the skill folder.');
|
|
147
|
+
if (existsSync(path.join(skillRoot, 'CHANGELOG.md'))) fail('Do not create CHANGELOG.md inside hb-sdk skill.');
|
|
148
|
+
if (existsSync(path.join(skillRoot, 'INSTALLATION_GUIDE.md'))) fail('Do not create INSTALLATION_GUIDE.md inside hb-sdk skill.');
|
|
149
|
+
|
|
150
|
+
const textFiles = existsSync(skillRoot)
|
|
151
|
+
? walk(skillRoot).filter(file => /\.(md|yaml|yml|json|txt)$/.test(file))
|
|
152
|
+
: [];
|
|
153
|
+
|
|
154
|
+
const negativeMarkers = [
|
|
155
|
+
'do not',
|
|
156
|
+
"don't",
|
|
157
|
+
'does not',
|
|
158
|
+
'must not',
|
|
159
|
+
'never',
|
|
160
|
+
'no ',
|
|
161
|
+
'without',
|
|
162
|
+
'forbidden',
|
|
163
|
+
'refuse',
|
|
164
|
+
'block',
|
|
165
|
+
'fail',
|
|
166
|
+
'negative',
|
|
167
|
+
'不会',
|
|
168
|
+
'不应',
|
|
169
|
+
'不要',
|
|
170
|
+
'不能',
|
|
171
|
+
'不开放',
|
|
172
|
+
'无',
|
|
173
|
+
'禁止',
|
|
174
|
+
];
|
|
175
|
+
const positiveCredentialPattern = /\b(read|extract|forward|store|persist|depend on|use|request|expose|get|parse|send)\b.*\b(token|cookie|credentials?|phone number|login state)\b/i;
|
|
176
|
+
const unsupportedStoragePattern = /\b(removeStorage|clearStorage|getStorageInfo|getStorageV2|delete storage|clear storage)\b/i;
|
|
177
|
+
const rawProtocolPattern = /\b(raw postMessage|construct bridge|bridge envelope|nonce handling|mutate bridge|sdk\.handshake)\b/i;
|
|
178
|
+
const removedUserLoginPattern = /\b(?:hbSDK\.)?user\.login\s*\(/;
|
|
179
|
+
|
|
180
|
+
for (const file of textFiles) {
|
|
181
|
+
const relative = skillRel(file);
|
|
182
|
+
const content = read(file);
|
|
183
|
+
if (removedUserLoginPattern.test(content)) {
|
|
184
|
+
fail(`Removed API guidance found in ${relative}: use auth.login() instead of user.login().`);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
if (new RegExp(`${HB_SDK_PACKAGE_NAME.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}\\/(?!protocol\\b)[A-Za-z0-9_./-]+`).test(content)) {
|
|
188
|
+
fail(`Deep/internal hb-sdk import guidance found in ${relative}. Only @heybox/hb-sdk and @heybox/hb-sdk/protocol are allowed.`);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
const lines = content.split('\n');
|
|
192
|
+
lines.forEach((line, index) => {
|
|
193
|
+
const lower = line.toLowerCase();
|
|
194
|
+
const isNegative = negativeMarkers.some(marker => lower.includes(marker));
|
|
195
|
+
if (positiveCredentialPattern.test(line) && !isNegative) {
|
|
196
|
+
fail(`Unsafe positive credential guidance in ${relative}:${index + 1}: ${line.trim()}`);
|
|
197
|
+
}
|
|
198
|
+
if (unsupportedStoragePattern.test(line) && !isNegative) {
|
|
199
|
+
fail(`Unsupported storage capability guidance in ${relative}:${index + 1}: ${line.trim()}`);
|
|
200
|
+
}
|
|
201
|
+
if (rawProtocolPattern.test(line) && !isNegative && !/host\/runtime|runtime|protocol|parent-container|parent container|父容器/i.test(line)) {
|
|
202
|
+
fail(`Raw protocol guidance without host/runtime context in ${relative}:${index + 1}: ${line.trim()}`);
|
|
203
|
+
}
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
const recipesPath = path.join(skillRoot, 'references/recipes.md');
|
|
208
|
+
if (existsSync(recipesPath)) {
|
|
209
|
+
const recipes = read(recipesPath);
|
|
210
|
+
if (/api-protocol\.md/.test(recipes) && !/host\/runtime-only|host\/runtime|runtime only/i.test(recipes)) {
|
|
211
|
+
fail('references/recipes.md references api-protocol.md without a nearby host/runtime-only warning.');
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
const protocolPath = path.join(skillRoot, 'references/api-protocol.md');
|
|
216
|
+
if (existsSync(protocolPath)) {
|
|
217
|
+
const protocol = read(protocolPath);
|
|
218
|
+
if (!/Host\/runtime-only warning/i.test(protocol)) fail('references/api-protocol.md must include a Host/runtime-only warning section.');
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
const zipPath = path.join(packageRoot, 'hb-sdk.zip');
|
|
222
|
+
if (existsSync(zipPath)) {
|
|
223
|
+
const size = statSync(zipPath).size;
|
|
224
|
+
if (size <= 0) fail('packages/hb-sdk/hb-sdk.zip exists but is empty.');
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
for (const warning of warnings) console.error(`warning: ${warning}`);
|
|
228
|
+
if (errors.length > 0) {
|
|
229
|
+
for (const error of errors) console.error(`error: ${error}`);
|
|
230
|
+
process.exit(1);
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
console.log(JSON.stringify({ ok: true, skillRoot: rel(skillRoot), checkedFiles: textFiles.length, warnings: warnings.length }));
|
package/skill/skill.json
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "hb-sdk",
|
|
3
|
+
"skillVersion": "0.2.0-alpha.1+skill.66c7559e5e13",
|
|
4
|
+
"sdk": {
|
|
5
|
+
"package": "@heybox/hb-sdk",
|
|
6
|
+
"version": "0.2.0-alpha.1",
|
|
7
|
+
"compatibility": "0.2.0-alpha.1"
|
|
8
|
+
},
|
|
9
|
+
"source": "https://open.xiaoheihe.cn/agent-skills/hb-sdk",
|
|
10
|
+
"integrity": "sha256-66c7559e5e13eaaefc9718b0f643dffd5853c864913e57e5a65ab0c663553797"
|
|
11
|
+
}
|
package/types/core/client.d.ts
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
import type { MiniProgramEventHandler, MiniProgramEventName } from '../protocol/types';
|
|
2
|
-
/**
|
|
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
|
-
/**
|
|
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;
|
package/types/core/errors.d.ts
CHANGED
|
@@ -1,5 +1,13 @@
|
|
|
1
1
|
import type { MiniProgramBridgeError } from '../protocol/types';
|
|
2
|
-
|
|
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
|
-
/**
|
|
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
|
+
}
|
package/types/core/sdk.d.ts
CHANGED
|
@@ -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
|
-
/**
|
|
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
|
|
38
|
+
/** 视口相关开放能力。 */
|
|
39
|
+
readonly viewport: MiniProgramViewportModule;
|
|
40
|
+
/** Storage 相关开放能力。 */
|
|
41
|
+
readonly storage: MiniProgramStorageModule;
|
|
42
|
+
/** 网络请求相关开放能力。 */
|
|
43
|
+
readonly network: MiniProgramNetworkModule;
|
|
15
44
|
constructor(options?: MiniProgramSDKOptions);
|
|
16
|
-
/**
|
|
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
|
-
/**
|
|
67
|
+
/**
|
|
68
|
+
* 销毁 SDK 实例。
|
|
69
|
+
*
|
|
70
|
+
* @remarks
|
|
71
|
+
* 销毁后会移除 message 监听,并拒绝尚未完成的 bridge 请求。
|
|
72
|
+
* 已销毁实例不应继续复用。
|
|
73
|
+
*/
|
|
23
74
|
destroy(): void;
|
|
24
75
|
}
|
|
25
|
-
/**
|
|
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
|
-
/**
|
|
8
|
+
/**
|
|
9
|
+
* 等待默认 SDK 实例与父容器完成握手。
|
|
10
|
+
*
|
|
11
|
+
* @returns 当默认 SDK 单例与父容器握手成功后 resolve。
|
|
12
|
+
* @throws {HbMiniProgramSDKError} 当当前页面不在 iframe 中、缺少 nonce 或握手超时时抛出。
|
|
13
|
+
*/
|
|
6
14
|
export declare function ready(): Promise<void>;
|
|
7
|
-
/**
|
|
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
|
-
/**
|
|
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
|
|
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;
|
package/types/core/utils.d.ts
CHANGED
|
@@ -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,28 @@
|
|
|
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,
|
|
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
|
|
9
|
-
export
|
|
10
|
-
export
|
|
8
|
+
export { AUTH_LOGIN_METHOD, NETWORK_REQUEST_METHOD, SHARE_SCREENSHOT_METHOD, SHARE_SHOW_SHARE_MENU_METHOD, STORAGE_GET_STORAGE_METHOD, STORAGE_SET_STORAGE_METHOD, USER_GET_INFO_METHOD, VIEWPORT_GET_WINDOW_INFO_METHOD, } from './protocol/capabilities';
|
|
9
|
+
export type { MiniProgramAuthMethod, MiniProgramBridgeMethod, MiniProgramCapabilityDefinition, MiniProgramCapabilityModule, MiniProgramCapabilityPayload, MiniProgramCapabilityPayloadMap, MiniProgramCapabilityResult, MiniProgramCapabilityResultMap, MiniProgramCapabilityRisk, MiniProgramNetworkMethod, MiniProgramShareMethod, MiniProgramStorageMethod, MiniProgramUserMethod, MiniProgramViewportMethod, } from './protocol/capabilities';
|
|
10
|
+
export type { LoginPayload, LoginResult, MiniProgramAuthModule } from './modules/auth';
|
|
11
|
+
export type { GetUserInfoPayload, GetUserInfoResult, MiniProgramUserInfo, MiniProgramUserInfoResult, MiniProgramUserModule, } from './modules/user';
|
|
12
|
+
export type { MiniProgramScreenshotOptions, MiniProgramScreenshotRect, MiniProgramShareChannel, MiniProgramShareModule, MiniProgramShowShareMenuOptions, ScreenshotPayload, ScreenshotResult, ShowShareMenuPayload, ShowShareMenuResult, } from './modules/share';
|
|
13
|
+
export type { GetWindowInfoPayload, GetWindowInfoResult, MiniProgramSafeArea, MiniProgramViewportModule, MiniProgramWindowInfoResult, } from './modules/viewport';
|
|
14
|
+
export type { GetStoragePayload, GetStorageResult, MiniProgramStorageModule, SetStoragePayload, } from './modules/storage';
|
|
15
|
+
export type { MiniProgramNetworkHeaders, MiniProgramNetworkModule, MiniProgramNetworkParams, MiniProgramNetworkRequestConfig, MiniProgramNetworkRequestMethod, MiniProgramNetworkResponse, MiniProgramNetworkValidateStatus, NetworkRequestPayload, NetworkResponsePayload, } from './modules/network';
|
|
11
16
|
import { off, on, ready } from './core/singleton';
|
|
12
17
|
declare const hbSDK: {
|
|
13
18
|
ready: typeof ready;
|
|
14
19
|
on: typeof on;
|
|
15
20
|
off: typeof off;
|
|
21
|
+
auth: import(".").MiniProgramAuthModule;
|
|
16
22
|
user: import(".").MiniProgramUserModule;
|
|
17
23
|
share: import(".").MiniProgramShareModule;
|
|
18
|
-
|
|
24
|
+
viewport: import(".").MiniProgramViewportModule;
|
|
25
|
+
storage: import(".").MiniProgramStorageModule;
|
|
26
|
+
network: import(".").MiniProgramNetworkModule;
|
|
19
27
|
};
|
|
20
28
|
export default hbSDK;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { MiniProgramRequester } from '../../core/client';
|
|
2
|
+
import type { MiniProgramUserInfoResult } from '../user';
|
|
3
|
+
export { AUTH_LOGIN_METHOD } from '../../protocol/capabilities';
|
|
4
|
+
/** `auth.login` 不需要入参。 */
|
|
5
|
+
export type LoginPayload = void;
|
|
6
|
+
/** `auth.login` 返回登录流程结束后的最新用户登录态与公开基础资料。 */
|
|
7
|
+
export type LoginResult = MiniProgramUserInfoResult;
|
|
8
|
+
export type { MiniProgramAuthMethod } from '../../protocol/capabilities';
|
|
9
|
+
/** 外部小程序可调用的授权模块。 */
|
|
10
|
+
export interface MiniProgramAuthModule {
|
|
11
|
+
/**
|
|
12
|
+
* 唤起登录授权,并返回登录后的最新用户信息。
|
|
13
|
+
*
|
|
14
|
+
* 该能力不会向小程序暴露 token、cookie 或任何登录凭据。
|
|
15
|
+
*/
|
|
16
|
+
login(): Promise<LoginResult>;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* 唤起登录授权,并在流程返回后刷新用户公开基础资料。
|
|
20
|
+
*
|
|
21
|
+
* @param requester 底层 bridge 请求能力。
|
|
22
|
+
* @returns 登录流程完成后的最新用户登录态与公开资料。
|
|
23
|
+
* @throws {HbMiniProgramSDKError} 当 bridge、父容器或宿主运行时调用失败时抛出。
|
|
24
|
+
*
|
|
25
|
+
* @remarks
|
|
26
|
+
* 该能力不会向小程序暴露 token、cookie 或其他登录凭据。
|
|
27
|
+
*/
|
|
28
|
+
export declare function login(requester: MiniProgramRequester): Promise<LoginResult>;
|
|
29
|
+
/**
|
|
30
|
+
* 创建授权模块。
|
|
31
|
+
*
|
|
32
|
+
* @param requester 底层 bridge 请求能力。
|
|
33
|
+
* @returns 面向业务层的授权模块对象。
|
|
34
|
+
*/
|
|
35
|
+
export declare function createAuthModule(requester: MiniProgramRequester): MiniProgramAuthModule;
|