97 lines
3.0 KiB
JavaScript
97 lines
3.0 KiB
JavaScript
var __defProp = Object.defineProperty;
|
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
var __export = (target, all) => {
|
|
for (var name in all)
|
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
};
|
|
var __copyProps = (to, from, except, desc) => {
|
|
if (from && typeof from === "object" || typeof from === "function") {
|
|
for (let key of __getOwnPropNames(from))
|
|
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
}
|
|
return to;
|
|
};
|
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
|
|
// src/index.tsx
|
|
var src_exports = {};
|
|
__export(src_exports, {
|
|
reqConfig: () => reqConfig
|
|
});
|
|
module.exports = __toCommonJS(src_exports);
|
|
var import_antd = require("antd");
|
|
var reqConfig = (config) => {
|
|
const {
|
|
authorization,
|
|
showMsg = true,
|
|
onError,
|
|
onExpired,
|
|
expiredCodes = [403, 401, 203]
|
|
} = config || {};
|
|
return {
|
|
timeout: 1e3,
|
|
baseURL: location.origin,
|
|
errorConfig: {
|
|
// @ts-ignore
|
|
errorHandler(response) {
|
|
const { code, message: resMessage } = (response == null ? void 0 : response.data) || {};
|
|
let msg = resMessage || "\u8BF7\u6C42\u5931\u8D25\uFF01";
|
|
if (response.status !== 200) {
|
|
msg = "\u7F51\u7EDC\u5F02\u5E38";
|
|
onError == null ? void 0 : onError(response);
|
|
} else if (expiredCodes.includes(code)) {
|
|
msg = "\u767B\u5F55\u8FC7\u671F\uFF0C\u8BF7\u91CD\u65B0\u767B\u5F55";
|
|
onExpired == null ? void 0 : onExpired(response);
|
|
}
|
|
showMsg && import_antd.message.error(msg);
|
|
},
|
|
// @ts-ignore
|
|
errorThrower(res) {
|
|
console.log("\u62A5\u9519\u5566\uFF1A", res);
|
|
}
|
|
},
|
|
// @ts-ignore
|
|
responseInterceptors: [
|
|
// 一个二元组,第一个元素是 request 拦截器,第二个元素是错误处理
|
|
[
|
|
(response) => {
|
|
const { code } = (response == null ? void 0 : response.data) || {};
|
|
if (code !== 200) {
|
|
return Promise.reject(response);
|
|
}
|
|
return (response == null ? void 0 : response.data) || {};
|
|
}
|
|
]
|
|
],
|
|
// 请求
|
|
requestInterceptors: [
|
|
// 一个二元组,第一个元素是 request 拦截器,第二个元素是错误处理
|
|
[
|
|
(url, options) => {
|
|
const Authorization = authorization || localStorage.getItem("ZHST_AUTH_TOKEN") || "";
|
|
return {
|
|
url,
|
|
options: {
|
|
...options,
|
|
headers: {
|
|
Authorization
|
|
}
|
|
}
|
|
};
|
|
},
|
|
(error) => {
|
|
return Promise.reject(error);
|
|
}
|
|
]
|
|
],
|
|
...config
|
|
};
|
|
};
|
|
// Annotate the CommonJS export names for ESM import in node:
|
|
0 && (module.exports = {
|
|
reqConfig
|
|
});
|