@hylid/call
Advanced tools
Comparing version 3.0.0-alpha.13 to 3.0.0-alpha.14
@@ -7,5 +7,3 @@ /// <reference types="miniprogram" /> | ||
import { notFound } from './notFound'; | ||
interface Config { | ||
type?: string; | ||
} | ||
import { Config } from './types'; | ||
export declare function call<T extends keyof MPApi>(name: T | ({} & string), options?: PickMPArgs<T> | Common | Callback<PickMpReturns<T>>, config?: Config): any; | ||
@@ -12,0 +10,0 @@ export { WebViewBridge } from './webviewBridge'; |
import { PickMPArgs, PickMpReturns, MPApi } from '@hylid/types'; | ||
import { MpWebJsApiConfig } from './types'; | ||
declare type Callback<T> = (result?: T) => void; | ||
export declare const mpWebCall: <T extends keyof MPApi>(apiName: ({} & string) | T, options?: PickMPArgs<T> | Callback<ReturnType<MPApi[T]>> | undefined, config?: { | ||
type?: string; | ||
}) => void; | ||
export declare const mpWebCall: <T extends keyof MPApi>(api: ({} & string) | T, options?: PickMPArgs<T> | Callback<ReturnType<MPApi[T]>> | undefined, config?: MpWebJsApiConfig) => void; | ||
export {}; |
var loadAppxBridge = function loadAppxBridge(cb) { | ||
var _a; | ||
try { | ||
// umd 包用的 IFF 模式,会立即向 window.my 上注入 jsapi,所以不能试用 window.my 判断是否已经加载了 appx 脚本 | ||
// umd 包用的 IFF 模式,会立即向 window.my 上注入 jsapi,所以不能使用 window.my 判断是否已经加载了 appx 脚本 | ||
// umd 版本和 modules 版本不能混用 | ||
if ((_a = window.my) === null || _a === void 0 ? void 0 : _a.__hy_mounted__) { | ||
@@ -43,3 +44,3 @@ cb && cb("ready" /* BridgeReady.READY */); | ||
} | ||
// 下面兼容老版本 | ||
// 下面兼容老版本(老版本消息识别不明确,导致 H5 中走 success/fail 逻辑可能岔了) | ||
// @ts-ignore | ||
@@ -60,7 +61,9 @@ if (res === null || res === void 0 ? void 0 : res.error) { | ||
window.my.onMessage = function (message) { | ||
var _a; | ||
var serialId = message.serialId; | ||
var data = message.result; | ||
var type = message.type; | ||
var type = (_a = message.config) === null || _a === void 0 ? void 0 : _a.type; // JsApi 的类型 | ||
if (serialId) { | ||
var callback = callMap[serialId]; | ||
if (!callback) return; | ||
if (type === 'callback') { | ||
@@ -78,7 +81,7 @@ callback(data); | ||
function fireMessage(_a) { | ||
var apiName = _a.apiName, | ||
var api = _a.api, | ||
options = _a.options, | ||
config = _a.config; | ||
var random = Math.floor(Math.random() * 1000000); | ||
var serialId = apiName + '_' + random; | ||
var serialId = api + '_' + random; | ||
if (options) { | ||
@@ -88,4 +91,5 @@ callMap[serialId] = options; | ||
var params = { | ||
source: 'hylid', | ||
type: 'apiCall', | ||
api: apiName, | ||
api: api, | ||
serialId: serialId, | ||
@@ -97,6 +101,6 @@ options: options, | ||
} | ||
return function call(apiName, options, config) { | ||
return function call(api, options, config) { | ||
loadAppxBridge(function (bridgeReady) { | ||
var params = { | ||
apiName: apiName, | ||
api: api, | ||
options: options, | ||
@@ -103,0 +107,0 @@ config: config |
@@ -0,7 +1,24 @@ | ||
/// <reference types="miniprogram" /> | ||
import { MpWebMessage } from './types'; | ||
interface Config { | ||
whitelist?: string[]; | ||
blacklist?: string[]; | ||
customApi?: CustomApi; | ||
} | ||
interface CustomApi { | ||
[key: string]: (params: { | ||
success(v: any): void; | ||
fail(v: any): void; | ||
}) => void; | ||
} | ||
export declare class WebViewBridge { | ||
webview: any; | ||
customApi: {}; | ||
constructor(ctx: any, customApi?: {}); | ||
listen: (data: any) => Promise<void>; | ||
handleApi: (data: any) => Promise<unknown>; | ||
webview: WebViewContext; | ||
config: Config; | ||
constructor(ctx: WebViewContext, config?: Config); | ||
listen: (data: { | ||
detail: MpWebMessage; | ||
}) => Promise<true | undefined>; | ||
isAllowed(api: string): boolean; | ||
handleApi: (data: MpWebMessage) => Promise<unknown>; | ||
} | ||
export {}; |
@@ -127,45 +127,50 @@ var __assign = this && this.__assign || function () { | ||
var WebViewBridge = /** @class */function () { | ||
function WebViewBridge(ctx, customApi) { | ||
if (customApi === void 0) { | ||
customApi = {}; | ||
function WebViewBridge(ctx, config) { | ||
if (config === void 0) { | ||
config = {}; | ||
} | ||
var _this = this; | ||
this.customApi = {}; | ||
this.config = {}; | ||
this.listen = function (data) { | ||
return __awaiter(_this, void 0, void 0, function () { | ||
var command, config, api_1, serialId_1, type_1, _a, _b; | ||
var _c; | ||
var command, config, api, type, notAllowedMessage; | ||
var _this = this; | ||
return __generator(this, function (_d) { | ||
switch (_d.label) { | ||
case 0: | ||
command = data.detail; | ||
if (!command.api) return [3 /*break*/, 3]; | ||
config = command.config, api_1 = command.api, serialId_1 = command.serialId; | ||
type_1 = (config || {}).type; | ||
if (!(type_1 === 'callback')) return [3 /*break*/, 1]; | ||
return __generator(this, function (_a) { | ||
command = data.detail; | ||
if (command.source === 'hylid') { | ||
config = command.config, api = command.api; | ||
type = (config || {}).type; | ||
// 权限管控不允许调用 JSAPI | ||
if (!this.isAllowed(api)) { | ||
notAllowedMessage = __assign(__assign({}, command), { | ||
result: { | ||
_type_: 'fail', | ||
_data_: { | ||
errorCode: -2, | ||
errorMessage: 'api is not allowed' | ||
} | ||
} | ||
}); | ||
this.webview.postMessage(notAllowedMessage); | ||
return [2 /*return*/]; | ||
} | ||
if (type === 'callback') { | ||
// @ts-ignore | ||
my[api_1](function (result) { | ||
_this.webview.postMessage({ | ||
api: api_1, | ||
serialId: serialId_1, | ||
type: type_1, | ||
my[api](function (result) { | ||
_this.webview.postMessage(__assign(__assign({}, command), { | ||
result: result | ||
}); | ||
})); | ||
}); | ||
return [3 /*break*/, 3]; | ||
case 1: | ||
_b = (_a = this.webview).postMessage; | ||
_c = { | ||
api: api_1, | ||
serialId: serialId_1, | ||
type: type_1 | ||
}; | ||
return [4 /*yield*/, this.handleApi(command)]; | ||
case 2: | ||
_b.apply(_a, [(_c.result = _d.sent(), _c)]); | ||
_d.label = 3; | ||
case 3: | ||
return [2 /*return*/]; | ||
} else { | ||
this.handleApi(command).then(function (result) { | ||
_this.webview.postMessage(__assign(__assign({}, command), { | ||
result: result | ||
})); | ||
}); | ||
} | ||
// 表示被 hylid-bridge 消费了 | ||
return [2 /*return*/, true]; | ||
} | ||
return [2 /*return*/]; | ||
}); | ||
@@ -182,2 +187,3 @@ }); | ||
return new Promise(function (resolve) { | ||
var _a; | ||
var options = __assign(__assign({}, _options), { | ||
@@ -198,3 +204,3 @@ success: function success(res) { | ||
// @ts-ignore | ||
var mpApi = _this.customApi[api] || my[api]; | ||
var mpApi = ((_a = _this.config.customApi) === null || _a === void 0 ? void 0 : _a[api]) || my[api]; | ||
if (type === 'sync') { | ||
@@ -218,6 +224,12 @@ var data_1 = mpApi(_options); | ||
this.webview = ctx; | ||
this.customApi = customApi; | ||
this.config = config; | ||
} | ||
WebViewBridge.prototype.isAllowed = function (api) { | ||
// 黑白名单校验 | ||
if (this.config.whitelist && !this.config.whitelist.includes(api)) return false; | ||
if (this.config.blacklist && this.config.blacklist.includes(api)) return false; | ||
return true; | ||
}; | ||
return WebViewBridge; | ||
}(); | ||
export { WebViewBridge }; |
{ | ||
"name": "@hylid/call", | ||
"version": "3.0.0-alpha.13", | ||
"version": "3.0.0-alpha.14", | ||
"main": "lib/index.js", | ||
@@ -9,4 +9,4 @@ "files": [ | ||
"dependencies": { | ||
"@hylid/env": "^3.0.0-alpha.13", | ||
"@hylid/types": "^3.0.0-alpha.13" | ||
"@hylid/env": "^3.0.0-alpha.14", | ||
"@hylid/types": "^3.0.0-alpha.14" | ||
}, | ||
@@ -13,0 +13,0 @@ "publishConfig": { |
16982
18
521
Updated@hylid/env@^3.0.0-alpha.14
Updated@hylid/types@^3.0.0-alpha.14