@base-web-kits/base-tools-web 1.4.3 → 1.4.5

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.
@@ -15,6 +15,8 @@ export type UploadFileOption = {
15
15
  data?: Record<string, string | number>;
16
16
  /** 超时时间,单位 ms,默认 0(不超时) */
17
17
  timeout?: number;
18
+ /** 响应类型, 默认'text' */
19
+ responseType?: 'text' | 'json';
18
20
  };
19
21
  export type OnUploadProgressUpdate = (res: UploadProgressEvent) => void;
20
22
  export type UploadProgressEvent = {
@@ -34,8 +36,6 @@ export type UploadTask = {
34
36
  export type UploadConfig = {
35
37
  /** 获取task对象 */
36
38
  onTaskReady?: (task: UploadTask) => void;
37
- /** 响应类型, 默认'text' */
38
- responseType?: 'text' | 'json';
39
39
  };
40
40
  export type UploadFail = {
41
41
  message: string;
@@ -57,11 +57,10 @@ export type UploadFail = {
57
57
  * });
58
58
  *
59
59
  * // 直接返回json对象
60
- * const json = await uploadFile({ url: 'https://xx', file: file}, { responseType: 'json' });
60
+ * const json = await uploadFile({ url: 'https://xx', file: file, responseType: 'json' });
61
61
  *
62
62
  * // 解析上传结果
63
63
  * console.log('uploadFile ok', JSON.parse(res));
64
64
  */
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>;
65
+ export declare function uploadFile<T = string>(option: UploadFileOption, config?: UploadConfig & WebApiConfig<T, UploadFail>): Promise<T>;
67
66
  //# 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;I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,KAAK,EAAE,MAAM,IAAI,CAAC;CACnB,CAAC;AAEF,MAAM,MAAM,YAAY,GAAG;IACzB,eAAe;IACf,WAAW,CAAC,EAAE,CAAC,IAAI,EAAE,UAAU,KAAK,IAAI,CAAC;CAC1C,CAAC;AAEF,MAAM,MAAM,UAAU,GAAG;IACvB,OAAO,EAAE,MAAM,CAAC;IAChB,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,CAAC,EAAE,OAAO,CAAC;CAChB,CAAC;AA6GF;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,UAAU,CAAC,CAAC,GAAG,MAAM,EACnC,MAAM,EAAE,gBAAgB,EACxB,MAAM,CAAC,EAAE,YAAY,GAAG,YAAY,CAAC,CAAC,EAAE,UAAU,CAAC,GAClD,OAAO,CAAC,CAAC,CAAC,CAKZ"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@base-web-kits/base-tools-web",
3
- "version": "1.4.3",
3
+ "version": "1.4.5",
4
4
  "sideEffects": false,
5
5
  "description": "Independent Web utilities package built from src/web.",
6
6
  "keywords": [
@@ -22,6 +22,9 @@ export type UploadFileOption = {
22
22
 
23
23
  /** 超时时间,单位 ms,默认 0(不超时) */
24
24
  timeout?: number;
25
+
26
+ /** 响应类型, 默认'text' */
27
+ responseType?: 'text' | 'json';
25
28
  };
26
29
 
27
30
  export type OnUploadProgressUpdate = (res: UploadProgressEvent) => void;
@@ -42,70 +45,56 @@ export type UploadTask = {
42
45
  abort: () => void;
43
46
  };
44
47
 
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
- };
48
+ export type UploadConfig = {
49
+ /** 获取task对象 */
50
+ onTaskReady?: (task: UploadTask) => void;
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<T = unknown>(option: UploadFileOption, config?: UploadConfig) {
79
+ return new Promise<string | T>((resolve, reject) => {
80
+ const xhr = new XMLHttpRequest();
81
+ const { url, file, name = 'file', header, data, timeout = 0, responseType = 'text' } = option;
82
+
83
+ const fail = (error: UploadFail) => reject(error);
84
+
85
+ const success = (responseText: string) => {
86
+ if (responseType === 'json') {
87
+ const parsed = parseJsonSafe(responseText);
88
+ if (parsed === null) {
89
+ fail({ message: '响应不是合法 JSON', status: xhr.status });
90
+ return;
91
+ }
92
+ resolve(parsed as T);
93
+ return;
94
+ }
95
+
96
+ resolve(responseText);
97
+ };
109
98
 
110
99
  // 构造任务对象
111
100
  let onProgressUpdate: OnUploadProgressUpdate;
@@ -128,20 +117,20 @@ function upload<T = unknown>(option: UploadFileOption, config?: UploadConfig) {
128
117
  onProgressUpdate?.(ev);
129
118
  };
130
119
 
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 });
120
+ // 监听事件
121
+ xhr.onload = () => {
122
+ const responseText = xhr.responseText || '';
123
+ if (xhr.status >= 200 && xhr.status < 300) {
124
+ success(responseText);
125
+ } else {
126
+ fail({
127
+ message: getErrorMessage(responseText, '上传失败'),
128
+ status: xhr.status,
129
+ data: parseJsonSafe(responseText),
130
+ });
131
+ }
132
+ };
133
+ xhr.onerror = () => fail({ message: '网络错误', status: 0 });
145
134
  xhr.ontimeout = () => fail({ message: '上传超时', status: -1 });
146
135
  xhr.onabort = () => fail({ message: '用户取消', status: -2 });
147
136
 
@@ -182,37 +171,24 @@ function upload<T = unknown>(option: UploadFileOption, config?: UploadConfig) {
182
171
  * // 上传
183
172
  * const res = await uploadFile({ url: 'https://xx', file: file});
184
173
  *
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
- }
174
+ * // 监听上传进度
175
+ * const res = await uploadFile({ url: 'https://xx', file: file}, {
176
+ * onTaskReady: (task) =>
177
+ * task.onProgressUpdate((res) => console.log('上传进度:', res.progress)),
178
+ * });
179
+ *
180
+ * // 直接返回json对象
181
+ * const json = await uploadFile({ url: 'https://xx', file: file, responseType: 'json' });
182
+ *
183
+ * // 解析上传结果
184
+ * console.log('uploadFile ok', JSON.parse(res));
185
+ */
186
+ export function uploadFile<T = string>(
187
+ option: UploadFileOption,
188
+ config?: UploadConfig & WebApiConfig<T, UploadFail>,
189
+ ): Promise<T> {
190
+ return enhanceWebApi(
191
+ upload as (option: UploadFileOption, config?: UploadConfig) => Promise<T>,
192
+ 'uploadFile',
193
+ )(option, config);
194
+ }