@base-web-kits/base-tools-web 1.4.1 → 1.4.3
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/base-tools-web.umd.global.js +40 -5
- package/dist/base-tools-web.umd.global.js.map +1 -1
- package/dist/index.cjs +40 -5
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +8 -1
- package/dist/index.mjs +40 -5
- package/dist/index.mjs.map +1 -1
- package/dist/network/uploadFile.d.ts +8 -1
- package/dist/network/uploadFile.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/web/network/uploadFile.ts +112 -41
package/dist/index.d.cts
CHANGED
|
@@ -585,10 +585,13 @@ type UploadTask = {
|
|
|
585
585
|
type UploadConfig = {
|
|
586
586
|
/** 获取task对象 */
|
|
587
587
|
onTaskReady?: (task: UploadTask) => void;
|
|
588
|
+
/** 响应类型, 默认'text' */
|
|
589
|
+
responseType?: 'text' | 'json';
|
|
588
590
|
};
|
|
589
591
|
type UploadFail = {
|
|
590
592
|
message: string;
|
|
591
593
|
status: number;
|
|
594
|
+
data?: unknown;
|
|
592
595
|
};
|
|
593
596
|
/**
|
|
594
597
|
* 上传文件
|
|
@@ -604,10 +607,14 @@ type UploadFail = {
|
|
|
604
607
|
* task.onProgressUpdate((res) => console.log('上传进度:', res.progress)),
|
|
605
608
|
* });
|
|
606
609
|
*
|
|
610
|
+
* // 直接返回json对象
|
|
611
|
+
* const json = await uploadFile({ url: 'https://xx', file: file}, { responseType: 'json' });
|
|
612
|
+
*
|
|
607
613
|
* // 解析上传结果
|
|
608
614
|
* console.log('uploadFile ok', JSON.parse(res));
|
|
609
615
|
*/
|
|
610
|
-
declare function uploadFile(option: UploadFileOption, config?: UploadConfig & WebApiConfig): Promise<string>;
|
|
616
|
+
declare function uploadFile(option: UploadFileOption, config?: UploadConfig & WebApiConfig<string, UploadFail>): Promise<string>;
|
|
617
|
+
declare function uploadFile<T = unknown>(option: UploadFileOption, config?: UploadConfig & WebApiConfig<T, UploadFail>): Promise<T>;
|
|
611
618
|
|
|
612
619
|
/**
|
|
613
620
|
* 写入 localStorage(自动 JSON 序列化)
|
package/dist/index.mjs
CHANGED
|
@@ -2341,13 +2341,40 @@ function enhanceWebApi(webApi, apiName) {
|
|
|
2341
2341
|
}
|
|
2342
2342
|
|
|
2343
2343
|
// src/web/network/uploadFile.ts
|
|
2344
|
+
function parseJsonSafe(text) {
|
|
2345
|
+
try {
|
|
2346
|
+
return JSON.parse(text);
|
|
2347
|
+
} catch (e) {
|
|
2348
|
+
return null;
|
|
2349
|
+
}
|
|
2350
|
+
}
|
|
2351
|
+
function getErrorMessage(responseText, fallback) {
|
|
2352
|
+
const parsed = parseJsonSafe(responseText);
|
|
2353
|
+
if (parsed && typeof parsed === "object" && "message" in parsed) {
|
|
2354
|
+
const message = parsed.message;
|
|
2355
|
+
if (typeof message === "string" && message.trim()) {
|
|
2356
|
+
return message;
|
|
2357
|
+
}
|
|
2358
|
+
}
|
|
2359
|
+
return fallback;
|
|
2360
|
+
}
|
|
2344
2361
|
function upload(option, config) {
|
|
2345
2362
|
return new Promise((resolve, reject) => {
|
|
2346
|
-
var _a;
|
|
2363
|
+
var _a, _b;
|
|
2347
2364
|
const xhr = new XMLHttpRequest();
|
|
2348
2365
|
const { url, file, name = "file", header, data, timeout = 0 } = option;
|
|
2366
|
+
const responseType = (_a = config == null ? void 0 : config.responseType) != null ? _a : "text";
|
|
2349
2367
|
const fail = (error) => reject(error);
|
|
2350
2368
|
const success = (responseText) => {
|
|
2369
|
+
if (responseType === "json") {
|
|
2370
|
+
const parsed = parseJsonSafe(responseText);
|
|
2371
|
+
if (parsed === null) {
|
|
2372
|
+
fail({ message: "\u54CD\u5E94\u4E0D\u662F\u5408\u6CD5 JSON", status: xhr.status });
|
|
2373
|
+
return;
|
|
2374
|
+
}
|
|
2375
|
+
resolve(parsed);
|
|
2376
|
+
return;
|
|
2377
|
+
}
|
|
2351
2378
|
resolve(responseText);
|
|
2352
2379
|
};
|
|
2353
2380
|
let onProgressUpdate;
|
|
@@ -2357,7 +2384,7 @@ function upload(option, config) {
|
|
|
2357
2384
|
},
|
|
2358
2385
|
abort: () => xhr.abort()
|
|
2359
2386
|
};
|
|
2360
|
-
(
|
|
2387
|
+
(_b = config == null ? void 0 : config.onTaskReady) == null ? void 0 : _b.call(config, task);
|
|
2361
2388
|
xhr.upload.onprogress = (e) => {
|
|
2362
2389
|
if (!e.lengthComputable) return;
|
|
2363
2390
|
const ev = {
|
|
@@ -2368,10 +2395,15 @@ function upload(option, config) {
|
|
|
2368
2395
|
onProgressUpdate == null ? void 0 : onProgressUpdate(ev);
|
|
2369
2396
|
};
|
|
2370
2397
|
xhr.onload = () => {
|
|
2398
|
+
const responseText = xhr.responseText || "";
|
|
2371
2399
|
if (xhr.status >= 200 && xhr.status < 300) {
|
|
2372
|
-
success(
|
|
2400
|
+
success(responseText);
|
|
2373
2401
|
} else {
|
|
2374
|
-
fail({
|
|
2402
|
+
fail({
|
|
2403
|
+
message: getErrorMessage(responseText, "\u4E0A\u4F20\u5931\u8D25"),
|
|
2404
|
+
status: xhr.status,
|
|
2405
|
+
data: parseJsonSafe(responseText)
|
|
2406
|
+
});
|
|
2375
2407
|
}
|
|
2376
2408
|
};
|
|
2377
2409
|
xhr.onerror = () => fail({ message: "\u7F51\u7EDC\u9519\u8BEF", status: 0 });
|
|
@@ -2395,7 +2427,10 @@ function upload(option, config) {
|
|
|
2395
2427
|
});
|
|
2396
2428
|
}
|
|
2397
2429
|
function uploadFile(option, config) {
|
|
2398
|
-
return enhanceWebApi(
|
|
2430
|
+
return enhanceWebApi(
|
|
2431
|
+
upload,
|
|
2432
|
+
"uploadFile"
|
|
2433
|
+
)(option, config);
|
|
2399
2434
|
}
|
|
2400
2435
|
|
|
2401
2436
|
// src/web/storage/index.ts
|