cwj_monitoring 0.0.23 → 0.0.25

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 CHANGED
@@ -1,25 +1,302 @@
1
- type Plugin = 'error' | 'click' | 'performance' | 'router';
1
+ /**
2
+ * 事件发送类型标识符
3
+ * 用于向追踪系统发送事件时使用
4
+ */
5
+ declare enum EMIT_TYPE {
6
+ ERROR = "error",
7
+ BEHAVIOR_CLICK = "click",
8
+ ROUTE_CHANGE = "route_change",
9
+ PERFORMANCE_FP = "performance_fp",
10
+ PERFORMANCE_FCP = "performance_fcp",
11
+ PERFORMANCE_LCP = "performance_lcp",
12
+ PERFORMANCE_INP = "performance_inp",
13
+ PERFORMANCE_LONGTASK = "performance_longtask",
14
+ PERFORMANCE_RESOURCE = "performance_resource",
15
+ PERFORMANCE_LOAF = "performance_loaf",
16
+ XHR = "xhr",
17
+ FETCH = "fetch",
18
+ CUSTOM = "custom"
19
+ }
20
+
21
+ /**
22
+ * 插件执行上下文
23
+ * 提供插件所需的最小功能集,实现插件与核心实例的解耦
24
+ */
25
+ interface PluginContext {
26
+ /** 发送事件 */
27
+ emit: (type: EMIT_TYPE | string, data: any) => void;
28
+ /** 监控上报地址(用于网络插件过滤) */
29
+ url: string;
30
+ }
31
+ /**
32
+ * 基础插件接口
33
+ * 所有监控插件必须实现此接口
34
+ */
35
+ interface IPlugin {
36
+ /**
37
+ * 唯一的插件标识符
38
+ */
39
+ readonly name: string;
40
+ /**
41
+ * 插件安装函数
42
+ * 当插件注册到追踪器时调用
43
+ *
44
+ * @param context - 插件执行上下文
45
+ */
46
+ install(context: PluginContext): void;
47
+ /**
48
+ * 插件卸载函数(可选)
49
+ * 用于清理监听器等
50
+ */
51
+ uninstall?(): void;
52
+ }
53
+
54
+ /**
55
+ * SDK 收集的完整设备信息
56
+ */
57
+ interface Device {
58
+ /** 浏览器信息 */
59
+ browser: {
60
+ name?: string;
61
+ version?: string;
62
+ };
63
+ /** 操作系统 */
64
+ os: {
65
+ name?: string;
66
+ version?: string;
67
+ versionName?: string;
68
+ };
69
+ /** 设备种类 */
70
+ platform: {
71
+ type?: string;
72
+ };
73
+ /** 浏览器缩放比例 */
74
+ ratio: number;
75
+ /** 浏览器宽高 */
76
+ wh: {
77
+ /** 浏览器宽 */
78
+ width: number;
79
+ /** 浏览器高 */
80
+ height: number;
81
+ };
82
+ }
83
+
84
+ /**
85
+ * 发送到监控后端的数据格式
86
+ */
87
+ interface MonitoringPayload {
88
+ /** 设备信息 */
89
+ device: Device;
90
+ /** 唯一访客标识(指纹) */
91
+ uuid: string;
92
+ /** 事件类型 */
93
+ type: string;
94
+ /** 事件特定数据 */
95
+ data: any;
96
+ /** ISO 8601 格式的时间戳 */
97
+ date: string;
98
+ /** 用户自定义元数据 */
99
+ userData?: Record<string, any>;
100
+ }
101
+
102
+ /**
103
+ * 数据发送和批处理配置
104
+ */
105
+ interface TransportConfig {
106
+ /**
107
+ * 批量发送前的最大事件数
108
+ * @default 5
109
+ */
110
+ maxBatchSize?: number;
111
+ /**
112
+ * 批量发送前的最大等待时间(毫秒)
113
+ * @default 30000(30秒)
114
+ */
115
+ maxWaitTime?: number;
116
+ }
2
117
 
3
- //初始化时传入的参数
118
+
119
+ /**
120
+ * SDK 初始化配置选项
121
+ */
4
122
  interface Options {
123
+ /**
124
+ * 发送监控数据的后端 URL
125
+ * @required 必填
126
+ */
5
127
  url: string;
6
- max?: number;
7
- time?: number;
8
- plugin?: Plugin[];
9
- data?: any;
128
+
129
+ /**
130
+ * 要启用的插件列表
131
+ * @optional 可选
132
+ */
133
+ plugin?: IPlugin[];
134
+
135
+ /**
136
+ * 附加到所有事件的自定义用户元数据
137
+ * 用于存储应用版本、环境、用户属性等
138
+ * @optional 可选
139
+ * @example { version: '1.2.3', env: 'production', userId: '12345' }
140
+ */
141
+ data?: Record<string, any>;
142
+
143
+ /**
144
+ * 传输/批处理配置
145
+ * @optional 可选
146
+ */
147
+ transport?: TransportConfig;
148
+
149
+ /**
150
+ * 挂载在 window 上的全局变量名称
151
+ * @default '$track'
152
+ */
153
+ globalKey?: string;
154
+
155
+ /**
156
+ * 存储 UUID 的 localStorage key
157
+ * @default 'track_uuid'
158
+ */
159
+ uuidKey?: string;
10
160
  }
11
161
 
