@hylid/call 2.12.0-alpha.41 → 2.12.0-alpha.9

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 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
- interface Config {
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 };
@@ -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>(apiName: ({} & string) | T, options?: PickMPArgs<T> | Callback<ReturnType<MPApi[T]>> | undefined, config?: {
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
- // @ts-ignore
4
- if (window.my) {
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
  };
@@ -26,8 +32,18 @@ var callbackFn = function callbackFn(res, callback) {
26
32
  success = _a.success,
27
33
  fail = _a.fail,
28
34
  complete = _a.complete;
35
+ var _b = res || {},
36
+ _data_ = _b._data_,
37
+ _type_ = _b._type_;
38
+ // 新版本协议
39
+ if (_type_) {
40
+ if (_type_ === 'success') success === null || success === void 0 ? void 0 : success(_data_);
41
+ if (_type_ === 'fail') fail === null || fail === void 0 ? void 0 : fail(_data_);
42
+ return complete === null || complete === void 0 ? void 0 : complete(_data_);
43
+ }
44
+ // 下面兼容老版本(老版本消息识别不明确,导致 H5 中走 success/fail 逻辑可能岔了)
29
45
  // @ts-ignore
30
- if ((res === null || res === void 0 ? void 0 : res.error) || (res === null || res === void 0 ? void 0 : res.errorCode) || (res === null || res === void 0 ? void 0 : res.errorMessage)) {
46
+ if (res === null || res === void 0 ? void 0 : res.error) {
31
47
  fail === null || fail === void 0 ? void 0 : fail(res);
32
48
  } else {
33
49
  success === null || success === void 0 ? void 0 : success(res);
@@ -41,11 +57,13 @@ export var mpWebCall = function () {
41
57
  function onMessage() {
42
58
  var messageHandler = window.my.onMessage;
43
59
  window.my.onMessage = function (message) {
60
+ var _a;
44
61
  var serialId = message.serialId;
45
62
  var data = message.result;
46
- var type = message.type;
63
+ var type = (_a = message.config) === null || _a === void 0 ? void 0 : _a.type; // JsApi 的类型
47
64
  if (serialId) {
48
65
  var callback = callMap[serialId];
66
+ if (!callback) return;
49
67
  if (type === 'callback') {
50
68
  callback(data);
51
69
  } else {
@@ -58,27 +76,28 @@ export var mpWebCall = function () {
58
76
  };
59
77
  }
60
78
  function fireMessage(_a) {
61
- var apiName = _a.apiName,
79
+ var api = _a.api,
62
80
  options = _a.options,
63
81
  config = _a.config;
64
82
  var random = Math.floor(Math.random() * 1000000);
65
- var serialId = apiName + '_' + random;
83
+ var serialId = api + '_' + random;
66
84
  if (options) {
67
85
  callMap[serialId] = options;
68
86
  }
69
87
  var params = {
88
+ source: 'hylid',
70
89
  type: 'apiCall',
71
- api: apiName,
90
+ api: api,
72
91
  serialId: serialId,
73
92
  options: options,
74
93
  config: config
75
94
  };
76
95
  window.my.postMessage(params);
77
96
  }
78
- return function call(apiName, options, config) {
97
+ return function call(api, options, config) {
79
98
  loadAppxBridge(function (bridgeReady) {
80
99
  var params = {
81
- apiName: apiName,
100
+ api: api,
82
101
  options: options,
83
102
  config: config
84
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 {};
@@ -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: any;
3
- customApi: {};
4
- constructor(ctx: any, customApi?: {});
5
- listen: (data: any) => Promise<void>;
6
- handleApi: (data: any) => Promise<unknown>;
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 {};
@@ -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, customApi) {
128
- if (customApi === void 0) {
129
- customApi = {};
127
+ function WebViewBridge(ctx, config) {
128
+ if (config === void 0) {
129
+ config = {};
130
130
  }
131
131
  var _this = this;
132
- this.customApi = {};
132
+ this.config = {};
133
133
  this.listen = function (data) {
134
134
  return __awaiter(_this, void 0, void 0, function () {
135
- var command, config, api_1, serialId_1, type_1, _a, _b;
136
- var _c;
135
+ var command, config, api, type, notAllowedMessage;
137
136
  var _this = this;
138
- return __generator(this, function (_d) {
139
- switch (_d.label) {
140
- case 0:
141
- command = data.detail;
142
- if (!command.api) return [3 /*break*/, 3];
143
- config = command.config, api_1 = command.api, serialId_1 = command.serialId;
144
- type_1 = (config || {}).type;
145
- if (!(type_1 === 'callback')) return [3 /*break*/, 1];
146
- // @ts-ignore
147
- my[api_1](function (res) {
148
- _this.webview.postMessage({
149
- api: api_1,
150
- serialId: serialId_1,
151
- type: type_1,
152
- result: res
153
- });
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
+ }
154
152
  });
155
- return [3 /*break*/, 3];
156
- case 1:
157
- _b = (_a = this.webview).postMessage;
158
- _c = {
159
- api: api_1,
160
- serialId: serialId_1,
161
- type: type_1
162
- };
163
- return [4 /*yield*/, this.handleApi(command)];
164
- case 2:
165
- _b.apply(_a, [(_c.result = _d.sent(), _c)]);
166
- _d.label = 3;
167
- case 3:
153
+ this.webview.postMessage(notAllowedMessage);
168
154
  return [2 /*return*/];
155
+ }
156
+
157
+ if (type === 'callback') {
158
+ // @ts-ignore
159
+ my[api](function (result) {
160
+ _this.webview.postMessage(__assign(__assign({}, command), {
161
+ result: result
162
+ }));
163
+ });
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,23 +183,29 @@ 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
- success: resolve,
183
- fail: function fail(res) {
184
- if (!res || !res.error) {
185
- resolve(__assign({
186
- error: "".concat(api, " error")
187
- }, res));
188
- } else {
189
- resolve(res);
190
- }
188
+ success: function success(res) {
189
+ resolve({
190
+ _type_: 'success',
191
+ _data_: res
192
+ });
191
193
  },
192
- complete: resolve
194
+ fail: function fail(res) {
195
+ resolve({
196
+ _type_: 'fail',
197
+ _data_: res
198
+ });
199
+ }
193
200
  });
194
201
  // @ts-ignore
195
- var mpApi = _this.customApi[api] || my[api];
202
+ var mpApi = ((_a = _this.config.customApi) === null || _a === void 0 ? void 0 : _a[api]) || my[api];
196
203
  if (type === 'sync') {
197
- resolve(mpApi(options));
204
+ var data_1 = mpApi(_options);
205
+ resolve({
206
+ _type_: 'success',
207
+ _data_: data_1
208
+ });
198
209
  }
199
210
  if (type === 'async') {
200
211
  if (!mpApi) {
@@ -207,8 +218,14 @@ var WebViewBridge = /** @class */function () {
207
218
  });
208
219
  };
209
220
  this.webview = ctx;
210
- this.customApi = customApi;
221
+ this.config = config;
211
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
+ };
212
229
  return WebViewBridge;
213
230
  }();
214
231
  export { WebViewBridge };
package/package.json CHANGED
@@ -1,19 +1,18 @@
1
1
  {
2
2
  "name": "@hylid/call",
3
- "version": "2.12.0-alpha.41",
3
+ "version": "2.12.0-alpha.9",
4
4
  "main": "lib/index.js",
5
5
  "files": [
6
6
  "lib"
7
7
  ],
8
8
  "dependencies": {
9
- "@hylid/env": "^2.12.0-alpha.41",
10
- "@hylid/types": "^2.12.0-alpha.41"
9
+ "@hylid/env": "^2.12.0-alpha.9",
10
+ "@hylid/types": "^2.12.0-alpha.9"
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"
19
- }
17
+ "registry": "https://registry.npmjs.org/"
18
+ }