@openuverse/uvapi 0.0.0-stage → 1.1.3
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.md +17 -2
- package/dist/_virtual/_commonjsHelpers.js +6 -0
- package/dist/_virtual/index.js +4 -0
- package/dist/api/RestAPIClient.d.ts +191 -0
- package/dist/api/RestAPIClient.js +328 -0
- package/dist/api/WebSocketClient.d.ts +174 -0
- package/dist/api/WebSocketClient.js +277 -0
- package/dist/api/index.d.ts +6 -0
- package/dist/api/modules/index.d.ts +2 -0
- package/dist/api/modules/task.d.ts +124 -0
- package/dist/api/modules/task.js +121 -0
- package/dist/api/modules/user.d.ts +114 -0
- package/dist/api/modules/user.js +113 -0
- package/dist/config/index.d.ts +120 -0
- package/dist/config/index.js +112 -0
- package/dist/hooks/index.d.ts +1 -0
- package/dist/hooks/useAttributeMonitor.d.ts +45 -0
- package/dist/hooks/useAttributeMonitor.js +115 -0
- package/dist/index.d.ts +20 -0
- package/dist/index.js +93 -0
- package/dist/locale/de-DE.d.ts +18 -0
- package/dist/locale/de-DE.js +20 -0
- package/dist/locale/en-US.d.ts +18 -0
- package/dist/locale/en-US.js +20 -0
- package/dist/locale/es-ES.d.ts +18 -0
- package/dist/locale/es-ES.js +20 -0
- package/dist/locale/fr-FR.d.ts +18 -0
- package/dist/locale/fr-FR.js +20 -0
- package/dist/locale/id-ID.d.ts +18 -0
- package/dist/locale/id-ID.js +20 -0
- package/dist/locale/index.d.ts +19 -0
- package/dist/locale/index.js +70 -0
- package/dist/locale/ja-JP.d.ts +18 -0
- package/dist/locale/ja-JP.js +20 -0
- package/dist/locale/ko-KR.d.ts +18 -0
- package/dist/locale/ko-KR.js +20 -0
- package/dist/locale/pl-PL.d.ts +18 -0
- package/dist/locale/pl-PL.js +20 -0
- package/dist/locale/pt-BR.d.ts +18 -0
- package/dist/locale/pt-BR.js +20 -0
- package/dist/locale/ru-RU.d.ts +18 -0
- package/dist/locale/ru-RU.js +20 -0
- package/dist/locale/th-TH.d.ts +18 -0
- package/dist/locale/th-TH.js +20 -0
- package/dist/locale/zh-CN.d.ts +18 -0
- package/dist/locale/zh-CN.js +20 -0
- package/dist/locale/zh-TW.d.ts +18 -0
- package/dist/locale/zh-TW.js +20 -0
- package/dist/node_modules/@msgpack/msgpack/dist.esm/CachedKeyDecoder.js +38 -0
- package/dist/node_modules/@msgpack/msgpack/dist.esm/DecodeError.js +14 -0
- package/dist/node_modules/@msgpack/msgpack/dist.esm/Decoder.js +474 -0
- package/dist/node_modules/@msgpack/msgpack/dist.esm/Encoder.js +232 -0
- package/dist/node_modules/@msgpack/msgpack/dist.esm/ExtData.js +8 -0
- package/dist/node_modules/@msgpack/msgpack/dist.esm/ExtensionCodec.js +46 -0
- package/dist/node_modules/@msgpack/msgpack/dist.esm/decode.js +7 -0
- package/dist/node_modules/@msgpack/msgpack/dist.esm/encode.js +7 -0
- package/dist/node_modules/@msgpack/msgpack/dist.esm/timestamp.js +66 -0
- package/dist/node_modules/@msgpack/msgpack/dist.esm/utils/int.js +24 -0
- package/dist/node_modules/@msgpack/msgpack/dist.esm/utils/prettyByte.js +6 -0
- package/dist/node_modules/@msgpack/msgpack/dist.esm/utils/typedArrays.js +9 -0
- package/dist/node_modules/@msgpack/msgpack/dist.esm/utils/utf8.js +93 -0
- package/dist/node_modules/axios/index.js +38 -0
- package/dist/node_modules/axios/lib/adapters/adapters.js +55 -0
- package/dist/node_modules/axios/lib/adapters/fetch.js +169 -0
- package/dist/node_modules/axios/lib/adapters/xhr.js +72 -0
- package/dist/node_modules/axios/lib/axios.js +46 -0
- package/dist/node_modules/axios/lib/cancel/CancelToken.js +77 -0
- package/dist/node_modules/axios/lib/cancel/CanceledError.js +11 -0
- package/dist/node_modules/axios/lib/cancel/isCancel.js +6 -0
- package/dist/node_modules/axios/lib/core/Axios.js +136 -0
- package/dist/node_modules/axios/lib/core/AxiosError.js +56 -0
- package/dist/node_modules/axios/lib/core/AxiosHeaders.js +181 -0
- package/dist/node_modules/axios/lib/core/InterceptorManager.js +58 -0
- package/dist/node_modules/axios/lib/core/buildFullPath.js +9 -0
- package/dist/node_modules/axios/lib/core/dispatchRequest.js +31 -0
- package/dist/node_modules/axios/lib/core/mergeConfig.js +70 -0
- package/dist/node_modules/axios/lib/core/settle.js +14 -0
- package/dist/node_modules/axios/lib/core/transformData.js +13 -0
- package/dist/node_modules/axios/lib/defaults/index.js +89 -0
- package/dist/node_modules/axios/lib/defaults/transitional.js +8 -0
- package/dist/node_modules/axios/lib/env/data.js +4 -0
- package/dist/node_modules/axios/lib/helpers/AxiosURLSearchParams.js +33 -0
- package/dist/node_modules/axios/lib/helpers/HttpStatusCode.js +71 -0
- package/dist/node_modules/axios/lib/helpers/bind.js +8 -0
- package/dist/node_modules/axios/lib/helpers/buildURL.js +23 -0
- package/dist/node_modules/axios/lib/helpers/combineURLs.js +6 -0
- package/dist/node_modules/axios/lib/helpers/composeSignals.js +30 -0
- package/dist/node_modules/axios/lib/helpers/cookies.js +32 -0
- package/dist/node_modules/axios/lib/helpers/formDataToJSON.js +31 -0
- package/dist/node_modules/axios/lib/helpers/isAbsoluteURL.js +6 -0
- package/dist/node_modules/axios/lib/helpers/isAxiosError.js +7 -0
- package/dist/node_modules/axios/lib/helpers/isURLSameOrigin.js +8 -0
- package/dist/node_modules/axios/lib/helpers/null.js +4 -0
- package/dist/node_modules/axios/lib/helpers/parseHeaders.js +30 -0
- package/dist/node_modules/axios/lib/helpers/parseProtocol.js +7 -0
- package/dist/node_modules/axios/lib/helpers/progressEventReducer.js +35 -0
- package/dist/node_modules/axios/lib/helpers/resolveConfig.js +33 -0
- package/dist/node_modules/axios/lib/helpers/speedometer.js +19 -0
- package/dist/node_modules/axios/lib/helpers/spread.js +8 -0
- package/dist/node_modules/axios/lib/helpers/throttle.js +15 -0
- package/dist/node_modules/axios/lib/helpers/toFormData.js +85 -0
- package/dist/node_modules/axios/lib/helpers/toURLEncodedForm.js +14 -0
- package/dist/node_modules/axios/lib/helpers/trackStream.js +63 -0
- package/dist/node_modules/axios/lib/helpers/validator.js +54 -0
- package/dist/node_modules/axios/lib/platform/browser/classes/Blob.js +4 -0
- package/dist/node_modules/axios/lib/platform/browser/classes/FormData.js +4 -0
- package/dist/node_modules/axios/lib/platform/browser/classes/URLSearchParams.js +5 -0
- package/dist/node_modules/axios/lib/platform/browser/index.js +15 -0
- package/dist/node_modules/axios/lib/platform/common/utils.js +9 -0
- package/dist/node_modules/axios/lib/platform/index.js +9 -0
- package/dist/node_modules/axios/lib/utils.js +237 -0
- package/dist/node_modules/postcss-pxtorem/index.js +109 -0
- package/dist/node_modules/postcss-pxtorem/lib/filter-prop-list.js +13 -0
- package/dist/node_modules/postcss-pxtorem/lib/pixel-unit-regex.js +7 -0
- package/dist/node_modules/postcss-pxtorem/lib/type.js +14 -0
- package/dist/preload/index.d.ts +125 -0
- package/dist/preload/index.js +79 -0
- package/dist/store/index.d.ts +1 -0
- package/dist/store/storeFactory.d.ts +584 -0
- package/dist/store/storeFactory.js +216 -0
- package/dist/theme/index.d.ts +1 -0
- package/dist/theme/index.js +14 -0
- package/dist/types/index.d.ts +57 -0
- package/dist/types/uVerseAPI.d.ts +67 -0
- package/dist/utils/_internal.d.ts +14 -0
- package/dist/utils/_internal.js +72 -0
- package/dist/utils/constance.d.ts +16 -0
- package/dist/utils/constance.js +7 -0
- package/dist/utils/index.d.ts +79 -0
- package/dist/utils/index.js +96 -0
- package/dist/utils/logger.d.ts +118 -0
- package/dist/utils/logger.js +170 -0
- package/dist/utils/px2rem/remAdapter.d.ts +17 -0
- package/dist/utils/px2rem/remAdapter.js +62 -0
- package/dist/utils/px2rem/vite.d.ts +14 -0
- package/dist/utils/px2rem/vite.js +33 -0
- package/package.json +102 -4
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WebSocket 配置接口
|
|
3
|
+
*/
|
|
4
|
+
export interface WebSocketConfig {
|
|
5
|
+
baseURL?: string;
|
|
6
|
+
apiVersion?: string;
|
|
7
|
+
reconnectInterval?: number;
|
|
8
|
+
maxReconnectAttempts?: number;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* WebSocket 消息类型
|
|
12
|
+
*/
|
|
13
|
+
export interface WebSocketMessage<T = any> {
|
|
14
|
+
type: string;
|
|
15
|
+
data?: T;
|
|
16
|
+
timestamp?: number;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* WebSocket 事件类型
|
|
20
|
+
*/
|
|
21
|
+
export type WebSocketEventType = 'open' | 'close' | 'error' | 'message' | 'reconnect' | 'reconnectFailed';
|
|
22
|
+
/**
|
|
23
|
+
* WebSocket 事件处理器
|
|
24
|
+
*/
|
|
25
|
+
export type WebSocketEventHandler = (event: any) => void;
|
|
26
|
+
/**
|
|
27
|
+
* WebSocket 连接状态
|
|
28
|
+
*/
|
|
29
|
+
export declare enum WebSocketState {
|
|
30
|
+
CONNECTING = 0,// 正在连接
|
|
31
|
+
OPEN = 1,// 已连接
|
|
32
|
+
CLOSING = 2,// 正在关闭
|
|
33
|
+
CLOSED = 3
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* WebSocket 客户端类
|
|
37
|
+
* 提供了统一的 WebSocket 封装,包括连接管理、自动重连、事件监听等功能
|
|
38
|
+
*/
|
|
39
|
+
export declare class WebSocketClient {
|
|
40
|
+
protected appId: string;
|
|
41
|
+
protected url: string;
|
|
42
|
+
protected apiVersion: string;
|
|
43
|
+
protected ws: WebSocket | null;
|
|
44
|
+
protected reconnectInterval: number;
|
|
45
|
+
protected maxReconnectAttempts: number;
|
|
46
|
+
protected reconnectAttempts: number;
|
|
47
|
+
protected reconnectTimer: number | null;
|
|
48
|
+
protected isManualClose: boolean;
|
|
49
|
+
protected eventHandlers: Map<WebSocketEventType, Set<WebSocketEventHandler>>;
|
|
50
|
+
protected messageQueue: WebSocketMessage[];
|
|
51
|
+
protected isReady: boolean;
|
|
52
|
+
/**
|
|
53
|
+
* 构造函数 - 创建 WebSocket 客户端实例
|
|
54
|
+
* @param appId - 目标应用的 ID
|
|
55
|
+
* @param config - WebSocket 配置对象
|
|
56
|
+
*
|
|
57
|
+
* @remarks
|
|
58
|
+
* 如果 config 和全局配置中都未指定 baseURL,则默认使用当前访问路径的 host + 9898 端口。
|
|
59
|
+
* WebSocket URL 格式:`/{apiVersion}/workers/attributes/{appId}/monitor`
|
|
60
|
+
*/
|
|
61
|
+
constructor(appId?: string, config?: WebSocketConfig);
|
|
62
|
+
/**
|
|
63
|
+
* 连接到 WebSocket 服务器
|
|
64
|
+
* @param url - 可选的服务器地址,如果提供则覆盖构造函数中的地址
|
|
65
|
+
* @returns 返回 Promise,连接成功时 resolve
|
|
66
|
+
*/
|
|
67
|
+
connect(url?: string): Promise<void>;
|
|
68
|
+
/**
|
|
69
|
+
* 发送消息
|
|
70
|
+
* @param message - 要发送的消息,可以是字符串、对象或 WebSocketMessage
|
|
71
|
+
*/
|
|
72
|
+
send<T = any>(message: string | WebSocketMessage<T> | T): void;
|
|
73
|
+
/**
|
|
74
|
+
* 标准化消息格式
|
|
75
|
+
* @param message - 原始消息
|
|
76
|
+
* @returns 标准化后的消息
|
|
77
|
+
* @private
|
|
78
|
+
*/
|
|
79
|
+
private normalizeMessage;
|
|
80
|
+
/**
|
|
81
|
+
* 检查是否是 WebSocketMessage 类型
|
|
82
|
+
* @param obj - 要检查的对象
|
|
83
|
+
* @returns 是否是 WebSocketMessage 类型
|
|
84
|
+
* @private
|
|
85
|
+
*/
|
|
86
|
+
private isWebSocketMessage;
|
|
87
|
+
/**
|
|
88
|
+
* 刷新消息队列
|
|
89
|
+
* 将队列中的消息发送出去
|
|
90
|
+
* @private
|
|
91
|
+
*/
|
|
92
|
+
private flushMessageQueue;
|
|
93
|
+
/**
|
|
94
|
+
* 处理接收到的消息
|
|
95
|
+
* @param event - WebSocket 消息事件
|
|
96
|
+
* @private
|
|
97
|
+
*/
|
|
98
|
+
private handleMessage;
|
|
99
|
+
/**
|
|
100
|
+
* 关闭 WebSocket 连接
|
|
101
|
+
* @param code - 关闭代码
|
|
102
|
+
* @param reason - 关闭原因
|
|
103
|
+
*/
|
|
104
|
+
close(code?: number, reason?: string): void;
|
|
105
|
+
/**
|
|
106
|
+
* 安排重连
|
|
107
|
+
* @private
|
|
108
|
+
*/
|
|
109
|
+
private scheduleReconnect;
|
|
110
|
+
/**
|
|
111
|
+
* 停止重连
|
|
112
|
+
* @private
|
|
113
|
+
*/
|
|
114
|
+
private stopReconnect;
|
|
115
|
+
/**
|
|
116
|
+
* 注册事件监听器
|
|
117
|
+
* @param event - 事件类型
|
|
118
|
+
* @param handler - 事件处理器
|
|
119
|
+
*/
|
|
120
|
+
on(event: WebSocketEventType, handler: WebSocketEventHandler): void;
|
|
121
|
+
/**
|
|
122
|
+
* 移除事件监听器
|
|
123
|
+
* @param event - 事件类型
|
|
124
|
+
* @param handler - 事件处理器
|
|
125
|
+
*/
|
|
126
|
+
off(event: WebSocketEventType, handler: WebSocketEventHandler): void;
|
|
127
|
+
/**
|
|
128
|
+
* 注册一次性事件监听器
|
|
129
|
+
* @param event - 事件类型
|
|
130
|
+
* @param handler - 事件处理器
|
|
131
|
+
*/
|
|
132
|
+
once(event: WebSocketEventType, handler: WebSocketEventHandler): void;
|
|
133
|
+
/**
|
|
134
|
+
* 触发事件
|
|
135
|
+
* @param event - 事件类型
|
|
136
|
+
* @param data - 事件数据
|
|
137
|
+
* @private
|
|
138
|
+
*/
|
|
139
|
+
private emit;
|
|
140
|
+
/**
|
|
141
|
+
* 移除所有事件监听器
|
|
142
|
+
* @param event - 可选的事件类型,如果不提供则移除所有事件的监听器
|
|
143
|
+
*/
|
|
144
|
+
removeAllListeners(event?: WebSocketEventType): void;
|
|
145
|
+
/**
|
|
146
|
+
* 获取连接状态
|
|
147
|
+
* @returns WebSocket 连接状态
|
|
148
|
+
*/
|
|
149
|
+
getReadyState(): WebSocketState;
|
|
150
|
+
/**
|
|
151
|
+
* 检查是否已连接
|
|
152
|
+
* @returns 是否已连接
|
|
153
|
+
*/
|
|
154
|
+
isConnected(): boolean;
|
|
155
|
+
/**
|
|
156
|
+
* 获取原生 WebSocket 实例
|
|
157
|
+
* @returns WebSocket 实例
|
|
158
|
+
*/
|
|
159
|
+
getWebSocket(): WebSocket | null;
|
|
160
|
+
/**
|
|
161
|
+
* 获取重连尝试次数
|
|
162
|
+
* @returns 重连尝试次数
|
|
163
|
+
*/
|
|
164
|
+
getReconnectAttempts(): number;
|
|
165
|
+
/**
|
|
166
|
+
* 获取待发送消息队列长度
|
|
167
|
+
* @returns 队列长度
|
|
168
|
+
*/
|
|
169
|
+
getMessageQueueLength(): number;
|
|
170
|
+
/**
|
|
171
|
+
* 清空消息队列
|
|
172
|
+
*/
|
|
173
|
+
clearMessageQueue(): void;
|
|
174
|
+
}
|
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
import { websocketConfig as b, globalConfig as w } from "../config/index.js";
|
|
2
|
+
import { decode as g } from "../node_modules/@msgpack/msgpack/dist.esm/decode.js";
|
|
3
|
+
import { logger as i } from "../utils/logger.js";
|
|
4
|
+
var f = /* @__PURE__ */ ((r) => (r[r.CONNECTING = 0] = "CONNECTING", r[r.OPEN = 1] = "OPEN", r[r.CLOSING = 2] = "CLOSING", r[r.CLOSED = 3] = "CLOSED", r))(f || {});
|
|
5
|
+
class k {
|
|
6
|
+
/**
|
|
7
|
+
* 构造函数 - 创建 WebSocket 客户端实例
|
|
8
|
+
* @param appId - 目标应用的 ID
|
|
9
|
+
* @param config - WebSocket 配置对象
|
|
10
|
+
*
|
|
11
|
+
* @remarks
|
|
12
|
+
* 如果 config 和全局配置中都未指定 baseURL,则默认使用当前访问路径的 host + 9898 端口。
|
|
13
|
+
* WebSocket URL 格式:`/{apiVersion}/workers/attributes/{appId}/monitor`
|
|
14
|
+
*/
|
|
15
|
+
constructor(e, t) {
|
|
16
|
+
this.ws = null, this.reconnectAttempts = 0, this.reconnectTimer = null, this.isManualClose = !1, this.eventHandlers = /* @__PURE__ */ new Map(), this.messageQueue = [], this.isReady = !1;
|
|
17
|
+
const s = b.getConfig();
|
|
18
|
+
this.appId = e || w.getAppId(), this.apiVersion = (t == null ? void 0 : t.apiVersion) || s.apiVersion || "v1";
|
|
19
|
+
const n = () => {
|
|
20
|
+
var h, u;
|
|
21
|
+
const o = (h = window.uVerseAPI) == null ? void 0 : h.serverConfig;
|
|
22
|
+
if (o) {
|
|
23
|
+
const l = o.protocol || "ws", m = o.hostname || "localhost", d = o.port || 9898;
|
|
24
|
+
return `${l}://${m}:${d}`;
|
|
25
|
+
}
|
|
26
|
+
const a = (u = window.location) == null ? void 0 : u.hostname;
|
|
27
|
+
return a && a !== "" && a !== "localhost" ? `${window.location.protocol === "https:" ? "wss:" : "ws:"}//${a}:9898` : "ws://localhost:9898";
|
|
28
|
+
}, c = (t == null ? void 0 : t.baseURL) || s.baseURL || n(), p = `/${this.apiVersion}/workers/attributes/${this.appId}/monitor`;
|
|
29
|
+
this.url = `${c.replace(/\/$/, "")}${p}`, this.reconnectInterval = (t == null ? void 0 : t.reconnectInterval) ?? s.reconnectInterval ?? 5e3, this.maxReconnectAttempts = (t == null ? void 0 : t.maxReconnectAttempts) ?? s.maxReconnectAttempts ?? 5, i.info(
|
|
30
|
+
`[uv-api] WebSocket URL 已构建: ${this.url}, reconnectInterval=${this.reconnectInterval}ms, maxReconnectAttempts=${this.maxReconnectAttempts}`
|
|
31
|
+
);
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* 连接到 WebSocket 服务器
|
|
35
|
+
* @param url - 可选的服务器地址,如果提供则覆盖构造函数中的地址
|
|
36
|
+
* @returns 返回 Promise,连接成功时 resolve
|
|
37
|
+
*/
|
|
38
|
+
connect(e) {
|
|
39
|
+
return e && (this.url = e), new Promise((t, s) => {
|
|
40
|
+
try {
|
|
41
|
+
this.ws && this.ws.readyState !== 3 && (i.debug("[uv-api] WebSocket 已连接或正在连接,先关闭现有连接"), this.close()), i.info(`[uv-api] WebSocket 开始连接: ${this.url}`), this.ws = new WebSocket(this.url), this.ws.binaryType = "arraybuffer", this.isManualClose = !1, this.ws.onopen = (n) => {
|
|
42
|
+
i.info("[uv-api] WebSocket 连接成功"), this.isReady = !0, this.reconnectAttempts = 0, this.flushMessageQueue(), this.emit("open", n), t();
|
|
43
|
+
}, this.ws.onmessage = (n) => {
|
|
44
|
+
this.handleMessage(n);
|
|
45
|
+
}, this.ws.onclose = (n) => {
|
|
46
|
+
i.info("[uv-api] WebSocket 连接关闭:", n.code, n.reason), this.isReady = !1, this.emit("close", n), !this.isManualClose && this.reconnectAttempts < this.maxReconnectAttempts ? this.scheduleReconnect() : this.reconnectAttempts >= this.maxReconnectAttempts && (i.info("[uv-api] WebSocket 重连失败: 已达到最大重连次数"), this.emit("reconnectFailed", {
|
|
47
|
+
attempts: this.reconnectAttempts
|
|
48
|
+
}));
|
|
49
|
+
}, this.ws.onerror = (n) => {
|
|
50
|
+
i.debug("[uv-api] WebSocket 连接错误:", n), this.emit("error", n), s(new Error("WebSocket 连接失败"));
|
|
51
|
+
};
|
|
52
|
+
} catch (n) {
|
|
53
|
+
i.debug("[uv-api] WebSocket 连接异常:", n), s(n);
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* 发送消息
|
|
59
|
+
* @param message - 要发送的消息,可以是字符串、对象或 WebSocketMessage
|
|
60
|
+
*/
|
|
61
|
+
send(e) {
|
|
62
|
+
const t = this.normalizeMessage(e);
|
|
63
|
+
if (!this.isReady || !this.ws || this.ws.readyState !== 1) {
|
|
64
|
+
i.debug("[uv-api] WebSocket 未连接,消息已加入队列"), this.messageQueue.push(t);
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
try {
|
|
68
|
+
const s = typeof t == "string" ? t : JSON.stringify(t);
|
|
69
|
+
i.debug("[uv-api] WebSocket 发送消息:", s), this.ws.send(s);
|
|
70
|
+
} catch (s) {
|
|
71
|
+
i.debug("[uv-api] WebSocket 发送消息失败:", s), this.messageQueue.push(t);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* 标准化消息格式
|
|
76
|
+
* @param message - 原始消息
|
|
77
|
+
* @returns 标准化后的消息
|
|
78
|
+
* @private
|
|
79
|
+
*/
|
|
80
|
+
normalizeMessage(e) {
|
|
81
|
+
return typeof e == "string" ? {
|
|
82
|
+
type: "text",
|
|
83
|
+
data: e,
|
|
84
|
+
timestamp: Date.now()
|
|
85
|
+
} : this.isWebSocketMessage(e) ? {
|
|
86
|
+
...e,
|
|
87
|
+
timestamp: e.timestamp || Date.now()
|
|
88
|
+
} : {
|
|
89
|
+
type: "data",
|
|
90
|
+
data: e,
|
|
91
|
+
timestamp: Date.now()
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* 检查是否是 WebSocketMessage 类型
|
|
96
|
+
* @param obj - 要检查的对象
|
|
97
|
+
* @returns 是否是 WebSocketMessage 类型
|
|
98
|
+
* @private
|
|
99
|
+
*/
|
|
100
|
+
isWebSocketMessage(e) {
|
|
101
|
+
return e && typeof e == "object" && "type" in e;
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* 刷新消息队列
|
|
105
|
+
* 将队列中的消息发送出去
|
|
106
|
+
* @private
|
|
107
|
+
*/
|
|
108
|
+
flushMessageQueue() {
|
|
109
|
+
if (this.messageQueue.length === 0)
|
|
110
|
+
return;
|
|
111
|
+
i.debug(`[uv-api] WebSocket 刷新消息队列,共 ${this.messageQueue.length} 条消息`);
|
|
112
|
+
const e = [...this.messageQueue];
|
|
113
|
+
this.messageQueue = [], e.forEach((t) => {
|
|
114
|
+
this.send(t);
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* 处理接收到的消息
|
|
119
|
+
* @param event - WebSocket 消息事件
|
|
120
|
+
* @private
|
|
121
|
+
*/
|
|
122
|
+
handleMessage(e) {
|
|
123
|
+
try {
|
|
124
|
+
let t;
|
|
125
|
+
try {
|
|
126
|
+
t = JSON.parse(e.data), i.debug("[uv-api] WebSocket JSON 解析成功", t);
|
|
127
|
+
} catch {
|
|
128
|
+
try {
|
|
129
|
+
t = g(e.data), i.debug("[uv-api] WebSocket MessagePack 解码成功", t);
|
|
130
|
+
} catch {
|
|
131
|
+
i.debug("[uv-api] WebSocket MessagePack 解码失败", e.data), t = e.data;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
this.emit("message", {
|
|
135
|
+
data: t,
|
|
136
|
+
rawEvent: e
|
|
137
|
+
});
|
|
138
|
+
} catch (t) {
|
|
139
|
+
i.debug("[uv-api] WebSocket 处理消息失败:", t);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* 关闭 WebSocket 连接
|
|
144
|
+
* @param code - 关闭代码
|
|
145
|
+
* @param reason - 关闭原因
|
|
146
|
+
*/
|
|
147
|
+
close(e, t) {
|
|
148
|
+
if (i.info("[uv-api] WebSocket 手动关闭"), this.isManualClose = !0, this.isReady = !1, this.stopReconnect(), this.ws) {
|
|
149
|
+
try {
|
|
150
|
+
this.ws.close(e, t);
|
|
151
|
+
} catch (s) {
|
|
152
|
+
i.debug("[uv-api] WebSocket 关闭失败:", s);
|
|
153
|
+
}
|
|
154
|
+
this.ws = null;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* 安排重连
|
|
159
|
+
* @private
|
|
160
|
+
*/
|
|
161
|
+
scheduleReconnect() {
|
|
162
|
+
this.reconnectTimer || (this.reconnectAttempts++, i.info(
|
|
163
|
+
`[uv-api] WebSocket 将在 ${this.reconnectInterval}ms 后进行第 ${this.reconnectAttempts} 次重连`
|
|
164
|
+
), this.reconnectTimer = window.setTimeout(() => {
|
|
165
|
+
this.reconnectTimer = null, i.info(`[uv-api] WebSocket 开始第 ${this.reconnectAttempts} 次重连`), this.emit("reconnect", {
|
|
166
|
+
attempt: this.reconnectAttempts
|
|
167
|
+
}), this.connect().catch((e) => {
|
|
168
|
+
i.info(`[uv-api] WebSocket 第 ${this.reconnectAttempts} 次重连失败:`, e);
|
|
169
|
+
});
|
|
170
|
+
}, this.reconnectInterval));
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* 停止重连
|
|
174
|
+
* @private
|
|
175
|
+
*/
|
|
176
|
+
stopReconnect() {
|
|
177
|
+
this.reconnectTimer && (clearTimeout(this.reconnectTimer), this.reconnectTimer = null), this.reconnectAttempts = 0;
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* 注册事件监听器
|
|
181
|
+
* @param event - 事件类型
|
|
182
|
+
* @param handler - 事件处理器
|
|
183
|
+
*/
|
|
184
|
+
on(e, t) {
|
|
185
|
+
this.eventHandlers.has(e) || this.eventHandlers.set(e, /* @__PURE__ */ new Set()), this.eventHandlers.get(e).add(t);
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* 移除事件监听器
|
|
189
|
+
* @param event - 事件类型
|
|
190
|
+
* @param handler - 事件处理器
|
|
191
|
+
*/
|
|
192
|
+
off(e, t) {
|
|
193
|
+
const s = this.eventHandlers.get(e);
|
|
194
|
+
s && s.delete(t);
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* 注册一次性事件监听器
|
|
198
|
+
* @param event - 事件类型
|
|
199
|
+
* @param handler - 事件处理器
|
|
200
|
+
*/
|
|
201
|
+
once(e, t) {
|
|
202
|
+
const s = (n) => {
|
|
203
|
+
t(n), this.off(e, s);
|
|
204
|
+
};
|
|
205
|
+
this.on(e, s);
|
|
206
|
+
}
|
|
207
|
+
/**
|
|
208
|
+
* 触发事件
|
|
209
|
+
* @param event - 事件类型
|
|
210
|
+
* @param data - 事件数据
|
|
211
|
+
* @private
|
|
212
|
+
*/
|
|
213
|
+
emit(e, t) {
|
|
214
|
+
const s = this.eventHandlers.get(e);
|
|
215
|
+
s && s.forEach((n) => {
|
|
216
|
+
try {
|
|
217
|
+
n(t);
|
|
218
|
+
} catch (c) {
|
|
219
|
+
i.debug(`[uv-api] WebSocket 事件处理器 ${e} 执行失败:`, c);
|
|
220
|
+
}
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* 移除所有事件监听器
|
|
225
|
+
* @param event - 可选的事件类型,如果不提供则移除所有事件的监听器
|
|
226
|
+
*/
|
|
227
|
+
removeAllListeners(e) {
|
|
228
|
+
e ? this.eventHandlers.delete(e) : this.eventHandlers.clear();
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* 获取连接状态
|
|
232
|
+
* @returns WebSocket 连接状态
|
|
233
|
+
*/
|
|
234
|
+
getReadyState() {
|
|
235
|
+
var e;
|
|
236
|
+
return ((e = this.ws) == null ? void 0 : e.readyState) ?? 3;
|
|
237
|
+
}
|
|
238
|
+
/**
|
|
239
|
+
* 检查是否已连接
|
|
240
|
+
* @returns 是否已连接
|
|
241
|
+
*/
|
|
242
|
+
isConnected() {
|
|
243
|
+
var e;
|
|
244
|
+
return this.isReady && ((e = this.ws) == null ? void 0 : e.readyState) === 1;
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* 获取原生 WebSocket 实例
|
|
248
|
+
* @returns WebSocket 实例
|
|
249
|
+
*/
|
|
250
|
+
getWebSocket() {
|
|
251
|
+
return this.ws;
|
|
252
|
+
}
|
|
253
|
+
/**
|
|
254
|
+
* 获取重连尝试次数
|
|
255
|
+
* @returns 重连尝试次数
|
|
256
|
+
*/
|
|
257
|
+
getReconnectAttempts() {
|
|
258
|
+
return this.reconnectAttempts;
|
|
259
|
+
}
|
|
260
|
+
/**
|
|
261
|
+
* 获取待发送消息队列长度
|
|
262
|
+
* @returns 队列长度
|
|
263
|
+
*/
|
|
264
|
+
getMessageQueueLength() {
|
|
265
|
+
return this.messageQueue.length;
|
|
266
|
+
}
|
|
267
|
+
/**
|
|
268
|
+
* 清空消息队列
|
|
269
|
+
*/
|
|
270
|
+
clearMessageQueue() {
|
|
271
|
+
this.messageQueue = [];
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
export {
|
|
275
|
+
k as WebSocketClient,
|
|
276
|
+
f as WebSocketState
|
|
277
|
+
};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { RestAPIClient } from './RestAPIClient';
|
|
2
|
+
export type { RestAPIConfig, Response, ErrorResponse } from './RestAPIClient';
|
|
3
|
+
export { WebSocketClient } from './WebSocketClient';
|
|
4
|
+
export type { WebSocketConfig, WebSocketMessage, WebSocketEventType, WebSocketEventHandler } from './WebSocketClient';
|
|
5
|
+
export { WebSocketState } from './WebSocketClient';
|
|
6
|
+
export * from './modules';
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { RestAPIClient, RestAPIConfig } from '../RestAPIClient';
|
|
2
|
+
export interface Task {
|
|
3
|
+
id: number;
|
|
4
|
+
title: string;
|
|
5
|
+
description?: string;
|
|
6
|
+
completed?: boolean;
|
|
7
|
+
priority?: 1 | 2 | 3 | 4 | 5;
|
|
8
|
+
user_id?: number;
|
|
9
|
+
created_at?: string;
|
|
10
|
+
updated_at?: string;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* 任务客户端类,用于处理任务相关的 API 请求
|
|
14
|
+
* @extends RestAPIClient
|
|
15
|
+
*/
|
|
16
|
+
export declare class TaskClient extends RestAPIClient {
|
|
17
|
+
/**
|
|
18
|
+
* 创建 TaskClient 实例
|
|
19
|
+
* @param {Omit<RestAPIConfig, 'workerId'>} [config] - REST API 配置对象(可选参数)
|
|
20
|
+
* @param {string} [config.baseURL] - API 基础 URL(可选)
|
|
21
|
+
* @param {number} [config.timeout] - 请求超时时间(可选)
|
|
22
|
+
* @param {Record<string, string>} [config.headers] - 自定义请求头(可选)
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* const taskClient = new TaskClient({
|
|
26
|
+
* baseURL: 'https://api.example.com',
|
|
27
|
+
* timeout: 5000,
|
|
28
|
+
* headers: {
|
|
29
|
+
* 'Authorization': 'Bearer token123'
|
|
30
|
+
* }
|
|
31
|
+
* });
|
|
32
|
+
*/
|
|
33
|
+
constructor(config?: Omit<RestAPIConfig, 'workerId'>);
|
|
34
|
+
/**
|
|
35
|
+
* 创建新任务
|
|
36
|
+
* @param {Omit<Task, 'id' | 'created_at' | 'updated_at'>} task - 任务信息对象
|
|
37
|
+
* @param {string} task.title - 任务标题(必填)
|
|
38
|
+
* @param {string} [task.description] - 任务描述(可选参数)
|
|
39
|
+
* @param {boolean} [task.completed] - 任务完成状态(可选参数)
|
|
40
|
+
* @param {1 | 2 | 3 | 4 | 5} [task.priority] - 任务优先级,1-5(可选参数)
|
|
41
|
+
* @param {number} [task.user_id] - 关联的用户 ID(可选参数)
|
|
42
|
+
* @returns {Promise<Response<Task>>} 返回包含任务对象的响应
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* const response = await taskClient.create_task({
|
|
46
|
+
* title: '完成项目文档',
|
|
47
|
+
* description: '编写 API 接口文档',
|
|
48
|
+
* completed: false,
|
|
49
|
+
* priority: 1,
|
|
50
|
+
* user_id: 123
|
|
51
|
+
* });
|
|
52
|
+
* const newTask = response.result;
|
|
53
|
+
*/
|
|
54
|
+
create_task(task: Omit<Task, 'id' | 'created_at' | 'updated_at'>): Promise<import('../RestAPIClient').Response<Task>>;
|
|
55
|
+
/**
|
|
56
|
+
* 获取所有任务列表
|
|
57
|
+
* @returns {Promise<Response<Task[]>>} 返回包含任务列表的响应
|
|
58
|
+
*
|
|
59
|
+
* @example
|
|
60
|
+
* const response = await taskClient.get_tasks();
|
|
61
|
+
* const tasks = response.result;
|
|
62
|
+
*/
|
|
63
|
+
get_tasks(): Promise<import('../RestAPIClient').Response<Task[]>>;
|
|
64
|
+
/**
|
|
65
|
+
* 根据任务 ID 获取单个任务信息
|
|
66
|
+
* @param {Object} data - 查询参数对象
|
|
67
|
+
* @param {number} data.task_id - 任务 ID(必填)
|
|
68
|
+
* @returns {Promise<Response<Task>>} 返回包含任务对象的响应
|
|
69
|
+
*
|
|
70
|
+
* @example
|
|
71
|
+
* const response = await taskClient.get_task({
|
|
72
|
+
* task_id: 1
|
|
73
|
+
* });
|
|
74
|
+
* const task = response.result;
|
|
75
|
+
*/
|
|
76
|
+
get_task(data: {
|
|
77
|
+
task_id: number;
|
|
78
|
+
}): Promise<import('../RestAPIClient').Response<Task>>;
|
|
79
|
+
/**
|
|
80
|
+
* 更新任务信息
|
|
81
|
+
* @param {Object} data - 更新参数对象
|
|
82
|
+
* @param {number} data.task_id - 任务 ID(必填)
|
|
83
|
+
* @param {Object} data.task_update - 任务更新信息对象(必填)
|
|
84
|
+
* @param {string} [data.task_update.title] - 任务标题(可选参数)
|
|
85
|
+
* @param {string} [data.task_update.description] - 任务描述(可选参数)
|
|
86
|
+
* @param {boolean} [data.task_update.completed] - 任务完成状态(可选参数)
|
|
87
|
+
* @param {1 | 2 | 3 | 4 | 5} [data.task_update.priority] - 任务优先级,1-5(可选参数)
|
|
88
|
+
* @param {number} [data.task_update.user_id] - 关联的用户 ID(可选参数)
|
|
89
|
+
* @returns {Promise<Response<Task>>} 返回包含更新后任务对象的响应
|
|
90
|
+
*
|
|
91
|
+
* @example
|
|
92
|
+
* const response = await taskClient.update_task({
|
|
93
|
+
* task_id: 1,
|
|
94
|
+
* task_update: {
|
|
95
|
+
* title: '完成项目文档(已更新)',
|
|
96
|
+
* description: '编写完整的 API 接口文档',
|
|
97
|
+
* completed: true,
|
|
98
|
+
* priority: 2,
|
|
99
|
+
* user_id: 456
|
|
100
|
+
* }
|
|
101
|
+
* });
|
|
102
|
+
* const updatedTask = response.result;
|
|
103
|
+
*/
|
|
104
|
+
update_task(data: {
|
|
105
|
+
task_id: number;
|
|
106
|
+
task_update: Partial<Omit<Task, 'id' | 'created_at' | 'updated_at'>>;
|
|
107
|
+
}): Promise<import('../RestAPIClient').Response<Task>>;
|
|
108
|
+
/**
|
|
109
|
+
* 删除任务
|
|
110
|
+
* @param {Object} data - 删除参数对象
|
|
111
|
+
* @param {Object} data.task_delete - 任务删除信息对象(必填)
|
|
112
|
+
* @param {number} data.task_delete.id - 要删除的任务 ID(必填)
|
|
113
|
+
* @returns {Promise<Response<Task>>} 返回包含被删除任务对象的响应
|
|
114
|
+
*
|
|
115
|
+
* @example
|
|
116
|
+
* const response = await taskClient.delete_task({
|
|
117
|
+
* id: 1
|
|
118
|
+
* });
|
|
119
|
+
* const deletedTask = response.result;
|
|
120
|
+
*/
|
|
121
|
+
delete_task(data: {
|
|
122
|
+
id: number;
|
|
123
|
+
}): Promise<import('../RestAPIClient').Response<Task>>;
|
|
124
|
+
}
|