@ohbug/core 2.1.1 → 2.2.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.
@@ -0,0 +1,29 @@
1
+ import { OhbugClientConstructor, OhbugEventWithMethods, OhbugExtension } from '@ohbug/types';
2
+
3
+ declare const Client: OhbugClientConstructor;
4
+
5
+ declare function isEvent(eventLike: any): eventLike is OhbugEventWithMethods<any>;
6
+
7
+ declare function defineExtension(extension: OhbugExtension): OhbugExtension;
8
+
9
+ declare const enum EventTypes {
10
+ UNCAUGHT_ERROR = "uncaughtError",
11
+ RESOURCE_ERROR = "resourceError",
12
+ UNHANDLEDREJECTION_ERROR = "unhandledrejectionError",
13
+ AJAX_ERROR = "ajaxError",
14
+ FETCH_ERROR = "fetchError",
15
+ WEBSOCKET_ERROR = "websocketError",
16
+ UNKNOWN_ERROR = "unknownError",
17
+ MESSAGE = "message",
18
+ FEEDBACK = "feedback",
19
+ VIEW = "view",
20
+ REACT = "react",
21
+ VUE = "vue",
22
+ ANGULAR = "angular",
23
+ MINIAPP_ERROR = "miniappError",
24
+ MINIAPP_UNHANDLEDREJECTION_ERROR = "miniappUnhandledrejectionError",
25
+ MINIAPP_PAGENOTFOUND_ERROR = "miniappPagenotfoundError",
26
+ MINIAPP_MEMORYWARNING_ERROR = "miniappMemorywarningError"
27
+ }
28
+
29
+ export { Client, EventTypes, defineExtension, isEvent };
package/dist/index.js CHANGED
@@ -51,6 +51,7 @@ var import_utils4 = require("@ohbug/utils");
51
51
  // src/config.ts
52
52
  var import_utils = require("@ohbug/utils");
