@base-web-kits/base-tools-web 1.3.8 → 1.3.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/README.md CHANGED
@@ -128,40 +128,32 @@ npx skills add gancao-web/base-tools
128
128
 
129
129
  本工具库和相关依赖可能涉及的新特性及其最低兼容版本:
130
130
 
131
- | 特性 | ES 版本 | 最低兼容版本 (Browser/OS) |
132
- | :---------------------------------------- | :------ | :---------------------------------- |
133
- | **Array.prototype.includes** | ES2016 | Chrome 47+, iOS 9+, Android 6.0+ |
134
- | **Object.values / entries** | ES2017 | Chrome 54+, iOS 10.3+, Android 7.0+ |
135
- | **String.prototype.padStart / padEnd** | ES2017 | Chrome 57+, iOS 10.3+, Android 8.0+ |
136
- | **Object.getOwnPropertyDescriptors** | ES2017 | Chrome 54+, iOS 10.3+, Android 7.0+ |
137
- | **Promise.prototype.finally** | ES2018 | Chrome 63+, iOS 11.3+, Android 9.0+ |
138
- | **Async Iterator (Symbol.asyncIterator)** | ES2018 | Chrome 63+, iOS 12.0+, Android 9.0+ |
139
- | **Object.fromEntries** | ES2019 | Chrome 73+, iOS 12.2+, Android 10+ |
140
- | **Array.prototype.flat / flatMap** | ES2019 | Chrome 69+, iOS 12.0+, Android 9.0+ |
141
- | **String.prototype.trimStart / trimEnd** | ES2019 | Chrome 66+, iOS 12.0+, Android 9.0+ |
142
- | **Promise.allSettled** | ES2020 | Chrome 76+, iOS 13.0+, Android 10+ |
143
- | **String.prototype.matchAll** | ES2020 | Chrome 80+, iOS 13.0+, Android 10+ |
144
- | **BigInt** | ES2020 | Chrome 67+, iOS 14.0+, Android 11+ |
145
- | **globalThis** | ES2020 | Chrome 71+, iOS 12.2+, Android 9.0+ |
146
- | **String.prototype.replaceAll** | ES2021 | Chrome 85+, iOS 13.4+, Android 11+ |
147
- | **Promise.any** | ES2021 | Chrome 85+, iOS 14.0+, Android 11+ |
148
- | **WeakRef / FinalizationRegistry** | ES2021 | Chrome 84+, iOS 14.5+, Android 11+ |
149
- | **Array/String.prototype.at** | ES2022 | Chrome 92+, iOS 15.4+, Android 12+ |
150
- | **Error.prototype.cause** | ES2022 | Chrome 93+, iOS 15.0+, Android 12+ |
151
- | **Object.hasOwn** | ES2022 | Chrome 93+, iOS 15.4+, Android 12+ |
131
+ | 特性 | ES 版本 | 最低兼容版本 (Browser/OS) |
132
+ | :--------------------------------------- | :------ | :---------------------------------- |
133
+ | **Object.values / entries** | ES2017 | Chrome 54+, iOS 10.3+, Android 7.0+ |
134
+ | **String.prototype.padStart / padEnd** | ES2017 | Chrome 57+, iOS 10.3+, Android 8.0+ |
135
+ | **Object.getOwnPropertyDescriptors** | ES2017 | Chrome 54+, iOS 10.3+, Android 7.0+ |
136
+ | **Promise.prototype.finally** | ES2018 | Chrome 63+, iOS 11.3+, Android 9.0+ |
137
+ | **Symbol.asyncIterator** | ES2018 | Chrome 63+, iOS 12.0+, Android 9.0+ |
138
+ | **Object.fromEntries** | ES2019 | Chrome 73+, iOS 12.2+, Android 10+ |
139
+ | **Array.prototype.flat / flatMap** | ES2019 | Chrome 69+, iOS 12.0+, Android 9.0+ |
140
+ | **String.prototype.trimStart / trimEnd** | ES2019 | Chrome 66+, iOS 12.0+, Android 9.0+ |
141
+ | **Promise.allSettled** | ES2020 | Chrome 76+, iOS 13.0+, Android 10+ |
142
+ | **String.prototype.matchAll** | ES2020 | Chrome 80+, iOS 13.0+, Android 10+ |
143
+ | **BigInt** | ES2020 | Chrome 67+, iOS 14.0+, Android 11+ |
144
+ | **globalThis** | ES2020 | Chrome 71+, iOS 12.2+, Android 9.0+ |
145
+ | **String.prototype.replaceAll** | ES2021 | Chrome 85+, iOS 13.4+, Android 11+ |
146
+ | **Promise.any** | ES2021 | Chrome 85+, iOS 14.0+, Android 11+ |
147
+ | **WeakRef / FinalizationRegistry** | ES2021 | Chrome 84+, iOS 14.5+, Android 11+ |
148
+ | **Array/String.prototype.at** | ES2022 | Chrome 92+, iOS 15.4+, Android 12+ |
149
+ | **Error.prototype.cause** | ES2022 | Chrome 93+, iOS 15.0+, Android 12+ |
150
+ | **Object.hasOwn** | ES2022 | Chrome 93+, iOS 15.4+, Android 12+ |
152
151
 
153
152
  本工具库构建目标为 **ES2015+**。但不内置 Polyfill, 如需支持低版本浏览器, 请务必在项目中配置 Polyfill。
154
153
 
155
154
  ### 配置 Polyfill
156
155
 
