aegis-mp-rum-wrapper 0.1.0 → 0.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/README.md ADDED
@@ -0,0 +1,82 @@
1
+ ## aegis-mp-rum-wrapper
2
+
3
+ 基于 `aegis-mp-sdk` 的小程序上报封装 SDK,用于在小程序中上报 RUM 日志,并在原有 Aegis 能力上增加一层业务侧埋点封装。
4
+
5
+ ### 安装
6
+
7
+ ```bash
8
+ npm install aegis-mp-rum-wrapper aegis-mp-sdk --save
9
+ ```
10
+
11
+ 在小程序开发者工具中执行「构建 npm」,确保依赖可用。
12
+
13
+ ### 快速使用
14
+
15
+ #### 1. 在小程序入口初始化用户信息
16
+
17
+ ```ts
18
+ import { AegisW } from 'aegis-mp-rum-wrapper';
19
+
20
+ // 初始化用户信息(示例)
21
+ AegisW.user = {
22
+ openid: 'xxxx',
23
+ roleID: 'xxxx',
24
+ area: 1,
25
+ zone: 1,
26
+ channeldID: 100, // 可选,SDK 会自动映射到 channelID
27
+ plat: 9,
28
+ serviceType: 'xxx', // 必填
29
+ eventID: 'page_enter', // 必填
30
+ contentID: 'doc_001',
31
+ locale: 'zh',
32
+ accountType: 'wx',
33
+ channelUID: '600',
34
+ columnUID: '60055',
35
+ platName: 'PC',
36
+ ext1: '',
37
+ ext2: '',
38
+ ext3: '',
39
+ };
40
+ ```
41
+
42
+ 当 `openid`、`serviceType`、`eventID` 三个字段均非空时,会自动初始化默认 Aegis 实例并开始上报。
43
+
44
+ #### 2. 自定义事件上报
45
+
46
+ ```ts
47
+ // 与 aegis-mp-sdk 保持一致,自定义事件填写 name 和 extX 字段
48
+ AegisW.reportEvent({
49
+ name: 'vote',
50
+ ext1: '22',
51
+ ext2: '22',
52
+ ext3: '22',
53
+ });
54
+ ```
55
+
56
+ 内部会自动把扩展业务日志打包成 JSON 串写入 `ext1`,并通过 Aegis 实例上报。
57
+
58
+ ### 高级用法
59
+
60
+ 如果需要更细粒度控制,可以使用函数式 API:
61
+
62
+ ```ts
63
+ import {
64
+ configureWrapper,
65
+ setUser,
66
+ setRumID,
67
+ reportEvent,
68
+ getInstances,
69
+ } from 'aegis-mp-rum-wrapper';
70
+ ```
71
+
72
+ - `configureWrapper`:配置上报基础行为(如是否测速、延迟等)
73
+ - `setUser`:更新用户信息,并在条件满足时初始化默认实例
74
+ - `setRumID`:设置服务侧 RUM ID 并初始化服务实例
75
+ - `reportEvent`:事件上报(与 `AegisW.reportEvent` 等价)
76
+ - `getInstances`:获取底层 `defaultAegis` / `serviceAegis` 实例
77
+
78
+ ### 许可证
79
+
80
+ MIT
81
+
82
+
package/dist/index.js CHANGED
@@ -21,8 +21,8 @@ exports.setRumID = setRumID;
21
21
  exports.reportEvent = reportEvent;
22
22
  exports.getInstances = getInstances;
23
23
  var aegis_mp_sdk_1 = __importDefault(require("aegis-mp-sdk"));
