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

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/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];
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') {
@@ -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
  }
@@ -66,7 +71,7 @@ var WebViewBridge = /** @class */function () {
66
71
  if (data.type !== 'pageEvent') return;
67
72
  var event = data.event;
68
73
  if (!event) {
69
- _this.sendMessage(data, createMessage(createError('NOTFOUND', event), 'fail'));
74
+ _this.sendMessage(data, createMessage(createError('PARAMS_ERROR'), 'fail'));
70
75
  return;
71
76
  }
72
77
  if (!_this.context) {
@@ -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,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;
@@ -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,13 @@ 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";
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
+ }
128
134
  function getSyncApi(apiName, jsapi, options) {
129
135
  // 同步场景无该 API
130
136
  if (!jsapi) {
@@ -154,37 +160,35 @@ function getAsyncApi(apiName, jsapi, _options) {
154
160
  jsapi(options);
155
161
  });
156
162
  }
157
- export function callbackApi(apiName, jsapi, cb) {
163
+ export function callbackApi(jsapi, data, cb) {
164
+ var api = (data || {}).api;
158
165
  // 无该 API
159
166
  if (!jsapi) {
160
- return cb(createMessage(createError('NOTFOUND', apiName), 'fail'));
167
+ return cb(createMessage(createError('NOTFOUND', api), 'fail'));
161
168
  }
162
169
  // 传进来的 API 不是函数
163
170
  if (typeof jsapi !== 'function') {
164
- return cb(createMessage(createError('INVALID', apiName), 'fail'));
171
+ return cb(createMessage(createError('INVALID', api), 'fail'));
165
172
  }
166
173
  jsapi(function (result) {
167
174
  return cb(createMessage(result));
168
175
  });
169
176
  }
170
- export var getApiResult = function getApiResult(customApi, data) {
171
- if (customApi === void 0) {
172
- customApi = {};
173
- }
177
+ export var getApiResult = function getApiResult(jsapi, data) {
174
178
  return __awaiter(void 0, void 0, void 0, function () {
175
- var api, options, config, _a, type, jsapi;
179
+ var api, options, config, _a, type;
176
180
  return __generator(this, function (_b) {
177
181
  api = data.api, options = data.options, config = data.config;
178
182
  _a = (config || {}).type, type = _a === void 0 ? 'async' : _a;
179
- jsapi = customApi[api] || {
180
- __hy_internal__: __hy_internal__
181
- }[api] || my[api];
182
183
  if (type === 'sync') {
183
184
  return [2 /*return*/, getSyncApi(api, jsapi, options)];
184
185
  }
185
186
  if (type === 'async') {
186
187
  return [2 /*return*/, getAsyncApi(api, jsapi, options)];
187
188
  }
189
+ if (type === 'attribute') {
190
+ return [2 /*return*/, getAttributeApi(api, jsapi)];
191
+ }
188
192
  // 不识别的类型
189
193
  return [2 /*return*/, createMessage(createError('INVALID', api), 'fail')];
190
194
  });
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.5",
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.5",
10
+ "@hylid/types": "^3.2.0-alpha.5"
11
11
  },
12
12
  "publishConfig": {
13
13
  "access": "public"