@hylid/call 3.2.0-alpha.2 → 3.2.0-alpha.4

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/mpWebCall.js CHANGED
@@ -7,7 +7,7 @@ mpWebOnMessage(function (msg) {
7
7
  var message = msg;
8
8
  var serialId = message.serialId;
9
9
  // 不是 jsapi 的调用的消息
10
- if (message.type !== 'apiCall') return;
10
+ if (message.type && message.type !== 'apiCall') return;
11
11
  if (!serialId) return;
12
12
  if (!callMap[serialId]) return;
13
13
  var callback = callMap[serialId];
@@ -8,6 +8,7 @@ var __assign = this && this.__assign || function () {
8
8
  };
9
9
  return __assign.apply(this, arguments);
10
10
  };
11
+ import { __hy_internal__ } from "./internal";
11
12
  import { callbackApi, getApiResult, isAllowed } from "./utils";
12
13
  import { createMessage, createError } from "../utils";
13
14
  var broadcastTargets = [];
@@ -32,6 +33,7 @@ var WebViewBridge = /** @class */function () {
32
33
  });
33
34
  };
34
35
  this.jsApiHandler = function (data) {
36
+ var _a;
35
37
  if (data.type !== 'apiCall') return;
36
38
  var config = data.config,
37
39
  api = data.api;
@@ -40,12 +42,15 @@ var WebViewBridge = /** @class */function () {
40
42
  if (!isAllowed(_this.config, api)) {
41
43
  return _this.sendMessage(data, createMessage(createError('NOTALLOWED', api), 'fail'));
42
44
  }
45
+ var jsapi = ((_a = _this.config.customApi) === null || _a === void 0 ? void 0 : _a[api]) || {
46
+ __hy_internal__: __hy_internal__
47
+ }[api] || my[api];
43
48
  if (type === 'callback') {
44
- callbackApi(api, data, function (result) {
49
+ callbackApi(jsapi, data, function (result) {
45
50
  return _this.sendMessage(data, result);
46
51
  });
47
52
  } else {
48
- getApiResult(_this.config.customApi, data).then(function (result) {
53
+ getApiResult(jsapi, data).then(function (result) {
49
54
  _this.sendMessage(data, result);
50
55
  });
51
56
  }
@@ -76,7 +81,7 @@ var WebViewBridge = /** @class */function () {
76
81
  // 已经监听过了
77
82
  if ((_a = _this.context[event]) === null || _a === void 0 ? void 0 : _a._hy_listener_) return;
78
83
  _this.bindCtxEvent(event, function (payload) {
79
- return _this.sendMessage(data, createMessage(payload));
84
+ return _this.sendMessage(data, payload);
80
85
  });
81
86
  _this.context[event]._hy_listener_ = true;
82
87
  };
@@ -1,4 +1,4 @@
1
- import { MpWebJsApiMessage, CustomApi, MpWebMessageResult, WebviewBridgeConfig } from '../types';
2
- export declare function callbackApi(apiName: string, jsapi: any, cb: (msg: MpWebMessageResult) => void): void;
3
- export declare const getApiResult: (customApi: CustomApi | undefined, data: MpWebJsApiMessage) => Promise<MpWebMessageResult>;
1
+ import { MpWebJsApiMessage, MpWebMessageResult, WebviewBridgeConfig } from '../types';
2
+ export declare function callbackApi(jsapi: any, data: MpWebJsApiMessage, cb: (msg: MpWebMessageResult) => void): void;
3
+ export declare const getApiResult: (jsapi: any, data: MpWebJsApiMessage) => Promise<MpWebMessageResult>;
4
4
  export declare function isAllowed(config: WebviewBridgeConfig, api: string): boolean;
@@ -124,7 +124,6 @@ var __generator = this && this.__generator || function (thisArg, body) {
124
124
  }
125
125
  };
126
126
  import { createMessage, createError } from "../utils";
127
- import { __hy_internal__ } from "./internal";
128
127
  function getSyncApi(apiName, jsapi, options) {
129
128
  // 同步场景无该 API
130
129
  if (!jsapi) {
@@ -154,31 +153,26 @@ function getAsyncApi(apiName, jsapi, _options) {
154
153
  jsapi(options);
155
154
  });
156
155
  }
157
- export function callbackApi(apiName, jsapi, cb) {
156
+ export function callbackApi(jsapi, data, cb) {
157
+ var api = (data || {}).api;
158
158
  // 无该 API
159
159
  if (!jsapi) {
160
- return cb(createMessage(createError('NOTFOUND', apiName), 'fail'));
160
+ return cb(createMessage(createError('NOTFOUND', api), 'fail'));
161
161
  }
162
162
  // 传进来的 API 不是函数
163
163
  if (typeof jsapi !== 'function') {
164
- return cb(createMessage(createError('INVALID', apiName), 'fail'));
164
+ return cb(createMessage(createError('INVALID', api), 'fail'));
165
165
  }
166
166
  jsapi(function (result) {
167
167
  return cb(createMessage(result));
168
168
  });
169
169
  }
170
- export var getApiResult = function getApiResult(customApi, data) {
171
- if (customApi === void 0) {
172
- customApi = {};
173
- }
170
+ export var getApiResult = function getApiResult(jsapi, data) {
174
171
  return __awaiter(void 0, void 0, void 0, function () {
175
- var api, options, config, _a, type, jsapi;
172
+ var api, options, config, _a, type;
176
173
  return __generator(this, function (_b) {
177
174
  api = data.api, options = data.options, config = data.config;
178
175
  _a = (config || {}).type, type = _a === void 0 ? 'async' : _a;
179
- jsapi = customApi[api] || {
180
- __hy_internal__: __hy_internal__
181
- }[api] || my[api];
182
176
  if (type === 'sync') {
183
177
  return [2 /*return*/, getSyncApi(api, jsapi, options)];
184
178
  }
@@ -0,0 +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
+ }
14
+ export declare class WebViewBridge {
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>;
23
+ }
24
+ export {};
@@ -0,0 +1,229 @@
1
+ var __assign = this && this.__assign || function () {
2
+ __assign = Object.assign || function (t) {
3
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
4
+ s = arguments[i];
5
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
6
+ }
7
+ return t;
8
+ };
9
+ return __assign.apply(this, arguments);
10
+ };
11
+ var __awaiter = this && this.__awaiter || function (thisArg, _arguments, P, generator) {
12
+ function adopt(value) {
13
+ return value instanceof P ? value : new P(function (resolve) {
14
+ resolve(value);
15
+ });
16
+ }
17
+ return new (P || (P = Promise))(function (resolve, reject) {
18
+ function fulfilled(value) {
19
+ try {
20
+ step(generator.next(value));
21
+ } catch (e) {
22
+ reject(e);
23
+ }
24
+ }
25
+ function rejected(value) {
26
+ try {
27
+ step(generator["throw"](value));
28
+ } catch (e) {
29
+ reject(e);
30
+ }
31
+ }
32
+ function step(result) {
33
+ result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
34
+ }
35
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
36
+ });
37
+ };
38
+ var __generator = this && this.__generator || function (thisArg, body) {
39
+ var _ = {
40
+ label: 0,
41
+ sent: function sent() {
42
+ if (t[0] & 1) throw t[1];
43
+ return t[1];
44
+ },
45
+ trys: [],
46
+ ops: []
47
+ },
48
+ f,
49
+ y,
50
+ t,
51
+ g;
52
+ return g = {
53
+ next: verb(0),
54
+ "throw": verb(1),
55
+ "return": verb(2)
56
+ }, typeof Symbol === "function" && (g[Symbol.iterator] = function () {
57
+ return this;
58
+ }), g;
59
+ function verb(n) {
60
+ return function (v) {
61
+ return step([n, v]);
62
+ };
63
+ }
64
+ function step(op) {
65
+ if (f) throw new TypeError("Generator is already executing.");
66
+ while (_) try {
67
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
68
+ if (y = 0, t) op = [op[0] & 2, t.value];
69
+ switch (op[0]) {
70
+ case 0:
71
+ case 1:
72
+ t = op;
73
+ break;
74
+ case 4:
75
+ _.label++;
76
+ return {
77
+ value: op[1],
78
+ done: false
79
+ };
80
+ case 5:
81
+ _.label++;
82
+ y = op[1];
83
+ op = [0];
84
+ continue;
85
+ case 7:
86
+ op = _.ops.pop();
87
+ _.trys.pop();
88
+ continue;
89
+ default:
90
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
91
+ _ = 0;
92
+ continue;
93
+ }
94
+ if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
95
+ _.label = op[1];
96
+ break;
97
+ }
98
+ if (op[0] === 6 && _.label < t[1]) {
99
+ _.label = t[1];
100
+ t = op;
101
+ break;
102
+ }
103
+ if (t && _.label < t[2]) {
104
+ _.label = t[2];
105
+ _.ops.push(op);
106
+ break;
107
+ }
108
+ if (t[2]) _.ops.pop();
109
+ _.trys.pop();
110
+ continue;
111
+ }
112
+ op = body.call(thisArg, _);
113
+ } catch (e) {
114
+ op = [6, e];
115
+ y = 0;
116
+ } finally {
117
+ f = t = 0;
118
+ }
119
+ if (op[0] & 5) throw op[1];
120
+ return {
121
+ value: op[0] ? op[1] : void 0,
122
+ done: true
123
+ };
124
+ }
125
+ };
126
+ var WebViewBridge = /** @class */function () {
127
+ function WebViewBridge(ctx, config) {
128
+ if (config === void 0) {
129
+ config = {};
130
+ }
131
+ var _this = this;
132
+ this.config = {};
133
+ this.listen = function (data) {
134
+ return __awaiter(_this, void 0, void 0, function () {
135
+ var command, config, api, type, notAllowedMessage;
136
+ var _this = this;
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
+ if (type === 'callback') {
157
+ // @ts-ignore
158
+ my[api](function (result) {
159
+ _this.webview.postMessage(__assign(__assign({}, command), {
160
+ result: result
161
+ }));
162
+ });
163
+ } else {
164
+ this.handleApi(command).then(function (result) {
165
+ _this.webview.postMessage(__assign(__assign({}, command), {
166
+ result: result
167
+ }));
168
+ });
169
+ }
170
+ // 表示被 hylid-bridge 消费了
171
+ return [2 /*return*/, true];
172
+ }
173
+ return [2 /*return*/];
174
+ });
175
+ });
176
+ };
177
+ this.handleApi = function (data) {
178
+ var api = data.api,
179
+ _options = data.options,
180
+ config = data.config;
181
+ var _a = (config || {}).type,
182
+ type = _a === void 0 ? 'async' : _a;
183
+ return new Promise(function (resolve) {
184
+ var _a;
185
+ var options = __assign(__assign({}, _options), {
186
+ success: function success(res) {
187
+ resolve({
188
+ _type_: 'success',
189
+ _data_: res
190
+ });
191
+ },
192
+ fail: function fail(res) {
193
+ resolve({
194
+ _type_: 'fail',
195
+ _data_: res
196
+ });
197
+ }
198
+ });
199
+ // @ts-ignore
200
+ var mpApi = ((_a = _this.config.customApi) === null || _a === void 0 ? void 0 : _a[api]) || my[api];
201
+ if (type === 'sync') {
202
+ var data_1 = mpApi(_options);
203
+ resolve({
204
+ _type_: 'success',
205
+ _data_: data_1
206
+ });
207
+ }
208
+ if (type === 'async') {
209
+ if (!mpApi) {
210
+ // @ts-ignore
211
+ my.call(api, options);
212
+ } else {
213
+ mpApi(options);
214
+ }
215
+ }
216
+ });
217
+ };
218
+ this.webview = ctx;
219
+ this.config = config;
220
+ }
221
+ WebViewBridge.prototype.isAllowed = function (api) {
222
+ // 黑白名单校验
223
+ if (this.config.whitelist && !this.config.whitelist.includes(api)) return false;
224
+ if (this.config.blacklist && this.config.blacklist.includes(api)) return false;
225
+ return true;
226
+ };
227
+ return WebViewBridge;
228
+ }();
229
+ export { WebViewBridge };
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@hylid/call",
3
- "version": "3.2.0-alpha.2",
3
+ "version": "3.2.0-alpha.4",
4
4
  "main": "lib/index.js",
5
5
  "files": [
6
6
  "lib"
7
7
  ],
8
8
  "dependencies": {
9
- "@hylid/env": "^3.2.0-alpha.2",
10
- "@hylid/types": "^3.2.0-alpha.2"
9
+ "@hylid/env": "^3.2.0-alpha.4",
10
+ "@hylid/types": "^3.2.0-alpha.4"
11
11
  },
12
12
  "publishConfig": {
13
13
  "access": "public"