24
- // rum id
25
- exports.GPPRumID = 'zYpo1fz400Vk86JR5Z'; // cn services
24
+ // RUM 上报 ID(默认使用国服服务)
25
+ exports.GPPRumID = 'zYpo1fz400Vk86JR5Z';
26
26
  var logItemTemplate = {
27
27
  report_type: 11,
28
28
  event_id: '',
@@ -66,7 +66,7 @@ var logItemTemplate = {
66
66
  content_id: '',
67
67
  source: 'fe_rum',
68
68
  };
69
- // 內部狀態
69
+ // 内部状态
70
70
  var defaultAegis;
71
71
  var serviceAegis;
72
72
  var internalState = {
@@ -81,6 +81,7 @@ var internalState = {
81
81
  rumID: '',
82
82
  ready: false,
83
83
  };
84
+ // 构建一条上报日志 JSON 字符串(写入 ext1)
84
85
  function buildReportStr(params, isPV) {
85
86
  var _a, _b, _c, _d, _e;
86
87
  var logItem = __assign({}, logItemTemplate);
@@ -124,6 +125,7 @@ function buildReportStr(params, isPV) {
124
125
  logItem.log_ts = Math.floor(Date.now() / 1000);
125
126
  return JSON.stringify(logItem);
126
127
  }
128
+ // 初始化 Aegis 实例(isDefault=true 为默认实例,false 为服务实例)
127
129
  function initAegis(isDefault) {
128
130
  var id = internalState.id;
129
131
  var apiSpeed = internalState.reportApiSpeed;
@@ -148,7 +150,7 @@ function initAegis(isDefault) {
148
150
  ext3: internalState.user.ext3,
149
151
  }, true);
150
152
  }
151
- // 小程序環境直接使用構造函數
153
+ // 小程序环境直接使用构造函数
152
154
  if (typeof aegis_mp_sdk_1.default === 'function') {
153
155
  return new aegis_mp_sdk_1.default({
154
156
  id: id,
@@ -171,10 +173,10 @@ function initAegis(isDefault) {
171
173
  },
172
174
  });
173
175
  }
174
- console.error('[Aegis Wrapper] 找不到 aegis-mp-sdk 構造函數,請確認依賴是否正確安裝和引用');
176
+ console.error('[Aegis Wrapper] 找不到 aegis-mp-sdk 构造函数,请确认依赖是否正确安装和引用');
175
177
  return undefined;
176
178
  }
177
- // 對外暴露的 API
179
+ // 对外暴露的配置 API
178
180
  function configureWrapper(config) {
179
181
  if (config.id)
180
182
  internalState.id = config.id;
@@ -194,7 +196,7 @@ function configureWrapper(config) {
194
196
  function setUser(user) {
195
197
  var incoming = user;
196
198
  var nextUser = __assign(__assign({}, internalState.user), user);
197
- // 兼容 channeldID(文檔示例裡的字段名)到 channelID
199
+ // 兼容 channeldID(文档示例里的字段名)到 channelID
198
200
  if (incoming.channeldID != null && nextUser.channelID == null) {
199
201
  nextUser.channelID = incoming.channeldID;
200
202
  }
@@ -208,29 +210,30 @@ function setUser(user) {
208
210
  if (!internalState.ready) {
209
211
  internalState.ready = true;
210
212
  defaultAegis = initAegis(true);
211
- console.log('[Aegis Wrapper] default aegis ready');
213
+ console.log('[Aegis Wrapper] default aegis 已就绪');
212
214
  }
213
215
  }
214
216
  else {
215
- console.log('[Aegis Wrapper] 初始化條件不足: 需要 openid / serviceType / eventID', u);
217
+ console.log('[Aegis Wrapper] 初始化条件不足:需要 openid / serviceType / eventID', u);
216
218
  }
217
219
  }
218
220
  function setRumID(rumID) {
219
221
  internalState.rumID = rumID;
220
222
  serviceAegis = initAegis(false);
221
- console.log('[Aegis Wrapper] service aegis rumID 已設置');
223
+ console.log('[Aegis Wrapper] service aegis rumID 已设置');
222
224
  }
225
+ // 事件上报入口:兼容字符串和对象参数
223
226
  function reportEvent(params) {
224
- // 先調用服務實例(如果存在)
227
+ // 先调用服务实例(如果存在)
225
228
  if (serviceAegis && typeof serviceAegis.reportEvent === 'function') {
226
229
  serviceAegis.reportEvent(params);
227
230
  }
228
231
  if (!defaultAegis || typeof defaultAegis.reportEvent !== 'function') {
229
- console.error('[Aegis Wrapper] default aegis 尚未準備就緒,請先調用 setUser 並補全必要字段');
232
+ console.error('[Aegis Wrapper] default aegis 尚未准备就绪,请先调用 setUser 并补全必要字段');
230
233
  return;
231
234
  }
232
235
  if (typeof params !== 'string' && typeof params !== 'object') {
233
- console.error('[Aegis Wrapper] reportEvent 參數類型錯誤,應為 string 或 object');
236
+ console.error('[Aegis Wrapper] reportEvent 参数类型错误,应为 string 或 object');
234
237
  return;
235
238
  }
236
239
  if (typeof params === 'object' && params.name === undefined) {
@@ -241,7 +244,7 @@ function reportEvent(params) {
241
244
  payload.ext1 = buildReportStr(payload, false);
242
245
  defaultAegis.reportEvent(payload);
243
246
  }
244
- // 方便調試或高級用戶直接訪問實例
247
+ // 方便调试或高级用户直接访问底层实例
245
248
  function getInstances() {
246
249
  return {
247
250
  defaultAegis: defaultAegis,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "aegis-mp-rum-wrapper",
3
- "version": "0.1.0",
4
- "description": "基於 aegis-mp-sdk 的小程序上報封裝 SDK,支持雙實例與自定義 RUM 日誌構造。",
3
+ "version": "0.1.1",
4
+ "description": "基于 aegis-mp-sdk 的小程序上报封装 SDK,支持双实例与自定义 RUM 日志构造。",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
7
7
  "scripts": {
package/src/index.ts CHANGED
@@ -1,9 +1,9 @@
1
1
  import AegisCtor from 'aegis-mp-sdk';
2
2
 
3
- // rum id
4
- export const GPPRumID = 'zYpo1fz400Vk86JR5Z'; // cn services
3
+ // RUM 上报 ID(默认使用国服服务)
4
+ export const GPPRumID = 'zYpo1fz400Vk86JR5Z';
5
5
 
6
- // 封裝的用戶信息結構
6
+ // 封装的用户信息结构
7
7
  export interface AegisUserInfo {
8
8
  openid?: string;
9
9
  roleID?: string;
@@ -29,7 +29,7 @@ export interface AegisUserInfo {
29
29
  }
30
30
 
31
31
  export interface AegisMpWrapperConfig {
32
- id?: string; // 默認使用 GPPRumID
32
+ id?: string; // 默认使用 GPPRumID
33
33
  uin?: string;
34
34
  reportApiSpeed?: boolean;
35
35
  reportAssetSpeed?: boolean;
@@ -38,7 +38,7 @@ export interface AegisMpWrapperConfig {
38
38
  delay?: number;
39
39
  }
40
40
 
41
- // log item template
41
+ // 日志字段模板
42
42
  interface LogItem {
43
43
  report_type: number;
44
44
  event_id: string;
@@ -127,7 +127,7 @@ const logItemTemplate: LogItem = {
127
127
  source: 'fe_rum',
128
128
  };
129
129
 
130
- // 內部狀態
130
+ // 内部状态
131
131
  let defaultAegis: any;
132
132
  let serviceAegis: any;
133
133
 
@@ -152,6 +152,7 @@ export interface ReportParams {
152
152
  [key: string]: any;
153
153
  }
154
154
 
155
+ // 构建一条上报日志 JSON 字符串(写入 ext1)
155
156
  function buildReportStr(params: string | ReportParams, isPV: boolean): string {
156
157
  const logItem: LogItem = { ...logItemTemplate };
157
158
 
@@ -199,6 +200,7 @@ function buildReportStr(params: string | ReportParams, isPV: boolean): string {
199
200
  return JSON.stringify(logItem);
200
201
  }
201
202
 
203
+ // 初始化 Aegis 实例(isDefault=true 为默认实例,false 为服务实例)
202
204
  function initAegis(isDefault: boolean): any | undefined {
203
205
  let id = internalState.id;
204
206
  let apiSpeed = internalState.reportApiSpeed;
@@ -229,7 +231,7 @@ function initAegis(isDefault: boolean): any | undefined {
229
231
  );
230
232
  }
231
233
 
232
- // 小程序環境直接使用構造函數
234
+ // 小程序环境直接使用构造函数
233
235
  if (typeof AegisCtor === 'function') {
234
236
  return new (AegisCtor as any)({
235
237
  id,
@@ -253,11 +255,11 @@ function initAegis(isDefault: boolean): any | undefined {
253
255
  });
254
256
  }
255
257
 
256
- console.error('[Aegis Wrapper] 找不到 aegis-mp-sdk 構造函數,請確認依賴是否正確安裝和引用');
258
+ console.error('[Aegis Wrapper] 找不到 aegis-mp-sdk 构造函数,请确认依赖是否正确安装和引用');
257
259
  return undefined;
258
260
  }
259
261
 
260
- // 對外暴露的 API
262
+ // 对外暴露的配置 API
261
263
  export function configureWrapper(config: AegisMpWrapperConfig): void {
262
264
  if (config.id) internalState.id = config.id;
263
265
  if (typeof config.uin === 'string') internalState.uin = config.uin;
@@ -275,7 +277,7 @@ export function setUser(user: Partial<AegisUserInfo>): void {
275
277
  ...user,
276
278
  };
277
279
 
278
- // 兼容 channeldID(文檔示例裡的字段名)到 channelID
280
+ // 兼容 channeldID(文档示例里的字段名)到 channelID
279
281
  if (incoming.channeldID != null && nextUser.channelID == null) {
280
282
  (nextUser as any).channelID = incoming.channeldID;
281
283
  }
@@ -292,32 +294,33 @@ export function setUser(user: Partial<AegisUserInfo>): void {
292
294
  if (!internalState.ready) {
293
295
  internalState.ready = true;
294
296
  defaultAegis = initAegis(true);
295
- console.log('[Aegis Wrapper] default aegis ready');
297
+ console.log('[Aegis Wrapper] default aegis 已就绪');
296
298
  }
297
299
  } else {
298
- console.log('[Aegis Wrapper] 初始化條件不足: 需要 openid / serviceType / eventID', u);
300
+ console.log('[Aegis Wrapper] 初始化条件不足:需要 openid / serviceType / eventID', u);
299
301
  }
300
302
  }
301
303
 
302
304
  export function setRumID(rumID: string): void {
303
305
  internalState.rumID = rumID;
304
306
  serviceAegis = initAegis(false);
305
- console.log('[Aegis Wrapper] service aegis rumID 已設置');
307
+ console.log('[Aegis Wrapper] service aegis rumID 已设置');
306
308
  }
307
309
 
310
+ // 事件上报入口:兼容字符串和对象参数
308
311
  export function reportEvent(params: string | ReportParams): void {
309
- // 先調用服務實例(如果存在)
312
+ // 先调用服务实例(如果存在)
310
313
  if (serviceAegis && typeof (serviceAegis as any).reportEvent === 'function') {
311
314
  (serviceAegis as any).reportEvent(params as any);
312
315
  }
313
316
 
314
317
  if (!defaultAegis || typeof (defaultAegis as any).reportEvent !== 'function') {
315
- console.error('[Aegis Wrapper] default aegis 尚未準備就緒,請先調用 setUser 並補全必要字段');
318
+ console.error('[Aegis Wrapper] default aegis 尚未准备就绪,请先调用 setUser 并补全必要字段');
316
319
  return;
317
320
  }
318
321
 
319
322
  if (typeof params !== 'string' && typeof params !== 'object') {
320
- console.error('[Aegis Wrapper] reportEvent 參數類型錯誤,應為 string 或 object');
323
+ console.error('[Aegis Wrapper] reportEvent 参数类型错误,应为 string 或 object');
321
324
  return;
322
325
  }
323
326
 
@@ -331,7 +334,7 @@ export function reportEvent(params: string | ReportParams): void {
331
334
  (defaultAegis as any).reportEvent(payload);
332
335
  }
333
336
 
334
- // 方便調試或高級用戶直接訪問實例
337
+ // 方便调试或高级用户直接访问底层实例
335
338
  export function getInstances() {
336
339
  return {
337
340
  defaultAegis,
@@ -339,7 +342,7 @@ export function getInstances() {
339
342
  };
340
343
  }
341
344
 
342
- // 物件式封裝,支持示例中的兩種用法:
345
+ // 面向业务侧的简单封装对象,支持示例中的两种用法:
343
346
  // 1)AegisW.user = {...}
344
347
  // 2)AegisW.reportEvent({...})
345
348
  export interface AegisWrapperFacade {