@ticatec/restful_service_api 0.3.0 → 0.6.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 +28 -0
- package/README.md +27 -0
- package/dist/ApiError.d.ts +10 -4
- package/dist/ApiError.js +15 -3
- package/dist/FileService.d.ts +4 -3
- package/dist/RestService.d.ts +8 -8
- package/dist/index.d.ts +2 -0
- package/package.json +12 -9
package/README-CN.md
CHANGED
|
@@ -18,6 +18,34 @@
|
|
|
18
18
|
- 🌐 **浏览器优先**: 专为前端应用程序设计
|
|
19
19
|
- ✨ **PATCH 支持**: 完整支持 HTTP PATCH 方法进行部分更新
|
|
20
20
|
|
|
21
|
+
## ⚠️ v0.5.0 重大变更
|
|
22
|
+
|
|
23
|
+
**从 0.5.0 版本开始,此包已迁移到 ESM (ECMAScript Modules) 格式。**
|
|
24
|
+
|
|
25
|
+
### 对您的影响
|
|
26
|
+
|
|
27
|
+
- 您的项目必须使用 ESM 格式(package.json 中包含 `"type": "module"` 或使用 `.mjs` 扩展名)
|
|
28
|
+
- 不再支持 `require()`,请使用 `import` 语句
|
|
29
|
+
|
|
30
|
+
### 迁移指南
|
|
31
|
+
|
|
32
|
+
如果您正在从 0.5.0 之前的版本升级:
|
|
33
|
+
|
|
34
|
+
**之前 (CommonJS):**
|
|
35
|
+
```javascript
|
|
36
|
+
const RestService = require('@ticatec/restful_service_api');
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
**之后 (ESM):**
|
|
40
|
+
```typescript
|
|
41
|
+
import RestService from '@ticatec/restful_service_api';
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
对于 CommonJS 项目,您可能需要使用动态导入:
|
|
45
|
+
```javascript
|
|
46
|
+
const { default: RestService } = await import('@ticatec/restful_service_api');
|
|
47
|
+
```
|
|
48
|
+
|
|
21
49
|
## 安装
|
|
22
50
|
|
|
23
51
|
```bash
|
package/README.md
CHANGED
|
@@ -18,6 +18,33 @@ 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.5.0
|
|
22
|
+
|
|
23
|
+
**Starting from version 0.5.0, this package has migrated to ESM (ECMAScript Modules) format.**
|
|
24
|
+
|
|
25
|
+
### What This Means for You
|
|
26
|
+
|
|
27
|
+
- Your project must use ESM format (have `"type": "module"` in package.json or use `.mjs` extension)
|
|
28
|
+
- `require()` is no longer supported. Use `import` statements instead
|
|
29
|
+
### Migration Guide
|
|
30
|
+
|
|
31
|
+
If you're upgrading from a version prior to 0.5.0:
|
|
32
|
+
|
|
33
|
+
**Before (CommonJS):**
|
|
34
|
+
```javascript
|
|
35
|
+
const RestService = require('@ticatec/restful_service_api');
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
**After (ESM):**
|
|
39
|
+
```typescript
|
|
40
|
+
import RestService from '@ticatec/restful_service_api';
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
For CommonJS projects, you may need to use dynamic import:
|
|
44
|
+
```javascript
|
|
45
|
+
const { default: RestService } = await import('@ticatec/restful_service_api');
|
|
46
|
+
```
|
|
47
|
+
|
|
21
48
|
## Installation
|
|
22
49
|
|
|
23
50
|
```bash
|
package/dist/ApiError.d.ts
CHANGED
|
@@ -8,19 +8,25 @@ export default class ApiError extends Error {
|
|
|
8
8
|
/**
|
|
9
9
|
* Error code from the API response
|
|
10
10
|
*/
|
|
11
|
-
get code():
|
|
11
|
+
get code(): string;
|
|
12
12
|
/**
|
|
13
13
|
* Detailed error information from the API response
|
|
14
14
|
*/
|
|
15
|
-
get details():
|
|
15
|
+
get details(): ApiErrorPayload;
|
|
16
16
|
/**
|
|
17
17
|
* HTTP status code of the error response
|
|
18
18
|
*/
|
|
19
|
-
get status():
|
|
19
|
+
get status(): string | number;
|
|
20
20
|
/**
|
|
21
21
|
* Creates a new API error instance
|
|
22
22
|
* @param status HTTP status code
|
|
23
23
|
* @param err Error object containing code and details
|
|
24
24
|
*/
|
|
25
|
-
constructor(status:
|
|
25
|
+
constructor(status: number | string, err?: ApiErrorPayload | Error | string);
|
|
26
|
+
}
|
|
27
|
+
/** Error details returned by an API or produced by a request failure. */
|
|
28
|
+
export interface ApiErrorPayload {
|
|
29
|
+
code?: string;
|
|
30
|
+
message?: string;
|
|
31
|
+
[key: string]: unknown;
|
|
26
32
|
}
|
package/dist/ApiError.js
CHANGED
|
@@ -26,10 +26,22 @@ export default class ApiError extends Error {
|
|
|
26
26
|
* @param err Error object containing code and details
|
|
27
27
|
*/
|
|
28
28
|
constructor(status, err) {
|
|
29
|
-
|
|
29
|
+
var _a, _b;
|
|
30
|
+
const details = normalizeError(err);
|
|
31
|
+
const code = (_b = (_a = details.code) !== null && _a !== void 0 ? _a : details.message) !== null && _b !== void 0 ? _b : 'UNKNOWN_ERROR';
|
|
32
|
+
super(code);
|
|
30
33
|
this.name = this.constructor.name;
|
|
31
|
-
this._code =
|
|
34
|
+
this._code = code;
|
|
32
35
|
this._status = status;
|
|
33
|
-
this._details =
|
|
36
|
+
this._details = details;
|
|
34
37
|
}
|
|
35
38
|
}
|
|
39
|
+
const normalizeError = (err) => {
|
|
40
|
+
if (typeof err === 'string') {
|
|
41
|
+
return { code: err, message: err };
|
|
42
|
+
}
|
|
43
|
+
if (err instanceof Error) {
|
|
44
|
+
return { code: 'UNKNOWN_ERROR', message: err.message, cause: err };
|
|
45
|
+
}
|
|
46
|
+
return err !== null && err !== void 0 ? err : { code: 'UNKNOWN_ERROR' };
|
|
47
|
+
};
|
package/dist/FileService.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import UploadCallback from "./UploadCallback";
|
|
2
|
-
import {
|
|
1
|
+
import type UploadCallback from "./UploadCallback";
|
|
2
|
+
import type { UploadProgress } from "./UploadCallback";
|
|
3
|
+
import type { DataProcessor } from "./RestService";
|
|
3
4
|
/**
|
|
4
5
|
* File service interface for file upload and download operations
|
|
5
6
|
*/
|
|
@@ -60,7 +61,7 @@ export default interface FileService {
|
|
|
60
61
|
* progress.abort();
|
|
61
62
|
* ```
|
|
62
63
|
*/
|
|
63
|
-
asyncUpload(url: string, params: any, file: File, callback: UploadCallback, fileKey?: string): Promise<
|
|
64
|
+
asyncUpload(url: string, params: any, file: File, callback: UploadCallback, fileKey?: string): Promise<UploadProgress>;
|
|
64
65
|
/**
|
|
65
66
|
* Performs file download operation
|
|
66
67
|
* @param url The URL path to download the file
|
package/dist/RestService.d.ts
CHANGED
|
@@ -14,7 +14,7 @@ export interface PreInterceptorResult {
|
|
|
14
14
|
/**
|
|
15
15
|
* Data processor function type for transforming response data
|
|
16
16
|
*/
|
|
17
|
-
export type DataProcessor = (data: any) =>
|
|
17
|
+
export type DataProcessor<T = any> = (data: any) => T;
|
|
18
18
|
/**
|
|
19
19
|
* Pre-interceptor function type for modifying requests before sending
|
|
20
20
|
* @param method HTTP method (GET, POST, etc.)
|
|
@@ -37,7 +37,7 @@ export type ErrorHandler = (ex: Error) => boolean;
|
|
|
37
37
|
/**
|
|
38
38
|
* RESTful API request options
|
|
39
39
|
*/
|
|
40
|
-
export type RestfulOptions = {
|
|
40
|
+
export type RestfulOptions<T = any> = {
|
|
41
41
|
/**
|
|
42
42
|
* Optional Content-Type header, defaults to 'application/json'
|
|
43
43
|
*/
|
|
@@ -45,7 +45,7 @@ export type RestfulOptions = {
|
|
|
45
45
|
/**
|
|
46
46
|
* Optional data processing function to transform response data before returning
|
|
47
47
|
*/
|
|
48
|
-
dataProcessor?: DataProcessor
|
|
48
|
+
dataProcessor?: DataProcessor<T>;
|
|
49
49
|
/**
|
|
50
50
|
* Optional query parameters object that will be converted to URL query string
|
|
51
51
|
*/
|
|
@@ -90,7 +90,7 @@ export default interface RestService {
|
|
|
90
90
|
* const users = await restService.get('/api/users', null, (data) => data.items);
|
|
91
91
|
* ```
|
|
92
92
|
*/
|
|
93
|
-
get(url: string, params?: any, dataProcessor?: DataProcessor): Promise<
|
|
93
|
+
get<T = any>(url: string, params?: any, dataProcessor?: DataProcessor<T>): Promise<T>;
|
|
94
94
|
/**
|
|
95
95
|
* Performs an HTTP POST request to create a new resource
|
|
96
96
|
* @param url The target URL path for the request
|
|
@@ -114,7 +114,7 @@ export default interface RestService {
|
|
|
114
114
|
* });
|
|
115
115
|
* ```
|
|
116
116
|
*/
|
|
117
|
-
post(url: string, data?: any, options?: RestfulOptions): Promise<
|
|
117
|
+
post<T = any>(url: string, data?: any, options?: RestfulOptions<T>): Promise<T>;
|
|
118
118
|
/**
|
|
119
119
|
* Performs an HTTP PUT request to update an existing resource
|
|
120
120
|
* @param url The target URL path for the request
|
|
@@ -131,7 +131,7 @@ export default interface RestService {
|
|
|
131
131
|
* const result = await restService.put('/api/users/123/toggle-status');
|
|
132
132
|
* ```
|
|
133
133
|
*/
|
|
134
|
-
put(url: string, data?: any, options?: RestfulOptions): Promise<
|
|
134
|
+
put<T = any>(url: string, data?: any, options?: RestfulOptions<T>): Promise<T>;
|
|
135
135
|
/**
|
|
136
136
|
* Performs an HTTP DELETE request to delete a resource
|
|
137
137
|
* @param url The target URL path for the request
|
|
@@ -148,7 +148,7 @@ export default interface RestService {
|
|
|
148
148
|
* await restService.del('/api/users/batch', { ids: [1, 2, 3] });
|
|
149
149
|
* ```
|
|
150
150
|
*/
|
|
151
|
-
del(url: string, data?: any, options?: RestfulOptions): Promise<
|
|
151
|
+
del<T = any>(url: string, data?: any, options?: RestfulOptions<T>): Promise<T>;
|
|
152
152
|
/**
|
|
153
153
|
* Performs an HTTP PATCH request to partially update a resource
|
|
154
154
|
* @param url The target URL path for the request
|
|
@@ -167,5 +167,5 @@ export default interface RestService {
|
|
|
167
167
|
* });
|
|
168
168
|
* ```
|
|
169
169
|
*/
|
|
170
|
-
patch(url: string, data?: any, options?: RestfulOptions): Promise<
|
|
170
|
+
patch<T = any>(url: string, data?: any, options?: RestfulOptions<T>): Promise<T>;
|
|
171
171
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import ApiError from "./ApiError";
|
|
2
|
+
import type { ApiErrorPayload } from "./ApiError";
|
|
2
3
|
import RestService from "./RestService";
|
|
3
4
|
import FileService from "./FileService";
|
|
4
5
|
import { ErrorHandler, PostInterceptor, PreInterceptor, DataProcessor } from "./RestService";
|
|
@@ -8,6 +9,7 @@ import UploadCallback, { UploadProgress, ProgressUpdate, OnCompleted, OnUploaded
|
|
|
8
9
|
import { RestfulOptions } from "./RestService";
|
|
9
10
|
export default RestService;
|
|
10
11
|
export { ApiError };
|
|
12
|
+
export type { ApiErrorPayload };
|
|
11
13
|
export { RestService };
|
|
12
14
|
export { FileService };
|
|
13
15
|
export type { ErrorHandler, PostInterceptor, PreInterceptor, DataProcessor };
|
package/package.json
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
{
|
|
2
|
+
"type": "module",
|
|
2
3
|
"name": "@ticatec/restful_service_api",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "A lightweight TypeScript RESTful API client for browsers with error handling.",
|
|
4
|
+
"version": "0.6.0",
|
|
5
|
+
"description": "A lightweight TypeScript RESTful API client for browsers with error handling. (ESM only, v0.6.0+)",
|
|
5
6
|
"main": "dist/index.js",
|
|
6
7
|
"module": "dist/index.js",
|
|
7
8
|
"types": "dist/index.d.ts",
|
|
@@ -12,23 +13,24 @@
|
|
|
12
13
|
],
|
|
13
14
|
"exports": {
|
|
14
15
|
".": {
|
|
16
|
+
"types": "./dist/index.d.ts",
|
|
15
17
|
"import": "./dist/index.js",
|
|
16
|
-
"
|
|
17
|
-
"types": "./dist/index.d.ts"
|
|
18
|
+
"default": "./dist/index.js"
|
|
18
19
|
},
|
|
19
20
|
"./ApiError": {
|
|
21
|
+
"types": "./dist/ApiError.d.ts",
|
|
20
22
|
"import": "./dist/ApiError.js",
|
|
21
|
-
"
|
|
22
|
-
"types": "./dist/ApiError.d.ts"
|
|
23
|
+
"default": "./dist/ApiError.js"
|
|
23
24
|
},
|
|
24
25
|
"./utils": {
|
|
26
|
+
"types": "./dist/utils.d.ts",
|
|
25
27
|
"import": "./dist/utils.js",
|
|
26
|
-
"
|
|
27
|
-
"types": "./dist/utils.d.ts"
|
|
28
|
+
"default": "./dist/utils.js"
|
|
28
29
|
}
|
|
29
30
|
},
|
|
30
31
|
"scripts": {
|
|
31
32
|
"build": "tsc",
|
|
33
|
+
"test": "vitest run",
|
|
32
34
|
"clean": "rm -rf dist",
|
|
33
35
|
"prepare": "npm run clean && npm run build",
|
|
34
36
|
"publish:public": "npm publish --access public"
|
|
@@ -54,6 +56,7 @@
|
|
|
54
56
|
"dependencies": {
|
|
55
57
|
},
|
|
56
58
|
"devDependencies": {
|
|
57
|
-
"typescript": "^5.4.5"
|
|
59
|
+
"typescript": "^5.4.5",
|
|
60
|
+
"vitest": "^3.2.4"
|
|
58
61
|
}
|
|
59
62
|
}
|