New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@hylid/call

Package Overview
Dependencies
Maintainers
0
Versions
125
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@hylid/call - npm Package Compare versions

Comparing version 4.0.0-alpha.7 to 4.0.0-alpha.8

5

lib/mpWebCall.d.ts

@@ -12,1 +12,6 @@ import { MPApi } from '@hylid/types';

}
export declare const mpWebCallAsync: <T = any>(apiName: string, options?: any, config?: {
/** 超时时长,默认 20000, 0 表示不判断超时 */
timeout: number;
isSync?: boolean;
}) => Promise<any>;

122

lib/mpWebCall.js

@@ -0,2 +1,13 @@

var __assign = this && this.__assign || function () {
__assign = Object.assign || function (t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
import { mpWebOnMessage, mpWebPostMessage } from "./mpWebBridge";
import { alipayJSBridge } from "./webCall";
// 关联 postMessage 和 onMessage,根据 serialId 执行回调

@@ -111,2 +122,111 @@ var callMap = {};

mpWebCall.onReceiveGlobalData = onReceiveGlobalData;
mpWebCall.onPageEvent = onPageEvent;
mpWebCall.onPageEvent = onPageEvent;
var callbackFn = function callbackFn(res, callback) {
var _a = callback || {},
success = _a.success,
fail = _a.fail,
complete = _a.complete;
// @ts-ignore
if (res === null || res === void 0 ? void 0 : res.error) {
fail === null || fail === void 0 ? void 0 : fail(res);
} else {
success === null || success === void 0 ? void 0 : success(res);
}
complete === null || complete === void 0 ? void 0 : complete(res);
};
export var mpWebCallAsync = function () {
var callMap = {};
var isListening = false;
function onMessage() {
var messageHandler = function messageHandler(message) {
var serialId = message.serialId;
var data = message.result;
if (serialId) {
var callback = callMap[serialId];
callbackFn(data, callback);
delete callMap[serialId];
}
};
// 代码参考:https://alex.alipay.com/antcode/qianyu.fzy/af-appx/blob/r20191223-1.24.0/src/web-view/embed/index.tsx
document.addEventListener('onToWebViewMessage', function (e) {
var res = e.data.res;
if (res.type === 'message') {
messageHandler(JSON.parse(res.data));
}
// TODO: 参考的代码中还有 response 的类型,需要考虑一下是否实现,对应 api 为 'navigateTo','navigateBack','switchTab','reLaunch','redirectTo',
});
}
function fireMessage(_a) {
var _b;
var apiName = _a.apiName,
options = _a.options,
isSync = _a.isSync;
var env = window;
var random = Math.floor(Math.random() * 1000000);
var serialId = "".concat(apiName, "_").concat(random);
if (options) {
callMap[serialId] = options;
}
var params = {
type: 'apiCall',
api: apiName,
serialId: serialId,
options: options,
isSync: isSync,
source: 'hylid'
};
(_b = env.AlipayJSBridge) === null || _b === void 0 ? void 0 : _b.call('postWebViewMessage', {
type: 'message',
detail: params
});
}
return function mpWebCall(apiName, options, config) {
return new Promise(function (resolve, reject) {
var _a = options || {},
_success = _a.success,
_fail = _a.fail;
var _b = config || {},
_c = _b.timeout,
timeout = _c === void 0 ? 20000 : _c,
isSync = _b.isSync;
var timer;
if (timeout > 0) {
timer = setTimeout(function () {
var err = {
error: -101,
errorMessage: "[".concat(apiName, "]: Timeout")
};
reject(err);
_fail === null || _fail === void 0 ? void 0 : _fail(err);
}, timeout);
}
var params = {
apiName: apiName,
options: __assign(__assign({}, options), {
success: function success(res) {
if (timer) {
clearTimeout(timer);
}
resolve(res);
_success === null || _success === void 0 ? void 0 : _success(res);
},
fail: function fail(err) {
if (timer) {
clearTimeout(timer);
}
reject(err);
_fail === null || _fail === void 0 ? void 0 : _fail(err);
}
}),
isSync: isSync
};
alipayJSBridge(function () {
if (!isListening) {
onMessage();
isListening = true;
}
fireMessage(params);
});
});
};
}();

@@ -9,2 +9,6 @@ import { JsApiType, MPApi, PickMPArgs, PickMpReturns } from '@hylid/types';

isSync?: boolean;
/**
* timeout 配置,单位 ms
*/
timeout?: number;
}

@@ -11,0 +15,0 @@ export declare type Config = JsApiConfig;

import { AliJsApi } from '@hylid/types';
import { JsApiConfig } from './types';
import { AsyncCallback } from '@hylid/types';
export declare const alipayJSBridge: (cb: Function) => any;
export declare function webCall<T extends keyof AliJsApi.Api>(apiName: T | ({} & string), options?: any, config?: JsApiConfig): void;
export declare function webCallAsync<T = any>(apiName: string, options?: AsyncCallback<T> & any, config?: {
timeout?: number;
}): Promise<any>;

@@ -29,2 +29,40 @@ export var alipayJSBridge = function alipayJSBridge(cb) {

});
}
export function webCallAsync(apiName, options, config) {
return new Promise(function (resolve, reject) {
var _a = options || {},
success = _a.success,
fail = _a.fail,
complete = _a.complete;
var _b = (config || {}).timeout,
timeout = _b === void 0 ? 20000 : _b;
var timer;
if (timeout > 0) {
timer = setTimeout(function () {
var err = {
error: -101,
errorMessage: "[".concat(apiName, "]: Timeout")
};
reject(err);
fail === null || fail === void 0 ? void 0 : fail(err);
}, timeout);
}
window.AlipayJSBridge.call(apiName, options, function (res) {
var _a = options || {},
success = _a.success,
fail = _a.fail,
complete = _a.complete;
if (timer) {
clearTimeout(timer);
}
if (res === null || res === void 0 ? void 0 : res.error) {
reject(res);
fail === null || fail === void 0 ? void 0 : fail(res);
} else {
resolve(res);
success === null || success === void 0 ? void 0 : success(res);
}
complete === null || complete === void 0 ? void 0 : complete(res);
});
});
}

6

package.json
{
"name": "@hylid/call",
"version": "4.0.0-alpha.7",
"version": "4.0.0-alpha.8",
"main": "lib/index.js",

@@ -9,4 +9,4 @@ "files": [

"dependencies": {
"@hylid/env": "^4.0.0-alpha.7",
"@hylid/types": "^4.0.0-alpha.7"
"@hylid/env": "^4.0.0-alpha.8",
"@hylid/types": "^4.0.0-alpha.8"
},

@@ -13,0 +13,0 @@ "publishConfig": {

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc