@hylid/call 0.0.90011288758-dev.2 → 0.0.90011293572-dev.2

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.
@@ -11,25 +11,17 @@ export function mpWebOnMessage(cb) {
11
11
  }
12
12
  export function mpWebPostMessage(message) {
13
13
  if (!isListening) {
14
- // SSR 兼容:检查 document 是否可用
15
- if (typeof document !== 'undefined') {
16
- document.addEventListener('onToWebViewMessage', function (e) {
17
- var res = e.data.res;
18
- if (!res || res.type !== 'message') return;
19
- var data = JSON.parse(res.data);
20
- MESSAGE_HANDLER.forEach(function (fn) {
21
- return fn(data);
22
- });
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);
23
20
  });
24
- }
21
+ });
25
22
  isListening = true;
26
23
  }
27
24
  return alipayJSBridge(function () {
28
- // SSR 兼容:检查 AlipayJSBridge 是否可用
29
- if (typeof window === 'undefined' || !window.AlipayJSBridge) {
30
- console.warn('[mpWebPostMessage] AlipayJSBridge not available in SSR environment');
31
- return;
32
- }
33
25
  window.AlipayJSBridge.call('postWebViewMessage', {
34
26
  type: 'message',
35
27
  detail: message
package/lib/mpWebCall.js CHANGED
@@ -72,7 +72,8 @@ export function mpWebCall(api, options, config) {
72
72
  serialId: serialId,
73
73
  options: options,
74
74
  config: config,
75
- isSync: config === null || config === void 0 ? void 0 : config.isSync
75
+ isSync: config === null || config === void 0 ? void 0 : config.isSync,
76
+ useMyCallToInvoke: config === null || config === void 0 ? void 0 : config.useMyCallToInvoke
76
77
  };
77
78
  mpWebPostMessage(message);
78
79
  // 设置 jsapi 通信超时时间
@@ -86,17 +87,10 @@ export function mpWebCall(api, options, config) {
86
87
  }
87
88
  }
88
89
  function broadcastGlobalData(key, result) {
89
- // SSR 兼容:安全获取当前 URL
90
- var getCurrentUrl = function getCurrentUrl() {
91
- if (typeof window !== 'undefined' && window.location) {
92
- return window.location.href;
93
- }
94
- return 'unknown'; // SSR 环境默认值
95
- };
96
90
  var message = {
97
91
  source: 'hylid',
98
92
  type: 'broadcast',
99
- from: getCurrentUrl(),
93
+ from: window.location.href,
100
94
  key: key,
101
95
  result: result
102
96
  };
@@ -197,16 +191,13 @@ export var mpWebCallAsync = function () {
197
191
  delete callMap[serialId];
198
192
  };
199
193
  // 代码参考:https://alex.alipay.com/antcode/qianyu.fzy/af-appx/blob/r20191223-1.24.0/src/web-view/embed/index.tsx
200
- // SSR 兼容:安全的 document 事件监听
201
- if (typeof document !== 'undefined') {
202
- document.addEventListener('onToWebViewMessage', function (e) {
203
- var res = e.data.res;
204
- if (res.type === 'message') {
205
- messageHandler(JSON.parse(res.data));
206
- }
207
- // TODO: 参考的代码中还有 response 的类型,需要考虑一下是否实现,对应 api 为 'navigateTo','navigateBack','switchTab','reLaunch','redirectTo',
208
- });
209
- }
194
+ document.addEventListener('onToWebViewMessage', function (e) {
195
+ var res = e.data.res;
196
+ if (res.type === 'message') {
197
+ messageHandler(JSON.parse(res.data));
198
+ }
199
+ // TODO: 参考的代码中还有 response 的类型,需要考虑一下是否实现,对应 api 为 'navigateTo','navigateBack','switchTab','reLaunch','redirectTo',
200
+ });
210
201
  }
211
202
  function fireMessage(_a) {
212
203
  var _b;
package/lib/types.d.ts CHANGED
@@ -10,6 +10,7 @@ export interface JsApiConfig {
10
10
  * 套壳 jsapi 调用,通信超时时间。type = "callback" 时不生效
11
11
  */
12
12
  timeout?: number;
13
+ useMyCallToInvoke?: boolean;
13
14
  }
14
15
  export declare type Config = JsApiConfig;
15
16
  export interface MpWebMessageResult<T = any> {
@@ -28,6 +29,10 @@ export interface MpWebJsApiMessage {
28
29
  * @deprecated 优先使用 config.type = "sync" 配置
29
30
  */
30
31
  isSync?: boolean;
32
+ /**
33
+ * 使用my.call来调用JSAPI
34
+ */
35
+ useMyCallToInvoke?: boolean;
31
36
  }
32
37
  export interface MpWebBroadcastMessage {
33
38
  source: 'hylid';
package/lib/webCall.js CHANGED
@@ -1,29 +1,13 @@
1
1
  export var alipayJSBridge = function alipayJSBridge(cb) {
2
- // SSR 兼容:安全的 window 和 document 访问
3
- if (typeof window === 'undefined') {
4
- console.warn('[alipayJSBridge] window not available in SSR environment');
5
- return;
6
- }
7
2
  if (window.AlipayJSBridge) return cb();
8
- if (typeof document !== 'undefined') {
9
- document.addEventListener('AlipayJSBridgeReady', function () {
10
- return cb();
11
- }, false);
12
- }
3
+ document.addEventListener('AlipayJSBridgeReady', function () {
4
+ return cb();
5
+ }, false);
13
6
  };
14
7
  export function webCall(apiName, options, config) {
15
8
  alipayJSBridge(function () {
16
9
  var _a = (config || {}).type,
17
10
  type = _a === void 0 ? 'async' : _a;
18
- // SSR 兼容:检查 AlipayJSBridge 是否可用
19
- if (typeof window === 'undefined' || !window.AlipayJSBridge) {
20
- console.warn('[webCall] AlipayJSBridge not available in SSR environment');
21
- var fail_1 = (options || {}).fail;
22
- fail_1 === null || fail_1 === void 0 ? void 0 : fail_1({
23
- error: 'AlipayJSBridge not available'
24
- });
25
- return;
26
- }
27
11
  if (type === 'async') {
28
12
  window.AlipayJSBridge.call(apiName, options, function (res) {
29
13
  var _a = options || {},
@@ -52,16 +36,6 @@ export function webCallAsync(apiName, options, config) {
52
36
  complete = _a.complete;
53
37
  var _b = (config || {}).timeout,
54
38
  timeout = _b === void 0 ? 20000 : _b;
55
- // SSR 兼容:检查 AlipayJSBridge 是否可用
56
- if (typeof window === 'undefined' || !window.AlipayJSBridge) {
57
- var err = {
58
- error: -102,
59
- errorMessage: "[".concat(apiName, "]: AlipayJSBridge not available in SSR environment")
60
- };
61
- reject(err);
62
- fail === null || fail === void 0 ? void 0 : fail(err);
63
- return;
64
- }
65
39
  var timer;
66
40
  if (timeout > 0) {
67
41
  timer = setTimeout(function () {
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@hylid/call",
3
- "version": "0.0.90011288758-dev.2",
3
+ "version": "0.0.90011293572-dev.2",
4
4
  "main": "lib/index.js",
5
5
  "files": [
6
6
  "lib"
7
7
  ],
8
8
  "dependencies": {
9
- "@hylid/env": "^0.0.90011288758-dev.2",
10
- "@hylid/types": "^0.0.90011288758-dev.2"
9
+ "@hylid/env": "^0.0.90011293572-dev.2",
10
+ "@hylid/types": "^0.0.90011293572-dev.2"
11
11
  },
12
12
  "publishConfig": {
13
13
  "access": "public"