@ticatec/restful_service_api 0.5.0 → 0.6.1

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.
@@ -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(): any;
11
+ get code(): string;
12
12
  /**
13
13
  * Detailed error information from the API response
14
14
  */
15
- get details(): any;
15
+ get details(): ApiErrorPayload;
16
16
  /**
17
17
  * HTTP status code of the error response
18
18
  */
19
- get status(): any;
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: any, err: any);
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
- super(err.code);
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 = err.code;
34
+ this._code = code;
32
35
  this._status = status;
33
- this._details = err;
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
+ };
@@ -1,5 +1,6 @@
1
- import UploadCallback from "./UploadCallback";
2
- import { DataProcessor } from "./RestService";
1
+ import type UploadCallback from "./UploadCallback.js";
2
+ import type { UploadProgress } from "./UploadCallback.js";
3
+ import type { DataProcessor } from "./RestService.js";
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<import("./UploadCallback").UploadProgress>;
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
@@ -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) => 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.)
@@ -31,13 +31,13 @@ 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 Boolean indicating if the error was handled (true) or should be thrown (false)
34
+ * @returns Optional boolean or void. Errors always reject the returned Promise.
35
35
  */
36
- export type ErrorHandler = (ex: Error) => boolean;
36
+ export type ErrorHandler = (ex: Error) => boolean | void;
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<any>;
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<any>;
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<any>;
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<any>;
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<any>;
170
+ patch<T = any>(url: string, data?: any, options?: RestfulOptions<T>): Promise<T>;
171
171
  }
@@ -35,7 +35,7 @@ export default interface UploadCallback {
35
35
  /**
36
36
  * HTTP request method, defaults to POST, but allows PUT or other methods in special cases
37
37
  */
38
- method: string;
38
+ method?: string;
39
39
  /**
40
40
  * Optional upload progress update callback function
41
41
  */
package/dist/index.d.ts CHANGED
@@ -1,13 +1,14 @@
1
- import ApiError from "./ApiError";
2
- import RestService from "./RestService";
3
- import FileService from "./FileService";
4
- import { ErrorHandler, PostInterceptor, PreInterceptor, DataProcessor } from "./RestService";
5
- import { PreInterceptorResult } from "./RestService";
6
- import { CONTENT_TYPE_NAME, TYPE_JSON, TYPE_HTML, TYPE_TEXT } from "./RestService";
7
- import UploadCallback, { UploadProgress, ProgressUpdate, OnCompleted, OnUploaded } from "./UploadCallback";
8
- import { RestfulOptions } from "./RestService";
1
+ import ApiError from "./ApiError.js";
2
+ import type { ApiErrorPayload } from "./ApiError.js";
3
+ import RestService from "./RestService.js";
4
+ import FileService from "./FileService.js";
5
+ import type { ErrorHandler, PostInterceptor, PreInterceptor, DataProcessor, PreInterceptorResult, RestfulOptions } from "./RestService.js";
6
+ import { CONTENT_TYPE_NAME, TYPE_JSON, TYPE_HTML, TYPE_TEXT } from "./RestService.js";
7
+ import type UploadCallback from "./UploadCallback.js";
8
+ import type { UploadProgress, ProgressUpdate, OnCompleted, OnUploaded } from "./UploadCallback.js";
9
9
  export default RestService;
10
10
  export { ApiError };
11
+ export type { ApiErrorPayload };
11
12
  export { RestService };
12
13
  export { FileService };
13
14
  export type { ErrorHandler, PostInterceptor, PreInterceptor, DataProcessor };
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import ApiError from "./ApiError";
2
- import { CONTENT_TYPE_NAME, TYPE_JSON, TYPE_HTML, TYPE_TEXT } from "./RestService";
1
+ import ApiError from "./ApiError.js";
2
+ import { CONTENT_TYPE_NAME, TYPE_JSON, TYPE_HTML, TYPE_TEXT } from "./RestService.js";
3
3
  export { ApiError };
4
4
  export { CONTENT_TYPE_NAME, TYPE_JSON, TYPE_HTML, TYPE_TEXT };
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "type": "module",
3
3
  "name": "@ticatec/restful_service_api",
4
- "version": "0.5.0",
5
- "description": "A lightweight TypeScript RESTful API client for browsers with error handling. (ESM only, v0.5.0+)",
4
+ "version": "0.6.1",
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",
8
8
  "types": "dist/index.d.ts",
@@ -13,23 +13,24 @@
13
13
  ],
14
14
  "exports": {
15
15
  ".": {
16
+ "types": "./dist/index.d.ts",
16
17
  "import": "./dist/index.js",
17
- "require": "./dist/index.js",
18
- "types": "./dist/index.d.ts"
18
+ "default": "./dist/index.js"
19
19
  },
20
20
  "./ApiError": {
21
+ "types": "./dist/ApiError.d.ts",
21
22
  "import": "./dist/ApiError.js",
22
- "require": "./dist/ApiError.js",
23
- "types": "./dist/ApiError.d.ts"
23
+ "default": "./dist/ApiError.js"
24
24
  },
25
25
  "./utils": {
26
+ "types": "./dist/utils.d.ts",
26
27
  "import": "./dist/utils.js",
27
- "require": "./dist/utils.js",
28
- "types": "./dist/utils.d.ts"
28
+ "default": "./dist/utils.js"
29
29
  }
30
30
  },
31
31
  "scripts": {
32
32
  "build": "tsc",
33
+ "test": "vitest run",
33
34
  "clean": "rm -rf dist",
34
35
  "prepare": "npm run clean && npm run build",
35
36
  "publish:public": "npm publish --access public"
@@ -55,6 +56,7 @@
55
56
  "dependencies": {
56
57
  },
57
58
  "devDependencies": {
58
- "typescript": "^5.4.5"
59
+ "typescript": "^5.4.5",
60
+ "vitest": "^3.2.4"
59
61
  }
60
62
  }