@ohbug/core 1.1.7 → 2.0.0

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 ADDED
@@ -0,0 +1,471 @@
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 __export = (target, all) => {
18
+ for (var name in all)
19
+ __defProp(target, name, { get: all[name], enumerable: true });
20
+ };
21
+
22
+ // src/client.ts
23
+ import { isObject as isObject3, isString as isString3 } from "@ohbug/utils";
24
+
25
+ // src/config.ts
26
+ import { isFunction, isNumber, isObject, isString, logger } from "@ohbug/utils";
27
+ var schema = {
28
+ apiKey: {
29
+ defaultValue: void 0,
30
+ message: "is required",
31
+ validate: (value) => Boolean(value) && isString(value)
32
+ },
33
+ appVersion: {
34
+ defaultValue: void 0,
35
+ message: "should be a string",
36
+ validate: (value) => value === void 0 || isString(value)
37
+ },
38
+ appType: {
39
+ defaultValue: void 0,
40
+ message: "should be a string",
41
+ validate: (value) => value === void 0 || isString(value)
42
+ },
43
+ releaseStage: {
44
+ defaultValue: "production",
45
+ message: "should be a string",
46
+ validate: (value) => value === void 0 || isString(value)
47
+ },
48
+ endpoint: {
49
+ defaultValue: "http://localhost:6660",
50
+ message: "should be a string",
51
+ validate: (value) => value === void 0 || isString(value)
52
+ },
53
+ maxActions: {
54
+ defaultValue: 30,
55
+ message: "should be a number between 0 and 100",
56
+ validate: (value) => value === void 0 || isNumber(value) && value >= 1 && value <= 100
57
+ },
58
+ created: {
59
+ defaultValue: (event) => event,
60
+ message: "should be a function",
61
+ validate: (value) => value === void 0 || isFunction(value)
62
+ },
63
+ notified: {
64
+ defaultValue: () => {
65
+ },
66
+ message: "should be a function",
67
+ validate: (value) => value === void 0 || isFunction(value)
68
+ },
69
+ logger: {
70
+ defaultValue: logger,
71
+ message: "should be null or an object with methods { log, info, warn, error }",
72
+ validate: (value) => value === void 0 || value && ["log", "info", "warn", "error"].reduce((accumulator, method) => accumulator && typeof value[method] === "function", true)
73
+ },
74
+ user: {
75
+ defaultValue: void 0,
76
+ message: "should be an object and have up to 6 attributes",
77
+ validate: (value) => value === void 0 || isObject(value) && Object.keys(value).length <= 6
78
+ },
79
+ metaData: {
80
+ defaultValue: void 0,
81
+ message: "should be an object",
82
+ validate: (value) => value === void 0 || isObject(value)
83
+ }
84
+ };
85
+
86
+ // src/event.ts
87
+ import { isFunction as isFunction2, isObject as isObject2, isString as isString2 } from "@ohbug/utils";
88
+
89
+ // src/action.ts
90
+ var Action = class {
91
+ type;
92
+ timestamp;
93
+ message;
94
+ data;
95
+ constructor(message, data, type, timestamp) {
96
+ this.type = type;
97
+ this.timestamp = timestamp || new Date().toISOString();
98
+ this.message = message;
99
+ this.data = data;
100
+ }
101
+ };
102
+
103
+ // src/lib/getErrorMessage.ts
104
+ function getConfigErrorMessage(errors, config) {
105
+ return new Error(`Invalid configuration
106
+ ${Object.keys(errors).map((key) => {
107
+ return `- ${key} ${errors[key]}, got ${JSON.stringify(config[key])}`;
108
+ }).join("\n")}
109
+ `);
110
+ }
111
+ function getErrorMessage(message, data) {
112
+ return new Error(`Invalid data
113
+ - ${message}, got ${JSON.stringify(data)}`);
114
+ }
115
+
116
+ // src/lib/metaData.ts
117
+ function addMetaData(map, section, data) {
118
+ if (!section)
119
+ return;
120
+ map[section] = data;
121
+ }
122
+ function getMetaData(map, section) {
123
+ if (map[section])
124
+ return map[section];
125
+ return void 0;
126
+ }
127
+ function deleteMetaData(map, section) {
128
+ if (map[section])
129
+ return delete map[section];
130
+ return void 0;
131
+ }
132
+
133
+ // src/event.ts
134
+ var Event = class {
135
+ apiKey;
136
+ appVersion;
137
+ appType;
138
+ timestamp;
139
+ category;
140
+ type;
141
+ sdk;
142
+ device;
143
+ detail;
144
+ user;
145
+ actions;
146
+ metaData;
147
+ releaseStage;
148
+ __client;
149
+ constructor(values, client) {
150
+ const {
151
+ apiKey,
152
+ appVersion,
153
+ appType,
154
+ releaseStage,
155
+ timestamp,
156
+ category,
157
+ type,
158
+ sdk,
159
+ detail,
160
+ device,
161
+ user,
162
+ actions,
163
+ metaData
164
+ } = values;
165
+ this.apiKey = apiKey;
166
+ this.appVersion = appVersion;
167
+ this.appType = appType;
168
+ this.releaseStage = releaseStage;
169
+ this.timestamp = timestamp;
170
+ this.category = category;
171
+ this.type = type;
172
+ this.sdk = sdk;
173
+ this.detail = detail;
174
+ this.device = device;
175
+ this.user = user;
176
+ this.actions = actions;
177
+ this.metaData = metaData != null ? metaData : {};
178
+ this.__client = client;
179
+ }
180
+ get __isOhbugEvent() {
181
+ return true;
182
+ }
183
+ addAction(message, data, type, timestamp) {
184
+ var _a, _b;
185
+ const actions = this.actions;
186
+ const targetMessage = isString2(message) ? message : "";
187
+ const targetData = data || {};
188
+ const targetType = isString2(type) ? type : "";
189
+ const action = new Action(targetMessage, targetData, targetType, timestamp);
190
+ if (actions.length >= ((_b = (_a = this.__client) == null ? void 0 : _a.__config.maxActions) != null ? _b : 30))
191
+ actions.shift();
192
+ actions.push(action);
193
+ }
194
+ getUser() {
195
+ return this.user;
196
+ }
197
+ setUser(user) {
198
+ var _a;
199
+ if (isObject2(user) && Object.keys(user).length <= 6) {
200
+ this.user = __spreadValues(__spreadValues({}, this.user), user);
201
+ return this.getUser();
202
+ }
203
+ (_a = this.__client) == null ? void 0 : _a.__logger.error(getErrorMessage("setUser should be an object and have up to 6 attributes", user));
204
+ return void 0;
205
+ }
206
+ addMetaData(section, data) {
207
+ return addMetaData(this.metaData, section, data);
208
+ }
209
+ getMetaData(section) {
210
+ return getMetaData(this.metaData, section);
211
+ }
212
+ deleteMetaData(section) {
213
+ return deleteMetaData(this.metaData, section);
214
+ }
215
+ toJSON() {
216
+ const {
217
+ apiKey,
218
+ appVersion,
219
+ appType,
220
+ timestamp,
221
+ category,
222
+ type,
223
+ sdk,
224
+ device,
225
+ detail,
226
+ user,
227
+ actions,
228
+ metaData,
229
+ releaseStage
230
+ } = this;
231
+ return {
232
+ apiKey,
233
+ appVersion,
234
+ appType,
235
+ timestamp,
236
+ category,
237
+ type,
238
+ sdk,
239
+ device,
240
+ detail,
241
+ user,
242
+ actions,
243
+ metaData,
244
+ releaseStage
245
+ };
246
+ }
247
+ };
248
+ function createEvent(values, client) {
249
+ const { apiKey, appVersion, appType, releaseStage } = client.__config;
250
+ const timestamp = new Date().toISOString();
251
+ const device = client.__device(client);
252
+ let category;
253
+ let type;
254
+ let detail;
255
+ if (isObject2(values) && Object.prototype.hasOwnProperty.call(values, "type") && Object.prototype.hasOwnProperty.call(values, "detail")) {
256
+ category = values.category || "error";
257
+ type = values.type;
258
+ detail = values.detail;
259
+ } else {
260
+ category = "error";
261
+ type = "unknownError";
262
+ detail = values;
263
+ }
264
+ return new Event({
265
+ apiKey,
266
+ appVersion,
267
+ appType,
268
+ timestamp,
269
+ category,
270
+ type,
271
+ sdk: client.__sdk,
272
+ device,
273
+ user: client.__user,
274
+ detail,
275
+ actions: client.__actions,
276
+ metaData: client.__metaData,
277
+ releaseStage
278
+ }, client);
279
+ }
280
+ function handleEventCreated(event, client) {
281
+ const funcs = [
282
+ client.__config.created,
283
+ ...client.__extensions.map(({ created }) => created)
284
+ ].filter((v) => isFunction2(v));
285
+ if (funcs.length === 0)
286
+ return event;
287
+ return funcs.reduce((previous, current) => {
288
+ if (previous && isFunction2(current))
289
+ return current(previous, client);
290
+ return null;
291
+ }, event);
292
+ }
293
+ function isEvent(eventLike) {
294
+ return Boolean(eventLike == null ? void 0 : eventLike.__isOhbugEvent);
295
+ }
296
+
297
+ // src/notify.ts
298
+ import { isFunction as isFunction3 } from "@ohbug/utils";
299
+ function handleNotified(event, client) {
300
+ const funcs = [
301
+ client.__config.notified,
302
+ ...client.__extensions.filter(({ notified }) => isFunction3(notified)).map(({ notified }) => notified)
303
+ ];
304
+ funcs.forEach((func) => func == null ? void 0 : func(event, client));
305
+ }
306
+ async function notify(event, client) {
307
+ try {
308
+ let result = null;
309
+ if (event) {
310
+ result = await client.__notifier(event);
311
+ handleNotified(event, client);
312
+ }
313
+ return result;
314
+ } catch (e) {
315
+ client.__logger.error(e);
316
+ }
317
+ }
318
+
319
+ // src/lib/verifyConfig.ts
320
+ function verifyConfig(config, schema2) {
321
+ const keys = Object.keys(schema2);
322
+ return keys.reduce((accumulator, key) => {
323
+ const configValue = config[key];
324
+ const { defaultValue, message, validate } = schema2[key];
325
+ if (configValue !== void 0) {
326
+ const valid = validate(configValue);
327
+ if (valid) {
328
+ accumulator.config[key] = configValue;
329
+ } else {
330
+ accumulator.config[key] = defaultValue;
331
+ accumulator.errors[key] = message;
332
+ }
333
+ } else {
334
+ accumulator.config[key] = defaultValue;
335
+ }
336
+ return accumulator;
337
+ }, {
338
+ config: {},
339
+ errors: {}
340
+ });
341
+ }
342
+
343
+ // src/client.ts
344
+ var Client = class Client2 {
345
+ __sdk;
346
+ __config;
347
+ __logger;
348
+ __device;
349
+ __notifier;
350
+ __extensions;
351
+ __actions;
352
+ __user;
353
+ __metaData;
354
+ constructor({
355
+ sdk,
356
+ config: baseConfig,
357
+ schema: schema2 = schema,
358
+ device,
359
+ notifier
360
+ }) {
361
+ const { config, errors } = verifyConfig(baseConfig, schema2);
362
+ this.__sdk = sdk;
363
+ this.__config = config;
364
+ this.__logger = config.logger;
365
+ this.__device = device;
366
+ this.__notifier = notifier;
367
+ this.__extensions = [];
368
+ this.__actions = [];
369
+ this.__user = config.user;
370
+ this.__metaData = {};
371
+ if (isObject3(config.metaData)) {
372
+ Object.keys(config.metaData).forEach((key) => {
373
+ this.addMetaData(key, config.metaData[key]);
374
+ });
375
+ }
376
+ if (Object.keys(errors).length)
377
+ this.__logger.warn(getConfigErrorMessage(errors, baseConfig));
378
+ }
379
+ use(extension) {
380
+ var _a;
381
+ this.__extensions.push(extension);
382
+ (_a = extension.setup) == null ? void 0 : _a.call(extension, this);
383
+ return this;
384
+ }
385
+ createEvent(value) {
386
+ const event = createEvent(value, this);
387
+ return handleEventCreated(event, this);
388
+ }
389
+ notify(eventLike, beforeNotify) {
390
+ let event;
391
+ if (Boolean(eventLike) && !isEvent(eventLike))
392
+ event = this.createEvent(eventLike);
393
+ else
394
+ event = eventLike;
395
+ if (beforeNotify)
396
+ event = beforeNotify(event);
397
+ return notify(event, this);
398
+ }
399
+ addAction(message, data, type, timestamp) {
400
+ const actions = this.__actions;
401
+ const targetMessage = isString3(message) ? message : "";
402
+ const targetData = data || {};
403
+ const targetType = isString3(type) ? type : "";
404
+ const action = new Action(targetMessage, targetData, targetType, timestamp);
405
+ if (actions.length >= this.__config.maxActions)
406
+ actions.shift();
407
+ actions.push(action);
408
+ }
409
+ getUser() {
410
+ return this.__user;
411
+ }
412
+ setUser(user) {
413
+ if (isObject3(user) && Object.keys(user).length <= 6) {
414
+ this.__user = __spreadValues(__spreadValues({}, this.__user), user);
415
+ return this.getUser();
416
+ }
417
+ this.__logger.warn(getErrorMessage("setUser should be an object and have up to 6 attributes", user));
418
+ return void 0;
419
+ }
420
+ addMetaData(section, data) {
421
+ return addMetaData(this.__metaData, section, data);
422
+ }
423
+ getMetaData(section) {
424
+ return getMetaData(this.__metaData, section);
425
+ }
426
+ deleteMetaData(section) {
427
+ return deleteMetaData(this.__metaData, section);
428
+ }
429
+ };
430
+
431
+ // src/extension.ts
432
+ function defineExtension(extension) {
433
+ return extension;
434
+ }
435
+ function createExtensionUI(extensionUI) {
436
+ return extensionUI;
437
+ }
438
+
439
+ // src/types.ts
440
+ var types_exports = {};
441
+ __export(types_exports, {
442
+ EventTypes: () => EventTypes
443
+ });
444
+ var EventTypes = /* @__PURE__ */ ((EventTypes2) => {
445
+ EventTypes2["UNCAUGHT_ERROR"] = "uncaughtError";
446
+ EventTypes2["RESOURCE_ERROR"] = "resourceError";
447
+ EventTypes2["UNHANDLEDREJECTION_ERROR"] = "unhandledrejectionError";
448
+ EventTypes2["AJAX_ERROR"] = "ajaxError";
449
+ EventTypes2["FETCH_ERROR"] = "fetchError";
450
+ EventTypes2["WEBSOCKET_ERROR"] = "websocketError";
451
+ EventTypes2["UNKNOWN_ERROR"] = "unknownError";
452
+ EventTypes2["MESSAGE"] = "message";
453
+ EventTypes2["FEEDBACK"] = "feedback";
454
+ EventTypes2["VIEW"] = "view";
455
+ EventTypes2["REACT"] = "react";
456
+ EventTypes2["VUE"] = "vue";
457
+ EventTypes2["ANGULAR"] = "angular";
458
+ EventTypes2["MINIAPP_ERROR"] = "miniappError";
459
+ EventTypes2["MINIAPP_UNHANDLEDREJECTION_ERROR"] = "miniappUnhandledrejectionError";
460
+ EventTypes2["MINIAPP_PAGENOTFOUND_ERROR"] = "miniappPagenotfoundError";
461
+ EventTypes2["MINIAPP_MEMORYWARNING_ERROR"] = "miniappMemorywarningError";
462
+ return EventTypes2;
463
+ })(EventTypes || {});
464
+ export {
465
+ Client,
466
+ EventTypes,
467
+ createExtensionUI,
468
+ defineExtension,
469
+ isEvent,
470
+ types_exports as types
471
+ };
package/package.json CHANGED
@@ -1,7 +1,8 @@
1
1
  {
2
2
  "name": "@ohbug/core",
3
- "version": "1.1.7",
3
+ "version": "2.0.0",
4
4
  "description": "Responsible for managing Ohbug's workflow",
5
+ "license": "Apache-2.0",
5
6
  "author": "chenyueban <jasonchan0527@gmail.com>",
6
7
  "homepage": "https://github.com/ohbug-org/ohbug",
7
8
  "bugs": {
@@ -11,35 +12,31 @@
11
12
  "type": "git",
12
13
  "url": "https://github.com/ohbug-org/ohbug"
13
14
  },
14
- "license": "Apache-2.0",
15
- "main": "./dist/ohbug-core.umd.js",
16
- "module": "./dist/ohbug-core.es.js",
17
- "unpkg": "./dist/ohbug-core.umd.js",
18
- "jsdelivr": "./dist/ohbug-core.umd.js",
19
- "types": "./dist/index.d.ts",
15
+ "main": "dist/index.js",
16
+ "module": "dist/index.mjs",
17
+ "types": "dist/index.d.ts",
20
18
  "exports": {
21
19
  ".": {
22
- "import": "./dist/ohbug-core.es.js",
23
- "require": "./dist/ohbug-core.umd.js"
20
+ "require": "./dist/index.js",
21
+ "import": "./dist/index.mjs",
22
+ "types": "./dist/index.d.ts"
24
23
  }
25
24
  },
26
25
  "files": [
27
- "dist"
26
+ "dist",
27
+ "src"
28
28
  ],
29
- "buildOptions": {
30
- "name": "OhbugCore",
31
- "formats": [
32
- "es",
33
- "umd"
34
- ],
35
- "order": 2
36
- },
29
+ "sideEffects": false,
37
30
  "publishConfig": {
38
31
  "access": "public"
39
32
  },
40
33
  "dependencies": {
41
- "@ohbug/types": "^1.1.4",
42
- "@ohbug/utils": "^1.0.13"
34
+ "@ohbug/types": "2.0.0",
35
+ "@ohbug/utils": "2.0.0"
36
+ },
37
+ "scripts": {
38
+ "build": "tsup",
39
+ "dev": "tsup --watch"
43
40
  },
44
- "gitHead": "323e4755a0da5893748c19901f3f709e903b6dd2"
45
- }
41
+ "readme": "# `@ohbug/core`\n\n[![npm](https://img.shields.io/npm/v/@ohbug/core.svg?style=flat-square)](https://www.npmjs.com/package/@ohbug/core)\n[![npm bundle size](https://img.shields.io/bundlephobia/min/@ohbug/core?style=flat-square)](https://bundlephobia.com/result?p=@ohbug/core)\n\nEnglish | [简体中文](./README-zh_CN.md)\n\n## Introduction\n\nohbug's core module is responsible for managing ohbug's workflow.\n\n## Installation\n\n```\npnpm instal @ohbug/core\n```\n"
42
+ }
package/src/action.ts ADDED
@@ -0,0 +1,23 @@
1
+ import type { OhbugAction } from '@ohbug/types'
2
+
3
+ export class Action implements OhbugAction {
4
+ readonly type: string
5
+
6
+ readonly timestamp: string
7
+
8
+ readonly message: string
9
+
10
+ readonly data: Record<string, any>
11
+
12
+ constructor(
13
+ message: string,
14
+ data: Record<string, any>,
15
+ type: string,
16
+ timestamp?: string,
17
+ ) {
18
+ this.type = type
19
+ this.timestamp = timestamp || new Date().toISOString()
20
+ this.message = message
21
+ this.data = data
22
+ }
23
+ }