mtl-js-sdk
Version:
114 lines (89 loc) • 3.73 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
var SUCCESS_CODE = 200;
function mock(data) {
var extend = window.MTL.extend;
if (typeof extend !== "function") {
return;
}
var mockdatas = data instanceof Array ? data : _typeof(data) === "object" ? [data] : null;
if (!mockdatas) {
return;
}
var modules = [];
mockdatas.forEach(function (item) {
if (_typeof(item) !== "object") {
return;
}
var symbolPath = item.module && item.symbolPath || "";
var module = item.module || item;
var _loop = function _loop(key) {
if (module.hasOwnProperty(key)) {
var element = module[key];
if (typeof element === "function") {
var fn = function fn(object) {
var mockResult = element(object);
var code = mockResult.code,
message = mockResult.message,
data = mockResult.data,
rest = _objectWithoutProperties(mockResult, ["code", "message", "data"]);
if (!(code !== undefined && Object.keys(rest).length === 0)) {
code = SUCCESS_CODE;
data = mockResult;
}
if (code === SUCCESS_CODE) {
message = message || key + ":ok!";
object.success && object.success(data);
} else {
object.fail && object.fail({
code: code,
message: message,
data: data
});
}
object.complete && object.complete({
code: code,
message: message,
data: data
});
};
module[key] = fn;
} else {
module[key] = element;
}
}
};
for (var key in module) {
_loop(key);
}
modules.push({
symbolPath: symbolPath,
module: module
});
});
modules.forEach(function (item) {
return extend(item);
});
}
function Plugin(options) {
var exports = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var _ref = options || {
platform: window.mtl.platform
},
platform = _ref.platform;
exports.module = {
mock: platform === "h5" ? mock : function () {
console.log("warning: 忽略 webdebugger.mock 方法,reason: webdebugger.mock 方法仅在 h5 平台下适用");
}
};
exports.symbolPath = exports.symbolPath || "webdebugger";
return exports;
}
var _default = Plugin;
exports["default"] = _default;