@hylid/call 4.0.0-alpha.8 → 4.0.0

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
@@ -1,12 +1,12 @@
1
1
  /// <reference types="miniprogram" />
2
2
  import { MPApi, PickMPArgs, PickMpReturns, Callback } from '@hylid/types';
3
3
  import { mpCall } from './mpCall';
4
- import { mpWebCall } from './mpWebCall';
5
- import { webCall } from './webCall';
4
+ import { mpWebCall, mpWebCallAsync } from './mpWebCall';
5
+ import { webCall, webCallAsync } from './webCall';
6
6
  import { notFound } from './notFound';
7
7
  import { Config } from './types';
8
8
  export declare function call<T extends keyof MPApi>(name: T | ({} & string), options?: PickMPArgs<T> | Common | Callback<PickMpReturns<T>>, config?: Config): any;
9
9
  export declare function createWebviewSrc(url: string): string;
10
10
  export { WebViewBridge } from './webviewBridge';
11
- export { mpCall, mpWebCall, webCall, notFound };
11
+ export { mpCall, mpWebCall, webCall, notFound, mpWebCallAsync, webCallAsync };
12
12
  export { promisify } from './promisify';
package/lib/index.js CHANGED
@@ -1,8 +1,8 @@
1
1
  var _a;
2
2
  import { platform, PLATFORM } from '@hylid/env';
3
3
  import { mpCall } from "./mpCall";
4
- import { mpWebCall } from "./mpWebCall";
5
- import { webCall } from "./webCall";
4
+ import { mpWebCall, mpWebCallAsync } from "./mpWebCall";
5
+ import { webCall, webCallAsync } from "./webCall";
6
6
  import { notFound } from "./notFound";
7
7
  var callMap = (_a = {}, _a[PLATFORM.MP] = mpCall, _a[PLATFORM.MPWEB] = mpWebCall, _a[PLATFORM.WEB] = webCall, _a);
8
8
  export function call(name, options, config) {
@@ -24,7 +24,6 @@ export function createWebviewSrc(url) {
24
24
  var newUrl = url + separator + '__app__=' + app + hash;
25
25
  return newUrl;
26
26
  }
27
- ;
28
27
  export { WebViewBridge } from "./webviewBridge";
29
- export { mpCall, mpWebCall, webCall, notFound };
28
+ export { mpCall, mpWebCall, webCall, notFound, mpWebCallAsync, webCallAsync };
30
29
  export { promisify } from "./promisify";
package/lib/mpWebCall.js CHANGED
@@ -138,13 +138,43 @@ export var mpWebCallAsync = function () {
138
138
  var isListening = false;
139
139
  function onMessage() {
140
140
  var messageHandler = function messageHandler(message) {
141
+ var _a;
141
142
  var serialId = message.serialId;
143
+ if (!serialId) return;
144
+ if (!callMap[serialId]) return;
145
+ var callback = callMap[serialId];
142
146
  var data = message.result;
143
- if (serialId) {
144
- var callback = callMap[serialId];
145
- callbackFn(data, callback);
146
- delete callMap[serialId];
147
+ var type = (_a = message.config) === null || _a === void 0 ? void 0 : _a.type;
148
+ if (type === 'callback') {
149
+ var _b = data || {},
150
+ _data_2 = _b._data_,
151
+ _type_2 = _b._type_;
152
+ if (_type_2 === 'success') return callback(_data_2);
153
+ return callback(undefined, _data_2);
147
154
  }
155
+ var _c = callback || {},
156
+ success = _c.success,
157
+ fail = _c.fail,
158
+ complete = _c.complete;
159
+ var _d = data || {},
160
+ _data_ = _d._data_,
161
+ _type_ = _d._type_;
162
+ // 新版本协议
163
+ if (_type_) {
164
+ if (_type_ === 'success') success === null || success === void 0 ? void 0 : success(_data_);
165
+ if (_type_ === 'fail') fail === null || fail === void 0 ? void 0 : fail(_data_);
166
+ complete === null || complete === void 0 ? void 0 : complete(_data_);
167
+ } else {
168
+ // 下面兼容老版本(老版本消息识别不明确,导致 H5 中走 success/fail 逻辑可能岔了)
169
+ // @ts-ignore
170
+ if (data === null || data === void 0 ? void 0 : data.error) {
171
+ fail === null || fail === void 0 ? void 0 : fail(data);
172
+ } else {
173
+ success === null || success === void 0 ? void 0 : success(data);
174
+ }
175
+ complete === null || complete === void 0 ? void 0 : complete(data);
176
+ }
177
+ delete callMap[serialId];
148
178
  };
149
179
  // 代码参考:https://alex.alipay.com/antcode/qianyu.fzy/af-appx/blob/r20191223-1.24.0/src/web-view/embed/index.tsx
150
180
  document.addEventListener('onToWebViewMessage', function (e) {
package/lib/webCall.d.ts CHANGED
@@ -1,6 +1,5 @@
1
- import { AliJsApi } from '@hylid/types';
1
+ import { AliJsApi, AsyncCallback } from '@hylid/types';
2
2
  import { JsApiConfig } from './types';
3
- import { AsyncCallback } from '@hylid/types';
4
3
  export declare const alipayJSBridge: (cb: Function) => any;
5
4
  export declare function webCall<T extends keyof AliJsApi.Api>(apiName: T | ({} & string), options?: any, config?: JsApiConfig): void;
6
5
  export declare function webCallAsync<T = any>(apiName: string, options?: AsyncCallback<T> & any, config?: {
@@ -153,7 +153,7 @@ function getAsyncApi(apiName, jsapi, _options) {
153
153
  }
154
154
  });
155
155
  // @ts-ignore
156
- if (!jsapi) return my.call(api, options);
156
+ if (!jsapi) return my.call(apiName, options);
157
157
  if (typeof jsapi !== 'function') {
158
158
  return options.fail(createError('INVALID', apiName));
159
159
  }
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@hylid/call",
3
- "version": "4.0.0-alpha.8",
3
+ "version": "4.0.0",
4
4
  "main": "lib/index.js",
5
5
  "files": [
6
6
  "lib"
7
7
  ],
8
8
  "dependencies": {
9
- "@hylid/env": "^4.0.0-alpha.8",
10
- "@hylid/types": "^4.0.0-alpha.8"
9
+ "@hylid/env": "^4.0.0",
10
+ "@hylid/types": "^4.0.0"
11
11
  },
12
12
  "publishConfig": {
13
13
  "access": "public"