@ohbug/core 2.2.1 → 2.2.2

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.mjs CHANGED
@@ -1,612 +1,486 @@
1
- var __defProp = Object.defineProperty;
2
- var __getOwnPropSymbols = Object.getOwnPropertySymbols;
3
- var __hasOwnProp = Object.prototype.hasOwnProperty;
4
- var __propIsEnum = Object.prototype.propertyIsEnumerable;
5
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
6
- var __spreadValues = (a, b) => {
7
- for (var prop in b || (b = {}))
8
- if (__hasOwnProp.call(b, prop))
9
- __defNormalProp(a, prop, b[prop]);
10
- if (__getOwnPropSymbols)
11
- for (var prop of __getOwnPropSymbols(b)) {
12
- if (__propIsEnum.call(b, prop))
13
- __defNormalProp(a, prop, b[prop]);
14
- }
15
- return a;
16
- };
17
- var __publicField = (obj, key, value) => {
18
- __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
19
- return value;
20
- };
21
-
22
- // src/client.ts
23
- import { isObject as isObject3, isString as isString3 } from "@ohbug/utils";
24
-
25
- // src/config.ts
26
1
  import { isFunction, isNumber, isObject, isString, logger } from "@ohbug/utils";
27
- var schema = {
28
- // base
29
- apiKey: {
30
- defaultValue: void 0,
31
- message: "is required",
32
- validate: (value) => Boolean(value) && isString(value)
33
- },
34
- appVersion: {
35
- defaultValue: void 0,
36
- message: "should be a string",
37
- validate: (value) => value === void 0 || isString(value)
38
- },
39
- appType: {
40
- defaultValue: void 0,
41
- message: "should be a string",
42
- validate: (value) => value === void 0 || isString(value)
43
- },
44
- releaseStage: {
45
- defaultValue: "production",
46
- message: "should be a string",
47
- validate: (value) => value === void 0 || isString(value)
48
- },
49
- endpoint: {
50
- defaultValue: "http://localhost:6660",
51
- message: "should be a string",
52
- validate: (value) => value === void 0 || isString(value)
53
- },
54
- maxActions: {
55
- defaultValue: 30,
56
- message: "should be a number between 0 and 100",
57
- validate: (value) => value === void 0 || isNumber(value) && value >= 0 && value <= 100
58
- },
59
- // hooks
60
- onEvent: {
61
- defaultValue: (event) => event,
62
- message: "should be a function",
63
- validate: (value) => value === void 0 || isFunction(value)
64
- },
65
- onNotify: {
66
- defaultValue: () => {
67
- },
68
- message: "should be a function",
69
- validate: (value) => value === void 0 || isFunction(value)
70
- },
71
- // utils
72
- logger: {
73
- defaultValue: logger,
74
- message: "should be null or an object with methods { log, info, warn, error }",
75
- validate: (value) => value === void 0 || value && ["log", "info", "warn", "error"].reduce(
76
- (accumulator, method) => accumulator && typeof value[method] === "function",
77
- true
78
- )
79
- },
80
- // data
81
- user: {
82
- defaultValue: void 0,
83
- message: "should be an object and have up to 6 attributes",
84
- validate: (value) => value === void 0 || isObject(value) && Object.keys(value).length <= 6
85
- },
86
- metadata: {
87
- defaultValue: void 0,
88
- message: "should be an object",
89
- validate: (value) => value === void 0 || isObject(value)
90
- }
91
- };
92
-
93
- // src/event.ts
94
- import { isFunction as isFunction2, isObject as isObject2, isString as isString2 } from "@ohbug/utils";
95
-
96
- // src/action.ts
2
+ //#region src/action.ts
97
3
  var Action = class {
98
- constructor(message, data, type, timestamp) {
99
- __publicField(this, "type");
100
- __publicField(this, "timestamp");
101
- __publicField(this, "message");
102
- __publicField(this, "data");
103
- this.type = type;
104
- this.timestamp = timestamp || (/* @__PURE__ */ new Date()).toISOString();
105
- this.message = message;
106
- this.data = data;
107
- }
4
+ type;
5
+ timestamp;
6
+ message;
7
+ data;
8
+ constructor(message, data, type, timestamp) {
9
+ this.type = type;
10
+ this.timestamp = timestamp || (/* @__PURE__ */ new Date()).toISOString();
11
+ this.message = message;
12
+ this.data = data;
13
+ }
108
14
  };
