@ysgroup/core 0.1.9 → 0.1.10
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/package.json +2 -2
- package/src/index.ts +1 -1
- package/src/rest.ts +11 -0
- package/src/ws.ts +18 -1
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/* @ysgroup/core —— 前端运行时:WS RPC 客户端 / 登录态 / 差量订阅 / REST。 */
|
|
2
2
|
|
|
3
|
-
export { YsWsClient, type Envelope } from "./ws";
|
|
3
|
+
export { YsWsClient, YsRpcError, type Envelope } from "./ws";
|
|
4
4
|
export { useYsStore } from "./store";
|
|
5
5
|
export { YsRest, YsRestError, type YsAdminRpcCall, type DeleteBlocker, type DeleteImpact, type RevealedCredential, type TopologyNode } from "./rest";
|
package/src/rest.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
/* @ysgroup/core —— 管理接口客户端(支持 HTTP 或统一 WS RPC 传输)。 */
|
|
2
2
|
|
|
3
|
+
import { YsRpcError } from "./ws";
|
|
4
|
+
|
|
3
5
|
export type YsAdminRpcCall = (data: {
|
|
4
6
|
method: string;
|
|
5
7
|
path: string;
|
|
@@ -265,6 +267,15 @@ export class YsRest {
|
|
|
265
267
|
}
|
|
266
268
|
|
|
267
269
|
function rpcError(error: unknown): YsRestError {
|
|
270
|
+
if (error instanceof YsRpcError) {
|
|
271
|
+
let detail: unknown = error.clientMessage;
|
|
272
|
+
try {
|
|
273
|
+
detail = JSON.parse(error.clientMessage);
|
|
274
|
+
} catch {
|
|
275
|
+
// 普通文本错误保持原文。
|
|
276
|
+
}
|
|
277
|
+
return new YsRestError(error.code, detail);
|
|
278
|
+
}
|
|
268
279
|
const message = String(error).replace(/^Error:\s*/, "");
|
|
269
280
|
const match = message.match(/^\[(\d+)\]\s*(.*)$/s);
|
|
270
281
|
if (!match) return new YsRestError(500, message);
|
package/src/ws.ts
CHANGED
|
@@ -6,10 +6,27 @@ export interface Envelope {
|
|
|
6
6
|
reply_to?: string;
|
|
7
7
|
code?: number;
|
|
8
8
|
message?: string;
|
|
9
|
+
request_id?: string;
|
|
9
10
|
ts?: number;
|
|
10
11
|
data?: unknown;
|
|
11
12
|
}
|
|
12
13
|
|
|
14
|
+
/** 服务端非零信封。500 时展示文案附带编号,便于对照日志。 */
|
|
15
|
+
export class YsRpcError extends Error {
|
|
16
|
+
readonly clientMessage: string;
|
|
17
|
+
|
|
18
|
+
constructor(
|
|
19
|
+
public code: number,
|
|
20
|
+
message: string,
|
|
21
|
+
public requestId = "",
|
|
22
|
+
) {
|
|
23
|
+
const display = code >= 500 && requestId ? `${message}(编号 ${requestId})` : message;
|
|
24
|
+
super(display);
|
|
25
|
+
this.name = "YsRpcError";
|
|
26
|
+
this.clientMessage = message;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
13
30
|
type EventHandler = (data: unknown) => void;
|
|
14
31
|
|
|
15
32
|
export class YsWsClient {
|
|
@@ -106,7 +123,7 @@ export class YsWsClient {
|
|
|
106
123
|
if (p) {
|
|
107
124
|
this.pending.delete(msg.reply_to);
|
|
108
125
|
if ((msg.code ?? 0) === 0) p.resolve(msg.data);
|
|
109
|
-
else p.reject(new
|
|
126
|
+
else p.reject(new YsRpcError(msg.code ?? 500, msg.message ?? "", msg.request_id ?? ""));
|
|
110
127
|
}
|
|
111
128
|
return;
|
|
112
129
|
}
|