@hylid/call 3.0.0-alpha.9 → 3.1.0
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/lib/index.d.ts +1 -3
- package/lib/mpWebCall.d.ts +2 -3
- package/lib/mpWebCall.js +18 -9
- package/lib/types.d.ts +20 -0
- package/lib/types.js +1 -0
- package/lib/webviewBridge.d.ts +22 -5
- package/lib/webviewBridge.js +48 -36
- package/package.json +4 -5
package/lib/index.d.ts
CHANGED
|
@@ -4,9 +4,7 @@ import { mpCall } from './mpCall';
|
|
|
4
4
|
import { mpWebCall } from './mpWebCall';
|
|
5
5
|
import { webCall } from './webCall';
|
|
6
6
|
import { notFound } from './notFound';
|
|
7
|
-
|
|
8
|
-
type?: string;
|
|
9
|
-
}
|
|
7
|
+
import { Config } from './types';
|
|
10
8
|
export declare function call<T extends keyof MPApi>(name: T | ({} & string), options?: PickMPArgs<T> | Common | Callback<PickMpReturns<T>>, config?: Config): any;
|
|
11
9
|
export { WebViewBridge } from './webviewBridge';
|
|
12
10
|
export { mpCall, mpWebCall, webCall, notFound };
|
package/lib/mpWebCall.d.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { PickMPArgs, PickMpReturns, MPApi } from '@hylid/types';
|
|
2
|
+
import { MpWebJsApiConfig } from './types';
|
|
2
3
|
declare type Callback<T> = (result?: T) => void;
|
|
3
|
-
export declare const mpWebCall: <T extends keyof MPApi>(
|
|
4
|
-
type?: string;
|
|
5
|
-
}) => void;
|
|
4
|
+
export declare const mpWebCall: <T extends keyof MPApi>(api: ({} & string) | T, options?: PickMPArgs<T> | Callback<ReturnType<MPApi[T]>> | undefined, config?: MpWebJsApiConfig) => void;
|
|
6
5
|
export {};
|
package/lib/mpWebCall.js
CHANGED
|
@@ -1,16 +1,22 @@
|
|
|
1
1
|
var loadAppxBridge = function loadAppxBridge(cb) {
|
|
2
|
+
var _a;
|
|
2
3
|
try {
|
|
3
|
-
//
|
|
4
|
-
|
|
4
|
+
// umd 包用的 IFF 模式,会立即向 window.my 上注入 jsapi,所以不能使用 window.my 判断是否已经加载了 appx 脚本
|
|
5
|
+
// umd 版本和 modules 版本不能混用
|
|
6
|
+
if ((_a = window.my) === null || _a === void 0 ? void 0 : _a.__hy_mounted__) {
|
|
5
7
|
cb && cb("ready" /* BridgeReady.READY */);
|
|
6
8
|
return;
|
|
7
9
|
}
|
|
10
|
+
var hyMy_1 = window.my || {};
|
|
8
11
|
var script_1 = document.createElement('script');
|
|
9
12
|
script_1.src = 'https://appx/web-view.min.js';
|
|
10
13
|
script_1.onload = function onload() {
|
|
11
14
|
var readyState = script_1['readyState'];
|
|
12
15
|
if (typeof readyState === 'undefined' || /^(loaded|complete)$/.test(readyState)) {
|
|
13
16
|
script_1.onload = null;
|
|
17
|
+
Object.assign(window.my, hyMy_1, {
|
|
18
|
+
__hy_mounted__: true
|
|
19
|
+
});
|
|
14
20
|
cb && cb("loaded" /* BridgeReady.LOADED */);
|
|
15
21
|
}
|
|
16
22
|
};
|
|
@@ -35,7 +41,7 @@ var callbackFn = function callbackFn(res, callback) {
|
|
|
35
41
|
if (_type_ === 'fail') fail === null || fail === void 0 ? void 0 : fail(_data_);
|
|
36
42
|
return complete === null || complete === void 0 ? void 0 : complete(_data_);
|
|
37
43
|
}
|
|
38
|
-
// 下面兼容老版本
|
|
44
|
+
// 下面兼容老版本(老版本消息识别不明确,导致 H5 中走 success/fail 逻辑可能岔了)
|
|
39
45
|
// @ts-ignore
|
|
40
46
|
if (res === null || res === void 0 ? void 0 : res.error) {
|
|
41
47
|
fail === null || fail === void 0 ? void 0 : fail(res);
|
|
@@ -51,11 +57,13 @@ export var mpWebCall = function () {
|
|
|
51
57
|
function onMessage() {
|
|
52
58
|
var messageHandler = window.my.onMessage;
|
|
53
59
|
window.my.onMessage = function (message) {
|
|
60
|
+
var _a;
|
|
54
61
|
var serialId = message.serialId;
|
|
55
62
|
var data = message.result;
|
|
56
|
-
var type = message.type;
|
|
63
|
+
var type = (_a = message.config) === null || _a === void 0 ? void 0 : _a.type; // JsApi 的类型
|
|
57
64
|
if (serialId) {
|
|
58
65
|
var callback = callMap[serialId];
|
|
66
|
+
if (!callback) return;
|
|
59
67
|
if (type === 'callback') {
|
|
60
68
|
callback(data);
|
|
61
69
|
} else {
|
|
@@ -68,27 +76,28 @@ export var mpWebCall = function () {
|
|
|
68
76
|
};
|
|
69
77
|
}
|
|
70
78
|
function fireMessage(_a) {
|
|
71
|
-
var
|
|
79
|
+
var api = _a.api,
|
|
72
80
|
options = _a.options,
|
|
73
81
|
config = _a.config;
|
|
74
82
|
var random = Math.floor(Math.random() * 1000000);
|
|
75
|
-
var serialId =
|
|
83
|
+
var serialId = api + '_' + random;
|
|
76
84
|
if (options) {
|
|
77
85
|
callMap[serialId] = options;
|
|
78
86
|
}
|
|
79
87
|
var params = {
|
|
88
|
+
source: 'hylid',
|
|
80
89
|
type: 'apiCall',
|
|
81
|
-
api:
|
|
90
|
+
api: api,
|
|
82
91
|
serialId: serialId,
|
|
83
92
|
options: options,
|
|
84
93
|
config: config
|
|
85
94
|
};
|
|
86
95
|
window.my.postMessage(params);
|
|
87
96
|
}
|
|
88
|
-
return function call(
|
|
97
|
+
return function call(api, options, config) {
|
|
89
98
|
loadAppxBridge(function (bridgeReady) {
|
|
90
99
|
var params = {
|
|
91
|
-
|
|
100
|
+
api: api,
|
|
92
101
|
options: options,
|
|
93
102
|
config: config
|
|
94
103
|
};
|
package/lib/types.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { JsApiType, MPApi } from '@hylid/types';
|
|
2
|
+
export declare type JsApi = keyof MPApi;
|
|
3
|
+
export interface MpWebJsApiConfig {
|
|
4
|
+
type?: JsApiType;
|
|
5
|
+
}
|
|
6
|
+
export declare type Config = MpWebJsApiConfig;
|
|
7
|
+
export interface MpWebMessageResult {
|
|
8
|
+
_type_: 'success' | 'fail';
|
|
9
|
+
_data_: any;
|
|
10
|
+
}
|
|
11
|
+
export interface MpWebMessage {
|
|
12
|
+
source: 'hylid';
|
|
13
|
+
type: 'apiCall';
|
|
14
|
+
api: string;
|
|
15
|
+
serialId: string;
|
|
16
|
+
options: any;
|
|
17
|
+
config?: MpWebJsApiConfig;
|
|
18
|
+
result?: MpWebMessageResult;
|
|
19
|
+
}
|
|
20
|
+
export declare type JsApiInfo = Pick<MpWebMessage, 'api' | 'options' | 'config'>;
|
package/lib/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/lib/webviewBridge.d.ts
CHANGED
|
@@ -1,7 +1,24 @@
|
|
|
1
|
+
/// <reference types="miniprogram" />
|
|
2
|
+
import { MpWebMessage } from './types';
|
|
3
|
+
interface Config {
|
|
4
|
+
whitelist?: string[];
|
|
5
|
+
blacklist?: string[];
|
|
6
|
+
customApi?: CustomApi;
|
|
7
|
+
}
|
|
8
|
+
interface CustomApi {
|
|
9
|
+
[key: string]: (params: {
|
|
10
|
+
success(v: any): void;
|
|
11
|
+
fail(v: any): void;
|
|
12
|
+
}) => void;
|
|
13
|
+
}
|
|
1
14
|
export declare class WebViewBridge {
|
|
2
|
-
webview:
|
|
3
|
-
|
|
4
|
-
constructor(ctx:
|
|
5
|
-
listen: (data:
|
|
6
|
-
|
|
15
|
+
webview: WebViewContext;
|
|
16
|
+
config: Config;
|
|
17
|
+
constructor(ctx: WebViewContext, config?: Config);
|
|
18
|
+
listen: (data: {
|
|
19
|
+
detail: MpWebMessage;
|
|
20
|
+
}) => Promise<true | undefined>;
|
|
21
|
+
isAllowed(api: string): boolean;
|
|
22
|
+
handleApi: (data: MpWebMessage) => Promise<unknown>;
|
|
7
23
|
}
|
|
24
|
+
export {};
|
package/lib/webviewBridge.js
CHANGED
|
@@ -124,49 +124,54 @@ var __generator = this && this.__generator || function (thisArg, body) {
|
|
|
124
124
|
}
|
|
125
125
|
};
|
|
126
126
|
var WebViewBridge = /** @class */function () {
|
|
127
|
-
function WebViewBridge(ctx,
|
|
128
|
-
if (
|
|
129
|
-
|
|
127
|
+
function WebViewBridge(ctx, config) {
|
|
128
|
+
if (config === void 0) {
|
|
129
|
+
config = {};
|
|
130
130
|
}
|
|
131
131
|
var _this = this;
|
|
132
|
-
this.
|
|
132
|
+
this.config = {};
|
|
133
133
|
this.listen = function (data) {
|
|
134
134
|
return __awaiter(_this, void 0, void 0, function () {
|
|
135
|
-
var command, config,
|
|
136
|
-
var _c;
|
|
135
|
+
var command, config, api, type, notAllowedMessage;
|
|
137
136
|
var _this = this;
|
|
138
|
-
return __generator(this, function (
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
137
|
+
return __generator(this, function (_a) {
|
|
138
|
+
command = data.detail;
|
|
139
|
+
if (command.source === 'hylid') {
|
|
140
|
+
config = command.config, api = command.api;
|
|
141
|
+
type = (config || {}).type;
|
|
142
|
+
// 权限管控不允许调用 JSAPI
|
|
143
|
+
if (!this.isAllowed(api)) {
|
|
144
|
+
notAllowedMessage = __assign(__assign({}, command), {
|
|
145
|
+
result: {
|
|
146
|
+
_type_: 'fail',
|
|
147
|
+
_data_: {
|
|
148
|
+
errorCode: -2,
|
|
149
|
+
errorMessage: 'api is not allowed'
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
});
|
|
153
|
+
this.webview.postMessage(notAllowedMessage);
|
|
154
|
+
return [2 /*return*/];
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
if (type === 'callback') {
|
|
146
158
|
// @ts-ignore
|
|
147
|
-
my[
|
|
148
|
-
_this.webview.postMessage({
|
|
149
|
-
api: api_1,
|
|
150
|
-
serialId: serialId_1,
|
|
151
|
-
type: type_1,
|
|
159
|
+
my[api](function (result) {
|
|
160
|
+
_this.webview.postMessage(__assign(__assign({}, command), {
|
|
152
161
|
result: result
|
|
153
|
-
});
|
|
162
|
+
}));
|
|
154
163
|
});
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
case 2:
|
|
165
|
-
_b.apply(_a, [(_c.result = _d.sent(), _c)]);
|
|
166
|
-
_d.label = 3;
|
|
167
|
-
case 3:
|
|
168
|
-
return [2 /*return*/];
|
|
164
|
+
} else {
|
|
165
|
+
this.handleApi(command).then(function (result) {
|
|
166
|
+
_this.webview.postMessage(__assign(__assign({}, command), {
|
|
167
|
+
result: result
|
|
168
|
+
}));
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
// 表示被 hylid-bridge 消费了
|
|
172
|
+
return [2 /*return*/, true];
|
|
169
173
|
}
|
|
174
|
+
return [2 /*return*/];
|
|
170
175
|
});
|
|
171
176
|
});
|
|
172
177
|
};
|
|
@@ -178,6 +183,7 @@ var WebViewBridge = /** @class */function () {
|
|
|
178
183
|
var _a = (config || {}).type,
|
|
179
184
|
type = _a === void 0 ? 'async' : _a;
|
|
180
185
|
return new Promise(function (resolve) {
|
|
186
|
+
var _a;
|
|
181
187
|
var options = __assign(__assign({}, _options), {
|
|
182
188
|
success: function success(res) {
|
|
183
189
|
resolve({
|
|
@@ -193,7 +199,7 @@ var WebViewBridge = /** @class */function () {
|
|
|
193
199
|
}
|
|
194
200
|
});
|
|
195
201
|
// @ts-ignore
|
|
196
|
-
var mpApi = _this.customApi[api] || my[api];
|
|
202
|
+
var mpApi = ((_a = _this.config.customApi) === null || _a === void 0 ? void 0 : _a[api]) || my[api];
|
|
197
203
|
if (type === 'sync') {
|
|
198
204
|
var data_1 = mpApi(_options);
|
|
199
205
|
resolve({
|
|
@@ -212,8 +218,14 @@ var WebViewBridge = /** @class */function () {
|
|
|
212
218
|
});
|
|
213
219
|
};
|
|
214
220
|
this.webview = ctx;
|
|
215
|
-
this.
|
|
221
|
+
this.config = config;
|
|
216
222
|
}
|
|
223
|
+
WebViewBridge.prototype.isAllowed = function (api) {
|
|
224
|
+
// 黑白名单校验
|
|
225
|
+
if (this.config.whitelist && !this.config.whitelist.includes(api)) return false;
|
|
226
|
+
if (this.config.blacklist && this.config.blacklist.includes(api)) return false;
|
|
227
|
+
return true;
|
|
228
|
+
};
|
|
217
229
|
return WebViewBridge;
|
|
218
230
|
}();
|
|
219
231
|
export { WebViewBridge };
|
package/package.json
CHANGED
|
@@ -1,19 +1,18 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hylid/call",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.1.0",
|
|
4
4
|
"main": "lib/index.js",
|
|
5
5
|
"files": [
|
|
6
6
|
"lib"
|
|
7
7
|
],
|
|
8
8
|
"dependencies": {
|
|
9
|
-
"@hylid/env": "^3.
|
|
10
|
-
"@hylid/types": "^3.
|
|
9
|
+
"@hylid/env": "^3.1.0",
|
|
10
|
+
"@hylid/types": "^3.1.0"
|
|
11
11
|
},
|
|
12
12
|
"publishConfig": {
|
|
13
13
|
"access": "public"
|
|
14
14
|
},
|
|
15
15
|
"license": "MIT",
|
|
16
16
|
"sideEffects": false,
|
|
17
|
-
"registry": "https://registry.npmjs.org/"
|
|
18
|
-
"repository": "https://code.alipay.com/ant-ife/hylid-bridge.git"
|
|
17
|
+
"registry": "https://registry.npmjs.org/"
|
|
19
18
|
}
|