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

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/mpCall.d.ts CHANGED
@@ -1,4 +1,3 @@
1
1
  import { MPApi, PickMPArgs, Callback, PickMpReturns } from '@hylid/types';
2
- export declare function mpCall<T extends keyof MPApi>(apiName: T | ({} & string), options?: PickMPArgs<T> | Callback<PickMpReturns<T>>, config?: {
3
- type?: string;
4
- }): any;
2
+ import { JsApiConfig } from './types';
3
+ export declare function mpCall<T extends keyof MPApi>(apiName: T | ({} & string), options?: PickMPArgs<T> | Callback<PickMpReturns<T>>, config?: JsApiConfig): any;
@@ -1,3 +1,3 @@
1
1
  import { Message, MessageHandler } from './types';
2
2
  export declare function mpWebOnMessage(cb: MessageHandler): () => void;
3
- export declare function mpWebPostMessage(message: Message): void;
3
+ export declare function mpWebPostMessage(message: Message): any;
@@ -1,45 +1,5 @@
1
- var __rest = this && this.__rest || function (s, e) {
2
- var t = {};
3
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
4
- if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
5
- if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
6
- }
7
- return t;
8
- };
9
- /** 加载 appx 脚本 */
10
- var loadAppxBridge = function loadAppxBridge(cb) {
11
- var _a;
12
- try {
13
- // umd 包用的 IFF 模式,会立即向 window.my 上注入 jsapi,所以不能使用 window.my 判断是否已经加载了 appx 脚本
14
- // umd 版本和 modules 版本不能混用
15
- if ((_a = window.my) === null || _a === void 0 ? void 0 : _a.__hy_mounted__) {
16
- cb && cb("ready" /* BridgeReady.READY */);
17
- return;
18
- }
19
- var hyMy_1 = window.my || {};
20
- var script_1 = document.createElement('script');
21
- script_1.src = 'https://appx/web-view.min.js';
22
- script_1.onload = function onload() {
23
- var readyState = script_1['readyState'];
24
- if (typeof readyState === 'undefined' || /^(loaded|complete)$/.test(readyState)) {
25
- script_1.onload = null;
26
- // appx 脚本覆盖了 window 的 my 属性,这里再把 hylid-bridge 的 API 挂上去
27
- var postMessage_1 = hyMy_1.postMessage,
28
- api = __rest(hyMy_1, ["postMessage"]);
29
- Object.assign(window.my, api, {
30
- __hy_mounted__: true
31
- });
32
- cb && cb("loaded" /* BridgeReady.LOADED */);
33
- }
34
- };
35
- var head = document.getElementsByTagName('head')[0] || document.body;
36
- head.appendChild(script_1);
37
- } catch (e) {
38
- cb && cb(null);
39
- }
40
- };
1
+ import { alipayJSBridge } from "./webCall";
41
2
  var isListening = false;
42
- var WAITING_QUEUE = [];
43
3
  var MESSAGE_HANDLER = [];
44
4
  export function mpWebOnMessage(cb) {
45
5
  MESSAGE_HANDLER.push(cb);
@@ -50,25 +10,21 @@ export function mpWebOnMessage(cb) {
50
10
  };
51
11
  }