53
53
  var schema = {
54
+ // base
54
55
  apiKey: {
55
56
  defaultValue: void 0,
56
57
  message: "is required",
@@ -79,8 +80,9 @@ var schema = {
79
80
  maxActions: {
80
81
  defaultValue: 30,
81
82
  message: "should be a number between 0 and 100",
82
- validate: (value) => value === void 0 || (0, import_utils.isNumber)(value) && value >= 1 && value <= 100
83
+ validate: (value) => value === void 0 || (0, import_utils.isNumber)(value) && value >= 0 && value <= 100
83
84
  },
85
+ // hooks
84
86
  onEvent: {
85
87
  defaultValue: (event) => event,
86
88
  message: "should be a function",
@@ -92,6 +94,7 @@ var schema = {
92
94
  message: "should be a function",
93
95
  validate: (value) => value === void 0 || (0, import_utils.isFunction)(value)
94
96
  },
97
+ // utils
95
98
  logger: {
96
99
  defaultValue: import_utils.logger,
97
100
  message: "should be null or an object with methods { log, info, warn, error }",
@@ -100,6 +103,7 @@ var schema = {
100
103
  true
101
104
  )
102
105
  },
106
+ // data
103
107
  user: {
104
108
  defaultValue: void 0,
105
109
  message: "should be an object and have up to 6 attributes",
@@ -123,7 +127,7 @@ var Action = class {
123
127
  __publicField(this, "message");
124
128
  __publicField(this, "data");
125
129
  this.type = type;
126
- this.timestamp = timestamp || new Date().toISOString();
130
+ this.timestamp = timestamp || (/* @__PURE__ */ new Date()).toISOString();
127
131
  this.message = message;
128
132
  this.data = data;
129
133
  }
@@ -211,6 +215,17 @@ var Event = class {
211
215
  get __isOhbugEvent() {
212
216
  return true;
213
217
  }
218
+ /**
219
+ * Add an action.
220
+ * Once the threshold is reached, the oldest actions will be deleted.
221
+ * 新增一个动作。
222
+ * 一旦达到阈值,最老的 Action 将被删除。
223
+ *
224
+ * @param message
225
+ * @param data
226
+ * @param type
227
+ * @param timestamp
228
+ */
214
229
  addAction(message, data, type, timestamp) {
215
230
  var _a, _b;
216
231
  const actions = this.actions;
@@ -218,14 +233,25 @@ var Event = class {
218
233
  const targetData = data || {};
219
234
  const targetType = (0, import_utils2.isString)(type) ? type : "";
220
235
  const action = new Action(targetMessage, targetData, targetType, timestamp);
221
- if (actions.length >= ((_b = (_a = this.__client) == null ? void 0 : _a.__config.maxActions) != null ? _b : 30)) {
222
- actions.shift();
236
+ const maxActions = (_b = (_a = this.__client) == null ? void 0 : _a.__config.maxActions) != null ? _b : 30;
237
+ if (maxActions > 0) {
238
+ if (actions.length >= maxActions) {
239
+ actions.shift();
240
+ }
241
+ actions.push(action);
223
242
  }
224
- actions.push(action);
225
243
  }
244
+ /**
245
+ * Get current user information
246
+ * 获取当前的用户信息
247
+ */
226
248
  getUser() {
227
249
  return this.user;
228
250
  }
251
+ /**
252
+ * Set current user information
253
+ * 设置当前的用户信息
254
+ */
229
255
  setUser(user) {
230
256
  var _a;
231
257
  if ((0, import_utils2.isObject)(user) && Object.keys(user).length <= 6) {
@@ -238,12 +264,31 @@ var Event = class {
238
264
  ));
239
265
  return void 0;
240
266
  }
267
+ /**
268
+ * Add metadata
269
+ * 新增 metadata
270
+ *
271
+ * @param section
272
+ * @param data
273
+ */
241
274
  addMetadata(section, data) {
242
275
  return addMetadata(this.metadata, section, data);
243
276
  }
277
+ /**
278
+ * Get metadata
279
+ * 获取 metadata
280
+ *
281
+ * @param section
282
+ */
244
283
  getMetadata(section) {
245
284
  return getMetadata(this.metadata, section);
246
285
  }
286
+ /**
287
+ * Delete metadata
288
+ * 删除 metadata
289
+ *
290
+ * @param section
291
+ */
247
292
  deleteMetadata(section) {
248
293
  return deleteMetadata(this.metadata, section);
249
294
  }
@@ -282,7 +327,7 @@ var Event = class {
282
327
  };
283
328
  function createEvent(values, client) {
284
329
  const { apiKey, appVersion, appType, releaseStage } = client.__config;
285
- const timestamp = new Date().toISOString();
330
+ const timestamp = (/* @__PURE__ */ new Date()).toISOString();
286
331
  const device = client.__device(client);
287
332
  let category;
288
333
  let type;
@@ -390,13 +435,15 @@ var Client = class Client2 {
390
435
  config: baseConfig,
391
436
  schema: schema2 = schema,
392
437
  device,
393
- notifier
438
+ notifier,
439
+ destroy
394
440
  }) {
395
441
  __publicField(this, "__sdk");
396
442
  __publicField(this, "__config");
397
443
  __publicField(this, "__logger");
398
444
  __publicField(this, "__device");
399
445
  __publicField(this, "__notifier");
446
+ __publicField(this, "__destroy");
400
447
  __publicField(this, "__extensions");
401
448
  __publicField(this, "__actions");
402
449
  __publicField(this, "__user");
@@ -407,6 +454,7 @@ var Client = class Client2 {
407
454
  this.__logger = config.logger;
408
455
  this.__device = device;
409
456
  this.__notifier = notifier;
457
+ this.__destroy = destroy;
410
458
  this.__extensions = [];
411
459
  this.__actions = [];
412
460
  this.__user = config.user;
@@ -420,16 +468,51 @@ var Client = class Client2 {
420
468
  this.__logger.warn(getConfigErrorMessage(errors, baseConfig));
421
469
  }
422
470
  }
471
+ /**
472
+ * Load extension
473
+ * 加载扩展
474
+ *
475
+ * @param extension
476
+ */
423
477
  use(extension) {
424
478
  var _a;
425
479
  this.__extensions.push(extension);
426
480
  (_a = extension.onSetup) == null ? void 0 : _a.call(extension, this);
427
481
  return this;
428
482
  }
483
+ destroy() {
484
+ var _a;
485
+ if (this.__destroy) {
486
+ this.__logger.info(
487
+ "%c @ohbug/core %c has been destroyed %c",
488
+ "background:#333; padding: 2px 1px; color: #FFF",
489
+ "background:#FF6F61; padding: 2px 1px; color: #FFF",
490
+ "background:transparent"
491
+ );
492
+ this.__extensions.forEach((extension) => {
493
+ var _a2;
494
+ return (_a2 = extension.onDestroy) == null ? void 0 : _a2.call(extension, this);
495
+ });
496
+ return (_a = this.__destroy) == null ? void 0 : _a.call(this);
497
+ }
498
+ }
499
+ /**
500
+ * Create an event, you will get a data body containing device actions and other information
501
+ * 创建事件,将会得到一个含有 device actions 等信息的数据体
502
+ *
503
+ * @param value
504
+ */
429
505
  createEvent(value) {
430
506
  const event = createEvent(value, this);
431
507
  return handleEventCreated(event, this);
432
508
  }
509
+ /**
510
+ * Used to trigger the reporting interface
511
+ * 用于触发上报接口
512
+ *
513
+ * @param eventLike
514
+ * @param beforeNotify
515
+ */
433
516
  notify(eventLike, beforeNotify) {
434
517
  let event;
435
518
  if (Boolean(eventLike) && !isEvent(eventLike)) {
@@ -441,20 +524,43 @@ var Client = class Client2 {
441
524
  event = beforeNotify(event);
442
525
  return notify(event, this);
443
526
  }
527
+ /**
528
+ * Add an action.
529
+ * Once the threshold is reached, the oldest actions will be deleted.
530
+ * 新增一个动作。
531
+ * 一旦达到阈值,最老的 Action 将被删除。
532
+ *
533
+ * @param message
534
+ * @param data
535
+ * @param type
536
+ * @param timestamp
537
+ */
444
538
  addAction(message, data, type, timestamp) {
539
+ var _a;
445
540
  const actions = this.__actions;
446
541
  const targetMessage = (0, import_utils4.isString)(message) ? message : "";
447
542
  const targetData = data || {};
448
543
  const targetType = (0, import_utils4.isString)(type) ? type : "";
449
544
  const action = new Action(targetMessage, targetData, targetType, timestamp);
450
- if (actions.length >= this.__config.maxActions) {
451
- actions.shift();
545
+ const maxActions = (_a = this.__config.maxActions) != null ? _a : 30;
546
+ if (maxActions > 0) {
547
+ if (actions.length >= maxActions) {
548
+ actions.shift();
549
+ }
550
+ actions.push(action);
452
551
  }
453
- actions.push(action);
454
552
  }
553
+ /**
554
+ * Get current user information
555
+ * 获取当前的用户信息
556
+ */
455
557
  getUser() {
456
558
  return this.__user;
457
559
  }
560
+ /**
561
+ * Set current user information
562
+ * 设置当前的用户信息
563
+ */
458
564
  setUser(user) {
459
565
  if ((0, import_utils4.isObject)(user) && Object.keys(user).length <= 6) {
460
566
  this.__user = __spreadValues(__spreadValues({}, this.__user), user);
@@ -466,12 +572,31 @@ var Client = class Client2 {
466
572
  ));
467
573
  return void 0;
468
574
  }
575
+ /**
576
+ * Add metadata
577
+ * 新增 metadata
578
+ *
579
+ * @param section
580
+ * @param data
581
+ */
469
582
  addMetadata(section, data) {
470
583
  return addMetadata(this.__metadata, section, data);
471
584
  }
585
+ /**
586
+ * Get metadata
587
+ * 获取 metadata
588
+ *
589
+ * @param section
590
+ */
472
591
  getMetadata(section) {
473
592
  return getMetadata(this.__metadata, section);
474
593
  }
594
+ /**
595
+ * Delete metadata
596
+ * 删除 metadata
597
+ *
598
+ * @param section
599
+ */
475
600
  deleteMetadata(section) {
476
601
  return deleteMetadata(this.__metadata, section);
477
602
  }
package/dist/index.mjs CHANGED
@@ -25,6 +25,7 @@ import { isObject as isObject3, isString as isString3 } from "@ohbug/utils";
25
25
  // src/config.ts
26
26
  import { isFunction, isNumber, isObject, isString, logger } from "@ohbug/utils";
27
27
  var schema = {
28
+ // base
28
29
  apiKey: {
29
30
  defaultValue: void 0,
30
31
  message: "is required",
@@ -53,8 +54,9 @@ var schema = {
53
54
  maxActions: {
54
55
  defaultValue: 30,
55
56
  message: "should be a number between 0 and 100",
56
- validate: (value) => value === void 0 || isNumber(value) && value >= 1 && value <= 100
57
+ validate: (value) => value === void 0 || isNumber(value) && value >= 0 && value <= 100
57
58
  },
59
+ // hooks
58
60
  onEvent: {
59
61
  defaultValue: (event) => event,
60
62
  message: "should be a function",
@@ -66,6 +68,7 @@ var schema = {
66
68
  message: "should be a function",
67
69
  validate: (value) => value === void 0 || isFunction(value)
68
70
  },
71
+ // utils
69
72
  logger: {
70
73
  defaultValue: logger,
71
74
  message: "should be null or an object with methods { log, info, warn, error }",
@@ -74,6 +77,7 @@ var schema = {
74
77
  true
75
78
  )
76
79
  },
80
+ // data
77
81
  user: {
78
82
  defaultValue: void 0,
79
83
  message: "should be an object and have up to 6 attributes",
@@ -97,7 +101,7 @@ var Action = class {
97
101
  __publicField(this, "message");
98
102
  __publicField(this, "data");
99
103
  this.type = type;
100
- this.timestamp = timestamp || new Date().toISOString();
104
+ this.timestamp = timestamp || (/* @__PURE__ */ new Date()).toISOString();
101
105
  this.message = message;
102
106
  this.data = data;
103
107
  }
@@ -185,6 +189,17 @@ var Event = class {
185
189
  get __isOhbugEvent() {
186
190
  return true;
187
191
  }
192
+ /**
193
+ * Add an action.
194
+ * Once the threshold is reached, the oldest actions will be deleted.
195
+ * 新增一个动作。
196
+ * 一旦达到阈值,最老的 Action 将被删除。
197
+ *
198
+ * @param message
199
+ * @param data
200
+ * @param type
201
+ * @param timestamp
202
+ */
188
203
  addAction(message, data, type, timestamp) {
189
204
  var _a, _b;
190
205
  const actions = this.actions;
@@ -192,14 +207,25 @@ var Event = class {
192
207
  const targetData = data || {};
193
208
  const targetType = isString2(type) ? type : "";
194
209
  const action = new Action(targetMessage, targetData, targetType, timestamp);
195
- if (actions.length >= ((_b = (_a = this.__client) == null ? void 0 : _a.__config.maxActions) != null ? _b : 30)) {
196
- actions.shift();
210
+ const maxActions = (_b = (_a = this.__client) == null ? void 0 : _a.__config.maxActions) != null ? _b : 30;
211
+ if (maxActions > 0) {
212
+ if (actions.length >= maxActions) {
213
+ actions.shift();
214
+ }
215
+ actions.push(action);
197
216
  }
198
- actions.push(action);
199
217
  }
218
+ /**
219
+ * Get current user information
220
+ * 获取当前的用户信息
221
+ */
200
222
  getUser() {
201
223
  return this.user;
202
224
  }
225
+ /**
226
+ * Set current user information
227
+ * 设置当前的用户信息
228
+ */
203
229
  setUser(user) {
204
230
  var _a;
205
231
  if (isObject2(user) && Object.keys(user).length <= 6) {
@@ -212,12 +238,31 @@ var Event = class {
212
238
  ));
213
239
  return void 0;
214
240
  }
241
+ /**
242
+ * Add metadata
243
+ * 新增 metadata
244
+ *
245
+ * @param section
246
+ * @param data
247
+ */
215
248
  addMetadata(section, data) {
216
249
  return addMetadata(this.metadata, section, data);
217
250
  }
251
+ /**
252
+ * Get metadata
253
+ * 获取 metadata
254
+ *
255
+ * @param section
256
+ */
218
257
  getMetadata(section) {
219
258
  return getMetadata(this.metadata, section);
220
259
  }
260
+ /**
261
+ * Delete metadata
262
+ * 删除 metadata
263
+ *
264
+ * @param section
265
+ */
221
266
  deleteMetadata(section) {
222
267
  return deleteMetadata(this.metadata, section);
223
268
  }
@@ -256,7 +301,7 @@ var Event = class {
256
301
  };
257
302
  function createEvent(values, client) {
258
303
  const { apiKey, appVersion, appType, releaseStage } = client.__config;
259
- const timestamp = new Date().toISOString();
304
+ const timestamp = (/* @__PURE__ */ new Date()).toISOString();
260
305
  const device = client.__device(client);
261
306
  let category;
262
307
  let type;
@@ -364,13 +409,15 @@ var Client = class Client2 {
364
409
  config: baseConfig,
365
410
  schema: schema2 = schema,
366
411
  device,
367
- notifier
412
+ notifier,
413
+ destroy
368
414
  }) {
369
415
  __publicField(this, "__sdk");
370
416
  __publicField(this, "__config");
371
417
  __publicField(this, "__logger");
372
418
  __publicField(this, "__device");
373
419
  __publicField(this, "__notifier");
420
+ __publicField(this, "__destroy");
374
421
  __publicField(this, "__extensions");
375
422
  __publicField(this, "__actions");
376
423
  __publicField(this, "__user");
@@ -381,6 +428,7 @@ var Client = class Client2 {
381
428
  this.__logger = config.logger;
382
429
  this.__device = device;
383
430
  this.__notifier = notifier;
431
+ this.__destroy = destroy;
384
432
  this.__extensions = [];
385
433
  this.__actions = [];
386
434
  this.__user = config.user;
@@ -394,16 +442,51 @@ var Client = class Client2 {
394
442
  this.__logger.warn(getConfigErrorMessage(errors, baseConfig));
395
443
  }
396
444
  }
445
+ /**
446
+ * Load extension
447
+ * 加载扩展
448
+ *
449
+ * @param extension
450
+ */
397
451
  use(extension) {
398
452
  var _a;
399
453
  this.__extensions.push(extension);
400
454
  (_a = extension.onSetup) == null ? void 0 : _a.call(extension, this);
401
455
  return this;
402
456
  }
457
+ destroy() {
458
+ var _a;
459
+ if (this.__destroy) {
460
+ this.__logger.info(
461
+ "%c @ohbug/core %c has been destroyed %c",
462
+ "background:#333; padding: 2px 1px; color: #FFF",
463
+ "background:#FF6F61; padding: 2px 1px; color: #FFF",
464
+ "background:transparent"
465
+ );
466
+ this.__extensions.forEach((extension) => {
467
+ var _a2;
468
+ return (_a2 = extension.onDestroy) == null ? void 0 : _a2.call(extension, this);
469
+ });
470
+ return (_a = this.__destroy) == null ? void 0 : _a.call(this);
471
+ }
472
+ }
473
+ /**
474
+ * Create an event, you will get a data body containing device actions and other information
475
+ * 创建事件,将会得到一个含有 device actions 等信息的数据体
476
+ *
477
+ * @param value
478
+ */
403
479
  createEvent(value) {
404
480
  const event = createEvent(value, this);
405
481
  return handleEventCreated(event, this);
406
482
  }
483
+ /**
484
+ * Used to trigger the reporting interface
485
+ * 用于触发上报接口
486
+ *
487
+ * @param eventLike
488
+ * @param beforeNotify
489
+ */
407
490
  notify(eventLike, beforeNotify) {
408
491
  let event;
409
492
  if (Boolean(eventLike) && !isEvent(eventLike)) {
@@ -415,20 +498,43 @@ var Client = class Client2 {
415
498
  event = beforeNotify(event);
416
499
  return notify(event, this);
417
500
  }
501
+ /**
502
+ * Add an action.
503
+ * Once the threshold is reached, the oldest actions will be deleted.
504
+ * 新增一个动作。
505
+ * 一旦达到阈值,最老的 Action 将被删除。
506
+ *
507
+ * @param message
508
+ * @param data
509
+ * @param type
510
+ * @param timestamp
511
+ */
418
512
  addAction(message, data, type, timestamp) {
513
+ var _a;
419
514
  const actions = this.__actions;
420
515
  const targetMessage = isString3(message) ? message : "";
421
516
  const targetData = data || {};
422
517
  const targetType = isString3(type) ? type : "";
423
518
  const action = new Action(targetMessage, targetData, targetType, timestamp);
424
- if (actions.length >= this.__config.maxActions) {
425
- actions.shift();
519
+ const maxActions = (_a = this.__config.maxActions) != null ? _a : 30;
520
+ if (maxActions > 0) {
521
+ if (actions.length >= maxActions) {
522
+ actions.shift();
523
+ }
524
+ actions.push(action);
426
525
  }
427
- actions.push(action);
428
526
  }
527
+ /**
528
+ * Get current user information
529
+ * 获取当前的用户信息
530
+ */
429
531
  getUser() {
430
532
  return this.__user;
431
533
  }
534
+ /**
535
+ * Set current user information
536
+ * 设置当前的用户信息
537
+ */
432
538
  setUser(user) {
433
539
  if (isObject3(user) && Object.keys(user).length <= 6) {
434
540
  this.__user = __spreadValues(__spreadValues({}, this.__user), user);
@@ -440,12 +546,31 @@ var Client = class Client2 {
440
546
  ));
441
547
  return void 0;
442
548
  }
549
+ /**
550
+ * Add metadata
551
+ * 新增 metadata
552
+ *
553
+ * @param section
554
+ * @param data
555
+ */
443
556
  addMetadata(section, data) {
444
557
  return addMetadata(this.__metadata, section, data);
445
558
  }
559
+ /**
560
+ * Get metadata
561
+ * 获取 metadata
562
+ *
563
+ * @param section
564
+ */
446
565
  getMetadata(section) {
447
566
  return getMetadata(this.__metadata, section);
448
567
  }
568
+ /**
569
+ * Delete metadata
570
+ * 删除 metadata
571
+ *
572
+ * @param section
573
+ */
449
574
  deleteMetadata(section) {
450
575
  return deleteMetadata(this.__metadata, section);
451
576
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ohbug/core",
3
- "version": "2.1.1",
3
+ "version": "2.2.1",
4
4
  "description": "Responsible for managing Ohbug's workflow",
5
5
  "license": "Apache-2.0",
6
6
  "author": "chenyueban <jasonchan0527@gmail.com>",
@@ -30,8 +30,8 @@
30
30
  "access": "public"
31
31
  },
32
32
  "dependencies": {
33
- "@ohbug/types": "2.1.1",
34
- "@ohbug/utils": "2.0.6"
33
+ "@ohbug/types": "2.2.1",
34
+ "@ohbug/utils": "2.0.8"
35
35
  },
36
36
  "scripts": {
37
37
  "build": "tsup",