best-unit 1.2.8 → 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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "best-unit",
3
3
  "private": false,
4
- "version": "1.2.8",
4
+ "version": "1.2.10",
5
5
  "type": "module",
6
6
  "main": "dist/best-unit.cjs",
7
7
  "module": "dist/best-unit.js",
@@ -15,7 +15,7 @@ export interface CreateAxiosOptions {
15
15
 
16
16
  export function createAxiosInstance(options: CreateAxiosOptions = {}) {
17
17
  // 根据 fund_unit_params 中的 env 参数选择 API URL
18
- const fundUnitParams = JSON.parse(
18
+ let fundUnitParams = JSON.parse(
19
19
  sessionStorage.getItem("fund_unit_params") || "{}"
20
20
  );
21
21
 
@@ -44,7 +44,11 @@ export function createAxiosInstance(options: CreateAxiosOptions = {}) {
44
44
 
45
45
  // 请求拦截:加 token、国际化
46
46
  instance.interceptors.request.use((config: InternalAxiosRequestConfig) => {
47
- const { token, locale } = fundUnitParams;
47
+ // 每次请求读取最新的 fund_unit_params,避免闭包老数据
48
+ const latestParams = JSON.parse(
49
+ sessionStorage.getItem("fund_unit_params") || "{}"
50
+ );
51
+ const { token, locale } = latestParams || {};
48
52
  config.headers = {
49
53
  ...config.headers,
50
54
  Authorization: token,
@@ -93,13 +97,63 @@ export function createAxiosInstance(options: CreateAxiosOptions = {}) {
93
97
 
94
98
  // 缓存 axios 实例
95
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
+ }
96
119
 
97
120
  // 获取 axios 实例的函数
98
121
  export function http(): AxiosInstance {
99
- if (!httpInstance) {
100
- httpInstance = createAxiosInstance();
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;
101
136
  }
102
- return httpInstance;
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;
103
157
  }
104
158
 
105
159
  // 重置 axios 实例(当 fund_unit_params 更新时调用)