52
12
  export function mpWebPostMessage(message) {
53
- return loadAppxBridge(function (bridgeReady) {
54
- if (bridgeReady) {
55
- if (!isListening) {
56
- var messageHandler_1 = window.my.onMessage;
57
- window.my.onMessage = function (message) {
58
- MESSAGE_HANDLER.forEach(function (fn) {
59
- return fn(message);
60
- });
61
- if (messageHandler_1) messageHandler_1(message);
62
- };
63
- isListening = true;
64
- }
65
- if (WAITING_QUEUE.length) {
66
- WAITING_QUEUE.forEach(window.my.postMessage);
67
- WAITING_QUEUE = [];
68
- }
69
- window.my.postMessage(message);
70
- } else {
71
- WAITING_QUEUE.push(message);
72
- }
13
+ if (!isListening) {
14
+ document.addEventListener('onToWebViewMessage', function (e) {
15
+ var res = e.data.res;
16
+ if (!res || res.type !== 'message') return;
17
+ var data = JSON.parse(res.data);
18
+ MESSAGE_HANDLER.forEach(function (fn) {
19
+ return fn(data);
20
+ });
21
+ });
22
+ isListening = true;
23
+ }
24
+ return alipayJSBridge(function () {
25
+ window.AlipayJSBridge.call('postWebViewMessage', {
26
+ type: 'message',
27
+ detail: message
28
+ });
73
29
  });
74
30
  }
@@ -1,7 +1,7 @@
1
1
  import { MPApi } from '@hylid/types';
2
- import { MpWebJsApiConfig, MpWebJsApiOptions } from './types';
2
+ import { JsApiConfig, MpWebJsApiOptions } from './types';
3
3
  import { mpWebOnMessage, mpWebPostMessage } from './mpWebBridge';
4
- export declare function mpWebCall<T extends keyof MPApi>(api: T | ({} & string), options?: MpWebJsApiOptions<T>, config?: MpWebJsApiConfig): void;
4
+ export declare function mpWebCall<T extends keyof MPApi>(api: T | ({} & string), options?: MpWebJsApiOptions<T>, config?: JsApiConfig): void;
5
5
  export declare namespace mpWebCall {
6
6
  var onMessage: typeof mpWebOnMessage;
7
7
  var postMessage: typeof mpWebPostMessage;
package/lib/types.d.ts CHANGED
@@ -1,9 +1,9 @@
1
1
  import { JsApiType, MPApi, PickMPArgs, PickMpReturns } from '@hylid/types';
2
2
  export declare type JsApi = keyof MPApi;
3
- export interface MpWebJsApiConfig {
3
+ export interface JsApiConfig {
4
4
  type?: JsApiType;
5
5
  }
6
- export declare type Config = MpWebJsApiConfig;
6
+ export declare type Config = JsApiConfig;
7
7
  export interface MpWebMessageResult<T = any> {
8
8
  _type_: 'success' | 'fail';
9
9
  _data_: T;
@@ -14,7 +14,7 @@ export interface MpWebJsApiMessage {
14
14
  api: string;
15
15
  serialId: string;
16
16
  options: any;
17
- config?: MpWebJsApiConfig;
17
+ config?: JsApiConfig;
18
18
  result?: MpWebMessageResult;
19
19
  }
20
20
  export interface MpWebBroadcastMessage {
package/lib/utils.d.ts CHANGED
@@ -16,10 +16,15 @@ declare const ERROR: {
16
16
  code: number;
17
17
  message: (name: string) => string;
18
18
  };
19
+ PARAMS_ERROR: {
20
+ code: number;
21
+ message: (name: string) => string;
22
+ };
19
23
  };
20
- export declare const createError: (type: keyof typeof ERROR, name: string) => {
24
+ export declare const createError: (type: keyof typeof ERROR, name?: string) => {
21
25
  errorCode: number;
22
26
  errorMessage: string;
27
+ errorSouce: string;
23
28
  };
24
29
  export declare function createMessage(message: any, type?: 'success' | 'fail'): MpWebMessageResult;
25
30
  export {};
package/lib/utils.js CHANGED
@@ -22,15 +22,25 @@ var ERROR = {
22
22
  message: function message(name) {
23
23
  return "".concat(name, " exec fail, webviewBridge not bind context");
24
24
  }
25
+ },
26
+ PARAMS_ERROR: {
27
+ code: -5,
28
+ message: function message(name) {
29
+ return "".concat(name, " exec fail, params error");
30
+ }
25
31
  }
26
32
  };
27
33
  export var createError = function createError(type, name) {
34
+ if (name === void 0) {
35
+ name = '';
36
+ }
28
37
  var _a = ERROR[type],
29
38
  code = _a.code,
30
39
  message = _a.message;
31
40
  return {
32
41
  errorCode: code,
33
- errorMessage: message(name)
42
+ errorMessage: message(name),
43
+ errorSouce: 'hylid'
34
44
  };
35
45
  };
36
46
  export function createMessage(message, type) {
package/lib/webCall.d.ts CHANGED
@@ -1,4 +1,4 @@
1
1
  import { AliJsApi } from '@hylid/types';
2
- export declare function webCall<T extends keyof AliJsApi.Api>(apiName: T | ({} & string), options?: any, config?: {
3
- type?: string;
4
- }): void;
2
+ import { JsApiConfig } from './types';
3
+ export declare const alipayJSBridge: (cb: Function) => any;
4
+ export declare function webCall<T extends keyof AliJsApi.Api>(apiName: T | ({} & string), options?: any, config?: JsApiConfig): void;
package/lib/webCall.js CHANGED
@@ -1,11 +1,11 @@
1
- var alipayJSBridgeReady = function alipayJSBridgeReady(cb) {
1
+ export var alipayJSBridge = function alipayJSBridge(cb) {
2
2
  if (window.AlipayJSBridge) return cb();
3
3
  document.addEventListener('AlipayJSBridgeReady', function () {
4
4
  return cb();
5
5
  }, false);
6
6
  };
7
7
  export function webCall(apiName, options, config) {
8
- alipayJSBridgeReady(function () {
8
+ alipayJSBridge(function () {
9
9
  var _a = (config || {}).type,
10
10
  type = _a === void 0 ? 'async' : _a;
11
11
  if (type === 'async') {
@@ -71,7 +71,7 @@ var WebViewBridge = /** @class */function () {
71
71
  if (data.type !== 'pageEvent') return;
72
72
  var event = data.event;
73
73
  if (!event) {
74
- _this.sendMessage(data, createMessage(createError('NOTFOUND', event), 'fail'));
74
+ _this.sendMessage(data, createMessage(createError('PARAMS_ERROR'), 'fail'));
75
75
  return;
76
76
  }
77
77
  if (!_this.context) {
@@ -1,6 +1,3 @@
1
1
  export function _getCurrentPages() {
2
- if (typeof getCurrentPages === 'undefined') {
3
- return [];
4
- }
5
2
  return getCurrentPages();
6
3
  }
@@ -133,7 +133,7 @@ export function __hy_internal__(options) {
133
133
  _a = options || {}, success = _a.success, fail = _a.fail, name = _a.name, payload = _a.payload;
134
134
  jsapi = jsapis[name];
135
135
  if (!jsapi) {
136
- fail(createError('NOTFOUND', name));
136
+ fail(createError('NOTFOUND', "internal jsapi ".concat(name)));
137
137
  return [2 /*return*/];
138
138
  }
139
139
  _b.label = 1;
@@ -124,6 +124,13 @@ var __generator = this && this.__generator || function (thisArg, body) {
124
124
  }
125
125
  };
126
126
  import { createMessage, createError } from "../utils";
127
+ function getAttributeApi(apiName, jsapi) {
128
+ // my 上该 api 不存在
129
+ if (typeof jsapi === 'undefined') {
130
+ return createMessage(createError('NOTFOUND', apiName), 'fail');
131
+ }
132
+ return createMessage(jsapi);
133
+ }
127
134
  function getSyncApi(apiName, jsapi, options) {
128
135
  // 同步场景无该 API
129
136
  if (!jsapi) {
@@ -179,6 +186,9 @@ export var getApiResult = function getApiResult(jsapi, data) {
179
186
  if (type === 'async') {
180
187
  return [2 /*return*/, getAsyncApi(api, jsapi, options)];
181
188
  }
189
+ if (type === 'attribute') {
190
+ return [2 /*return*/, getAttributeApi(api, jsapi)];
191
+ }
182
192
  // 不识别的类型
183
193
  return [2 /*return*/, createMessage(createError('INVALID', api), 'fail')];
184
194
  });
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@hylid/call",
3
- "version": "3.2.0-alpha.4",
3
+ "version": "3.2.0-alpha.6",
4
4
  "main": "lib/index.js",
5
5
  "files": [
6
6
  "lib"
7
7
  ],
8
8
  "dependencies": {
9
- "@hylid/env": "^3.2.0-alpha.4",
10
- "@hylid/types": "^3.2.0-alpha.4"
9
+ "@hylid/env": "^3.2.0-alpha.6",
10
+ "@hylid/types": "^3.2.0-alpha.6"
11
11
  },
12
12
  "publishConfig": {
13
13
  "access": "public"
@@ -1,24 +0,0 @@
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 {};
@@ -1,229 +0,0 @@
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 };