@morjs/runtime-base
Version:
mor runtime base
228 lines • 9.77 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.markAsUnsupport = exports.transformApis = void 0;
var tslib_1 = require("tslib");
var env_1 = require("../env");
var logger_1 = require("../logger");
var hasOwnProperty_1 = require("./hasOwnProperty");
/**
* 获取原始小程序 request 函数
* @param global 小程序全局对象
* @returns request 函数
*/
function getOriginalRequest(global) {
return function request(options) {
options = options || {};
if (typeof options === 'string') {
options = {
url: options
};
}
var originSuccess = options.success;
var originFail = options.fail;
var originComplete = options.complete;
var requestTask;
var p = new Promise(function (resolve, reject) {
options.success = function (res) {
originSuccess && originSuccess(res);
resolve(res);
};
options.fail = function (res) {
originFail && originFail(res);
reject(res);
};
options.complete = function (res) {
originComplete && originComplete(res);
};
requestTask = global.request(options);
});
p.abort = function (cb) {
cb && cb();
if (requestTask) {
requestTask.abort();
}
return p;
};
return p;
};
}
/**
* 接口抹平转换
* @param mor - mor 接口对象
* @param global - 小程序目标平台全局对象
* @param config - 接口抹平配置
* @param installAllGlobalApis - 是否在 mor 中添加所有的 API
* @param allowOverride - 是否允许覆盖 API
*/
function transformApis(mor, global, config, installAllGlobalApis, allowOverride) {
if (config === void 0) { config = {}; }
if (installAllGlobalApis === void 0) { installAllGlobalApis = false; }
if (allowOverride === void 0) { allowOverride = true; }
var needPromisfiedApis = config.needPromisfiedApis || [];
var apiTransformConfig = config.apiTransformConfig || {};
var preservedApis = [
'global',
'env',
'getApp',
'getCurrentPages',
'requirePlugin',
'getEnv'
];
// 获取所有需要抹平的接口
var allApiNames = installAllGlobalApis ? Object.keys(global) : [];
// 合并需要处理的接口名称
Object.keys(apiTransformConfig)
.concat(needPromisfiedApis)
.forEach(function (apiName) {
if (allApiNames.indexOf(apiName) === -1) {
allApiNames.push(apiName);
}
});
// 处理接口差异
allApiNames.forEach(function (apiName) {
// 不处理 preserved 的 api
if (preservedApis.indexOf(apiName) !== -1)
return;
// 不处理 mor_ 开头的属性
if (/^mor_/.test(apiName))
return;
// 不重复添加接口
if (allowOverride === false && apiName in mor)
return;
var apiConfig = apiTransformConfig[apiName];
// 非函数处理
if (global[apiName] && typeof global[apiName] !== 'function') {
mor[apiName] = global[apiName];
return;
}
// 函数处理
mor[apiName] = function (options) {
var _a;
var args = [];
for (var _i = 1; _i < arguments.length; _i++) {
args[_i - 1] = arguments[_i];
}
// options 差异抹平
if (typeof (apiConfig === null || apiConfig === void 0 ? void 0 : apiConfig.opts) === 'function') {
apiConfig.opts.apply(apiConfig, tslib_1.__spreadArray([options], args, false));
}
else if (apiConfig === null || apiConfig === void 0 ? void 0 : apiConfig.opts) {
var change = apiConfig.opts.c;
var set = apiConfig.opts.s;
if (options == null)
options = {};
// 替换 键值
if (change) {
change.forEach(function (item) {
if (item.o in options)
options[item.n] = options[item.o];
});
}
// 改写值
if (set) {
set.forEach(function (item) {
options[item.k] =
typeof item.v === 'function' ? item.v(options) : item.v;
});
}
}
// 实际接口名称
var actualApiName = (apiConfig === null || apiConfig === void 0 ? void 0 : apiConfig.n) || apiName;
var task = null;
var obj = Object.assign({}, options);
// 执行替换函数
if (typeof (apiConfig === null || apiConfig === void 0 ? void 0 : apiConfig.fn) === 'function') {
return apiConfig.fn.apply(apiConfig, tslib_1.__spreadArray([global, options], args, false));
}
// 处理 request
if (actualApiName === 'request') {
return getOriginalRequest(global)(options);
}
// promisify 处理
if (needPromisfiedApis.indexOf(apiName) !== -1) {
// 新 apiName 可能不存在
if (!(0, hasOwnProperty_1.hasOwnProperty)(global, actualApiName)) {
return Promise.resolve(markAsUnsupport(actualApiName)());
}
// Promise 化
var p_1 = new Promise(function (resolve, reject) {
obj.success = function (res) {
var _a, _b;
(_a = apiConfig === null || apiConfig === void 0 ? void 0 : apiConfig.r) === null || _a === void 0 ? void 0 : _a.call(apiConfig, res);
(_b = options === null || options === void 0 ? void 0 : options.success) === null || _b === void 0 ? void 0 : _b.call(options, res);
if (actualApiName === 'connectSocket') {
resolve(Promise.resolve().then(function () {
return task ? Object.assign(task, res) : res;
}));
}
else {
resolve(res);
}
};
obj.fail = function (res) {
var _a;
(_a = options === null || options === void 0 ? void 0 : options.fail) === null || _a === void 0 ? void 0 : _a.call(options, res);
// 如果用户传入了 fail 则代表用户自行处理错误
// mor 不再抛出 promise 错误, 只标记完成
if (typeof (options === null || options === void 0 ? void 0 : options.fail) === 'function') {
resolve(null);
}
else {
reject(res);
}
logger_1.logger.error("\u63A5\u53E3 ".concat(actualApiName, " \u8C03\u7528\u9519\u8BEF: "), res, "\n\u53C2\u6570: ", tslib_1.__spreadArray([
options
], args, true));
};
obj.complete = function (res) {
var _a;
(_a = options === null || options === void 0 ? void 0 : options.complete) === null || _a === void 0 ? void 0 : _a.call(options, res);
};
if (args.length) {
task = global[actualApiName].apply(global, tslib_1.__spreadArray([obj], args, false));
}
else {
task = global[actualApiName](obj);
}
});
// 给 promise 对象挂载属性
if (actualApiName === 'uploadFile' ||
actualApiName === 'downloadFile') {
p_1.progress = function (cb) {
var _a;
(_a = task === null || task === void 0 ? void 0 : task.onProgressUpdate) === null || _a === void 0 ? void 0 : _a.call(task, cb);
return p_1;
};
p_1.abort = function (cb) {
var _a;
cb === null || cb === void 0 ? void 0 : cb();
(_a = task === null || task === void 0 ? void 0 : task.abort) === null || _a === void 0 ? void 0 : _a.call(task);
return p_1;
};
}
return p_1;
}
else {
// 新 apiName 可能不存在
if (!(0, hasOwnProperty_1.hasOwnProperty)(global, actualApiName)) {
return markAsUnsupport(actualApiName)();
}
var res = global[actualApiName].apply(global, tslib_1.__spreadArray([options], args, false));
(_a = apiConfig === null || apiConfig === void 0 ? void 0 : apiConfig.r) === null || _a === void 0 ? void 0 : _a.call(apiConfig, res);
return res;
}
};
});
}
exports.transformApis = transformApis;
/**
* 返回暂不支持的 函数
* @param apiName - 接口名称
*/
function markAsUnsupport(apiName) {
return function () {
logger_1.logger.warn("".concat((0, env_1.getEnvDesc)(), "\u6682\u4E0D\u652F\u6301 ").concat(apiName));
};
}
exports.markAsUnsupport = markAsUnsupport;
//# sourceMappingURL=transformApis.js.map