@ticatec/restful_service_api 0.7.0 → 0.8.0

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-CN.md CHANGED
@@ -18,6 +18,34 @@
18
18
  - 🌐 **浏览器优先**: 专为前端应用程序设计
19
19
  - ✨ **PATCH 支持**: 完整支持 HTTP PATCH 方法进行部分更新
20
20
 
21
+ ## ⚠️ v0.8.0 重大变更
22
+
23
+ **`ErrorHandler` 已变更为返回 `Promise`(支持异步处理)。**
24
+
25
+ ### 对您的影响
26
+
27
+ - `ErrorHandler` 函数类型现为 `(ex: Error) => Promise<boolean | void>`。
28
+ - 错误处理器现在应定义为 `async` 异步函数或返回 `Promise`。
29
+ - `UploadCallback.handleError` 统一采用 Promise 异步的 `ErrorHandler` 定义。
30
+
31
+ ### 迁移指南
32
+
33
+ **之前(v0.7.x 及更早版本):**
34
+ ```typescript
35
+ const errorHandler: ErrorHandler = (error: Error) => {
36
+ console.error('API 错误:', error);
37
+ return true;
38
+ };
39
+ ```
40
+
41
+ **之后(v0.8.0+):**
42
+ ```typescript
43
+ const errorHandler: ErrorHandler = async (error: Error) => {
44
+ console.error('API 错误:', error);
45
+ return true;
46
+ };
47
+ ```
48
+
21
49
  ## ⚠️ v0.7.0 重大变更
22
50
 
23
51
  **`RestfulOptions.contentType` 已被移除,改为使用通用的 `headers` 字段。**
@@ -282,7 +310,7 @@ const postInterceptor: PostInterceptor = async (data: any) => {
282
310
  };
283
311
 
284
312
  // 错误处理器
285
- const errorHandler: ErrorHandler = (error: Error) => {
313
+ const errorHandler: ErrorHandler = async (error: Error) => {
286
314
  console.error('API 错误:', error);
287
315
  return true; // 返回 true 表示错误已处理
288
316
  };
@@ -498,18 +526,18 @@ class FetchFileService implements FileService {
498
526
  }
499
527
  });
500
528
 
