best-unit 1.2.9 → 1.2.11
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 +9 -9
- package/dist/best-unit.js +929 -893
- package/package.json +1 -1
- package/src/api/axiosInstance.ts +81 -31
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
|
|
|
@@ -60,17 +42,35 @@ export function createAxiosInstance(options: CreateAxiosOptions = {}) {
|
|
|
60
42
|
|
|
61
43
|
const instance: AxiosInstance = axios.create({ baseURL, timeout });
|
|
62
44
|
|
|
63
|
-
//
|
|
64
|
-
instance.interceptors.request.use(
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
45
|
+
// 请求拦截:等待参数就绪、加 token、国际化
|
|
46
|
+
instance.interceptors.request.use(
|
|
47
|
+
async (config: InternalAxiosRequestConfig) => {
|
|
48
|
+
// 若参数未就绪,则等待至就绪
|
|
49
|
+
try {
|
|
50
|
+
const obj = JSON.parse(
|
|
51
|
+
sessionStorage.getItem("fund_unit_params") || "{}"
|
|
52
|
+
);
|
|
53
|
+
if (!obj || Object.keys(obj).length === 0) {
|
|
54
|
+
await waitUntilParamsReady();
|
|
55
|
+
}
|
|
56
|
+
} catch (_) {
|
|
57
|
+
await waitUntilParamsReady();
|
|
58
|
+
}
|
|
71
59
|
|
|
72
|
-
|
|
73
|
-
|
|
60
|
+
// 每次请求读取最新的 fund_unit_params,避免闭包老数据
|
|
61
|
+
const latestParams = JSON.parse(
|
|
62
|
+
sessionStorage.getItem("fund_unit_params") || "{}"
|
|
63
|
+
);
|
|
64
|
+
const { token, locale } = latestParams || {};
|
|
65
|
+
config.headers = {
|
|
66
|
+
...config.headers,
|
|
67
|
+
Authorization: token,
|
|
68
|
+
"x-locale": locale === Locale.ZH ? "zh-CN" : "en-US",
|
|
69
|
+
} as any;
|
|
70
|
+
|
|
71
|
+
return config;
|
|
72
|
+
}
|
|
73
|
+
);
|
|
74
74
|
|
|
75
75
|
// 响应拦截:code=0判定成功,其他走 onError
|
|
76
76
|
instance.interceptors.response.use(
|
|
@@ -111,13 +111,63 @@ export function createAxiosInstance(options: CreateAxiosOptions = {}) {
|
|
|
111
111
|
|
|
112
112
|
// 缓存 axios 实例
|
|
113
113
|
let httpInstance: AxiosInstance | null = null;
|
|
114
|
+
let creatingPromise: Promise<void> | null = null;
|
|
115
|
+
|
|
116
|
+
// 等待 fund_unit_params 不为空对象(无超时)
|
|
117
|
+
function waitUntilParamsReady(): Promise<void> {
|
|
118
|
+
return new Promise((resolve) => {
|
|
119
|
+
const check = () => {
|
|
120
|
+
try {
|
|
121
|
+
const obj = JSON.parse(
|
|
122
|
+
sessionStorage.getItem("fund_unit_params") || "{}"
|
|
123
|
+
);
|
|
124
|
+
if (obj && Object.keys(obj).length > 0) return resolve();
|
|
125
|
+
} catch (_) {
|
|
126
|
+
// ignore
|
|
127
|
+
}
|
|
128
|
+
setTimeout(check, 50);
|
|
129
|
+
};
|
|
130
|
+
check();
|
|
131
|
+
});
|
|
132
|
+
}
|
|
114
133
|
|
|
115
134
|
// 获取 axios 实例的函数
|
|
116
135
|
export function http(): AxiosInstance {
|
|
117
|
-
if (
|
|
118
|
-
|
|
136
|
+
if (httpInstance) return httpInstance;
|
|
137
|
+
|
|
138
|
+
async function ensureInstanceReady(): Promise<void> {
|
|
139
|
+
if (httpInstance) return;
|
|
140
|
+
if (!creatingPromise) {
|
|
141
|
+
creatingPromise = waitUntilParamsReady()
|
|
142
|
+
.then(() => {
|
|
143
|
+
httpInstance = createAxiosInstance();
|
|
144
|
+
})
|
|
145
|
+
.finally(() => {
|
|
146
|
+
creatingPromise = null;
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
return creatingPromise;
|
|
119
150
|
}
|
|
120
|
-
|
|
151
|
+
|
|
152
|
+
const proxy: any = {};
|
|
153
|
+
[
|
|
154
|
+
"request",
|
|
155
|
+
"get",
|
|
156
|
+
"delete",
|
|
157
|
+
"head",
|
|
158
|
+
"options",
|
|
159
|
+
"post",
|
|
160
|
+
"put",
|
|
161
|
+
"patch",
|
|
162
|
+
].forEach((method) => {
|
|
163
|
+
proxy[method] = async (...args: any[]) => {
|
|
164
|
+
await ensureInstanceReady();
|
|
165
|
+
// @ts-ignore
|
|
166
|
+
return (httpInstance as any)[method](...args);
|
|
167
|
+
};
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
return proxy as AxiosInstance;
|
|
121
171
|
}
|
|
122
172
|
|
|
123
173
|
// 重置 axios 实例(当 fund_unit_params 更新时调用)
|