@ysgroup/core 0.1.8 → 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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ysgroup/core",
3
- "version": "0.1.8",
3
+ "version": "0.1.10",
4
4
  "type": "module",
5
5
  "main": "src/index.ts",
6
6
  "types": "src/index.ts",
@@ -16,4 +16,4 @@
16
16
  "publishConfig": {
17
17
  "access": "public"
18
18
  }
19
- }
19
+ }
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;
@@ -213,9 +215,10 @@ export class YsRest {
213
215
  getUserApps(id: string) {
214
216
  return this.request<{ app_id: string; admin: boolean }[]>("GET", `/api/users/${id}/apps`);
215
217
  }
216
- setUserApps(id: string, grants: { app_id: string; admin: boolean }[]) {
218
+ setUserApps(id: string, grants: { app_id: string; admin: boolean }[], reason = "") {
217
219
  return this.request<{ grants: { app_id: string; admin: boolean }[] }>("PUT", `/api/users/${id}/apps`, {
218
220
  grants,
221
+ reason,
219
222
  });
220
223
  }
221
224
  uploadOpenAccountConfirmation(ownerId: string, file: File) {
@@ -237,8 +240,8 @@ export class YsRest {
237
240
  getRelations(entity: "owners" | "accounts", id: string, relation: "apps" | "strategies") {
238
241
  return this.request<{ ids: string[] }>("GET", `/api/${entity}/${id}/relations/${relation}`);
239
242
  }
240
- setRelations(entity: "owners" | "accounts", id: string, relation: "apps" | "strategies", ids: string[]) {
241
- return this.request<{ ids: string[] }>("PUT", `/api/${entity}/${id}/relations/${relation}`, { ids });
243
+ setRelations(entity: "owners" | "accounts", id: string, relation: "apps" | "strategies", ids: string[], reason = "") {
244
+ return this.request<{ ids: string[] }>("PUT", `/api/${entity}/${id}/relations/${relation}`, { ids, reason });
242
245
  }
243
246
  listOwnerFiles(ownerId: string) {
244
247
  return this.request<Record<string, unknown>[]>("GET", `/api/owners/${ownerId}/files`);
@@ -264,6 +267,15 @@ export class YsRest {
264
267
  }
265
268
 
266
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
+ }
267
279
  const message = String(error).replace(/^Error:\s*/, "");
268
280
  const match = message.match(/^\[(\d+)\]\s*(.*)$/s);
269
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 Error(`[${msg.code}] ${msg.message ?? ""}`));
126
+ else p.reject(new YsRpcError(msg.code ?? 500, msg.message ?? "", msg.request_id ?? ""));
110
127
  }
111
128
  return;
112
129
  }