bc3b464349
微信小程序 host-bridge 与 shell 文件改为职责命名 移动壳 host-bridge 与 shell 文件改为职责命名 更新原生壳结构门禁、微信 smoke 和宿主壳文档
138 lines
3.1 KiB
TypeScript
138 lines
3.1 KiB
TypeScript
import { Platform } from 'react-native';
|
|
|
|
import {
|
|
HOST_BRIDGE_PROTOCOL,
|
|
HOST_BRIDGE_VERSION,
|
|
type HostBridgeCapability,
|
|
type HostBridgeError,
|
|
type HostBridgeMethod,
|
|
type HostBridgeRequest,
|
|
type HostBridgeResponse,
|
|
isHostBridgeMethod,
|
|
normalizeHostBridgeRequestId,
|
|
} from '../../../../packages/shared/src/contracts/hostBridge';
|
|
|
|
export const HOST_BRIDGE_RESPONSE_CACHE_MAX = 128;
|
|
|
|
export const MOBILE_HOST_CAPABILITIES: HostBridgeCapability[] = [
|
|
'host.getRuntime',
|
|
'appearance.getColorScheme',
|
|
'host.events',
|
|
'app.lifecycle',
|
|
'share.open',
|
|
'share.setTarget',
|
|
'navigation.openNativePage',
|
|
'navigation.canGoBack',
|
|
'app.reloadWebView',
|
|
'app.openExternalUrl',
|
|
'network.status',
|
|
'network.statusChanged',
|
|
'clipboard.writeText',
|
|
'clipboard.readText',
|
|
'file.exportText',
|
|
'file.importText',
|
|
'file.exportImage',
|
|
'file.importImage',
|
|
'file.captureImage',
|
|
'file.importAudio',
|
|
'file.exportAudio',
|
|
'haptics.impact',
|
|
'notification.showLocal',
|
|
];
|
|
|
|
export const IOS_MOBILE_HOST_CAPABILITIES: HostBridgeCapability[] = [
|
|
...MOBILE_HOST_CAPABILITIES,
|
|
'app.setBadgeCount',
|
|
];
|
|
|
|
export function resolveMobileHostCapabilities(platform = Platform.OS) {
|
|
return platform === 'ios'
|
|
? IOS_MOBILE_HOST_CAPABILITIES
|
|
: MOBILE_HOST_CAPABILITIES;
|
|
}
|
|
|
|
export type MobileHostBridgeNavigation = {
|
|
allowedOrigin: string;
|
|
openWebViewUrl: (url: string) => void;
|
|
reloadWebView: () => void;
|
|
};
|
|
|
|
export function unsupported(method: HostBridgeMethod): HostBridgeError {
|
|
return {
|
|
code: 'unsupported_method',
|
|
message: `${method} unsupported in mobile shell`,
|
|
};
|
|
}
|
|
|
|
export function invalidRequest(message: string): HostBridgeError {
|
|
return {
|
|
code: 'invalid_request',
|
|
message,
|
|
};
|
|
}
|
|
|
|
export function isHostBridgeRequest(value: unknown): value is HostBridgeRequest {
|
|
if (!value || typeof value !== 'object') {
|
|
return false;
|
|
}
|
|
|
|
const candidate = value as Partial<HostBridgeRequest>;
|
|
const requestId = normalizeHostBridgeRequestId(candidate.id);
|
|
return (
|
|
candidate.bridge === HOST_BRIDGE_PROTOCOL &&
|
|
candidate.version === HOST_BRIDGE_VERSION &&
|
|
requestId !== null &&
|
|
isHostBridgeMethod(candidate.method)
|
|
);
|
|
}
|
|
|
|
export function parseRequest(raw: string) {
|
|
try {
|
|
return JSON.parse(raw) as unknown;
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
export function ok<Result>(
|
|
request: HostBridgeRequest,
|
|
result?: Result,
|
|
): HostBridgeResponse<Result> {
|
|
return {
|
|
bridge: HOST_BRIDGE_PROTOCOL,
|
|
version: HOST_BRIDGE_VERSION,
|
|
id: request.id,
|
|
ok: true,
|
|
result,
|
|
};
|
|
}
|
|
|
|
export function failure(
|
|
request: Pick<HostBridgeRequest, 'id'>,
|
|
error: HostBridgeError,
|
|
): HostBridgeResponse {
|
|
return {
|
|
bridge: HOST_BRIDGE_PROTOCOL,
|
|
version: HOST_BRIDGE_VERSION,
|
|
id: request.id,
|
|
ok: false,
|
|
error,
|
|
};
|
|
}
|
|
|
|
export function normalizeMobileHostBridgeError(error: unknown): HostBridgeError {
|
|
if (
|
|
error &&
|
|
typeof error === 'object' &&
|
|
'code' in error &&
|
|
'message' in error
|
|
) {
|
|
return error as HostBridgeError;
|
|
}
|
|
|
|
return {
|
|
code: 'host_error',
|
|
message: error instanceof Error ? error.message : String(error),
|
|
};
|
|
}
|