best-unit 1.2.9 → 1.2.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/best-unit.cjs +8 -8
- package/dist/best-unit.js +745 -719
- package/package.json +1 -1
- package/src/api/axiosInstance.ts +58 -22
package/package.json
CHANGED
package/src/api/axiosInstance.ts
CHANGED
|
@@ -19,24 +19,6 @@ export function createAxiosInstance(options: CreateAxiosOptions = {}) {
|
|
|
19
19
|
sessionStorage.getItem("fund_unit_params") || "{}"
|
|
20
20
|
);
|
|
21
21
|
|
|
22
|
-
// 简单同步轮询,尽量在不改动对外调用方式的情况下等待参数就绪
|
|
23
|
-
if (!fundUnitParams || Object.keys(fundUnitParams).length === 0) {
|
|
24
|
-
const start = Date.now();
|
|
25
|
-
const maxWaitMs = 3000; // 最多等待 3 秒
|
|
26
|
-
while (Date.now() - start < maxWaitMs) {
|
|
27
|
-
try {
|
|
28
|
-
const raw = sessionStorage.getItem("fund_unit_params") || "{}";
|
|
29
|
-
const obj = JSON.parse(raw);
|
|
30
|
-
if (obj && Object.keys(obj).length > 0) {
|
|
31
|
-
fundUnitParams = obj;
|
|
32
|
-
break;
|
|
33
|
-
}
|
|
34
|
-
} catch (_) {
|
|
35
|
-
// 略过解析错误,继续等待
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
|
|
40
22
|
console.log("fundUnitParams", fundUnitParams);
|
|
41
23
|
const { env } = fundUnitParams;
|
|
42
24
|
|
|
@@ -62,7 +44,11 @@ export function createAxiosInstance(options: CreateAxiosOptions = {}) {
|
|
|
62
44
|
|
|
63
45
|
// 请求拦截:加 token、国际化
|
|
64
46
|
instance.interceptors.request.use((config: InternalAxiosRequestConfig) => {
|
|
65
|
-
|
|
47
|
+
// 每次请求读取最新的 fund_unit_params,避免闭包老数据
|
|
48
|
+
const latestParams = JSON.parse(
|
|
49
|
+
sessionStorage.getItem("fund_unit_params") || "{}"
|
|
50
|
+
);
|
|
51
|
+
const { token, locale } = latestParams || {};
|
|
66
52
|
config.headers = {
|
|
67
53
|
...config.headers,
|
|
68
54
|
Authorization: token,
|
|
@@ -111,13 +97,63 @@ export function createAxiosInstance(options: CreateAxiosOptions = {}) {
|
|
|
111
97
|
|
|
112
98
|
// 缓存 axios 实例
|
|
113
99
|
let httpInstance: AxiosInstance | null = null;
|
|
100
|
+
let creatingPromise: Promise<void> | null = null;
|
|
101
|
+
|
|
102
|
+
// 等待 fund_unit_params 不为空对象(无超时)
|
|
103
|
+
function waitUntilParamsReady(): Promise<void> {
|
|
104
|
+
return new Promise((resolve) => {
|
|
105
|
+
const check = () => {
|
|
106
|
+
try {
|
|
107
|
+
const obj = JSON.parse(
|
|
108
|
+
sessionStorage.getItem("fund_unit_params") || "{}"
|
|
109
|
+
);
|
|
110
|
+
if (obj && Object.keys(obj).length > 0) return resolve();
|
|
111
|
+
} catch (_) {
|
|
112
|
+
// ignore
|
|
113
|
+
}
|
|
114
|
+
setTimeout(check, 50);
|
|
115
|
+
};
|
|
116
|
+
check();
|
|
117
|
+
});
|
|
118
|
+
}
|
|
114
119
|
|
|
115
120
|
// 获取 axios 实例的函数
|
|
116
121
|
export function http(): AxiosInstance {
|
|
117
|
-
if (
|
|
118
|
-
|
|
122
|
+
if (httpInstance) return httpInstance;
|
|
123
|
+
|
|
124
|
+
async function ensureInstanceReady(): Promise<void> {
|
|
125
|
+
if (httpInstance) return;
|
|
126
|
+
if (!creatingPromise) {
|
|
127
|
+
creatingPromise = waitUntilParamsReady()
|
|
128
|
+
.then(() => {
|
|
129
|
+
httpInstance = createAxiosInstance();
|
|
130
|
+
})
|
|
131
|
+
.finally(() => {
|
|
132
|
+
creatingPromise = null;
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
return creatingPromise;
|
|
119
136
|
}
|
|
120
|
-
|
|
137
|
+
|
|
138
|
+
const proxy: any = {};
|
|
139
|
+
[
|
|
140
|
+
"request",
|
|
141
|
+
"get",
|
|
142
|
+
"delete",
|
|
143
|
+
"head",
|
|
144
|
+
"options",
|
|
145
|
+
"post",
|
|
146
|
+
"put",
|
|
147
|
+
"patch",
|
|
148
|
+
].forEach((method) => {
|
|
149
|
+
proxy[method] = async (...args: any[]) => {
|
|
150
|
+
await ensureInstanceReady();
|
|
151
|
+
// @ts-ignore
|
|
152
|
+
return (httpInstance as any)[method](...args);
|
|
153
|
+
};
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
return proxy as AxiosInstance;
|
|
121
157
|
}
|
|
122
158
|
|
|
123
159
|
// 重置 axios 实例(当 fund_unit_params 更新时调用)
|