miniapp-web-jsapi
Version:
JSAPI/View adapter for miniprogram running on the web
124 lines • 4.89 kB
JavaScript
import _defineProperty from "@babel/runtime/helpers/defineProperty";
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
import axios from 'axios';
import { buildErrorResponse, decodeUri } from './result';
import JsUtils from "../../../utils/js-utils";
import { RpcError } from "../../rpc";
import { getRpcOptions } from "../../utils/web-utils";
import { getWebPolyfillOptions } from "../..";
export function rpc(options) {
var _getWebPolyfillOption, _getWebPolyfillOption2;
var interceptor = (_getWebPolyfillOption = getWebPolyfillOptions()) === null || _getWebPolyfillOption === void 0 ? void 0 : (_getWebPolyfillOption2 = _getWebPolyfillOption.rpcOptions) === null || _getWebPolyfillOption2 === void 0 ? void 0 : _getWebPolyfillOption2.rpcInterceptor;
if (interceptor && interceptor.onRpcError) {
// 重写 fail
var oldFail = options.fail;
var onRpcError = interceptor.onRpcError;
options.fail = function (err) {
onRpcError(err, {
notifyFail: function notifyFail(err) {
oldFail === null || oldFail === void 0 ? void 0 : oldFail(err);
},
resend: function resend() {
// 恢复 fail
options.fail = oldFail;
sendRpcInternal(options);
}
});
};
}
// 前置拦截
if (interceptor && interceptor.onBeforeSendRpc) {
if (interceptor.onBeforeSendRpc(options)) {
// 拦截器已处理, 不继续发送
return;
}
}
sendRpcInternal(options);
}
function sendRpcInternal(options) {
var success = options.success,
fail = options.fail,
complete = options.complete;
var operationType = options.operationType,
requestData = options.requestData,
gateway = options.gateway,
headers = options.headers;
var rpcOptions = getRpcOptions();
var requestHeaders = _objectSpread(_objectSpread(_objectSpread(_objectSpread(_objectSpread({}, rpcOptions.appId ? {
AppId: rpcOptions.appId
} : {}), rpcOptions.workspaceId ? {
WorkspaceId: rpcOptions.workspaceId
} : {}), rpcOptions.headers), headers), {}, _defineProperty({
'Operation-Type': operationType,
'Content-Type': 'application/json'
}, "X-CORS-".concat(rpcOptions.appId, "-").concat(rpcOptions.workspaceId), '1'));
var gatewayUrl = gateway ? gateway : rpcOptions.gatewayUrl;
var notifyFail = function notifyFail(e) {
// notify fail
fail === null || fail === void 0 ? void 0 : fail(e);
complete === null || complete === void 0 ? void 0 : complete();
};
var params = {
method: 'post',
url: gatewayUrl,
timeout: 30000,
headers: requestHeaders,
data: requestData,
withCredentials: true
};
axios(params).then(function (response) {
var resultData = null;
try {
resultData = parseResultData(response);
} catch (e) {
notifyFail(e);
return;
}
success === null || success === void 0 ? void 0 : success(resultData);
complete === null || complete === void 0 ? void 0 : complete();
}).catch(function (e) {
notifyFail(e);
});
}
function parseResultData(response) {
var resultStatus = response.headers['result-status'];
if (resultStatus === '1000') {
// Rpc 请求成功
return buildSuccessResult(response);
} else {
// Rpc 请求失败
throw buildRpcError(response, resultStatus);
}
}
function buildRpcError(response, resultStatus) {
var tips = decodeUri(response.headers['tips']);
var memo = decodeUri(response.headers['memo']);
var error = new RpcError("".concat(tips, " - ").concat(memo));
error.resultStatus = resultStatus;
error.traceId = response.headers['mgw-traceid'];
error.tips = tips;
error.memo = memo;
return error;
}
function buildSuccessResult(response) {
var data = response.data;
var traceId = response.headers['mgw-traceid'];
if (data == null) {
// Rpc 异常
return buildErrorResponse(decodeUri(response.headers['tips']), decodeUri(response.headers['memo']), traceId);
}
var resultData = null;
if (JsUtils.isString(data)) {
resultData = JsUtils.parseObjectSafe(data);
if (resultData == null) {
return data;
}
} else {
resultData = data;
}
if (JsUtils.isEmpty(resultData.traceId)) {
resultData.traceId = traceId;
}
return resultData;
}