12
- declare enum TYPES {
13
- ERROR = "error",
14
- CLICK = "click",
15
- PERFORMANCE = "performance",
16
- ROUTER = "router"
162
+ /**
163
+ * Reporter 类负责数据的批量上报、重试和调度
164
+ */
165
+ declare class Reporter {
166
+ private url;
167
+ private transportConfig;
168
+ private events;
169
+ private isSending;
170
+ private timer;
171
+ constructor(url: string, config?: TransportConfig);
172
+ /**
173
+ * 将事件加入队列
174
+ */
175
+ send(payload: MonitoringPayload): void;
176
+ /**
177
+ * 立即刷新所有待发送事件
178
+ */
179
+ flush(): Promise<void>;
180
+ private clearTimer;
181
+ /**
182
+ * 触发发送逻辑
183
+ */
184
+ private triggerSend;
185
+ /**
186
+ * 使用 navigator.sendBeacon 或 XMLHttpRequest 安全发送事件
187
+ */
188
+ private safeSend;
189
+ /**
190
+ * 通过 XMLHttpRequest 发送
191
+ */
192
+ private sendViaXHR;
17
193
  }
18
194
 
19
- declare const init: (options: Options) => void;
195
+ /**
196
+ * EventTrack 类处理事件收集和转换
197
+ * 职责:
198
+ * 1. 管理设备信息 (DeviceInfo)
199
+ * 2. 格式化原始数据为 MonitoringPayload
200
+ * 3. 将格式化后的数据交给 Reporter 发送
201
+ */
202
+ declare class EventTrack {
203
+ private deviceInfo;
204
+ private reporter;
205
+ private data?;
206
+ constructor(options: Options, reporter: Reporter);
207
+ /**
208
+ * 格式化事件数据用于传输
209
+ */
210
+ private formatter;
211
+ /**
212
+ * 公共方法:发送事件
213
+ * 将事件格式化后交给 reporter 处理
214
+ */
215
+ emit(type: EMIT_TYPE | string, data?: any): void;
216
+ /**
217
+ * 获取上报器实例 (供 Core 使用)
218
+ */
219
+ getReporter(): Reporter;
220
+ }
221
+
222
+ declare class Core extends EventTrack {
223
+ private pluginMap;
224
+ private options;
225
+ constructor(options: Options);
226
+ use(plugin: IPlugin): Core;
227
+ /**
228
+ * 自定义事件上报
229
+ * @param data 上报的数据
230
+ * @param type 事件类型,默认为 'custom'
231
+ */
232
+ log(data: any, type?: string): void;
233
+ run(): void;
234
+ stop(): void;
235
+ private mount;
236
+ private unmount;
237
+ }
238
+ /**
239
+ * 创建监控实例的工厂函数
240
+ * @param options 配置项
241
+ */
242
+ declare function createMonitor(options: Options): Core;
243
+
244
+ interface ErrorOptions {
245
+ /** 过滤函数,返回 false 则不记录该错误 */
246
+ filter?: (error: any) => boolean;
247
+ }
248
+ /**
249
+ * 错误监控插件
250
+ * 监控并捕获 JavaScript 错误、资源加载错误、Promise 拒绝以及 console.error 调用
251
+ */
252
+ declare const ErrorPlugin: (options?: ErrorOptions) => IPlugin;
253
+
254
+ interface PVOptions {
255
+ /** 过滤函数,返回 false 则不记录该路由变化 */
256
+ filter?: (to: string, from: string) => boolean;
257
+ }
258
+ declare const PVPlugin: (options?: PVOptions) => IPlugin;
20
259
 
21
- declare const _default: {
22
- init: (options: Options) => void;
23
- };
260
+ interface BehaviorOptions {
261
+ /** 过滤函数,返回 false 则不记录该点击事件 */
262
+ filter?: (element: HTMLElement) => boolean;
263
+ /** 节流延迟时间(ms),默认 500ms */
264
+ throttleDelay?: number;
265
+ }
266
+ declare const BehaviorPlugin: (options?: BehaviorOptions) => IPlugin;
267
+
268
+ interface PerformanceOptions {
269
+ /** 过滤函数,返回 false 则不记录该性能指标 */
270
+ filter?: (type: EMIT_TYPE, value: any) => boolean;
271
+ /** 长任务阈值 (ms),超过此值则上报,默认 100 */
272
+ longTaskThreshold?: number;
273
+ /** 资源加载阈值 (ms),超过此值则上报,默认 1000 */
274
+ resourceThreshold?: number;
275
+ /** INP 阈值 (ms),超过此值则上报,默认 200 */
276
+ inpThreshold?: number;
277
+ /** LoAF 阈值 (ms),超过此值则上报,默认 100 */
278
+ loafThreshold?: number;
279
+ }
280
+ declare const PerformancePlugin: (options?: PerformanceOptions) => IPlugin;
281
+
282
+ interface XHROptions {
283
+ /** 过滤函数,返回 false 则不记录该请求 */
284
+ filter?: (method: string, url: string) => boolean;
285
+ }
286
+ /**
287
+ * XHR 监控插件
288
+ * 监控 XMLHttpRequest 请求,记录失败的请求(状态码非 2xx)
289
+ */
290
+ declare const XHRPlugin: (options?: XHROptions) => IPlugin;
291
+
292
+ interface FetchOptions {
293
+ /** 过滤函数,返回 false 则不记录该请求 */
294
+ filter?: (method: string, url: string) => boolean;
295
+ }
296
+ /**
297
+ * Fetch 监控插件
298
+ * 监控 fetch 请求,记录失败的请求(状态码非 2xx 或网络错误)
299
+ */
300
+ declare const FetchPlugin: (options?: FetchOptions) => IPlugin;
24
301
 
25
- export { TYPES, _default as default, init };
302
+ export { BehaviorPlugin, Core, EMIT_TYPE, ErrorPlugin, FetchPlugin, PVPlugin, PerformancePlugin, XHRPlugin, createMonitor };