@base-web-kits/base-tools-web 1.4.2 → 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.
@@ -34,10 +34,13 @@ export type UploadTask = {
34
34
  export type UploadConfig = {
35
35
  /** 获取task对象 */
36
36
  onTaskReady?: (task: UploadTask) => void;
37
+ /** 响应类型, 默认'text' */
38
+ responseType?: 'text' | 'json';
37
39
  };
38
40
  export type UploadFail = {
39
41
  message: string;
40
42
  status: number;
43
+ data?: unknown;
41
44
  };
42
45
  /**
43
46
  * 上传文件
@@ -53,8 +56,12 @@ export type UploadFail = {
53
56
  * task.onProgressUpdate((res) => console.log('上传进度:', res.progress)),
54
57
  * });
55
58
  *
59
+ * // 直接返回json对象
60
+ * const json = await uploadFile({ url: 'https://xx', file: file}, { responseType: 'json' });
61
+ *
56
62
  * // 解析上传结果
57
63
  * console.log('uploadFile ok', JSON.parse(res));
58
64
  */
59
- export declare function uploadFile(option: UploadFileOption, config?: UploadConfig & WebApiConfig): Promise<string>;
65
+ export declare function uploadFile(option: UploadFileOption, config?: UploadConfig & WebApiConfig<string, UploadFail>): Promise<string>;
66
+ export declare function uploadFile<T = unknown>(option: UploadFileOption, config?: UploadConfig & WebApiConfig<T, UploadFail>): Promise<T>;
60
67
  //# sourceMappingURL=uploadFile.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"uploadFile.d.ts","sourceRoot":"","sources":["../../src/web/network/uploadFile.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AAE7C;;GAEG;AACH,MAAM,MAAM,gBAAgB,GAAG;IAC7B,aAAa;IACb,GAAG,EAAE,MAAM,CAAC;IAEZ,eAAe;IACf,IAAI,EAAE,IAAI,CAAC;IAEX,mDAAmD;IACnD,IAAI,CAAC,EAAE,MAAM,CAAC;IAEd,UAAU;IACV,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAA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OAAO,EACpC,MAAM,EAAE,gBAAgB,EACxB,MAAM,CAAC,EAAE,YAAY,GAAG,YAAY,CAAC,CAAC,EAAE,UAAU,CAAC,GAClD,OAAO,CAAC,CAAC,CAAC,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@base-web-kits/base-tools-web",
3
- "version": "1.4.2",
3
+ "version": "1.4.3",
4
4
  "sideEffects": false,
5
5
  "description": "Independent Web utilities package built from src/web.",
6
6
  "keywords": [
@@ -42,26 +42,70 @@ export type UploadTask = {
42
42
  abort: () => void;
43
43
  };
44
44
 
45
- export type UploadConfig = {
46
- /** 获取task对象 */
47
- onTaskReady?: (task: UploadTask) => void;
48
- };
49
-
50
- export type UploadFail = {
51
- message: string;
52
- status: number;
53
- };
54
-
55
- function upload(option: UploadFileOption, config?: UploadConfig) {
56
- return new Promise<string>((resolve, reject) => {
57
- const xhr = new XMLHttpRequest();
58
- const { url, file, name = 'file', header, data, timeout = 0 } = option;
59
-
60
- const fail = (error: UploadFail) => reject(error);
61
-
62
- const success = (responseText: string) => {
63
- resolve(responseText);
64
- };
45
+ export type UploadConfig = {
46
+ /** 获取task对象 */
47
+ onTaskReady?: (task: UploadTask) => void;
48
+
49
+ /** 响应类型, 默认'text' */
50
+ responseType?: 'text' | 'json';
51
+ };
52
+
53
+ export type UploadFail = {
54
+ message: string;
55
+ status: number;
56
+ data?: unknown;
57
+ };
58
+
59
+ function parseJsonSafe(text: string) {
60
+ try {
61
+ return JSON.parse(text);
62
+ } catch {
63
+ return null;
64
+ }
65
+ }
66
+
67
+ function getErrorMessage(responseText: string, fallback: string) {
68
+ const parsed = parseJsonSafe(responseText);
69
+ if (parsed && typeof parsed === 'object' && 'message' in parsed) {
70
+ const message = (parsed as { message?: unknown }).message;
71
+ if (typeof message === 'string' && message.trim()) {
72
+ return message;
73
+ }
74
+ }
75
+ return fallback;
76
+ }
77
+
78
+ function upload(
79
+ option: UploadFileOption,
80
+ config?: UploadConfig & { responseType?: 'text' },
81
+ ): Promise<string>;
82
+
83
+ function upload<T = unknown>(
84
+ option: UploadFileOption,
85
+ config?: UploadConfig & { responseType: 'json' },
86
+ ): Promise<T>;
87
+
88
+ function upload<T = unknown>(option: UploadFileOption, config?: UploadConfig) {
89
+ return new Promise<string | T>((resolve, reject) => {
90
+ const xhr = new XMLHttpRequest();
91
+ const { url, file, name = 'file', header, data, timeout = 0 } = option;
92
+ const responseType = config?.responseType ?? 'text';
93
+
94
+ const fail = (error: UploadFail) => reject(error);
95
+
96
+ const success = (responseText: string) => {
97
+ if (responseType === 'json') {
98
+ const parsed = parseJsonSafe(responseText);
99
+ if (parsed === null) {
100
+ fail({ message: '响应不是合法 JSON', status: xhr.status });
101
+ return;
102
+ }
103
+ resolve(parsed as T);
104
+ return;
105
+ }
106
+
107
+ resolve(responseText);
108
+ };
65
109
 
66
110
  // 构造任务对象
67
111
  let onProgressUpdate: OnUploadProgressUpdate;
@@ -84,15 +128,20 @@ function upload(option: UploadFileOption, config?: UploadConfig) {
84
128
  onProgressUpdate?.(ev);
85
129
  };
86
130
 
87
- // 监听事件
88
- xhr.onload = () => {
89
- if (xhr.status >= 200 && xhr.status < 300) {
90
- success(xhr.responseText);
91
- } else {
92
- fail({ message: `上传失败`, status: xhr.status });
93
- }
94
- };
95
- xhr.onerror = () => fail({ message: '网络错误', status: 0 });
131
+ // 监听事件
132
+ xhr.onload = () => {
133
+ const responseText = xhr.responseText || '';
134
+ if (xhr.status >= 200 && xhr.status < 300) {
135
+ success(responseText);
136
+ } else {
137
+ fail({
138
+ message: getErrorMessage(responseText, '上传失败'),
139
+ status: xhr.status,
140
+ data: parseJsonSafe(responseText),
141
+ });
142
+ }
143
+ };
144
+ xhr.onerror = () => fail({ message: '网络错误', status: 0 });
96
145
  xhr.ontimeout = () => fail({ message: '上传超时', status: -1 });
97
146
  xhr.onabort = () => fail({ message: '用户取消', status: -2 });
98
147
 
@@ -133,15 +182,37 @@ function upload(option: UploadFileOption, config?: UploadConfig) {
133
182
  * // 上传
134
183
  * const res = await uploadFile({ url: 'https://xx', file: file});
135
184
  *
136
- * // 监听上传进度
137
- * const res = await uploadFile({ url: 'https://xx', file: file}, {
138
- * onTaskReady: (task) =>
139
- * task.onProgressUpdate((res) => console.log('上传进度:', res.progress)),
140
- * });
141
- *
142
- * // 解析上传结果
143
- * console.log('uploadFile ok', JSON.parse(res));
144
- */
145
- export function uploadFile(option: UploadFileOption, config?: UploadConfig & WebApiConfig) {
146
- return enhanceWebApi(upload, 'uploadFile')(option, config);
147
- }
185
+ * // 监听上传进度
186
+ * const res = await uploadFile({ url: 'https://xx', file: file}, {
187
+ * onTaskReady: (task) =>
188
+ * task.onProgressUpdate((res) => console.log('上传进度:', res.progress)),
189
+ * });
190
+ *
191
+ * // 直接返回json对象
192
+ * const json = await uploadFile({ url: 'https://xx', file: file}, { responseType: 'json' });
193
+ *
194
+ * // 解析上传结果
195
+ * console.log('uploadFile ok', JSON.parse(res));
196
+ */
197
+ export function uploadFile(
198
+ option: UploadFileOption,
199
+ config?: UploadConfig & WebApiConfig<string, UploadFail>,
200
+ ): Promise<string>;
201
+
202
+ export function uploadFile<T = unknown>(
203
+ option: UploadFileOption,
204
+ config?: UploadConfig & WebApiConfig<T, UploadFail>,
205
+ ): Promise<T>;
206
+
207
+ export function uploadFile<T = string>(
208
+ option: UploadFileOption,
209
+ config?: UploadConfig & WebApiConfig<T, UploadFail>,
210
+ ): Promise<T> {
211
+ return enhanceWebApi(
212
+ upload as (
213
+ option: UploadFileOption,
214
+ config?: UploadConfig,
215
+ ) => Promise<T>,
216
+ 'uploadFile',
217
+ )(option, config);
218
+ }