@hylid/call 3.0.0-alpha.13 → 3.0.0-alpha.14

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,7 +1,8 @@
1
1
  var loadAppxBridge = function loadAppxBridge(cb) {
2
2
  var _a;
3
3
  try {
4
- // umd 包用的 IFF 模式,会立即向 window.my 上注入 jsapi,所以不能试用 window.my 判断是否已经加载了 appx 脚本
4
+ // umd 包用的 IFF 模式,会立即向 window.my 上注入 jsapi,所以不能使用 window.my 判断是否已经加载了 appx 脚本
5
+ // umd 版本和 modules 版本不能混用
5
6
  if ((_a = window.my) === null || _a === void 0 ? void 0 : _a.__hy_mounted__) {
6
7
  cb && cb("ready" /* BridgeReady.READY */);
7
8
  return;
@@ -40,7 +41,7 @@ var callbackFn = function callbackFn(res, callback) {
40
41
  if (_type_ === 'fail') fail === null || fail === void 0 ? void 0 : fail(_data_);
41
42
  return complete === null || complete === void 0 ? void 0 : complete(_data_);
42
43
  }
43
- // 下面兼容老版本
44
+ // 下面兼容老版本(老版本消息识别不明确,导致 H5 中走 success/fail 逻辑可能岔了)
44
45
  // @ts-ignore
45
46
  if (res === null || res === void 0 ? void 0 : res.error) {
46
47
  fail === null || fail === void 0 ? void 0 : fail(res);
@@ -56,11 +57,13 @@ export var mpWebCall = function () {
56
57
  function onMessage() {
57
58
  var messageHandler = window.my.onMessage;
58
59
  window.my.onMessage = function (message) {
60
+ var _a;
59
61
  var serialId = message.serialId;
60
62
  var data = message.result;
61
- var type = message.type;
63
+ var type = (_a = message.config) === null || _a === void 0 ? void 0 : _a.type; // JsApi 的类型
62
64
  if (serialId) {
63
65
  var callback = callMap[serialId];
66
+ if (!callback) return;
64
67
  if (type === 'callback') {
65
68
  callback(data);
66
69
  } else {
@@ -73,27 +76,28 @@ export var mpWebCall = function () {
73
76
  };
74
77
  }
75
78
  function fireMessage(_a) {
76
- var apiName = _a.apiName,
79
+ var api = _a.api,
77
80
  options = _a.options,
78
81
  config = _a.config;
79
82
  var random = Math.floor(Math.random() * 1000000);
80
- var serialId = apiName + '_' + random;
83
+ var serialId = api + '_' + random;
81
84
  if (options) {
82
85
  callMap[serialId] = options;
83
86
  }
84
87
  var params = {
88
+ source: 'hylid',
85
89
  type: 'apiCall',
86
- api: apiName,
90
+ api: api,
87
91
  serialId: serialId,
88
92
  options: options,
89
93
  config: config
90
94
  };
91
95
  window.my.postMessage(params);
92
96
  }
93
- return function call(apiName, options, config) {
97
+ return function call(api, options, config) {
94
98
  loadAppxBridge(function (bridgeReady) {
95
99
  var params = {
96
- apiName: apiName,
100
+ api: api,
97
101
  options: options,
98
102
  config: config
99
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];
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[api_1](function (result) {
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
- 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:
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.customApi = customApi;
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,13 +1,13 @@
1
1
  {
2
2
  "name": "@hylid/call",
3
- "version": "3.0.0-alpha.13",
3
+ "version": "3.0.0-alpha.14",
4
4
  "main": "lib/index.js",
5
5
  "files": [
6
6
  "lib"
7
7
  ],
8
8
  "dependencies": {
9
- "@hylid/env": "^3.0.0-alpha.13",
10
- "@hylid/types": "^3.0.0-alpha.13"
9
+ "@hylid/env": "^3.0.0-alpha.14",
10
+ "@hylid/types": "^3.0.0-alpha.14"
11
11
  },
12
12
  "publishConfig": {
13
13
  "access": "public"