501
- xhr.addEventListener('load', () => {
529
+ xhr.addEventListener('load', async () => {
502
530
  if (xhr.status >= 200 && xhr.status < 300) {
503
531
  const data = JSON.parse(xhr.responseText);
504
532
  callback.onCompleted(data);
505
533
  } else if (callback.handleError) {
506
- callback.handleError(new Error(`上传失败: ${xhr.statusText}`));
534
+ await callback.handleError(new Error(`上传失败: ${xhr.statusText}`));
507
535
  }
508
536
  });
509
537
 
510
- xhr.addEventListener('error', () => {
538
+ xhr.addEventListener('error', async () => {
511
539
  if (callback.handleError) {
512
- callback.handleError(new Error('网络错误'));
540
+ await callback.handleError(new Error('网络错误'));
513
541
  }
514
542
  });
515
543
 
@@ -572,7 +600,7 @@ const progress = await fileApi.asyncUpload('/upload', { userId: 123 }, file, {
572
600
  method: 'POST',
573
601
  progressUpdate: (loaded) => console.log(`已上传: ${loaded} 字节`),
574
602
  onCompleted: (data) => console.log('完成!', data),
575
- handleError: (err) => console.error('错误!', err)
603
+ handleError: async (err) => console.error('错误!', err)
576
604
  });
577
605
 
578
606
  // 下载文件
package/README.md CHANGED
@@ -18,6 +18,34 @@ A lightweight TypeScript RESTful API client for browsers with comprehensive erro
18
18
  - 🌐 **Browser-First**: Designed specifically for frontend applications
19
19
  - ✨ **PATCH Support**: Full support for HTTP PATCH method for partial updates
20
20
 
21
+ ## ⚠️ Breaking Changes in v0.8.0
22
+
23
+ **`ErrorHandler` has been changed to return a `Promise` (async support).**
24
+
25
+ ### What This Means for You
26
+
27
+ - The `ErrorHandler` function type is now `(ex: Error) => Promise<boolean | void>`.
28
+ - Error handler functions should now be `async` functions or return a `Promise`.
29
+ - `UploadCallback.handleError` now adopts the unified Promise-based `ErrorHandler` type.
30
+
31
+ ### Migration Guide
32
+
33
+ **Before (v0.7.x and earlier):**
34
+ ```typescript
35
+ const errorHandler: ErrorHandler = (error: Error) => {
36
+ console.error('API Error:', error);
37
+ return true;
38
+ };
39
+ ```
40
+
41
+ **After (v0.8.0+):**
42
+ ```typescript
43
+ const errorHandler: ErrorHandler = async (error: Error) => {
44
+ console.error('API Error:', error);
45
+ return true;
46
+ };
47
+ ```
48
+
21
49
  ## ⚠️ Breaking Changes in v0.7.0
22
50
 
23
51
  **`RestfulOptions.contentType` has been removed in favor of a general-purpose `headers` field.**
@@ -281,7 +309,7 @@ const postInterceptor: PostInterceptor = async (data: any) => {
281
309
  };
282
310
 
283
311
  // Error handler
284
- const errorHandler: ErrorHandler = (error: Error) => {
312
+ const errorHandler: ErrorHandler = async (error: Error) => {
285
313
  console.error('API Error:', error);
286
314
  return true; // Return true if error is handled
287
315
  };
@@ -498,18 +526,18 @@ class FetchFileService implements FileService {
498
526
  }
499
527
  });
500
528
 
501
- xhr.addEventListener('load', () => {
529
+ xhr.addEventListener('load', async () => {
502
530
  if (xhr.status >= 200 && xhr.status < 300) {
503
531
  const data = JSON.parse(xhr.responseText);
504
532
  callback.onCompleted(data);
505
533
  } else if (callback.handleError) {
506
- callback.handleError(new Error(`Upload failed: ${xhr.statusText}`));
534
+ await callback.handleError(new Error(`Upload failed: ${xhr.statusText}`));
507
535
  }
508
536
  });
509
537
 
510
- xhr.addEventListener('error', () => {
538
+ xhr.addEventListener('error', async () => {
511
539
  if (callback.handleError) {
512
- callback.handleError(new Error('Network error'));
540
+ await callback.handleError(new Error('Network error'));
513
541
  }
514
542
  });
515
543
 
@@ -572,7 +600,7 @@ const progress = await fileApi.asyncUpload('/upload', { userId: 123 }, file, {
572
600
  method: 'POST',
573
601
  progressUpdate: (loaded) => console.log(`Uploaded: ${loaded} bytes`),
574
602
  onCompleted: (data) => console.log('Done!', data),
575
- handleError: (err) => console.error('Error!', err)
603
+ handleError: async (err) => console.error('Error!', err)
576
604
  });
577
605
 
578
606
  // Download file
@@ -51,7 +51,7 @@ export default interface FileService {
51
51
  * onCompleted: (data) => {
52
52
  * console.log('Upload complete:', data);
53
53
  * },
54
- * handleError: (error) => {
54
+ * handleError: async (error) => {
55
55
  * console.error('Upload failed:', error);
56
56
  * }
57
57
  * }
@@ -31,9 +31,9 @@ export type PostInterceptor = (data: any) => Promise<any>;
31
31
  /**
32
32
  * Error handler function type for handling request errors
33
33
  * @param ex The error object
34
- * @returns Optional boolean or void. Errors always reject the returned Promise.
34
+ * @returns Promise that resolves to boolean or void. Errors always reject the returned Promise.
35
35
  */
36
- export type ErrorHandler = (ex: Error) => boolean | void;
36
+ export type ErrorHandler = (ex: Error) => Promise<boolean | void>;
37
37
  /**
38
38
  * RESTful API request options
39
39
  */
@@ -1,3 +1,4 @@
1
+ import type { ErrorHandler } from "./RestService.js";
1
2
  /**
2
3
  * Upload progress update callback function type
3
4
  * @param uploadBytes Number of bytes uploaded
@@ -5,9 +6,8 @@
5
6
  export type ProgressUpdate = (uploadBytes: number) => void;
6
7
  /**
7
8
  * Upload error handling callback function type
8
- * @param e Error object that occurred during upload
9
9
  */
10
- export type ErrorHandler = (e: Error) => void;
10
+ export type { ErrorHandler };
11
11
  /**
12
12
  * Upload completion callback function type
13
13
  * @param data Response data returned from the server
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "type": "module",
3
3
  "name": "@ticatec/restful_service_api",
4
- "version": "0.7.0",
4
+ "version": "0.8.0",
5
5
  "description": "A lightweight TypeScript RESTful API client for browsers with error handling. (ESM only, v0.6.0+)",
6
6
  "main": "dist/index.js",
7
7
  "module": "dist/index.js",