best-unit 1.2.11 → 1.2.13

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.11",
4
+ "version": "1.2.13",
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
- let fundUnitParams = JSON.parse(
18
+ const fundUnitParams = JSON.parse(
19
19
  sessionStorage.getItem("fund_unit_params") || "{}"
20
20
  );
21
21
 
@@ -42,35 +42,17 @@ export function createAxiosInstance(options: CreateAxiosOptions = {}) {
42
42
 
43
43
  const instance: AxiosInstance = axios.create({ baseURL, timeout });
44
44
 
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
- }
45
+ // 请求拦截:加 token、国际化
46
+ instance.interceptors.request.use((config: InternalAxiosRequestConfig) => {
47
+ const { token, locale } = fundUnitParams;
48
+ config.headers = {
49
+ ...config.headers,
50
+ Authorization: token,
51
+ "x-locale": locale === Locale.ZH ? "zh-CN" : "en-US",
52
+ } as any;
59
53
 
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
- );
54
+ return config;
55
+ });
74
56
 
75
57
  // 响应拦截:code=0判定成功,其他走 onError
76
58
  instance.interceptors.response.use(
@@ -111,63 +93,13 @@ export function createAxiosInstance(options: CreateAxiosOptions = {}) {
111
93
 
112
94
  // 缓存 axios 实例
113
95
  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
- }
133
96
 
134
97
  // 获取 axios 实例的函数
135
98
  export function http(): AxiosInstance {
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;
99
+ if (!httpInstance) {
100
+ httpInstance = createAxiosInstance();
150
101
  }
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;
102
+ return httpInstance;
171
103
  }
172
104
 
173
105
  // 重置 axios 实例(当 fund_unit_params 更新时调用)