@hylid/call 2.12.0-alpha.18

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 ADDED
@@ -0,0 +1,17 @@
1
+ # @hylid/env
2
+
3
+ ## Install
4
+
5
+ ```bash
6
+ npm i @hylid/env --save
7
+ ```
8
+
9
+ ## API
10
+
11
+ ```ts
12
+ import { appName, platform, appEnv, platformEnv, APP, PLATFORM } from '@hylid/env';
13
+ ```
14
+
15
+ ## License
16
+
17
+ MIT
package/lib/index.d.ts ADDED
@@ -0,0 +1,13 @@
1
+ /// <reference types="miniprogram" />
2
+ import { MPApi, PickMPArgs, PickMpReturns, Callback } from '@hylid/types';
3
+ import { mpCall } from './mpCall';
4
+ import { mpWebCall } from './mpWebCall';
5
+ import { webCall } from './webCall';
6
+ import { notFound } from './notFound';
7
+ interface Config {
8
+ type?: string;
9
+ }
10
+ export declare function call<T extends keyof MPApi>(name: T | ({} & string), options?: PickMPArgs<T> | Common | Callback<PickMpReturns<T>>, config?: Config): any;
11
+ export { WebViewBridge } from './webviewBridge';
12
+ export { mpCall, mpWebCall, webCall, notFound };
13
+ export { promisify } from './promisify';
package/lib/index.js ADDED
@@ -0,0 +1,17 @@
1
+ var _a;
2
+ import { platform, PLATFORM } from '@hylid/env';
3
+ import { mpCall } from "./mpCall";
4
+ import { mpWebCall } from "./mpWebCall";
5
+ import { webCall } 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
+ export function call(name, options, config) {
9
+ if (platform) {
10
+ return callMap[platform](name, options, config);
11
+ } else {
12
+ return notFound(options, config);
13
+ }
14
+ }
15
+ export { WebViewBridge } from "./webviewBridge";
16
+ export { mpCall, mpWebCall, webCall, notFound };
17
+ export { promisify } from "./promisify";
@@ -0,0 +1,4 @@
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;
package/lib/mpCall.js ADDED
@@ -0,0 +1,22 @@
1
+ export function mpCall(apiName, options, config) {
2
+ var _a = (config || {}).type,
3
+ type = _a === void 0 ? 'async' : _a;
4
+ if (type === 'sync') {
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
+ }
@@ -0,0 +1,6 @@
1
+ import { PickMPArgs, PickMpReturns, MPApi } from '@hylid/types';
2
+ declare type Callback<T> = (result?: T) => void;
3
+ export declare const mpWebCall: <T extends keyof MPApi>(apiName: ({} & string) | T, options?: PickMPArgs<T> | Callback<ReturnType<MPApi[T]>> | undefined, config?: {
4
+ type?: string;
5
+ }) => void;
6
+ export {};
@@ -0,0 +1,100 @@
1
+ var loadAppxBridge = function loadAppxBridge(cb) {
2
+ try {
3
+ // @ts-ignore
4
+ if (window.my) {
5
+ cb && cb("ready" /* BridgeReady.READY */);
6
+ return;
7
+ }
8
+ var script_1 = document.createElement('script');
9
+ script_1.src = 'https://appx/web-view.min.js';
10
+ script_1.onload = function onload() {
11
+ var readyState = script_1['readyState'];
12
+ if (typeof readyState === 'undefined' || /^(loaded|complete)$/.test(readyState)) {
13
+ script_1.onload = null;
14
+ cb && cb("loaded" /* BridgeReady.LOADED */);
15
+ }
16
+ };
17
+
18
+ var head = document.getElementsByTagName('head')[0] || document.body;
19
+ head.appendChild(script_1);
20
+ } catch (e) {
21
+ cb && cb(null);
22
+ }
23
+ };
24
+ var callbackFn = function callbackFn(res, callback) {
25
+ var _a = callback || {},
26
+ success = _a.success,
27
+ fail = _a.fail,
28
+ complete = _a.complete;
29
+ // @ts-ignore
30
+ if (res.error) {
31
+ fail === null || fail === void 0 ? void 0 : fail(res);
32
+ } else {
33
+ success === null || success === void 0 ? void 0 : success(res);
34
+ }
35
+ complete === null || complete === void 0 ? void 0 : complete(res);
36
+ };
37
+ export var mpWebCall = function () {
38
+ var callMap = {};
39
+ var WAITING_QUEUE = [];
40
+ var isListening = false;
41
+ function onMessage() {
42
+ var messageHandler = window.my.onMessage;
43
+ window.my.onMessage = function (message) {
44
+ var serialId = message.serialId;
45
+ var data = message.result;
46
+ var type = message.type;
47
+ if (serialId) {
48
+ var callback = callMap[serialId];
49
+ if (type === 'callback') {
50
+ callback(data);
51
+ } else {
52
+ callbackFn(data, callback);
53
+ delete callMap[serialId];
54
+ }
55
+ } else if (messageHandler) {
56
+ messageHandler(message);
57
+ }
58
+ };
59
+ }
60
+ function fireMessage(_a) {
61
+ var apiName = _a.apiName,
62
+ options = _a.options,
63
+ config = _a.config;
64
+ var random = Math.floor(Math.random() * 1000000);
65
+ var serialId = apiName + '_' + random;
66
+ if (options) {
67
+ callMap[serialId] = options;
68
+ }
69
+ var params = {
70
+ type: 'apiCall',
71
+ api: apiName,
72
+ serialId: serialId,
73
+ options: options,
74
+ config: config
75
+ };
76
+ window.my.postMessage(params);
77
+ }
78
+ return function call(apiName, options, config) {
79
+ loadAppxBridge(function (bridgeReady) {
80
+ var params = {
81
+ apiName: apiName,
82
+ options: options,
83
+ config: config
84
+ };
85
+ if (bridgeReady) {
86
+ if (!isListening) {
87
+ onMessage();
88
+ isListening = true;
89
+ }
90
+ if (WAITING_QUEUE.length) {
91
+ WAITING_QUEUE.forEach(fireMessage);
92
+ WAITING_QUEUE = [];
93
+ }
94
+ fireMessage(params);
95
+ } else {
96
+ WAITING_QUEUE.push(params);
97
+ }
98
+ });
99
+ };
100
+ }();
@@ -0,0 +1 @@
1
+ export declare const notFound: any;
@@ -0,0 +1,19 @@
1
+ export var notFound = function notFound(name, opt, config) {
2
+ var _a;
3
+ if (config === void 0) {
4
+ config = {};
5
+ }
6
+ var _b = config.type,
7
+ type = _b === void 0 ? 'async' : _b;
8
+ var response = {
9
+ error: -1,
10
+ errorMessage: "".concat(name, " is not found")
11
+ };
12
+ if (type === 'sync') return response;
13
+ if (type === 'callback') {
14
+ opt === null || opt === void 0 ? void 0 : opt(response);
15
+ }
16
+ if (type === 'async') {
17
+ (_a = opt === null || opt === void 0 ? void 0 : opt.fail) === null || _a === void 0 ? void 0 : _a.call(opt, response);
18
+ }
19
+ };
@@ -0,0 +1,4 @@
1
+ export interface Options {
2
+ success?: (res: any) => void;
3
+ }
4
+ export declare const promisify: <T extends Options, P = Parameters<Required<Options> & T["success"]>[0]>(fn: (opt: T) => void) => (args?: Omit<T, "success" | "fail" | "complete"> | undefined) => Promise<P>;
@@ -0,0 +1,21 @@
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: resolve,
16
+ fail: reject
17
+ });
18
+ fn(options);
19
+ });
20
+ };
21
+ };
@@ -0,0 +1,4 @@
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;
package/lib/webCall.js ADDED
@@ -0,0 +1,29 @@
1
+ var alipayJSBridgeReady = function alipayJSBridgeReady(cb) {
2
+ if (window.AlipayJSBridge) return cb();
3
+ document.addEventListener('AlipayJSBridgeReady', function () {
4
+ return cb();
5
+ }, false);
6
+ };
7
+ export function webCall(apiName, options, config) {
8
+ alipayJSBridgeReady(function () {
9
+ var _a = (config || {}).type,
10
+ type = _a === void 0 ? 'async' : _a;
11
+ if (type === 'async') {
12
+ window.AlipayJSBridge.call(apiName, options, function (res) {
13
+ var _a = options || {},
14
+ success = _a.success,
15
+ fail = _a.fail,
16
+ complete = _a.complete;
17
+ if (res.error) {
18
+ fail === null || fail === void 0 ? void 0 : fail(res);
19
+ } else {
20
+ success === null || success === void 0 ? void 0 : success(res);
21
+ }
22
+ complete === null || complete === void 0 ? void 0 : complete(res);
23
+ });
24
+ }
25
+ if (type === 'callback') {
26
+ window.AlipayJSBridge.call(apiName, options);
27
+ }
28
+ });
29
+ }
@@ -0,0 +1,7 @@
1
+ export declare class WebViewBridge {
2
+ webview: any;
3
+ customApi: {};
4
+ constructor(ctx: any, customApi?: {});
5
+ listen: (data: any) => Promise<void>;
6
+ handleApi: (data: any) => Promise<unknown>;
7
+ }
@@ -0,0 +1,214 @@
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, customApi) {
128
+ if (customApi === void 0) {
129
+ customApi = {};
130
+ }
131
+ var _this = this;
132
+ this.customApi = {};
133
+ this.listen = function (data) {
134
+ return __awaiter(_this, void 0, void 0, function () {
135
+ var command, config, api_1, serialId_1, type_1, _a, _b;
136
+ var _c;
137
+ var _this = this;
138
+ return __generator(this, function (_d) {
139
+ switch (_d.label) {
140
+ case 0:
141
+ command = data.detail;
142
+ if (!command.api) return [3 /*break*/, 3];
143
+ config = command.config, api_1 = command.api, serialId_1 = command.serialId;
144
+ type_1 = (config || {}).type;
145
+ if (!(type_1 === 'callback')) return [3 /*break*/, 1];
146
+ // @ts-ignore
147
+ my[api_1](function (res) {
148
+ _this.webview.postMessage({
149
+ api: api_1,
150
+ serialId: serialId_1,
151
+ type: type_1,
152
+ result: res
153
+ });
154
+ });
155
+ return [3 /*break*/, 3];
156
+ case 1:
157
+ _b = (_a = this.webview).postMessage;
158
+ _c = {
159
+ api: api_1,
160
+ serialId: serialId_1,
161
+ type: type_1
162
+ };
163
+ return [4 /*yield*/, this.handleApi(command)];
164
+ case 2:
165
+ _b.apply(_a, [(_c.result = _d.sent(), _c)]);
166
+ _d.label = 3;
167
+ case 3:
168
+ return [2 /*return*/];
169
+ }
170
+ });
171
+ });
172
+ };
173
+
174
+ this.handleApi = function (data) {
175
+ var api = data.api,
176
+ _options = data.options,
177
+ config = data.config;
178
+ var _a = (config || {}).type,
179
+ type = _a === void 0 ? 'async' : _a;
180
+ return new Promise(function (resolve) {
181
+ var options = __assign(__assign({}, _options), {
182
+ success: resolve,
183
+ fail: function fail(res) {
184
+ if (!res || !res.error) {
185
+ resolve(__assign({
186
+ error: "".concat(api, " error")
187
+ }, res));
188
+ } else {
189
+ resolve(res);
190
+ }
191
+ },
192
+ complete: resolve
193
+ });
194
+ // @ts-ignore
195
+ var mpApi = _this.customApi[api] || my[api];
196
+ if (type === 'sync') {
197
+ resolve(mpApi(options));
198
+ }
199
+ if (type === 'async') {
200
+ if (!mpApi) {
201
+ // @ts-ignore
202
+ my.call(api, options);
203
+ } else {
204
+ mpApi(options);
205
+ }
206
+ }
207
+ });
208
+ };
209
+ this.webview = ctx;
210
+ this.customApi = customApi;
211
+ }
212
+ return WebViewBridge;
213
+ }();
214
+ export { WebViewBridge };
package/package.json ADDED
@@ -0,0 +1,19 @@
1
+ {
2
+ "name": "@hylid/call",
3
+ "version": "2.12.0-alpha.18",
4
+ "main": "lib/index.js",
5
+ "files": [
6
+ "lib"
7
+ ],
8
+ "dependencies": {
9
+ "@hylid/env": "^2.12.0-alpha.18",
10
+ "@hylid/types": "^2.12.0-alpha.18"
11
+ },
12
+ "publishConfig": {
13
+ "access": "public"
14
+ },
15
+ "license": "MIT",
16
+ "sideEffects": false,
17
+ "registry": "https://registry.npmjs.org/",
18
+ "repository": "https://code.alipay.com/ant-ife/hylid-bridge.git"
19
+ }