157
- 需经过两步:
158
-
159
- 1. 配置打包工具, 将ES6+代码转换为ES5。
160
- 2. 在入口文件顶部引入 `core-js` 进行 Polyfill。
161
-
162
- 以 Vite 项目为例:
163
-
164
- 1. 使用 `@vitejs/plugin-legacy` 插件,将ES6+代码转换为ES5。
156
+ 以 Vite 项目为例, 使用 `@vitejs/plugin-legacy` 插件自动按需注入 Polyfill。
165
157
 
166
158
  ```ts
167
159
  // vite.config.ts
@@ -179,15 +171,9 @@ export default {
179
171
  // 与Element对齐: https://element-plus.org/zh-CN/guide/installation
180
172
  targets: ['Chrome >= 64', 'Edge >= 79', 'Firefox >= 78', 'Safari >= 12', 'not IE 11'],
181
173
 
182
- renderLegacyChunks: true, // 必须开启, 确保生成 ES5 兼容包
183
- modernPolyfills: false, // 关闭按需注入polyfill, 因为legacy无法全量分析项目代码间接依赖了哪些新API, 需在入口文件 main.ts 全量引入 'core-js/stable'
174
+ // 按需自动引入polyfill (需使用vite3以上版本,因为vite2.x缺少新的ES特性,如Object.hasOwn)
175
+ modernPolyfills: true,
184
176
  }),
185
177
  ],
186
178
  };
187
179
  ```
188
-
189
- 2. 入口文件 main.ts 顶部引入 core-js/stable 进行 Polyfill
190
-
191
- ```ts
192
- import 'core-js/stable'; // 安装命令: npm install core-js
193
- ```
@@ -2064,7 +2064,7 @@ var baseToolsWeb = (() => {
2064
2064
  onTaskReady == null ? void 0 : onTaskReady(task);
2065
2065
  return new Promise((resolve, reject) => {
2066
2066
  const execute = () => __async(null, null, function* () {
2067
- var _a, _b, _c, _d, _e, _f, _g, _h;
2067
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i;
2068
2068
  const isGet = method === "GET";
2069
2069
  const isObjectData = isPlainObject(data);
2070
2070
  const isArrayData = !isObjectData && Array.isArray(data);
@@ -2118,7 +2118,7 @@ var baseToolsWeb = (() => {
2118
2118
  controller.abort();
2119
2119
  }, timeout);
2120
2120
  try {
2121
- const response = yield fetch(fillUrl, {
2121
+ let response = yield fetch(fillUrl, {
2122
2122
  method,
2123
2123
  headers: fillHeader,
2124
2124
  body: fillBody,
@@ -2126,9 +2126,20 @@ var baseToolsWeb = (() => {
2126
2126
  });
2127
2127
  if (!response.ok) {
2128
2128
  if (showLoading) (_b = appConfig2.hideLoading) == null ? void 0 : _b.call(appConfig2);
2129
- throw new Error(`HTTP Error ${response.status}: ${response.statusText}`);
2129
+ let errorText;
2130
+ try {
2131
+ errorText = yield response.text();
2132
+ JSON.parse(errorText);
2133
+ } catch (e) {
2134
+ throw new Error(`HTTP Error ${response.status}`);
2135
+ }
2136
+ response = new Response(errorText, {
2137
+ status: response.status,
2138
+ statusText: response.statusText,
2139
+ headers: response.headers
2140
+ });
2130
2141
  }
2131
- if (enableChunked) {
2142
+ if (enableChunked && response.ok) {
2132
2143
  if (showLoading) (_c = appConfig2.hideLoading) == null ? void 0 : _c.call(appConfig2);
2133
2144
  const res2 = yield handleStreamResponse(response, sseTask);
2134
2145
  logRequestInfo({ status: "success", config: logConfig, startTime, res: res2 });
@@ -2155,17 +2166,18 @@ var baseToolsWeb = (() => {
2155
2166
  reject(res);
2156
2167
  }
2157
2168
  } catch (e) {
2169
+ if (showLoading) (_g = appConfig2.hideLoading) == null ? void 0 : _g.call(appConfig2);
2158
2170
  const status = "fail";
2159
2171
  const isAbortError = e instanceof DOMException && e.name === "AbortError";
2160
2172
  if (isAbortError && isTimeout) {
2161
- if (toastError) (_g = appConfig2.toast) == null ? void 0 : _g.call(appConfig2, { status, msg: "\u8BF7\u6C42\u8D85\u65F6" });
2173
+ if (toastError) (_h = appConfig2.toast) == null ? void 0 : _h.call(appConfig2, { status, msg: "\u8BF7\u6C42\u8D85\u65F6" });
2162
2174
  const timeoutError = new Error("Request Timeout");
2163
2175
  logRequestInfo({ status, config: logConfig, startTime, e: timeoutError });
2164
2176
  reject(timeoutError);
2165
2177
  return;
2166
2178
  }
2167
2179
  if (!isAbortError && toastError)
2168
- (_h = appConfig2.toast) == null ? void 0 : _h.call(appConfig2, { status, msg: `\u8BF7\u6C42\u5931\u8D25,${String(e)}` });
2180
+ (_i = appConfig2.toast) == null ? void 0 : _i.call(appConfig2, { status, msg: `\u8BF7\u6C42\u5931\u8D25,${String(e)}` });
2169
2181
  logRequestInfo({ status, config: logConfig, startTime, e });
2170
2182
  reject(e);
2171
2183
  } finally {