@hylid/call 0.0.2-dev.1
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/README.md +17 -0
- package/lib/index.d.ts +16 -0
- package/lib/index.js +37 -0
- package/lib/mpCall.d.ts +3 -0
- package/lib/mpCall.js +26 -0
- package/lib/mpWebBridge.d.ts +3 -0
- package/lib/mpWebBridge.js +30 -0
- package/lib/mpWebCall.d.ts +16 -0
- package/lib/mpWebCall.js +274 -0
- package/lib/notFound.d.ts +1 -0
- package/lib/notFound.js +17 -0
- package/lib/promisify.d.ts +5 -0
- package/lib/promisify.js +29 -0
- package/lib/types.d.ts +63 -0
- package/lib/types.js +1 -0
- package/lib/utils.d.ts +34 -0
- package/lib/utils.js +60 -0
- package/lib/webCall.d.ts +7 -0
- package/lib/webCall.js +68 -0
- package/lib/webviewBridge/index.d.ts +26 -0
- package/lib/webviewBridge/index.js +129 -0
- package/lib/webviewBridge/internal/canIUse.d.ts +3 -0
- package/lib/webviewBridge/internal/canIUse.js +4 -0
- package/lib/webviewBridge/internal/getCurrentPages.d.ts +3 -0
- package/lib/webviewBridge/internal/getCurrentPages.js +8 -0
- package/lib/webviewBridge/internal/index.d.ts +18 -0
- package/lib/webviewBridge/internal/index.js +156 -0
- package/lib/webviewBridge/internal/memory.d.ts +10 -0
- package/lib/webviewBridge/internal/memory.js +12 -0
- package/lib/webviewBridge/utils.d.ts +4 -0
- package/lib/webviewBridge/utils.js +205 -0
- package/lib/webviewBridge/webview-bridge-component/index.axml +1 -0
- package/lib/webviewBridge/webview-bridge-component/index.d.ts +1 -0
- package/lib/webviewBridge/webview-bridge-component/index.js +40 -0
- package/lib/webviewBridge/webview-bridge-component/index.json +3 -0
- package/package.json +18 -0
package/README.md
ADDED
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/// <reference types="miniprogram" />
|
|
2
|
+
import { MPApi, PickMPArgs, PickMpReturns, Callback } from '@hylid/types';
|
|
3
|
+
import { mpCall } from './mpCall';
|
|
4
|
+
import { mpWebCall, mpWebCallAsync } from './mpWebCall';
|
|
5
|
+
import { webCall, webCallAsync } from './webCall';
|
|
6
|
+
import { notFound } from './notFound';
|
|
7
|
+
import { Config } from './types';
|
|
8
|
+
export declare function call<T extends keyof MPApi>(name: T | ({} & string), options?: PickMPArgs<T> | Common | Callback<PickMpReturns<T>>, config?: Config): any;
|
|
9
|
+
export declare function callAsync<T = any>(apiName: string, options?: AsyncCallback<T> & any, config?: {
|
|
10
|
+
timeout: number;
|
|
11
|
+
isSync?: boolean;
|
|
12
|
+
}): Promise<any>;
|
|
13
|
+
export declare function createWebviewSrc(url: string): string;
|
|
14
|
+
export { WebViewBridge } from './webviewBridge';
|
|
15
|
+
export { mpCall, mpWebCall, webCall, notFound, mpWebCallAsync, webCallAsync };
|
|
16
|
+
export { promisify } from './promisify';
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
var _a, _b;
|
|
2
|
+
import { platform, PLATFORM } from '@hylid/env';
|
|
3
|
+
import { mpCall } from "./mpCall";
|
|
4
|
+
import { mpWebCall, mpWebCallAsync } from "./mpWebCall";
|
|
5
|
+
import { webCall, webCallAsync } from "./webCall";
|
|
6
|
+
import { notFound } from "./notFound";
|
|
7
|
+
var callMap = (_a = {}, _a[PLATFORM.MP] = mpCall, _a[PLATFORM.MPWEB] = mpWebCall, _a[PLATFORM.WEB] = webCall, _a);
|
|
8
|
+
var callMapAsync = (_b = {}, _b[PLATFORM.MPWEB] = mpWebCallAsync, _b[PLATFORM.WEB] = webCallAsync, _b);
|
|
9
|
+
export function call(name, options, config) {
|
|
10
|
+
if (platform) {
|
|
11
|
+
return callMap[platform](name, options, config);
|
|
12
|
+
} else {
|
|
13
|
+
return notFound(options, config);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
export function callAsync(apiName, options, config) {
|
|
17
|
+
if (platform) {
|
|
18
|
+
return callMapAsync[platform](apiName, options, config);
|
|
19
|
+
} else {
|
|
20
|
+
return Promise.reject(notFound(options, config));
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
export function createWebviewSrc(url) {
|
|
24
|
+
var app = my.getSystemInfoSync().app;
|
|
25
|
+
var hashIndex = url.indexOf('#');
|
|
26
|
+
var hash = '';
|
|
27
|
+
if (hashIndex !== -1) {
|
|
28
|
+
hash = url.slice(hashIndex);
|
|
29
|
+
url = url.slice(0, hashIndex);
|
|
30
|
+
}
|
|
31
|
+
var separator = url.includes('?') ? '&' : '?';
|
|
32
|
+
var newUrl = url + separator + '__app__=' + app + hash;
|
|
33
|
+
return newUrl;
|
|
34
|
+
}
|
|
35
|
+
export { WebViewBridge } from "./webviewBridge";
|
|
36
|
+
export { mpCall, mpWebCall, webCall, notFound, mpWebCallAsync, webCallAsync };
|
|
37
|
+
export { promisify } from "./promisify";
|
package/lib/mpCall.d.ts
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import { MPApi, PickMPArgs, Callback, PickMpReturns } from '@hylid/types';
|
|
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;
|
package/lib/mpCall.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export function mpCall(apiName, options, config) {
|
|
2
|
+
var _a = (config || {}).type,
|
|
3
|
+
type = _a === void 0 ? 'async' : _a;
|
|
4
|
+
if (type === 'sync' || (config === null || config === void 0 ? void 0 : config.isSync)) {
|
|
5
|
+
// @ts-ignore
|
|
6
|
+
return my[apiName](options);
|
|
7
|
+
}
|
|
8
|
+
if (type === 'async') {
|
|
9
|
+
// @ts-ignore
|
|
10
|
+
if (my[apiName]) {
|
|
11
|
+
// @ts-ignore
|
|
12
|
+
my[apiName](options);
|
|
13
|
+
} else {
|
|
14
|
+
// @ts-ignore
|
|
15
|
+
my.call(apiName, options);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
if (type === 'callback') {
|
|
19
|
+
// @ts-ignore
|
|
20
|
+
my[apiName](options);
|
|
21
|
+
}
|
|
22
|
+
if (type === 'attribute') {
|
|
23
|
+
// @ts-ignore
|
|
24
|
+
return my[apiName];
|
|
25
|
+
}
|
|
26
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { alipayJSBridge } from "./webCall";
|
|
2
|
+
var isListening = false;
|
|
3
|
+
var MESSAGE_HANDLER = [];
|
|
4
|
+
export function mpWebOnMessage(cb) {
|
|
5
|
+
MESSAGE_HANDLER.push(cb);
|
|
6
|
+
return function () {
|
|
7
|
+
MESSAGE_HANDLER = MESSAGE_HANDLER.filter(function (o) {
|
|
8
|
+
return o !== cb;
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
export function mpWebPostMessage(message) {
|
|
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
|
+
});
|
|
29
|
+
});
|
|
30
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { MPApi } from '@hylid/types';
|
|
2
|
+
import { JsApiConfig, MpWebJsApiOptions } from './types';
|
|
3
|
+
import { mpWebOnMessage, mpWebPostMessage } from './mpWebBridge';
|
|
4
|
+
export declare function mpWebCall<T extends keyof MPApi>(api: T | ({} & string), options?: MpWebJsApiOptions<T>, config?: JsApiConfig): void;
|
|
5
|
+
export declare namespace mpWebCall {
|
|
6
|
+
var onMessage: typeof mpWebOnMessage;
|
|
7
|
+
var postMessage: typeof mpWebPostMessage;
|
|
8
|
+
var broadcastGlobalData: (key: string, result: any) => void;
|
|
9
|
+
var onReceiveGlobalData: (key: string, callback: (data: any, error: any, form: string) => void) => () => void;
|
|
10
|
+
var onPageEvent: (event: ({} & string) | "onShow" | "onHide" | "onTitleClick" | "onOptionMenuClick" | "onTabItemTap", callback: (data: any, err?: any) => void) => () => void;
|
|
11
|
+
}
|
|
12
|
+
export declare const mpWebCallAsync: <T = any>(apiName: string, options?: any, config?: {
|
|
13
|
+
/** 超时时长,默认 20000, 0 表示不判断超时 */
|
|
14
|
+
timeout: number;
|
|
15
|
+
isSync?: boolean;
|
|
16
|
+
}) => Promise<any>;
|
package/lib/mpWebCall.js
ADDED
|
@@ -0,0 +1,274 @@
|
|
|
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
|
+
import { mpWebOnMessage, mpWebPostMessage } from "./mpWebBridge";
|
|
12
|
+
import { createError } from "./utils";
|
|
13
|
+
import { alipayJSBridge } from "./webCall";
|
|
14
|
+
// 关联 postMessage 和 onMessage,根据 serialId 执行回调
|
|
15
|
+
var callMap = {};
|
|
16
|
+
// 注册 jsapi 的 message 处理器
|
|
17
|
+
mpWebOnMessage(function (msg) {
|
|
18
|
+
var _a;
|
|
19
|
+
var message = msg;
|
|
20
|
+
var serialId = message.serialId;
|
|
21
|
+
// 不是 jsapi 的调用的消息
|
|
22
|
+
if (!serialId) return;
|
|
23
|
+
if (!callMap[serialId]) return;
|
|
24
|
+
var callback = callMap[serialId];
|
|
25
|
+
var data = message.result;
|
|
26
|
+
var type = (_a = message.config) === null || _a === void 0 ? void 0 : _a.type; // JsApi 的类型
|
|
27
|
+
if (type === 'callback') {
|
|
28
|
+
var _b = data || {},
|
|
29
|
+
_data_1 = _b._data_,
|
|
30
|
+
_type_1 = _b._type_;
|
|
31
|
+
if (_type_1 === 'success') return callback(_data_1);
|
|
32
|
+
return callback(undefined, _data_1);
|
|
33
|
+
}
|
|
34
|
+
var _c = callback || {},
|
|
35
|
+
success = _c.success,
|
|
36
|
+
fail = _c.fail,
|
|
37
|
+
complete = _c.complete;
|
|
38
|
+
var _d = data || {},
|
|
39
|
+
_data_ = _d._data_,
|
|
40
|
+
_type_ = _d._type_;
|
|
41
|
+
// 新版本协议
|
|
42
|
+
if (_type_) {
|
|
43
|
+
if (_type_ === 'success') success === null || success === void 0 ? void 0 : success(_data_);
|
|
44
|
+
if (_type_ === 'fail') fail === null || fail === void 0 ? void 0 : fail(_data_);
|
|
45
|
+
complete === null || complete === void 0 ? void 0 : complete(_data_);
|
|
46
|
+
} else {
|
|
47
|
+
// 下面兼容老版本(老版本消息识别不明确,导致 H5 中走 success/fail 逻辑可能岔了)
|
|
48
|
+
// @ts-ignore
|
|
49
|
+
if (data === null || data === void 0 ? void 0 : data.error) {
|
|
50
|
+
fail === null || fail === void 0 ? void 0 : fail(data);
|
|
51
|
+
} else {
|
|
52
|
+
success === null || success === void 0 ? void 0 : success(data);
|
|
53
|
+
}
|
|
54
|
+
complete === null || complete === void 0 ? void 0 : complete(data);
|
|
55
|
+
}
|
|
56
|
+
delete callMap[serialId];
|
|
57
|
+
});
|
|
58
|
+
export function mpWebCall(api, options, config) {
|
|
59
|
+
config = __assign({
|
|
60
|
+
timeout: 20000
|
|
61
|
+
}, config);
|
|
62
|
+
var random = Math.floor(Math.random() * 1000000);
|
|
63
|
+
var serialId = api + '_' + random;
|
|
64
|
+
// 只调用,没有参数,不需要接收 jsapi 执行结果
|
|
65
|
+
if (options) {
|
|
66
|
+
callMap[serialId] = options;
|
|
67
|
+
}
|
|
68
|
+
var message = {
|
|
69
|
+
source: 'hylid',
|
|
70
|
+
type: 'apiCall',
|
|
71
|
+
api: api,
|
|
72
|
+
serialId: serialId,
|
|
73
|
+
options: options,
|
|
74
|
+
config: config,
|
|
75
|
+
isSync: config === null || config === void 0 ? void 0 : config.isSync
|
|
76
|
+
};
|
|
77
|
+
mpWebPostMessage(message);
|
|
78
|
+
// 设置 jsapi 通信超时时间
|
|
79
|
+
var _options = options;
|
|
80
|
+
if ((config === null || config === void 0 ? void 0 : config.timeout) && (_options === null || _options === void 0 ? void 0 : _options.fail)) {
|
|
81
|
+
setTimeout(function () {
|
|
82
|
+
if (!callMap[serialId]) return;
|
|
83
|
+
delete callMap[serialId];
|
|
84
|
+
_options.fail(createError('TIMEOUT'));
|
|
85
|
+
}, config.timeout);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
function broadcastGlobalData(key, result) {
|
|
89
|
+
var message = {
|
|
90
|
+
source: 'hylid',
|
|
91
|
+
type: 'broadcast',
|
|
92
|
+
from: window.location.href,
|
|
93
|
+
key: key,
|
|
94
|
+
result: result
|
|
95
|
+
};
|
|
96
|
+
mpWebPostMessage(message);
|
|
97
|
+
}
|
|
98
|
+
function onReceiveGlobalData(key, callback) {
|
|
99
|
+
return mpWebOnMessage(function (msg) {
|
|
100
|
+
var message = msg;
|
|
101
|
+
if (message.source !== 'hylid') return;
|
|
102
|
+
if (message.type !== 'broadcast') return;
|
|
103
|
+
// 不是接收的目标消息
|
|
104
|
+
if (message.key !== key) return;
|
|
105
|
+
var _a = message.result || {},
|
|
106
|
+
_data_ = _a._data_,
|
|
107
|
+
_type_ = _a._type_;
|
|
108
|
+
if (_type_ === 'success') return callback(_data_, undefined, message.from);
|
|
109
|
+
return callback(undefined, _data_, message.from);
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
function onPageEvent(event, callback) {
|
|
113
|
+
// 告诉小程序侧,监听 event 事件
|
|
114
|
+
mpWebPostMessage({
|
|
115
|
+
source: 'hylid',
|
|
116
|
+
type: 'pageEvent',
|
|
117
|
+
event: event
|
|
118
|
+
});
|
|
119
|
+
return mpWebOnMessage(function (msg) {
|
|
120
|
+
var message = msg;
|
|
121
|
+
if (message.source !== 'hylid') return;
|
|
122
|
+
if (message.type !== 'pageEvent') return;
|
|
123
|
+
if (message.event !== event) return;
|
|
124
|
+
var _a = message.result || {},
|
|
125
|
+
_data_ = _a._data_,
|
|
126
|
+
_type_ = _a._type_;
|
|
127
|
+
if (_type_ === 'success') return callback(_data_);
|
|
128
|
+
return callback(undefined, _data_);
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
mpWebCall.onMessage = mpWebOnMessage;
|
|
132
|
+
mpWebCall.postMessage = mpWebPostMessage;
|
|
133
|
+
mpWebCall.broadcastGlobalData = broadcastGlobalData;
|
|
134
|
+
mpWebCall.onReceiveGlobalData = onReceiveGlobalData;
|
|
135
|
+
mpWebCall.onPageEvent = onPageEvent;
|
|
136
|
+
var callbackFn = function callbackFn(res, callback) {
|
|
137
|
+
var _a = callback || {},
|
|
138
|
+
success = _a.success,
|
|
139
|
+
fail = _a.fail,
|
|
140
|
+
complete = _a.complete;
|
|
141
|
+
// @ts-ignore
|
|
142
|
+
if (res === null || res === void 0 ? void 0 : res.error) {
|
|
143
|
+
fail === null || fail === void 0 ? void 0 : fail(res);
|
|
144
|
+
} else {
|
|
145
|
+
success === null || success === void 0 ? void 0 : success(res);
|
|
146
|
+
}
|
|
147
|
+
complete === null || complete === void 0 ? void 0 : complete(res);
|
|
148
|
+
};
|
|
149
|
+
export var mpWebCallAsync = function () {
|
|
150
|
+
var callMap = {};
|
|
151
|
+
var isListening = false;
|
|
152
|
+
function onMessage() {
|
|
153
|
+
var messageHandler = function messageHandler(message) {
|
|
154
|
+
var _a;
|
|
155
|
+
var serialId = message.serialId;
|
|
156
|
+
if (!serialId) return;
|
|
157
|
+
if (!callMap[serialId]) return;
|
|
158
|
+
var callback = callMap[serialId];
|
|
159
|
+
var data = message.result;
|
|
160
|
+
var type = (_a = message.config) === null || _a === void 0 ? void 0 : _a.type;
|
|
161
|
+
if (type === 'callback') {
|
|
162
|
+
var _b = data || {},
|
|
163
|
+
_data_2 = _b._data_,
|
|
164
|
+
_type_2 = _b._type_;
|
|
165
|
+
if (_type_2 === 'success') return callback(_data_2);
|
|
166
|
+
return callback(undefined, _data_2);
|
|
167
|
+
}
|
|
168
|
+
var _c = callback || {},
|
|
169
|
+
success = _c.success,
|
|
170
|
+
fail = _c.fail,
|
|
171
|
+
complete = _c.complete;
|
|
172
|
+
var _d = data || {},
|
|
173
|
+
_data_ = _d._data_,
|
|
174
|
+
_type_ = _d._type_;
|
|
175
|
+
// 新版本协议
|
|
176
|
+
if (_type_) {
|
|
177
|
+
if (_type_ === 'success') success === null || success === void 0 ? void 0 : success(_data_);
|
|
178
|
+
if (_type_ === 'fail') fail === null || fail === void 0 ? void 0 : fail(_data_);
|
|
179
|
+
complete === null || complete === void 0 ? void 0 : complete(_data_);
|
|
180
|
+
} else {
|
|
181
|
+
// 下面兼容老版本(老版本消息识别不明确,导致 H5 中走 success/fail 逻辑可能岔了)
|
|
182
|
+
// @ts-ignore
|
|
183
|
+
if (data === null || data === void 0 ? void 0 : data.error) {
|
|
184
|
+
fail === null || fail === void 0 ? void 0 : fail(data);
|
|
185
|
+
} else {
|
|
186
|
+
success === null || success === void 0 ? void 0 : success(data);
|
|
187
|
+
}
|
|
188
|
+
complete === null || complete === void 0 ? void 0 : complete(data);
|
|
189
|
+
}
|
|
190
|
+
delete callMap[serialId];
|
|
191
|
+
};
|
|
192
|
+
// 代码参考:https://alex.alipay.com/antcode/qianyu.fzy/af-appx/blob/r20191223-1.24.0/src/web-view/embed/index.tsx
|
|
193
|
+
document.addEventListener('onToWebViewMessage', function (e) {
|
|
194
|
+
var res = e.data.res;
|
|
195
|
+
if (res.type === 'message') {
|
|
196
|
+
messageHandler(JSON.parse(res.data));
|
|
197
|
+
}
|
|
198
|
+
// TODO: 参考的代码中还有 response 的类型,需要考虑一下是否实现,对应 api 为 'navigateTo','navigateBack','switchTab','reLaunch','redirectTo',
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
function fireMessage(_a) {
|
|
202
|
+
var _b;
|
|
203
|
+
var apiName = _a.apiName,
|
|
204
|
+
options = _a.options,
|
|
205
|
+
isSync = _a.isSync;
|
|
206
|
+
var env = window;
|
|
207
|
+
var random = Math.floor(Math.random() * 1000000);
|
|
208
|
+
var serialId = "".concat(apiName, "_").concat(random);
|
|
209
|
+
if (options) {
|
|
210
|
+
callMap[serialId] = options;
|
|
211
|
+
}
|
|
212
|
+
var params = {
|
|
213
|
+
type: 'apiCall',
|
|
214
|
+
api: apiName,
|
|
215
|
+
serialId: serialId,
|
|
216
|
+
options: options,
|
|
217
|
+
isSync: isSync,
|
|
218
|
+
source: 'hylid'
|
|
219
|
+
};
|
|
220
|
+
(_b = env.AlipayJSBridge) === null || _b === void 0 ? void 0 : _b.call('postWebViewMessage', {
|
|
221
|
+
type: 'message',
|
|
222
|
+
detail: params
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
return function mpWebCall(apiName, options, config) {
|
|
226
|
+
return new Promise(function (resolve, reject) {
|
|
227
|
+
var _a = options || {},
|
|
228
|
+
_success = _a.success,
|
|
229
|
+
_fail = _a.fail;
|
|
230
|
+
var _b = config || {},
|
|
231
|
+
_c = _b.timeout,
|
|
232
|
+
timeout = _c === void 0 ? 20000 : _c,
|
|
233
|
+
isSync = _b.isSync;
|
|
234
|
+
var timer;
|
|
235
|
+
if (timeout > 0) {
|
|
236
|
+
timer = setTimeout(function () {
|
|
237
|
+
var err = {
|
|
238
|
+
error: -101,
|
|
239
|
+
errorMessage: "[".concat(apiName, "]: Timeout")
|
|
240
|
+
};
|
|
241
|
+
reject(err);
|
|
242
|
+
_fail === null || _fail === void 0 ? void 0 : _fail(err);
|
|
243
|
+
}, timeout);
|
|
244
|
+
}
|
|
245
|
+
var params = {
|
|
246
|
+
apiName: apiName,
|
|
247
|
+
options: __assign(__assign({}, options), {
|
|
248
|
+
success: function success(res) {
|
|
249
|
+
if (timer) {
|
|
250
|
+
clearTimeout(timer);
|
|
251
|
+
}
|
|
252
|
+
resolve(res);
|
|
253
|
+
_success === null || _success === void 0 ? void 0 : _success(res);
|
|
254
|
+
},
|
|
255
|
+
fail: function fail(err) {
|
|
256
|
+
if (timer) {
|
|
257
|
+
clearTimeout(timer);
|
|
258
|
+
}
|
|
259
|
+
reject(err);
|
|
260
|
+
_fail === null || _fail === void 0 ? void 0 : _fail(err);
|
|
261
|
+
}
|
|
262
|
+
}),
|
|
263
|
+
isSync: isSync
|
|
264
|
+
};
|
|
265
|
+
alipayJSBridge(function () {
|
|
266
|
+
if (!isListening) {
|
|
267
|
+
onMessage();
|
|
268
|
+
isListening = true;
|
|
269
|
+
}
|
|
270
|
+
fireMessage(params);
|
|
271
|
+
});
|
|
272
|
+
});
|
|
273
|
+
};
|
|
274
|
+
}();
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const notFound: any;
|
package/lib/notFound.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { createError } from "./utils";
|
|
2
|
+
export var notFound = function notFound(name, opt, config) {
|
|
3
|
+
var _a;
|
|
4
|
+
if (config === void 0) {
|
|
5
|
+
config = {};
|
|
6
|
+
}
|
|
7
|
+
var _b = config.type,
|
|
8
|
+
type = _b === void 0 ? 'async' : _b;
|
|
9
|
+
var response = createError('NOTFOUND', name);
|
|
10
|
+
if (type === 'sync') return response;
|
|
11
|
+
if (type === 'callback') {
|
|
12
|
+
opt === null || opt === void 0 ? void 0 : opt(response);
|
|
13
|
+
}
|
|
14
|
+
if (type === 'async') {
|
|
15
|
+
(_a = opt === null || opt === void 0 ? void 0 : opt.fail) === null || _a === void 0 ? void 0 : _a.call(opt, response);
|
|
16
|
+
}
|
|
17
|
+
};
|
package/lib/promisify.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
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
|
+
export var promisify = function promisify(fn) {
|
|
12
|
+
return function (args) {
|
|
13
|
+
return new Promise(function (resolve, reject) {
|
|
14
|
+
var options = __assign(__assign({}, args), {
|
|
15
|
+
success: function success(opt) {
|
|
16
|
+
var _a;
|
|
17
|
+
resolve(opt);
|
|
18
|
+
(_a = args === null || args === void 0 ? void 0 : args.success) === null || _a === void 0 ? void 0 : _a.call(args, opt);
|
|
19
|
+
},
|
|
20
|
+
fail: function fail(opt) {
|
|
21
|
+
var _a;
|
|
22
|
+
reject(opt);
|
|
23
|
+
(_a = args === null || args === void 0 ? void 0 : args.fail) === null || _a === void 0 ? void 0 : _a.call(args, opt);
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
fn(options);
|
|
27
|
+
});
|
|
28
|
+
};
|
|
29
|
+
};
|
package/lib/types.d.ts
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { JsApiType, MPApi, PickMPArgs, PickMpReturns } from '@hylid/types';
|
|
2
|
+
export declare type JsApi = keyof MPApi;
|
|
3
|
+
export interface JsApiConfig {
|
|
4
|
+
type?: JsApiType;
|
|
5
|
+
/**
|
|
6
|
+
* @deprecated 优先使用 type = "sync" 配置
|
|
7
|
+
*/
|
|
8
|
+
isSync?: boolean;
|
|
9
|
+
/**
|
|
10
|
+
* 套壳 jsapi 调用,通信超时时间。type = "callback" 时不生效
|
|
11
|
+
*/
|
|
12
|
+
timeout?: number;
|
|
13
|
+
}
|
|
14
|
+
export declare type Config = JsApiConfig;
|
|
15
|
+
export interface MpWebMessageResult<T = any> {
|
|
16
|
+
_type_: 'success' | 'fail';
|
|
17
|
+
_data_: T;
|
|
18
|
+
}
|
|
19
|
+
export interface MpWebJsApiMessage {
|
|
20
|
+
source: 'hylid';
|
|
21
|
+
type: 'apiCall';
|
|
22
|
+
api: string;
|
|
23
|
+
serialId: string;
|
|
24
|
+
options: any;
|
|
25
|
+
config?: JsApiConfig;
|
|
26
|
+
result?: MpWebMessageResult;
|
|
27
|
+
/**
|
|
28
|
+
* @deprecated 优先使用 config.type = "sync" 配置
|
|
29
|
+
*/
|
|
30
|
+
isSync?: boolean;
|
|
31
|
+
}
|
|
32
|
+
export interface MpWebBroadcastMessage {
|
|
33
|
+
source: 'hylid';
|
|
34
|
+
type: 'broadcast';
|
|
35
|
+
from: string;
|
|
36
|
+
key: string;
|
|
37
|
+
result?: MpWebMessageResult;
|
|
38
|
+
}
|
|
39
|
+
export interface MpWebPageEventMessage {
|
|
40
|
+
source: 'hylid';
|
|
41
|
+
type: 'pageEvent';
|
|
42
|
+
event: 'onShow' | 'onHide' | 'onTitleClick' | 'onOptionMenuClick' | 'onTabItemTap' | ({} & string);
|
|
43
|
+
result?: MpWebMessageResult;
|
|
44
|
+
}
|
|
45
|
+
export declare type Message = MpWebJsApiMessage | MpWebBroadcastMessage | MpWebPageEventMessage;
|
|
46
|
+
export declare type MessageHandler = (message: Message) => void;
|
|
47
|
+
declare type Callback<T> = (result?: T) => void;
|
|
48
|
+
export declare type MpWebJsApiOptions<T extends keyof MPApi> = PickMPArgs<T> | Callback<PickMpReturns<T>>;
|
|
49
|
+
export interface WebviewBridgeConfig {
|
|
50
|
+
/** 允许调用 jsApi 的白名单 */
|
|
51
|
+
whitelist?: string[];
|
|
52
|
+
/** 禁止调用 jsApi 的黑名单 */
|
|
53
|
+
blacklist?: string[];
|
|
54
|
+
/** 自定义 jsApi [ 优先级比较高,可以覆盖原生的 jsApi ] */
|
|
55
|
+
customApi?: CustomApi;
|
|
56
|
+
}
|
|
57
|
+
export interface CustomApi {
|
|
58
|
+
[key: string]: (params: {
|
|
59
|
+
success(v: any): void;
|
|
60
|
+
fail(v: any): void;
|
|
61
|
+
}) => void;
|
|
62
|
+
}
|
|
63
|
+
export {};
|
package/lib/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/lib/utils.d.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { MpWebMessageResult } from './types';
|
|
2
|
+
declare const ERROR: {
|
|
3
|
+
NOTFOUND: {
|
|
4
|
+
code: number;
|
|
5
|
+
message: (name: string) => string;
|
|
6
|
+
};
|
|
7
|
+
NOTALLOWED: {
|
|
8
|
+
code: number;
|
|
9
|
+
message: (name: string) => string;
|
|
10
|
+
};
|
|
11
|
+
INVALID: {
|
|
12
|
+
code: number;
|
|
13
|
+
message: (name: string) => string;
|
|
14
|
+
};
|
|
15
|
+
NOTBIND: {
|
|
16
|
+
code: number;
|
|
17
|
+
message: (name: string) => string;
|
|
18
|
+
};
|
|
19
|
+
PARAMS_ERROR: {
|
|
20
|
+
code: number;
|
|
21
|
+
message: (name: string) => string;
|
|
22
|
+
};
|
|
23
|
+
TIMEOUT: {
|
|
24
|
+
code: number;
|
|
25
|
+
message: (name: string) => string;
|
|
26
|
+
};
|
|
27
|
+
};
|
|
28
|
+
export declare const createError: (type: keyof typeof ERROR, name?: string) => {
|
|
29
|
+
errorCode: number;
|
|
30
|
+
errorMessage: string;
|
|
31
|
+
errorSouce: string;
|
|
32
|
+
};
|
|
33
|
+
export declare function createMessage(message: any, type?: 'success' | 'fail'): MpWebMessageResult;
|
|
34
|
+
export {};
|
package/lib/utils.js
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
var ERROR = {
|
|
2
|
+
NOTFOUND: {
|
|
3
|
+
code: -1,
|
|
4
|
+
message: function message(name) {
|
|
5
|
+
return "".concat(name, " is not found");
|
|
6
|
+
}
|
|
7
|
+
},
|
|
8
|
+
NOTALLOWED: {
|
|
9
|
+
code: -2,
|
|
10
|
+
message: function message(name) {
|
|
11
|
+
return "".concat(name, " is not allowed");
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
INVALID: {
|
|
15
|
+
code: -3,
|
|
16
|
+
message: function message(name) {
|
|
17
|
+
return "".concat(name, " is invalid");
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
NOTBIND: {
|
|
21
|
+
code: -4,
|
|
22
|
+
message: function message(name) {
|
|
23
|
+
return "".concat(name, " exec fail, webviewBridge not bind context");
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
PARAMS_ERROR: {
|
|
27
|
+
code: -5,
|
|
28
|
+
message: function message(name) {
|
|
29
|
+
return "".concat(name, " exec fail, params error");
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
TIMEOUT: {
|
|
33
|
+
code: -6,
|
|
34
|
+
message: function message(name) {
|
|
35
|
+
return "".concat(name, " exec fail, timeout");
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
export var createError = function createError(type, name) {
|
|
40
|
+
if (name === void 0) {
|
|
41
|
+
name = '';
|
|
42
|
+
}
|
|
43
|
+
var _a = ERROR[type],
|
|
44
|
+
code = _a.code,
|
|
45
|
+
message = _a.message;
|
|
46
|
+
return {
|
|
47
|
+
errorCode: code,
|
|
48
|
+
errorMessage: message(name),
|
|
49
|
+
errorSouce: 'hylid'
|
|
50
|
+
};
|
|
51
|
+
};
|
|
52
|
+
export function createMessage(message, type) {
|
|
53
|
+
if (type === void 0) {
|
|
54
|
+
type = 'success';
|
|
55
|
+
}
|
|
56
|
+
return {
|
|
57
|
+
_type_: type,
|
|
58
|
+
_data_: message
|
|
59
|
+
};
|
|
60
|
+
}
|
package/lib/webCall.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { AliJsApi, AsyncCallback } from '@hylid/types';
|
|
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;
|
|
5
|
+
export declare function webCallAsync<T = any>(apiName: string, options?: AsyncCallback<T> & any, config?: {
|
|
6
|
+
timeout?: number;
|
|
7
|
+
}): Promise<any>;
|