按宿主能力声明启用原生能力
HostBridge 契约沉淀 method 与 capability 白名单 H5 解析 hostCapabilities 并按能力调用原生桥 发布分享弹窗仅在声明 share.open 时显示系统分享 补充能力声明测试和宿主壳文档
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import { describe, expect, test } from 'vitest';
|
||||
|
||||
import {
|
||||
isHostBridgeCapability,
|
||||
normalizeHostBridgeExportFileName,
|
||||
normalizeHostBridgeExternalUrl,
|
||||
} from './hostBridge';
|
||||
@@ -42,4 +43,11 @@ describe('HostBridge shared contract helpers', () => {
|
||||
120,
|
||||
);
|
||||
});
|
||||
|
||||
test('识别 HostBridge 能力白名单', () => {
|
||||
expect(isHostBridgeCapability('share.open')).toBe(true);
|
||||
expect(isHostBridgeCapability('navigation.canGoBack')).toBe(true);
|
||||
expect(isHostBridgeCapability('unknown.capability')).toBe(false);
|
||||
expect(isHostBridgeCapability(null)).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -13,23 +13,38 @@ export type NativeHostPlatform =
|
||||
| 'linux'
|
||||
| 'unknown';
|
||||
|
||||
export type HostBridgeMethod =
|
||||
| 'host.getRuntime'
|
||||
| 'auth.requestLogin'
|
||||
| 'payment.request'
|
||||
| 'share.setTarget'
|
||||
| 'share.open'
|
||||
| 'navigation.openNativePage'
|
||||
| 'app.openExternalUrl'
|
||||
| 'app.setTitle'
|
||||
| 'clipboard.writeText'
|
||||
| 'file.exportText'
|
||||
| 'haptics.impact';
|
||||
export const HOST_BRIDGE_METHODS = [
|
||||
'host.getRuntime',
|
||||
'auth.requestLogin',
|
||||
'payment.request',
|
||||
'share.setTarget',
|
||||
'share.open',
|
||||
'navigation.openNativePage',
|
||||
'app.openExternalUrl',
|
||||
'app.setTitle',
|
||||
'clipboard.writeText',
|
||||
'file.exportText',
|
||||
'haptics.impact',
|
||||
] as const;
|
||||
|
||||
export type HostBridgeCapability =
|
||||
| HostBridgeMethod
|
||||
| 'host.events'
|
||||
| 'navigation.canGoBack';
|
||||
export type HostBridgeMethod = (typeof HOST_BRIDGE_METHODS)[number];
|
||||
|
||||
export const HOST_BRIDGE_CAPABILITIES = [
|
||||
...HOST_BRIDGE_METHODS,
|
||||
'host.events',
|
||||
'navigation.canGoBack',
|
||||
] as const;
|
||||
|
||||
export type HostBridgeCapability = (typeof HOST_BRIDGE_CAPABILITIES)[number];
|
||||
|
||||
export function isHostBridgeCapability(
|
||||
value: unknown,
|
||||
): value is HostBridgeCapability {
|
||||
return (
|
||||
typeof value === 'string' &&
|
||||
HOST_BRIDGE_CAPABILITIES.includes(value as HostBridgeCapability)
|
||||
);
|
||||
}
|
||||
|
||||
export type HostBridgeRuntimeResult = {
|
||||
shell: NativeHostShell;
|
||||
|
||||
Reference in New Issue
Block a user