@seam-rpc/client 5.0.2 → 5.1.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.
- package/dist/index.d.ts +3 -0
- package/dist/index.js +16 -2
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -3,6 +3,7 @@ export { ApiError };
|
|
|
3
3
|
export type { Result };
|
|
4
4
|
export type SeamRequestMiddleware = (context: SeamRequestMiddlewareContext) => void | Promise<void>;
|
|
5
5
|
export type SeamResponseMiddleware = (context: SeamResponseMiddlewareContext) => void | Promise<void>;
|
|
6
|
+
export type SeamOnErrorHandler = (error: SeamClientError) => void | Promise<void>;
|
|
6
7
|
export interface SeamRequestMiddlewareContext {
|
|
7
8
|
request: RequestInit;
|
|
8
9
|
routerName: string;
|
|
@@ -18,6 +19,7 @@ export interface SeamClientOptions {
|
|
|
18
19
|
request?: SeamRequestMiddleware[];
|
|
19
20
|
response?: SeamResponseMiddleware[];
|
|
20
21
|
};
|
|
22
|
+
onError?: SeamOnErrorHandler[];
|
|
21
23
|
}
|
|
22
24
|
export declare class SeamClient<ApiType> {
|
|
23
25
|
readonly baseUrl: string;
|
|
@@ -26,6 +28,7 @@ export declare class SeamClient<ApiType> {
|
|
|
26
28
|
constructor(baseUrl: string, options?: SeamClientOptions);
|
|
27
29
|
preRequest(middleware: SeamRequestMiddleware): void;
|
|
28
30
|
postRequest(middleware: SeamResponseMiddleware): void;
|
|
31
|
+
onError(handler: SeamOnErrorHandler): void;
|
|
29
32
|
}
|
|
30
33
|
type SeamClientErrorType = "REQUEST_FAILED" | "INVALID_CONTENT_TYPE";
|
|
31
34
|
export declare class SeamClientError extends Error {
|
package/dist/index.js
CHANGED
|
@@ -7,7 +7,8 @@ export class SeamClient {
|
|
|
7
7
|
middleware: {
|
|
8
8
|
request: options?.middleware?.request || [],
|
|
9
9
|
response: options?.middleware?.response || [],
|
|
10
|
-
}
|
|
10
|
+
},
|
|
11
|
+
onError: options?.onError || [],
|
|
11
12
|
};
|
|
12
13
|
const client = this;
|
|
13
14
|
this.api = new Proxy({}, {
|
|
@@ -15,7 +16,17 @@ export class SeamClient {
|
|
|
15
16
|
return new Proxy({}, {
|
|
16
17
|
get(_subTarget, procName) {
|
|
17
18
|
return async (input) => {
|
|
18
|
-
|
|
19
|
+
try {
|
|
20
|
+
return await callApi(client, String(routerName), String(procName), input);
|
|
21
|
+
}
|
|
22
|
+
catch (err) {
|
|
23
|
+
if (!client.options.onError || !(err instanceof SeamClientError))
|
|
24
|
+
throw err;
|
|
25
|
+
for (const handler of client.options?.onError) {
|
|
26
|
+
handler(err);
|
|
27
|
+
}
|
|
28
|
+
return { ok: false, error: new ApiError("") };
|
|
29
|
+
}
|
|
19
30
|
};
|
|
20
31
|
},
|
|
21
32
|
});
|
|
@@ -28,6 +39,9 @@ export class SeamClient {
|
|
|
28
39
|
postRequest(middleware) {
|
|
29
40
|
this.options?.middleware?.response?.push(middleware);
|
|
30
41
|
}
|
|
42
|
+
onError(handler) {
|
|
43
|
+
this.options.onError?.push(handler);
|
|
44
|
+
}
|
|
31
45
|
}
|
|
32
46
|
export class SeamClientError extends Error {
|
|
33
47
|
constructor(type, message, url, request, cause) {
|