109
-
110
- // src/lib/getErrorMessage.ts
15
+ //#endregion
16
+ //#region src/config.ts
17
+ const schema = {
18
+ apiKey: {
19
+ defaultValue: void 0,
20
+ message: "is required",
21
+ validate: (value) => Boolean(value) && isString(value)
22
+ },
23
+ appVersion: {
24
+ defaultValue: void 0,
25
+ message: "should be a string",
26
+ validate: (value) => value === void 0 || isString(value)
27
+ },
28
+ appType: {
29
+ defaultValue: void 0,
30
+ message: "should be a string",
31
+ validate: (value) => value === void 0 || isString(value)
32
+ },
33
+ releaseStage: {
34
+ defaultValue: "production",
35
+ message: "should be a string",
36
+ validate: (value) => value === void 0 || isString(value)
37
+ },
38
+ endpoint: {
39
+ defaultValue: "http://localhost:6660",
40
+ message: "should be a string",
41
+ validate: (value) => value === void 0 || isString(value)
42
+ },
43
+ notifier: {
44
+ defaultValue: void 0,
45
+ message: "should be a function",
46
+ validate: (value) => value === void 0 || isFunction(value)
47
+ },
48
+ maxActions: {
49
+ defaultValue: 30,
50
+ message: "should be a number between 0 and 100",
51
+ validate: (value) => value === void 0 || isNumber(value) && value >= 0 && value <= 100
52
+ },
53
+ onEvent: {
54
+ defaultValue: (event) => event,
55
+ message: "should be a function",
56
+ validate: (value) => value === void 0 || isFunction(value)
57
+ },
58
+ onNotify: {
59
+ defaultValue: () => {},
60
+ message: "should be a function",
61
+ validate: (value) => value === void 0 || isFunction(value)
62
+ },
63
+ logger: {
64
+ defaultValue: logger,
65
+ message: "should be null or an object with methods { log, info, warn, error }",
66
+ validate: (value) => value === void 0 || value && [
67
+ "log",
68
+ "info",
69
+ "warn",
70
+ "error"
71
+ ].reduce((accumulator, method) => accumulator && typeof value[method] === "function", true)
72
+ },
73
+ user: {
74
+ defaultValue: void 0,
75
+ message: "should be an object and have up to 6 attributes",
76
+ validate: (value) => value === void 0 || isObject(value) && Object.keys(value).length <= 6
77
+ },
78
+ metadata: {
79
+ defaultValue: void 0,
80
+ message: "should be an object",
81
+ validate: (value) => value === void 0 || isObject(value)
82
+ }
83
+ };
84
+ //#endregion
85
+ //#region src/lib/getErrorMessage.ts
111
86
  function getConfigErrorMessage(errors, config) {
112
- return new Error(`Invalid configuration
113
- ${Object.keys(errors).map((key) => {
114
- return `- ${key} ${errors[key]}, got ${JSON.stringify(config[key])}`;
115
- }).join("\n")}
87
+ return /* @__PURE__ */ new Error(`Invalid configuration\n${Object.keys(errors).map((key) => {
88
+ return `- ${key} ${errors[key]}, got ${JSON.stringify(config[key])}`;
89
+ }).join("\n")}
116
90
  `);
117
91
  }
118
92
  function getErrorMessage(message, data) {
119
- return new Error(`Invalid data
120
- - ${message}, got ${JSON.stringify(data)}`);
93
+ return /* @__PURE__ */ new Error(`Invalid data\n- ${message}, got ${JSON.stringify(data)}`);
121
94
  }
122
-
123
- // src/lib/metadata.ts
95
+ //#endregion
96
+ //#region src/lib/metadata.ts
124
97
  function addMetadata(map, section, data) {
125
- if (!section)
126
- return;
127
- map[section] = data;
98
+ if (!section) return;
99
+ map[section] = data;
128
100
  }
129
101
  function getMetadata(map, section) {
130
- if (map[section]) {
131
- return map[section];
132
- }
133
- return void 0;
102
+ if (map[section]) return map[section];
134
103
  }
135
104
  function deleteMetadata(map, section) {
136
- if (map[section]) {
137
- return delete map[section];
138
- }
139
- return void 0;
105
+ if (map[section]) return delete map[section];
140
106
  }
141
-
142
- // src/event.ts
107
+ //#endregion
108
+ //#region src/event.ts
143
109
  var Event = class {
144
- constructor(values, client) {
145
- __publicField(this, "apiKey");
146
- __publicField(this, "appVersion");
147
- __publicField(this, "appType");
148
- __publicField(this, "timestamp");
149
- __publicField(this, "category");
150
- __publicField(this, "type");
151
- __publicField(this, "sdk");
152
- __publicField(this, "device");
153
- __publicField(this, "detail");
154
- __publicField(this, "user");
155
- __publicField(this, "actions");
156
- __publicField(this, "metadata");
157
- __publicField(this, "releaseStage");
158
- __publicField(this, "__client");
159
- const {
160
- apiKey,
161
- appVersion,
162
- appType,
163
- releaseStage,
164
- timestamp,
165
- category,
166
- type,
167
- sdk,
168
- detail,
169
- device,
170
- user,
171
- actions,
172
- metadata
173
- } = values;
174
- this.apiKey = apiKey;
175
- this.appVersion = appVersion;
176
- this.appType = appType;
177
- this.releaseStage = releaseStage;
178
- this.timestamp = timestamp;
179
- this.category = category;
180
- this.type = type;
181
- this.sdk = sdk;
182
- this.detail = detail;
183
- this.device = device;
184
- this.user = user;
185
- this.actions = actions;
186
- this.metadata = metadata != null ? metadata : {};
187
- this.__client = client;
188
- }
189
- get __isOhbugEvent() {
190
- return true;
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
- */
203
- addAction(message, data, type, timestamp) {
204
- var _a, _b;
205
- const actions = this.actions;
206
- const targetMessage = isString2(message) ? message : "";
207
- const targetData = data || {};
208
- const targetType = isString2(type) ? type : "";
209
- const action = new Action(targetMessage, targetData, targetType, timestamp);
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);
216
- }
217
- }
218
- /**
219
- * Get current user information
220
- * 获取当前的用户信息
221
- */
222
- getUser() {
223
- return this.user;
224
- }
225
- /**
226
- * Set current user information
227
- * 设置当前的用户信息
228
- */
229
- setUser(user) {
230
- var _a;
231
- if (isObject2(user) && Object.keys(user).length <= 6) {
232
- this.user = __spreadValues(__spreadValues({}, this.user), user);
233
- return this.getUser();
234
- }
235
- (_a = this.__client) == null ? void 0 : _a.__logger.error(getErrorMessage(
236
- "setUser should be an object and have up to 6 attributes",
237
- user
238
- ));
239
- return void 0;
240
- }
241
- /**
242
- * Add metadata
243
- * 新增 metadata
244
- *
245
- * @param section
246
- * @param data
247
- */
248
- addMetadata(section, data) {
249
- return addMetadata(this.metadata, section, data);
250
- }
251
- /**
252
- * Get metadata
253
- * 获取 metadata
254
- *
255
- * @param section
256
- */
257
- getMetadata(section) {
258
- return getMetadata(this.metadata, section);
259
- }
260
- /**
261
- * Delete metadata
262
- * 删除 metadata
263
- *
264
- * @param section
265
- */
266
- deleteMetadata(section) {
267
- return deleteMetadata(this.metadata, section);
268
- }
269
- toJSON() {
270
- const {
271
- apiKey,
272
- appVersion,
273
- appType,
274
- timestamp,
275
- category,
276
- type,
277
- sdk,
278
- device,
279
- detail,
280
- user,
281
- actions,
282
- metadata,
283
- releaseStage
284
- } = this;
285
- return {
286
- apiKey,
287
- appVersion,
288
- appType,
289
- timestamp,
290
- category,
291
- type,
292
- sdk,
293
- device,
294
- detail,
295
- user,
296
- actions,
297
- metadata,
298
- releaseStage
299
- };
300
- }
110
+ apiKey;
111
+ appVersion;
112
+ appType;
113
+ timestamp;
114
+ category;
115
+ type;
116
+ sdk;
117
+ device;
118
+ detail;
119
+ user;
120
+ actions;
121
+ metadata;
122
+ releaseStage;
123
+ __client;
124
+ constructor(values, client) {
125
+ const { apiKey, appVersion, appType, releaseStage, timestamp, category, type, sdk, detail, device, user, actions, metadata } = values;
126
+ this.apiKey = apiKey;
127
+ this.appVersion = appVersion;
128
+ this.appType = appType;
129
+ this.releaseStage = releaseStage;
130
+ this.timestamp = timestamp;
131
+ this.category = category;
132
+ this.type = type;
133
+ this.sdk = sdk;
134
+ this.detail = detail;
135
+ this.device = device;
136
+ this.user = user;
137
+ this.actions = actions;
138
+ this.metadata = metadata ?? {};
139
+ this.__client = client;
140
+ }
141
+ get __isOhbugEvent() {
142
+ return true;
143
+ }
144
+ /**
145
+ * Add an action.
146
+ * Once the threshold is reached, the oldest actions will be deleted.
147
+ * 新增一个动作。
148
+ * 一旦达到阈值,最老的 Action 将被删除。
149
+ *
150
+ * @param message
151
+ * @param data
152
+ * @param type
153
+ * @param timestamp
154
+ */
155
+ addAction(message, data, type, timestamp) {
156
+ const actions = this.actions;
157
+ const action = new Action(isString(message) ? message : "", data || {}, isString(type) ? type : "", timestamp);
158
+ const maxActions = this.__client?.__config.maxActions ?? 30;
159
+ if (maxActions > 0) {
160
+ if (actions.length >= maxActions) actions.shift();
161
+ actions.push(action);
162
+ }
163
+ }
164
+ /**
165
+ * Get current user information
166
+ * 获取当前的用户信息
167
+ */
168
+ getUser() {
169
+ return this.user;
170
+ }
171
+ /**
172
+ * Set current user information
173
+ * 设置当前的用户信息
174
+ */
175
+ setUser(user) {
176
+ if (isObject(user) && Object.keys(user).length <= 6) {
177
+ this.user = {
178
+ ...this.user,
179
+ ...user
180
+ };
181
+ return this.getUser();
182
+ }
183
+ this.__client?.__logger.error(getErrorMessage("setUser should be an object and have up to 6 attributes", user));
184
+ }
185
+ /**
186
+ * Add metadata
187
+ * 新增 metadata
188
+ *
189
+ * @param section
190
+ * @param data
191
+ */
192
+ addMetadata(section, data) {
193
+ return addMetadata(this.metadata, section, data);
194
+ }
195
+ /**
196
+ * Get metadata
197
+ * 获取 metadata
198
+ *
199
+ * @param section
200
+ */
201
+ getMetadata(section) {
202
+ return getMetadata(this.metadata, section);
203
+ }
204
+ /**
205
+ * Delete metadata
206
+ * 删除 metadata
207
+ *
208
+ * @param section
209
+ */
210
+ deleteMetadata(section) {
211
+ return deleteMetadata(this.metadata, section);
212
+ }
213
+ toJSON() {
214
+ const { apiKey, appVersion, appType, timestamp, category, type, sdk, device, detail, user, actions, metadata, releaseStage } = this;
215
+ return {
216
+ apiKey,
217
+ appVersion,
218
+ appType,
219
+ timestamp,
220
+ category,
221
+ type,
222
+ sdk,
223
+ device,
224
+ detail,
225
+ user,
226
+ actions,
227
+ metadata,
228
+ releaseStage
229
+ };
230
+ }
301
231
  };
302
232
  function createEvent(values, client) {
303
- const { apiKey, appVersion, appType, releaseStage } = client.__config;
304
- const timestamp = (/* @__PURE__ */ new Date()).toISOString();
305
- const device = client.__device(client);
306
- let category;
307
- let type;
308
- let detail;
309
- if (isObject2(values) && Object.prototype.hasOwnProperty.call(values, "type") && Object.prototype.hasOwnProperty.call(values, "detail")) {
310
- category = values.category || "error";
311
- type = values.type;
312
- detail = values.detail;
313
- } else {
314
- category = "error";
315
- type = "unknownError";
316
- detail = values;
317
- }
318
- return new Event(
319
- {
320
- apiKey,
321
- appVersion,
322
- appType,
323
- timestamp,
324
- category,
325
- type,
326
- sdk: client.__sdk,
327
- device,
328
- user: client.__user,
329
- detail,
330
- actions: client.__actions,
331
- metadata: client.__metadata,
332
- releaseStage
333
- },
334
- client
335
- );
233
+ const { apiKey, appVersion, appType, releaseStage } = client.__config;
234
+ const timestamp = (/* @__PURE__ */ new Date()).toISOString();
235
+ const device = client.__device(client);
236
+ let category;
237
+ let type;
238
+ let detail;
239
+ if (isObject(values) && Object.prototype.hasOwnProperty.call(values, "type") && Object.prototype.hasOwnProperty.call(values, "detail")) {
240
+ category = values.category || "error";
241
+ type = values.type;
242
+ detail = values.detail;
243
+ } else {
244
+ category = "error";
245
+ type = "unknownError";
246
+ detail = values;
247
+ }
248
+ return new Event({
249
+ apiKey,
250
+ appVersion,
251
+ appType,
252
+ timestamp,
253
+ category,
254
+ type,
255
+ sdk: client.__sdk,
256
+ device,
257
+ user: client.__user,
258
+ detail,
259
+ actions: client.__actions,
260
+ metadata: client.__metadata,
261
+ releaseStage
262
+ }, client);
336
263
  }
337
264
  function handleEventCreated(event, client) {
338
- const funcs = [
339
- client.__config.onEvent,
340
- ...client.__extensions.map(({ onEvent }) => onEvent)
341
- ].filter((v) => isFunction2(v));
342
- if (funcs.length === 0) {
343
- return event;
344
- }
345
- return funcs.reduce((previous, current) => {
346
- if (previous && isFunction2(current)) {
347
- return current(previous, client);
348
- }
349
- return null;
350
- }, event);
265
+ const funcs = [client.__config.onEvent, ...client.__extensions.map(({ onEvent }) => onEvent)].filter((v) => isFunction(v));
266
+ if (funcs.length === 0) return event;
267
+ return funcs.reduce((previous, current) => {
268
+ if (previous && isFunction(current)) return current(previous, client);
269
+ return null;
270
+ }, event);
351
271
  }
352
272
  function isEvent(eventLike) {
353
- return Boolean(eventLike == null ? void 0 : eventLike.__isOhbugEvent);
273
+ return Boolean(eventLike?.__isOhbugEvent);
354
274
  }
355
-
356
- // src/notify.ts
357
- import { isFunction as isFunction3 } from "@ohbug/utils";
275
+ //#endregion
276
+ //#region src/lib/verifyConfig.ts
277
+ function verifyConfig(config, schema) {
278
+ return Object.keys(schema).reduce((accumulator, key) => {
279
+ const configValue = config[key];
280
+ const { defaultValue, message, validate } = schema[key];
281
+ if (configValue !== void 0) if (validate(configValue)) accumulator.config[key] = configValue;
282
+ else {
283
+ accumulator.config[key] = defaultValue;
284
+ accumulator.errors[key] = message;
285
+ }
286
+ else accumulator.config[key] = defaultValue;
287
+ return accumulator;
288
+ }, {
289
+ config: {},
290
+ errors: {}
291
+ });
292
+ }
293
+ //#endregion
294
+ //#region src/notify.ts
358
295
  function handleNotified(event, client) {
359
- const funcs = [
360
- client.__config.onNotify,
361
- ...client.__extensions.filter(({ onNotify }) => isFunction3(onNotify)).map(({ onNotify }) => onNotify)
362
- ];
363
- funcs.forEach((func) => func == null ? void 0 : func(event, client));
296
+ [client.__config.onNotify, ...client.__extensions.filter(({ onNotify }) => isFunction(onNotify)).map(({ onNotify }) => onNotify)].forEach((func) => func?.(event, client));
364
297
  }
298
+ /**
299
+ * Used to control the timing of reporting events and the related life cycle.
300
+ *
301
+ * @param event
302
+ * @param client
303
+ */
365
304
  async function notify(event, client) {
366
- try {
367
- let result = null;
368
- if (event) {
369
- result = await client.__notifier(event);
370
- handleNotified(event, client);
371
- }
372
- return result;
373
- } catch (e) {
374
- client.__logger.error(e);
375
- }
376
- }
377
-
378
- // src/lib/verifyConfig.ts
379
- function verifyConfig(config, schema2) {
380
- const keys = Object.keys(schema2);
381
- return keys.reduce(
382
- (accumulator, key) => {
383
- const configValue = config[key];
384
- const { defaultValue, message, validate } = schema2[key];
385
- if (configValue !== void 0) {
386
- const valid = validate(configValue);
387
- if (valid) {
388
- accumulator.config[key] = configValue;
389
- } else {
390
- accumulator.config[key] = defaultValue;
391
- accumulator.errors[key] = message;
392
- }
393
- } else {
394
- accumulator.config[key] = defaultValue;
395
- }
396
- return accumulator;
397
- },
398
- {
399
- config: {},
400
- errors: {}
401
- }
402
- );
305
+ if (!event) return null;
306
+ let result = null;
307
+ try {
308
+ result = await client.__notifier(event);
309
+ } catch (e) {
310
+ client.__logger.error(e);
311
+ }
312
+ handleNotified(event, client);
313
+ return result;
403
314
  }
404
-
405
- // src/client.ts
406
- var Client = class Client2 {
407
- constructor({
408
- sdk,
409
- config: baseConfig,
410
- schema: schema2 = schema,
411
- device,
412
- notifier,
413
- destroy
414
- }) {
415
- __publicField(this, "__sdk");
416
- __publicField(this, "__config");
417
- __publicField(this, "__logger");
418
- __publicField(this, "__device");
419
- __publicField(this, "__notifier");
420
- __publicField(this, "__destroy");
421
- __publicField(this, "__extensions");
422
- __publicField(this, "__actions");
423
- __publicField(this, "__user");
424
- __publicField(this, "__metadata");
425
- const { config, errors } = verifyConfig(baseConfig, schema2);
426
- this.__sdk = sdk;
427
- this.__config = config;
428
- this.__logger = config.logger;
429
- this.__device = device;
430
- this.__notifier = notifier;
431
- this.__destroy = destroy;
432
- this.__extensions = [];
433
- this.__actions = [];
434
- this.__user = config.user;
435
- this.__metadata = {};
436
- if (isObject3(config.metadata)) {
437
- Object.keys(config.metadata).forEach((key) => {
438
- this.addMetadata(key, config.metadata[key]);
439
- });
440
- }
441
- if (Object.keys(errors).length) {
442
- this.__logger.warn(getConfigErrorMessage(errors, baseConfig));
443
- }
444
- }
445
- /**
446
- * Load extension
447
- * 加载扩展
448
- *
449
- * @param extension
450
- */
451
- use(extension) {
452
- var _a;
453
- this.__extensions.push(extension);
454
- (_a = extension.onSetup) == null ? void 0 : _a.call(extension, this);
455
- return this;
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
- */
479
- createEvent(value) {
480
- const event = createEvent(value, this);
481
- return handleEventCreated(event, this);
482
- }
483
- /**
484
- * Used to trigger the reporting interface
485
- * 用于触发上报接口
486
- *
487
- * @param eventLike
488
- * @param beforeNotify
489
- */
490
- notify(eventLike, beforeNotify) {
491
- let event;
492
- if (Boolean(eventLike) && !isEvent(eventLike)) {
493
- event = this.createEvent(eventLike);
494
- } else {
495
- event = eventLike;
496
- }
497
- if (beforeNotify)
498
- event = beforeNotify(event);
499
- return notify(event, this);
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
- */
512
- addAction(message, data, type, timestamp) {
513
- var _a;
514
- const actions = this.__actions;
515
- const targetMessage = isString3(message) ? message : "";
516
- const targetData = data || {};
517
- const targetType = isString3(type) ? type : "";
518
- const action = new Action(targetMessage, targetData, targetType, timestamp);
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);
525
- }
526
- }
527
- /**
528
- * Get current user information
529
- * 获取当前的用户信息
530
- */
531
- getUser() {
532
- return this.__user;
533
- }
534
- /**
535
- * Set current user information
536
- * 设置当前的用户信息
537
- */
538
- setUser(user) {
539
- if (isObject3(user) && Object.keys(user).length <= 6) {
540
- this.__user = __spreadValues(__spreadValues({}, this.__user), user);
541
- return this.getUser();
542
- }
543
- this.__logger.warn(getErrorMessage(
544
- "setUser should be an object and have up to 6 attributes",
545
- user
546
- ));
547
- return void 0;
548
- }
549
- /**
550
- * Add metadata
551
- * 新增 metadata
552
- *
553
- * @param section
554
- * @param data
555
- */
556
- addMetadata(section, data) {
557
- return addMetadata(this.__metadata, section, data);
558
- }
559
- /**
560
- * Get metadata
561
- * 获取 metadata
562
- *
563
- * @param section
564
- */
565
- getMetadata(section) {
566
- return getMetadata(this.__metadata, section);
567
- }
568
- /**
569
- * Delete metadata
570
- * 删除 metadata
571
- *
572
- * @param section
573
- */
574
- deleteMetadata(section) {
575
- return deleteMetadata(this.__metadata, section);
576
- }
315
+ //#endregion
316
+ //#region src/client.ts
317
+ const Client = class Client {
318
+ __sdk;
319
+ __config;
320
+ __logger;
321
+ __device;
322
+ __notifier;
323
+ __destroy;
324
+ __extensions;
325
+ __actions;
326
+ __user;
327
+ __metadata;
328
+ constructor({ sdk, config: baseConfig, schema: schema$1 = schema, device, notifier, destroy }) {
329
+ const { config, errors } = verifyConfig(baseConfig, schema$1);
330
+ this.__sdk = sdk;
331
+ this.__config = config;
332
+ this.__logger = config.logger;
333
+ this.__device = device;
334
+ this.__notifier = notifier;
335
+ this.__destroy = destroy;
336
+ this.__extensions = [];
337
+ this.__actions = [];
338
+ this.__user = config.user;
339
+ this.__metadata = {};
340
+ if (isObject(config.metadata)) Object.keys(config.metadata).forEach((key) => {
341
+ this.addMetadata(key, config.metadata[key]);
342
+ });
343
+ if (Object.keys(errors).length) this.__logger.warn(getConfigErrorMessage(errors, baseConfig));
344
+ }
345
+ /**
346
+ * Load extension
347
+ * 加载扩展
348
+ *
349
+ * @param extension
350
+ */
351
+ use(extension) {
352
+ this.__extensions.push(extension);
353
+ extension.onSetup?.(this);
354
+ return this;
355
+ }
356
+ destroy() {
357
+ if (this.__destroy) {
358
+ this.__logger.info("%c @ohbug/core %c has been destroyed %c", "background:#333; padding: 2px 1px; color: #FFF", "background:#FF6F61; padding: 2px 1px; color: #FFF", "background:transparent");
359
+ this.__extensions.forEach((extension) => extension.onDestroy?.(this));
360
+ return this.__destroy?.();
361
+ }
362
+ }
363
+ /**
364
+ * Create an event, you will get a data body containing device actions and other information
365
+ * 创建事件,将会得到一个含有 device actions 等信息的数据体
366
+ *
367
+ * @param value
368
+ */
369
+ createEvent(value) {
370
+ return handleEventCreated(createEvent(value, this), this);
371
+ }
372
+ /**
373
+ * Used to trigger the reporting interface
374
+ * 用于触发上报接口
375
+ *
376
+ * @param eventLike
377
+ * @param beforeNotify
378
+ */
379
+ notify(eventLike, beforeNotify) {
380
+ let event;
381
+ if (Boolean(eventLike) && !isEvent(eventLike)) event = this.createEvent(eventLike);
382
+ else event = eventLike;
383
+ if (beforeNotify) event = beforeNotify(event);
384
+ return notify(event, this);
385
+ }
386
+ /**
387
+ * Add an action.
388
+ * Once the threshold is reached, the oldest actions will be deleted.
389
+ * 新增一个动作。
390
+ * 一旦达到阈值,最老的 Action 将被删除。
391
+ *
392
+ * @param message
393
+ * @param data
394
+ * @param type
395
+ * @param timestamp
396
+ */
397
+ addAction(message, data, type, timestamp) {
398
+ const actions = this.__actions;
399
+ const action = new Action(isString(message) ? message : "", data || {}, isString(type) ? type : "", timestamp);
400
+ const maxActions = this.__config.maxActions ?? 30;
401
+ if (maxActions > 0) {
402
+ if (actions.length >= maxActions) actions.shift();
403
+ actions.push(action);
404
+ }
405
+ }
406
+ /**
407
+ * Get current user information
408
+ * 获取当前的用户信息
409
+ */
410
+ getUser() {
411
+ return this.__user;
412
+ }
413
+ /**
414
+ * Set current user information
415
+ * 设置当前的用户信息
416
+ */
417
+ setUser(user) {
418
+ if (isObject(user) && Object.keys(user).length <= 6) {
419
+ this.__user = {
420
+ ...this.__user,
421
+ ...user
422
+ };
423
+ return this.getUser();
424
+ }
425
+ this.__logger.warn(getErrorMessage("setUser should be an object and have up to 6 attributes", user));
426
+ }
427
+ /**
428
+ * Add metadata
429
+ * 新增 metadata
430
+ *
431
+ * @param section
432
+ * @param data
433
+ */
434
+ addMetadata(section, data) {
435
+ return addMetadata(this.__metadata, section, data);
436
+ }
437
+ /**
438
+ * Get metadata
439
+ * 获取 metadata
440
+ *
441
+ * @param section
442
+ */
443
+ getMetadata(section) {
444
+ return getMetadata(this.__metadata, section);
445
+ }
446
+ /**
447
+ * Delete metadata
448
+ * 删除 metadata
449
+ *
450
+ * @param section
451
+ */
452
+ deleteMetadata(section) {
453
+ return deleteMetadata(this.__metadata, section);
454
+ }
577
455
  };
578
-
579
- // src/extension.ts
456
+ //#endregion
457
+ //#region src/extension.ts
580
458
  function defineExtension(extension) {
581
- if (!extension)
582
- return {};
583
- return extension;
459
+ if (!extension) return {};
460
+ return extension;
584
461
  }
585
-
586
- // src/types.ts
587
- var EventTypes = /* @__PURE__ */ ((EventTypes2) => {
588
- EventTypes2["UNCAUGHT_ERROR"] = "uncaughtError";
589
- EventTypes2["RESOURCE_ERROR"] = "resourceError";
590
- EventTypes2["UNHANDLEDREJECTION_ERROR"] = "unhandledrejectionError";
591
- EventTypes2["AJAX_ERROR"] = "ajaxError";
592
- EventTypes2["FETCH_ERROR"] = "fetchError";
593
- EventTypes2["WEBSOCKET_ERROR"] = "websocketError";
594
- EventTypes2["UNKNOWN_ERROR"] = "unknownError";
595
- EventTypes2["MESSAGE"] = "message";
596
- EventTypes2["FEEDBACK"] = "feedback";
597
- EventTypes2["VIEW"] = "view";
598
- EventTypes2["REACT"] = "react";
599
- EventTypes2["VUE"] = "vue";
600
- EventTypes2["ANGULAR"] = "angular";
601
- EventTypes2["MINIAPP_ERROR"] = "miniappError";
602
- EventTypes2["MINIAPP_UNHANDLEDREJECTION_ERROR"] = "miniappUnhandledrejectionError";
603
- EventTypes2["MINIAPP_PAGENOTFOUND_ERROR"] = "miniappPagenotfoundError";
604
- EventTypes2["MINIAPP_MEMORYWARNING_ERROR"] = "miniappMemorywarningError";
605
- return EventTypes2;
606
- })(EventTypes || {});
607
- export {
608
- Client,
609
- EventTypes,
610
- defineExtension,
611
- isEvent
462
+ //#endregion
463
+ //#region src/types.ts
464
+ const EventTypes = {
465
+ UNCAUGHT_ERROR: "uncaughtError",
466
+ RESOURCE_ERROR: "resourceError",
467
+ UNHANDLEDREJECTION_ERROR: "unhandledrejectionError",
468
+ AJAX_ERROR: "ajaxError",
469
+ FETCH_ERROR: "fetchError",
470
+ WEBSOCKET_ERROR: "websocketError",
471
+ UNKNOWN_ERROR: "unknownError",
472
+ MESSAGE: "message",
473
+ FEEDBACK: "feedback",
474
+ VIEW: "view",
475
+ REACT: "react",
476
+ VUE: "vue",
477
+ ANGULAR: "angular",
478
+ MINIAPP_ERROR: "miniappError",
479
+ MINIAPP_UNHANDLEDREJECTION_ERROR: "miniappUnhandledrejectionError",
480
+ MINIAPP_PAGENOTFOUND_ERROR: "miniappPagenotfoundError",
481
+ MINIAPP_MEMORYWARNING_ERROR: "miniappMemorywarningError"
612
482
  };
483
+ //#endregion
484
+ export { Client, EventTypes, defineExtension, isEvent };
485
+
486
+ //# sourceMappingURL=index.mjs.map