@ohbug/core 2.1.0 → 2.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/dist/index.js CHANGED
@@ -1,5 +1,514 @@
1
- "use strict";var R=Object.defineProperty;var I=Object.getOwnPropertyDescriptor;var k=Object.getOwnPropertyNames;var P=Object.prototype.hasOwnProperty;var K=(e,t)=>{for(var n in t)R(e,n,{get:t[n],enumerable:!0})},G=(e,t,n,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let a of k(t))!P.call(e,a)&&a!==n&&R(e,a,{get:()=>t[a],enumerable:!(r=I(t,a))||r.enumerable});return e};var F=e=>G(R({},"__esModule",{value:!0}),e);var H={};K(H,{Client:()=>$,EventTypes:()=>W,defineExtension:()=>B,isEvent:()=>y});module.exports=F(H);var c=require("@ohbug/utils");var u=require("@ohbug/utils"),A={apiKey:{defaultValue:void 0,message:"is required",validate:e=>Boolean(e)&&(0,u.isString)(e)},appVersion:{defaultValue:void 0,message:"should be a string",validate:e=>e===void 0||(0,u.isString)(e)},appType:{defaultValue:void 0,message:"should be a string",validate:e=>e===void 0||(0,u.isString)(e)},releaseStage:{defaultValue:"production",message:"should be a string",validate:e=>e===void 0||(0,u.isString)(e)},endpoint:{defaultValue:"http://localhost:6660",message:"should be a string",validate:e=>e===void 0||(0,u.isString)(e)},maxActions:{defaultValue:30,message:"should be a number between 0 and 100",validate:e=>e===void 0||(0,u.isNumber)(e)&&e>=1&&e<=100},onEvent:{defaultValue:e=>e,message:"should be a function",validate:e=>e===void 0||(0,u.isFunction)(e)},onNotify:{defaultValue:()=>{},message:"should be a function",validate:e=>e===void 0||(0,u.isFunction)(e)},logger:{defaultValue:u.logger,message:"should be null or an object with methods { log, info, warn, error }",validate:e=>e===void 0||e&&["log","info","warn","error"].reduce((t,n)=>t&&typeof e[n]=="function",!0)},user:{defaultValue:void 0,message:"should be an object and have up to 6 attributes",validate:e=>e===void 0||(0,u.isObject)(e)&&Object.keys(e).length<=6},metadata:{defaultValue:void 0,message:"should be an object",validate:e=>e===void 0||(0,u.isObject)(e)}};var l=require("@ohbug/utils");var b=class{type;timestamp;message;data;constructor(t,n,r,a){this.type=r,this.timestamp=a||new Date().toISOString(),this.message=t,this.data=n}};function S(e,t){return new Error(`Invalid configuration
2
- ${Object.keys(e).map(n=>`- ${n} ${e[n]}, got ${JSON.stringify(t[n])}`).join(`
3
- `)}
4
- `)}function O(e,t){return new Error(`Invalid data
5
- - ${e}, got ${JSON.stringify(t)}`)}function p(e,t,n){!t||(e[t]=n)}function m(e,t){if(e[t])return e[t]}function _(e,t){if(e[t])return delete e[t]}var D=class{apiKey;appVersion;appType;timestamp;category;type;sdk;device;detail;user;actions;metadata;releaseStage;__client;constructor(t,n){let{apiKey:r,appVersion:a,appType:i,releaseStage:o,timestamp:d,category:g,type:h,sdk:f,detail:E,device:C,user:M,actions:w,metadata:j}=t;this.apiKey=r,this.appVersion=a,this.appType=i,this.releaseStage=o,this.timestamp=d,this.category=g,this.type=h,this.sdk=f,this.detail=E,this.device=C,this.user=M,this.actions=w,this.metadata=j??{},this.__client=n}get __isOhbugEvent(){return!0}addAction(t,n,r,a){var f;let i=this.actions,o=(0,l.isString)(t)?t:"",d=n||{},g=(0,l.isString)(r)?r:"",h=new b(o,d,g,a);i.length>=(((f=this.__client)==null?void 0:f.__config.maxActions)??30)&&i.shift(),i.push(h)}getUser(){return this.user}setUser(t){var n;if((0,l.isObject)(t)&&Object.keys(t).length<=6)return this.user={...this.user,...t},this.getUser();(n=this.__client)==null||n.__logger.error(O("setUser should be an object and have up to 6 attributes",t))}addMetadata(t,n){return p(this.metadata,t,n)}getMetadata(t){return m(this.metadata,t)}deleteMetadata(t){return _(this.metadata,t)}toJSON(){let{apiKey:t,appVersion:n,appType:r,timestamp:a,category:i,type:o,sdk:d,device:g,detail:h,user:f,actions:E,metadata:C,releaseStage:M}=this;return{apiKey:t,appVersion:n,appType:r,timestamp:a,category:i,type:o,sdk:d,device:g,detail:h,user:f,actions:E,metadata:C,releaseStage:M}}};function v(e,t){let{apiKey:n,appVersion:r,appType:a,releaseStage:i}=t.__config,o=new Date().toISOString(),d=t.__device(t),g,h,f;return(0,l.isObject)(e)&&Object.prototype.hasOwnProperty.call(e,"type")&&Object.prototype.hasOwnProperty.call(e,"detail")?(g=e.category||"error",h=e.type,f=e.detail):(g="error",h="unknownError",f=e),new D({apiKey:n,appVersion:r,appType:a,timestamp:o,category:g,type:h,sdk:t.__sdk,device:d,user:t.__user,detail:f,actions:t.__actions,metadata:t.__metadata,releaseStage:i},t)}function x(e,t){let n=[t.__config.onEvent,...t.__extensions.map(({onEvent:r})=>r)].filter(r=>(0,l.isFunction)(r));return n.length===0?e:n.reduce((r,a)=>r&&(0,l.isFunction)(a)?a(r,t):null,e)}function y(e){return Boolean(e==null?void 0:e.__isOhbugEvent)}var U=require("@ohbug/utils");function J(e,t){[t.__config.onNotify,...t.__extensions.filter(({onNotify:r})=>(0,U.isFunction)(r)).map(({onNotify:r})=>r)].forEach(r=>r==null?void 0:r(e,t))}async function N(e,t){try{let n=null;return e&&(n=await t.__notifier(e),J(e,t)),n}catch(n){t.__logger.error(n)}}function V(e,t){return Object.keys(t).reduce((r,a)=>{let i=e[a],{defaultValue:o,message:d,validate:g}=t[a];return i!==void 0?g(i)?r.config[a]=i:(r.config[a]=o,r.errors[a]=d):r.config[a]=o,r},{config:{},errors:{}})}var $=class{__sdk;__config;__logger;__device;__notifier;__extensions;__actions;__user;__metadata;constructor({sdk:t,config:n,schema:r=A,device:a,notifier:i}){let{config:o,errors:d}=V(n,r);this.__sdk=t,this.__config=o,this.__logger=o.logger,this.__device=a,this.__notifier=i,this.__extensions=[],this.__actions=[],this.__user=o.user,this.__metadata={},(0,c.isObject)(o.metadata)&&Object.keys(o.metadata).forEach(g=>{this.addMetadata(g,o.metadata[g])}),Object.keys(d).length&&this.__logger.warn(S(d,n))}use(t){var n;return this.__extensions.push(t),(n=t.onSetup)==null||n.call(t,this),this}createEvent(t){let n=v(t,this);return x(n,this)}notify(t,n){let r;return Boolean(t)&&!y(t)?r=this.createEvent(t):r=t,n&&(r=n(r)),N(r,this)}addAction(t,n,r,a){let i=this.__actions,o=(0,c.isString)(t)?t:"",d=n||{},g=(0,c.isString)(r)?r:"",h=new b(o,d,g,a);i.length>=this.__config.maxActions&&i.shift(),i.push(h)}getUser(){return this.__user}setUser(t){if((0,c.isObject)(t)&&Object.keys(t).length<=6)return this.__user={...this.__user,...t},this.getUser();this.__logger.warn(O("setUser should be an object and have up to 6 attributes",t))}addMetadata(t,n){return p(this.__metadata,t,n)}getMetadata(t){return m(this.__metadata,t)}deleteMetadata(t){return _(this.__metadata,t)}};function B(e){return e||{}}var W=(s=>(s.UNCAUGHT_ERROR="uncaughtError",s.RESOURCE_ERROR="resourceError",s.UNHANDLEDREJECTION_ERROR="unhandledrejectionError",s.AJAX_ERROR="ajaxError",s.FETCH_ERROR="fetchError",s.WEBSOCKET_ERROR="websocketError",s.UNKNOWN_ERROR="unknownError",s.MESSAGE="message",s.FEEDBACK="feedback",s.VIEW="view",s.REACT="react",s.VUE="vue",s.ANGULAR="angular",s.MINIAPP_ERROR="miniappError",s.MINIAPP_UNHANDLEDREJECTION_ERROR="miniappUnhandledrejectionError",s.MINIAPP_PAGENOTFOUND_ERROR="miniappPagenotfoundError",s.MINIAPP_MEMORYWARNING_ERROR="miniappMemorywarningError",s))(W||{});0&&(module.exports={Client,EventTypes,defineExtension,isEvent});
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
8
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
9
+ var __spreadValues = (a, b) => {
10
+ for (var prop in b || (b = {}))
11
+ if (__hasOwnProp.call(b, prop))
12
+ __defNormalProp(a, prop, b[prop]);
13
+ if (__getOwnPropSymbols)
14
+ for (var prop of __getOwnPropSymbols(b)) {
15
+ if (__propIsEnum.call(b, prop))
16
+ __defNormalProp(a, prop, b[prop]);
17
+ }
18
+ return a;
19
+ };
20
+ var __export = (target, all) => {
21
+ for (var name in all)
22
+ __defProp(target, name, { get: all[name], enumerable: true });
23
+ };
24
+ var __copyProps = (to, from, except, desc) => {
25
+ if (from && typeof from === "object" || typeof from === "function") {
26
+ for (let key of __getOwnPropNames(from))
27
+ if (!__hasOwnProp.call(to, key) && key !== except)
28
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
29
+ }
30
+ return to;
31
+ };
32
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
33
+ var __publicField = (obj, key, value) => {
34
+ __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
35
+ return value;
36
+ };
37
+
38
+ // src/index.ts
39
+ var src_exports = {};
40
+ __export(src_exports, {
41
+ Client: () => Client,
42
+ EventTypes: () => EventTypes,
43
+ defineExtension: () => defineExtension,
44
+ isEvent: () => isEvent
45
+ });
46
+ module.exports = __toCommonJS(src_exports);
47
+
48
+ // src/client.ts
49
+ var import_utils4 = require("@ohbug/utils");
50
+
51
+ // src/config.ts
52
+ var import_utils = require("@ohbug/utils");
53
+ var schema = {
54
+ apiKey: {
55
+ defaultValue: void 0,
56
+ message: "is required",
57
+ validate: (value) => Boolean(value) && (0, import_utils.isString)(value)
58
+ },
59
+ appVersion: {
60
+ defaultValue: void 0,
61
+ message: "should be a string",
62
+ validate: (value) => value === void 0 || (0, import_utils.isString)(value)
63
+ },
64
+ appType: {
65
+ defaultValue: void 0,
66
+ message: "should be a string",
67
+ validate: (value) => value === void 0 || (0, import_utils.isString)(value)
68
+ },
69
+ releaseStage: {
70
+ defaultValue: "production",
71
+ message: "should be a string",
72
+ validate: (value) => value === void 0 || (0, import_utils.isString)(value)
73
+ },
74
+ endpoint: {
75
+ defaultValue: "http://localhost:6660",
76
+ message: "should be a string",
77
+ validate: (value) => value === void 0 || (0, import_utils.isString)(value)
78
+ },
79
+ maxActions: {
80
+ defaultValue: 30,
81
+ 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
+ },
84
+ onEvent: {
85
+ defaultValue: (event) => event,
86
+ message: "should be a function",
87
+ validate: (value) => value === void 0 || (0, import_utils.isFunction)(value)
88
+ },
89
+ onNotify: {
90
+ defaultValue: () => {
91
+ },
92
+ message: "should be a function",
93
+ validate: (value) => value === void 0 || (0, import_utils.isFunction)(value)
94
+ },
95
+ logger: {
96
+ defaultValue: import_utils.logger,
97
+ message: "should be null or an object with methods { log, info, warn, error }",
98
+ validate: (value) => value === void 0 || value && ["log", "info", "warn", "error"].reduce(
99
+ (accumulator, method) => accumulator && typeof value[method] === "function",
100
+ true
101
+ )
102
+ },
103
+ user: {
104
+ defaultValue: void 0,
105
+ message: "should be an object and have up to 6 attributes",
106
+ validate: (value) => value === void 0 || (0, import_utils.isObject)(value) && Object.keys(value).length <= 6
107
+ },
108
+ metadata: {
109
+ defaultValue: void 0,
110
+ message: "should be an object",
111
+ validate: (value) => value === void 0 || (0, import_utils.isObject)(value)
112
+ }
113
+ };
114
+
115
+ // src/event.ts
116
+ var import_utils2 = require("@ohbug/utils");
117
+
118
+ // src/action.ts
119
+ var Action = class {
120
+ constructor(message, data, type, timestamp) {
121
+ __publicField(this, "type");
122
+ __publicField(this, "timestamp");
123
+ __publicField(this, "message");
124
+ __publicField(this, "data");
125
+ this.type = type;
126
+ this.timestamp = timestamp || new Date().toISOString();
127
+ this.message = message;
128
+ this.data = data;
129
+ }
130
+ };
131
+
132
+ // src/lib/getErrorMessage.ts
133
+ function getConfigErrorMessage(errors, config) {
134
+ return new Error(`Invalid configuration
135
+ ${Object.keys(errors).map((key) => {
136
+ return `- ${key} ${errors[key]}, got ${JSON.stringify(config[key])}`;
137
+ }).join("\n")}
138
+ `);
139
+ }
140
+ function getErrorMessage(message, data) {
141
+ return new Error(`Invalid data
142
+ - ${message}, got ${JSON.stringify(data)}`);
143
+ }
144
+
145
+ // src/lib/metadata.ts
146
+ function addMetadata(map, section, data) {
147
+ if (!section)
148
+ return;
149
+ map[section] = data;
150
+ }
151
+ function getMetadata(map, section) {
152
+ if (map[section]) {
153
+ return map[section];
154
+ }
155
+ return void 0;
156
+ }
157
+ function deleteMetadata(map, section) {
158
+ if (map[section]) {
159
+ return delete map[section];
160
+ }
161
+ return void 0;
162
+ }
163
+
164
+ // src/event.ts
165
+ var Event = class {
166
+ constructor(values, client) {
167
+ __publicField(this, "apiKey");
168
+ __publicField(this, "appVersion");
169
+ __publicField(this, "appType");
170
+ __publicField(this, "timestamp");
171
+ __publicField(this, "category");
172
+ __publicField(this, "type");
173
+ __publicField(this, "sdk");
174
+ __publicField(this, "device");
175
+ __publicField(this, "detail");
176
+ __publicField(this, "user");
177
+ __publicField(this, "actions");
178
+ __publicField(this, "metadata");
179
+ __publicField(this, "releaseStage");
180
+ __publicField(this, "__client");
181
+ const {
182
+ apiKey,
183
+ appVersion,
184
+ appType,
185
+ releaseStage,
186
+ timestamp,
187
+ category,
188
+ type,
189
+ sdk,
190
+ detail,
191
+ device,
192
+ user,
193
+ actions,
194
+ metadata
195
+ } = values;
196
+ this.apiKey = apiKey;
197
+ this.appVersion = appVersion;
198
+ this.appType = appType;
199
+ this.releaseStage = releaseStage;
200
+ this.timestamp = timestamp;
201
+ this.category = category;
202
+ this.type = type;
203
+ this.sdk = sdk;
204
+ this.detail = detail;
205
+ this.device = device;
206
+ this.user = user;
207
+ this.actions = actions;
208
+ this.metadata = metadata != null ? metadata : {};
209
+ this.__client = client;
210
+ }
211
+ get __isOhbugEvent() {
212
+ return true;
213
+ }
214
+ addAction(message, data, type, timestamp) {
215
+ var _a, _b;
216
+ const actions = this.actions;
217
+ const targetMessage = (0, import_utils2.isString)(message) ? message : "";
218
+ const targetData = data || {};
219
+ const targetType = (0, import_utils2.isString)(type) ? type : "";
220
+ 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();
223
+ }
224
+ actions.push(action);
225
+ }
226
+ getUser() {
227
+ return this.user;
228
+ }
229
+ setUser(user) {
230
+ var _a;
231
+ if ((0, import_utils2.isObject)(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
+ addMetadata(section, data) {
242
+ return addMetadata(this.metadata, section, data);
243
+ }
244
+ getMetadata(section) {
245
+ return getMetadata(this.metadata, section);
246
+ }
247
+ deleteMetadata(section) {
248
+ return deleteMetadata(this.metadata, section);
249
+ }
250
+ toJSON() {
251
+ const {
252
+ apiKey,
253
+ appVersion,
254
+ appType,
255
+ timestamp,
256
+ category,
257
+ type,
258
+ sdk,
259
+ device,
260
+ detail,
261
+ user,
262
+ actions,
263
+ metadata,
264
+ releaseStage
265
+ } = this;
266
+ return {
267
+ apiKey,
268
+ appVersion,
269
+ appType,
270
+ timestamp,
271
+ category,
272
+ type,
273
+ sdk,
274
+ device,
275
+ detail,
276
+ user,
277
+ actions,
278
+ metadata,
279
+ releaseStage
280
+ };
281
+ }
282
+ };
283
+ function createEvent(values, client) {
284
+ const { apiKey, appVersion, appType, releaseStage } = client.__config;
285
+ const timestamp = new Date().toISOString();
286
+ const device = client.__device(client);
287
+ let category;
288
+ let type;
289
+ let detail;
290
+ if ((0, import_utils2.isObject)(values) && Object.prototype.hasOwnProperty.call(values, "type") && Object.prototype.hasOwnProperty.call(values, "detail")) {
291
+ category = values.category || "error";
292
+ type = values.type;
293
+ detail = values.detail;
294
+ } else {
295
+ category = "error";
296
+ type = "unknownError";
297
+ detail = values;
298
+ }
299
+ return new Event(
300
+ {
301
+ apiKey,
302
+ appVersion,
303
+ appType,
304
+ timestamp,
305
+ category,
306
+ type,
307
+ sdk: client.__sdk,
308
+ device,
309
+ user: client.__user,
310
+ detail,
311
+ actions: client.__actions,
312
+ metadata: client.__metadata,
313
+ releaseStage
314
+ },
315
+ client
316
+ );
317
+ }
318
+ function handleEventCreated(event, client) {
319
+ const funcs = [
320
+ client.__config.onEvent,
321
+ ...client.__extensions.map(({ onEvent }) => onEvent)
322
+ ].filter((v) => (0, import_utils2.isFunction)(v));
323
+ if (funcs.length === 0) {
324
+ return event;
325
+ }
326
+ return funcs.reduce((previous, current) => {
327
+ if (previous && (0, import_utils2.isFunction)(current)) {
328
+ return current(previous, client);
329
+ }
330
+ return null;
331
+ }, event);
332
+ }
333
+ function isEvent(eventLike) {
334
+ return Boolean(eventLike == null ? void 0 : eventLike.__isOhbugEvent);
335
+ }
336
+
337
+ // src/notify.ts
338
+ var import_utils3 = require("@ohbug/utils");
339
+ function handleNotified(event, client) {
340
+ const funcs = [
341
+ client.__config.onNotify,
342
+ ...client.__extensions.filter(({ onNotify }) => (0, import_utils3.isFunction)(onNotify)).map(({ onNotify }) => onNotify)
343
+ ];
344
+ funcs.forEach((func) => func == null ? void 0 : func(event, client));
345
+ }
346
+ async function notify(event, client) {
347
+ try {
348
+ let result = null;
349
+ if (event) {
350
+ result = await client.__notifier(event);
351
+ handleNotified(event, client);
352
+ }
353
+ return result;
354
+ } catch (e) {
355
+ client.__logger.error(e);
356
+ }
357
+ }
358
+
359
+ // src/lib/verifyConfig.ts
360
+ function verifyConfig(config, schema2) {
361
+ const keys = Object.keys(schema2);
362
+ return keys.reduce(
363
+ (accumulator, key) => {
364
+ const configValue = config[key];
365
+ const { defaultValue, message, validate } = schema2[key];
366
+ if (configValue !== void 0) {
367
+ const valid = validate(configValue);
368
+ if (valid) {
369
+ accumulator.config[key] = configValue;
370
+ } else {
371
+ accumulator.config[key] = defaultValue;
372
+ accumulator.errors[key] = message;
373
+ }
374
+ } else {
375
+ accumulator.config[key] = defaultValue;
376
+ }
377
+ return accumulator;
378
+ },
379
+ {
380
+ config: {},
381
+ errors: {}
382
+ }
383
+ );
384
+ }
385
+
386
+ // src/client.ts
387
+ var Client = class Client2 {
388
+ constructor({
389
+ sdk,
390
+ config: baseConfig,
391
+ schema: schema2 = schema,
392
+ device,
393
+ notifier
394
+ }) {
395
+ __publicField(this, "__sdk");
396
+ __publicField(this, "__config");
397
+ __publicField(this, "__logger");
398
+ __publicField(this, "__device");
399
+ __publicField(this, "__notifier");
400
+ __publicField(this, "__extensions");
401
+ __publicField(this, "__actions");
402
+ __publicField(this, "__user");
403
+ __publicField(this, "__metadata");
404
+ const { config, errors } = verifyConfig(baseConfig, schema2);
405
+ this.__sdk = sdk;
406
+ this.__config = config;
407
+ this.__logger = config.logger;
408
+ this.__device = device;
409
+ this.__notifier = notifier;
410
+ this.__extensions = [];
411
+ this.__actions = [];
412
+ this.__user = config.user;
413
+ this.__metadata = {};
414
+ if ((0, import_utils4.isObject)(config.metadata)) {
415
+ Object.keys(config.metadata).forEach((key) => {
416
+ this.addMetadata(key, config.metadata[key]);
417
+ });
418
+ }
419
+ if (Object.keys(errors).length) {
420
+ this.__logger.warn(getConfigErrorMessage(errors, baseConfig));
421
+ }
422
+ }
423
+ use(extension) {
424
+ var _a;
425
+ this.__extensions.push(extension);
426
+ (_a = extension.onSetup) == null ? void 0 : _a.call(extension, this);
427
+ return this;
428
+ }
429
+ createEvent(value) {
430
+ const event = createEvent(value, this);
431
+ return handleEventCreated(event, this);
432
+ }
433
+ notify(eventLike, beforeNotify) {
434
+ let event;
435
+ if (Boolean(eventLike) && !isEvent(eventLike)) {
436
+ event = this.createEvent(eventLike);
437
+ } else {
438
+ event = eventLike;
439
+ }
440
+ if (beforeNotify)
441
+ event = beforeNotify(event);
442
+ return notify(event, this);
443
+ }
444
+ addAction(message, data, type, timestamp) {
445
+ const actions = this.__actions;
446
+ const targetMessage = (0, import_utils4.isString)(message) ? message : "";
447
+ const targetData = data || {};
448
+ const targetType = (0, import_utils4.isString)(type) ? type : "";
449
+ const action = new Action(targetMessage, targetData, targetType, timestamp);
450
+ if (actions.length >= this.__config.maxActions) {
451
+ actions.shift();
452
+ }
453
+ actions.push(action);
454
+ }
455
+ getUser() {
456
+ return this.__user;
457
+ }
458
+ setUser(user) {
459
+ if ((0, import_utils4.isObject)(user) && Object.keys(user).length <= 6) {
460
+ this.__user = __spreadValues(__spreadValues({}, this.__user), user);
461
+ return this.getUser();
462
+ }
463
+ this.__logger.warn(getErrorMessage(
464
+ "setUser should be an object and have up to 6 attributes",
465
+ user
466
+ ));
467
+ return void 0;
468
+ }
469
+ addMetadata(section, data) {
470
+ return addMetadata(this.__metadata, section, data);
471
+ }
472
+ getMetadata(section) {
473
+ return getMetadata(this.__metadata, section);
474
+ }
475
+ deleteMetadata(section) {
476
+ return deleteMetadata(this.__metadata, section);
477
+ }
478
+ };
479
+
480
+ // src/extension.ts
481
+ function defineExtension(extension) {
482
+ if (!extension)
483
+ return {};
484
+ return extension;
485
+ }
486
+
487
+ // src/types.ts
488
+ var EventTypes = /* @__PURE__ */ ((EventTypes2) => {
489
+ EventTypes2["UNCAUGHT_ERROR"] = "uncaughtError";
490
+ EventTypes2["RESOURCE_ERROR"] = "resourceError";
491
+ EventTypes2["UNHANDLEDREJECTION_ERROR"] = "unhandledrejectionError";
492
+ EventTypes2["AJAX_ERROR"] = "ajaxError";
493
+ EventTypes2["FETCH_ERROR"] = "fetchError";
494
+ EventTypes2["WEBSOCKET_ERROR"] = "websocketError";
495
+ EventTypes2["UNKNOWN_ERROR"] = "unknownError";
496
+ EventTypes2["MESSAGE"] = "message";
497
+ EventTypes2["FEEDBACK"] = "feedback";
498
+ EventTypes2["VIEW"] = "view";
499
+ EventTypes2["REACT"] = "react";
500
+ EventTypes2["VUE"] = "vue";
501
+ EventTypes2["ANGULAR"] = "angular";
502
+ EventTypes2["MINIAPP_ERROR"] = "miniappError";
503
+ EventTypes2["MINIAPP_UNHANDLEDREJECTION_ERROR"] = "miniappUnhandledrejectionError";
504
+ EventTypes2["MINIAPP_PAGENOTFOUND_ERROR"] = "miniappPagenotfoundError";
505
+ EventTypes2["MINIAPP_MEMORYWARNING_ERROR"] = "miniappMemorywarningError";
506
+ return EventTypes2;
507
+ })(EventTypes || {});
508
+ // Annotate the CommonJS export names for ESM import in node:
509
+ 0 && (module.exports = {
510
+ Client,
511
+ EventTypes,
512
+ defineExtension,
513
+ isEvent
514
+ });
package/dist/index.mjs CHANGED
@@ -1,5 +1,487 @@
1
- import{isObject as w,isString as j}from"@ohbug/utils";import{isFunction as M,isNumber as P,isObject as R,isString as l,logger as K}from"@ohbug/utils";var D={apiKey:{defaultValue:void 0,message:"is required",validate:e=>Boolean(e)&&l(e)},appVersion:{defaultValue:void 0,message:"should be a string",validate:e=>e===void 0||l(e)},appType:{defaultValue:void 0,message:"should be a string",validate:e=>e===void 0||l(e)},releaseStage:{defaultValue:"production",message:"should be a string",validate:e=>e===void 0||l(e)},endpoint:{defaultValue:"http://localhost:6660",message:"should be a string",validate:e=>e===void 0||l(e)},maxActions:{defaultValue:30,message:"should be a number between 0 and 100",validate:e=>e===void 0||P(e)&&e>=1&&e<=100},onEvent:{defaultValue:e=>e,message:"should be a function",validate:e=>e===void 0||M(e)},onNotify:{defaultValue:()=>{},message:"should be a function",validate:e=>e===void 0||M(e)},logger:{defaultValue:K,message:"should be null or an object with methods { log, info, warn, error }",validate:e=>e===void 0||e&&["log","info","warn","error"].reduce((t,n)=>t&&typeof e[n]=="function",!0)},user:{defaultValue:void 0,message:"should be an object and have up to 6 attributes",validate:e=>e===void 0||R(e)&&Object.keys(e).length<=6},metadata:{defaultValue:void 0,message:"should be an object",validate:e=>e===void 0||R(e)}};import{isFunction as S,isObject as x,isString as v}from"@ohbug/utils";var f=class{type;timestamp;message;data;constructor(t,n,r,a){this.type=r,this.timestamp=a||new Date().toISOString(),this.message=t,this.data=n}};function A(e,t){return new Error(`Invalid configuration
2
- ${Object.keys(e).map(n=>`- ${n} ${e[n]}, got ${JSON.stringify(t[n])}`).join(`
3
- `)}
4
- `)}function b(e,t){return new Error(`Invalid data
5
- - ${e}, got ${JSON.stringify(t)}`)}function c(e,t,n){!t||(e[t]=n)}function O(e,t){if(e[t])return e[t]}function p(e,t){if(e[t])return delete e[t]}var E=class{apiKey;appVersion;appType;timestamp;category;type;sdk;device;detail;user;actions;metadata;releaseStage;__client;constructor(t,n){let{apiKey:r,appVersion:a,appType:i,releaseStage:o,timestamp:u,category:g,type:d,sdk:h,detail:m,device:_,user:y,actions:I,metadata:k}=t;this.apiKey=r,this.appVersion=a,this.appType=i,this.releaseStage=o,this.timestamp=u,this.category=g,this.type=d,this.sdk=h,this.detail=m,this.device=_,this.user=y,this.actions=I,this.metadata=k??{},this.__client=n}get __isOhbugEvent(){return!0}addAction(t,n,r,a){var h;let i=this.actions,o=v(t)?t:"",u=n||{},g=v(r)?r:"",d=new f(o,u,g,a);i.length>=(((h=this.__client)==null?void 0:h.__config.maxActions)??30)&&i.shift(),i.push(d)}getUser(){return this.user}setUser(t){var n;if(x(t)&&Object.keys(t).length<=6)return this.user={...this.user,...t},this.getUser();(n=this.__client)==null||n.__logger.error(b("setUser should be an object and have up to 6 attributes",t))}addMetadata(t,n){return c(this.metadata,t,n)}getMetadata(t){return O(this.metadata,t)}deleteMetadata(t){return p(this.metadata,t)}toJSON(){let{apiKey:t,appVersion:n,appType:r,timestamp:a,category:i,type:o,sdk:u,device:g,detail:d,user:h,actions:m,metadata:_,releaseStage:y}=this;return{apiKey:t,appVersion:n,appType:r,timestamp:a,category:i,type:o,sdk:u,device:g,detail:d,user:h,actions:m,metadata:_,releaseStage:y}}};function U(e,t){let{apiKey:n,appVersion:r,appType:a,releaseStage:i}=t.__config,o=new Date().toISOString(),u=t.__device(t),g,d,h;return x(e)&&Object.prototype.hasOwnProperty.call(e,"type")&&Object.prototype.hasOwnProperty.call(e,"detail")?(g=e.category||"error",d=e.type,h=e.detail):(g="error",d="unknownError",h=e),new E({apiKey:n,appVersion:r,appType:a,timestamp:o,category:g,type:d,sdk:t.__sdk,device:u,user:t.__user,detail:h,actions:t.__actions,metadata:t.__metadata,releaseStage:i},t)}function N(e,t){let n=[t.__config.onEvent,...t.__extensions.map(({onEvent:r})=>r)].filter(r=>S(r));return n.length===0?e:n.reduce((r,a)=>r&&S(a)?a(r,t):null,e)}function C(e){return Boolean(e==null?void 0:e.__isOhbugEvent)}import{isFunction as G}from"@ohbug/utils";function F(e,t){[t.__config.onNotify,...t.__extensions.filter(({onNotify:r})=>G(r)).map(({onNotify:r})=>r)].forEach(r=>r==null?void 0:r(e,t))}async function V(e,t){try{let n=null;return e&&(n=await t.__notifier(e),F(e,t)),n}catch(n){t.__logger.error(n)}}function W(e,t){return Object.keys(t).reduce((r,a)=>{let i=e[a],{defaultValue:o,message:u,validate:g}=t[a];return i!==void 0?g(i)?r.config[a]=i:(r.config[a]=o,r.errors[a]=u):r.config[a]=o,r},{config:{},errors:{}})}var dt=class{__sdk;__config;__logger;__device;__notifier;__extensions;__actions;__user;__metadata;constructor({sdk:t,config:n,schema:r=D,device:a,notifier:i}){let{config:o,errors:u}=W(n,r);this.__sdk=t,this.__config=o,this.__logger=o.logger,this.__device=a,this.__notifier=i,this.__extensions=[],this.__actions=[],this.__user=o.user,this.__metadata={},w(o.metadata)&&Object.keys(o.metadata).forEach(g=>{this.addMetadata(g,o.metadata[g])}),Object.keys(u).length&&this.__logger.warn(A(u,n))}use(t){var n;return this.__extensions.push(t),(n=t.onSetup)==null||n.call(t,this),this}createEvent(t){let n=U(t,this);return N(n,this)}notify(t,n){let r;return Boolean(t)&&!C(t)?r=this.createEvent(t):r=t,n&&(r=n(r)),V(r,this)}addAction(t,n,r,a){let i=this.__actions,o=j(t)?t:"",u=n||{},g=j(r)?r:"",d=new f(o,u,g,a);i.length>=this.__config.maxActions&&i.shift(),i.push(d)}getUser(){return this.__user}setUser(t){if(w(t)&&Object.keys(t).length<=6)return this.__user={...this.__user,...t},this.getUser();this.__logger.warn(b("setUser should be an object and have up to 6 attributes",t))}addMetadata(t,n){return c(this.__metadata,t,n)}getMetadata(t){return O(this.__metadata,t)}deleteMetadata(t){return p(this.__metadata,t)}};function lt(e){return e||{}}var J=(s=>(s.UNCAUGHT_ERROR="uncaughtError",s.RESOURCE_ERROR="resourceError",s.UNHANDLEDREJECTION_ERROR="unhandledrejectionError",s.AJAX_ERROR="ajaxError",s.FETCH_ERROR="fetchError",s.WEBSOCKET_ERROR="websocketError",s.UNKNOWN_ERROR="unknownError",s.MESSAGE="message",s.FEEDBACK="feedback",s.VIEW="view",s.REACT="react",s.VUE="vue",s.ANGULAR="angular",s.MINIAPP_ERROR="miniappError",s.MINIAPP_UNHANDLEDREJECTION_ERROR="miniappUnhandledrejectionError",s.MINIAPP_PAGENOTFOUND_ERROR="miniappPagenotfoundError",s.MINIAPP_MEMORYWARNING_ERROR="miniappMemorywarningError",s))(J||{});export{dt as Client,J as EventTypes,lt as defineExtension,C as isEvent};
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
+ 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
+ onEvent: {
59
+ defaultValue: (event) => event,
60
+ message: "should be a function",
61
+ validate: (value) => value === void 0 || isFunction(value)
62
+ },
63
+ onNotify: {
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(
73
+ (accumulator, method) => accumulator && typeof value[method] === "function",
74
+ true
75
+ )
76
+ },
77
+ user: {
78
+ defaultValue: void 0,
79
+ message: "should be an object and have up to 6 attributes",
80
+ validate: (value) => value === void 0 || isObject(value) && Object.keys(value).length <= 6
81
+ },
82
+ metadata: {
83
+ defaultValue: void 0,
84
+ message: "should be an object",
85
+ validate: (value) => value === void 0 || isObject(value)
86
+ }
87
+ };
88
+
89
+ // src/event.ts
90
+ import { isFunction as isFunction2, isObject as isObject2, isString as isString2 } from "@ohbug/utils";
91
+
92
+ // src/action.ts
93
+ var Action = class {
94
+ constructor(message, data, type, timestamp) {
95
+ __publicField(this, "type");
96
+ __publicField(this, "timestamp");
97
+ __publicField(this, "message");
98
+ __publicField(this, "data");
99
+ this.type = type;
100
+ this.timestamp = timestamp || new Date().toISOString();
101
+ this.message = message;
102
+ this.data = data;
103
+ }
104
+ };
105
+
106
+ // src/lib/getErrorMessage.ts
107
+ function getConfigErrorMessage(errors, config) {
108
+ return new Error(`Invalid configuration
109
+ ${Object.keys(errors).map((key) => {
110
+ return `- ${key} ${errors[key]}, got ${JSON.stringify(config[key])}`;
111
+ }).join("\n")}
112
+ `);
113
+ }
114
+ function getErrorMessage(message, data) {
115
+ return new Error(`Invalid data
116
+ - ${message}, got ${JSON.stringify(data)}`);
117
+ }
118
+
119
+ // src/lib/metadata.ts
120
+ function addMetadata(map, section, data) {
121
+ if (!section)
122
+ return;
123
+ map[section] = data;
124
+ }
125
+ function getMetadata(map, section) {
126
+ if (map[section]) {
127
+ return map[section];
128
+ }
129
+ return void 0;
130
+ }
131
+ function deleteMetadata(map, section) {
132
+ if (map[section]) {
133
+ return delete map[section];
134
+ }
135
+ return void 0;
136
+ }
137
+
138
+ // src/event.ts
139
+ var Event = class {
140
+ constructor(values, client) {
141
+ __publicField(this, "apiKey");
142
+ __publicField(this, "appVersion");
143
+ __publicField(this, "appType");
144
+ __publicField(this, "timestamp");
145
+ __publicField(this, "category");
146
+ __publicField(this, "type");
147
+ __publicField(this, "sdk");
148
+ __publicField(this, "device");
149
+ __publicField(this, "detail");
150
+ __publicField(this, "user");
151
+ __publicField(this, "actions");
152
+ __publicField(this, "metadata");
153
+ __publicField(this, "releaseStage");
154
+ __publicField(this, "__client");
155
+ const {
156
+ apiKey,
157
+ appVersion,
158
+ appType,
159
+ releaseStage,
160
+ timestamp,
161
+ category,
162
+ type,
163
+ sdk,
164
+ detail,
165
+ device,
166
+ user,
167
+ actions,
168
+ metadata
169
+ } = values;
170
+ this.apiKey = apiKey;
171
+ this.appVersion = appVersion;
172
+ this.appType = appType;
173
+ this.releaseStage = releaseStage;
174
+ this.timestamp = timestamp;
175
+ this.category = category;
176
+ this.type = type;
177
+ this.sdk = sdk;
178
+ this.detail = detail;
179
+ this.device = device;
180
+ this.user = user;
181
+ this.actions = actions;
182
+ this.metadata = metadata != null ? metadata : {};
183
+ this.__client = client;
184
+ }
185
+ get __isOhbugEvent() {
186
+ return true;
187
+ }
188
+ addAction(message, data, type, timestamp) {
189
+ var _a, _b;
190
+ const actions = this.actions;
191
+ const targetMessage = isString2(message) ? message : "";
192
+ const targetData = data || {};
193
+ const targetType = isString2(type) ? type : "";
194
+ 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();
197
+ }
198
+ actions.push(action);
199
+ }
200
+ getUser() {
201
+ return this.user;
202
+ }
203
+ setUser(user) {
204
+ var _a;
205
+ if (isObject2(user) && Object.keys(user).length <= 6) {
206
+ this.user = __spreadValues(__spreadValues({}, this.user), user);
207
+ return this.getUser();
208
+ }
209
+ (_a = this.__client) == null ? void 0 : _a.__logger.error(getErrorMessage(
210
+ "setUser should be an object and have up to 6 attributes",
211
+ user
212
+ ));
213
+ return void 0;
214
+ }
215
+ addMetadata(section, data) {
216
+ return addMetadata(this.metadata, section, data);
217
+ }
218
+ getMetadata(section) {
219
+ return getMetadata(this.metadata, section);
220
+ }
221
+ deleteMetadata(section) {
222
+ return deleteMetadata(this.metadata, section);
223
+ }
224
+ toJSON() {
225
+ const {
226
+ apiKey,
227
+ appVersion,
228
+ appType,
229
+ timestamp,
230
+ category,
231
+ type,
232
+ sdk,
233
+ device,
234
+ detail,
235
+ user,
236
+ actions,
237
+ metadata,
238
+ releaseStage
239
+ } = this;
240
+ return {
241
+ apiKey,
242
+ appVersion,
243
+ appType,
244
+ timestamp,
245
+ category,
246
+ type,
247
+ sdk,
248
+ device,
249
+ detail,
250
+ user,
251
+ actions,
252
+ metadata,
253
+ releaseStage
254
+ };
255
+ }
256
+ };
257
+ function createEvent(values, client) {
258
+ const { apiKey, appVersion, appType, releaseStage } = client.__config;
259
+ const timestamp = new Date().toISOString();
260
+ const device = client.__device(client);
261
+ let category;
262
+ let type;
263
+ let detail;
264
+ if (isObject2(values) && Object.prototype.hasOwnProperty.call(values, "type") && Object.prototype.hasOwnProperty.call(values, "detail")) {
265
+ category = values.category || "error";
266
+ type = values.type;
267
+ detail = values.detail;
268
+ } else {
269
+ category = "error";
270
+ type = "unknownError";
271
+ detail = values;
272
+ }
273
+ return new Event(
274
+ {
275
+ apiKey,
276
+ appVersion,
277
+ appType,
278
+ timestamp,
279
+ category,
280
+ type,
281
+ sdk: client.__sdk,
282
+ device,
283
+ user: client.__user,
284
+ detail,
285
+ actions: client.__actions,
286
+ metadata: client.__metadata,
287
+ releaseStage
288
+ },
289
+ client
290
+ );
291
+ }
292
+ function handleEventCreated(event, client) {
293
+ const funcs = [
294
+ client.__config.onEvent,
295
+ ...client.__extensions.map(({ onEvent }) => onEvent)
296
+ ].filter((v) => isFunction2(v));
297
+ if (funcs.length === 0) {
298
+ return event;
299
+ }
300
+ return funcs.reduce((previous, current) => {
301
+ if (previous && isFunction2(current)) {
302
+ return current(previous, client);
303
+ }
304
+ return null;
305
+ }, event);
306
+ }
307
+ function isEvent(eventLike) {
308
+ return Boolean(eventLike == null ? void 0 : eventLike.__isOhbugEvent);
309
+ }
310
+
311
+ // src/notify.ts
312
+ import { isFunction as isFunction3 } from "@ohbug/utils";
313
+ function handleNotified(event, client) {
314
+ const funcs = [
315
+ client.__config.onNotify,
316
+ ...client.__extensions.filter(({ onNotify }) => isFunction3(onNotify)).map(({ onNotify }) => onNotify)
317
+ ];
318
+ funcs.forEach((func) => func == null ? void 0 : func(event, client));
319
+ }
320
+ async function notify(event, client) {
321
+ try {
322
+ let result = null;
323
+ if (event) {
324
+ result = await client.__notifier(event);
325
+ handleNotified(event, client);
326
+ }
327
+ return result;
328
+ } catch (e) {
329
+ client.__logger.error(e);
330
+ }
331
+ }
332
+
333
+ // src/lib/verifyConfig.ts
334
+ function verifyConfig(config, schema2) {
335
+ const keys = Object.keys(schema2);
336
+ return keys.reduce(
337
+ (accumulator, key) => {
338
+ const configValue = config[key];
339
+ const { defaultValue, message, validate } = schema2[key];
340
+ if (configValue !== void 0) {
341
+ const valid = validate(configValue);
342
+ if (valid) {
343
+ accumulator.config[key] = configValue;
344
+ } else {
345
+ accumulator.config[key] = defaultValue;
346
+ accumulator.errors[key] = message;
347
+ }
348
+ } else {
349
+ accumulator.config[key] = defaultValue;
350
+ }
351
+ return accumulator;
352
+ },
353
+ {
354
+ config: {},
355
+ errors: {}
356
+ }
357
+ );
358
+ }
359
+
360
+ // src/client.ts
361
+ var Client = class Client2 {
362
+ constructor({
363
+ sdk,
364
+ config: baseConfig,
365
+ schema: schema2 = schema,
366
+ device,
367
+ notifier
368
+ }) {
369
+ __publicField(this, "__sdk");
370
+ __publicField(this, "__config");
371
+ __publicField(this, "__logger");
372
+ __publicField(this, "__device");
373
+ __publicField(this, "__notifier");
374
+ __publicField(this, "__extensions");
375
+ __publicField(this, "__actions");
376
+ __publicField(this, "__user");
377
+ __publicField(this, "__metadata");
378
+ const { config, errors } = verifyConfig(baseConfig, schema2);
379
+ this.__sdk = sdk;
380
+ this.__config = config;
381
+ this.__logger = config.logger;
382
+ this.__device = device;
383
+ this.__notifier = notifier;
384
+ this.__extensions = [];
385
+ this.__actions = [];
386
+ this.__user = config.user;
387
+ this.__metadata = {};
388
+ if (isObject3(config.metadata)) {
389
+ Object.keys(config.metadata).forEach((key) => {
390
+ this.addMetadata(key, config.metadata[key]);
391
+ });
392
+ }
393
+ if (Object.keys(errors).length) {
394
+ this.__logger.warn(getConfigErrorMessage(errors, baseConfig));
395
+ }
396
+ }
397
+ use(extension) {
398
+ var _a;
399
+ this.__extensions.push(extension);
400
+ (_a = extension.onSetup) == null ? void 0 : _a.call(extension, this);
401
+ return this;
402
+ }
403
+ createEvent(value) {
404
+ const event = createEvent(value, this);
405
+ return handleEventCreated(event, this);
406
+ }
407
+ notify(eventLike, beforeNotify) {
408
+ let event;
409
+ if (Boolean(eventLike) && !isEvent(eventLike)) {
410
+ event = this.createEvent(eventLike);
411
+ } else {
412
+ event = eventLike;
413
+ }
414
+ if (beforeNotify)
415
+ event = beforeNotify(event);
416
+ return notify(event, this);
417
+ }
418
+ addAction(message, data, type, timestamp) {
419
+ const actions = this.__actions;
420
+ const targetMessage = isString3(message) ? message : "";
421
+ const targetData = data || {};
422
+ const targetType = isString3(type) ? type : "";
423
+ const action = new Action(targetMessage, targetData, targetType, timestamp);
424
+ if (actions.length >= this.__config.maxActions) {
425
+ actions.shift();
426
+ }
427
+ actions.push(action);
428
+ }
429
+ getUser() {
430
+ return this.__user;
431
+ }
432
+ setUser(user) {
433
+ if (isObject3(user) && Object.keys(user).length <= 6) {
434
+ this.__user = __spreadValues(__spreadValues({}, this.__user), user);
435
+ return this.getUser();
436
+ }
437
+ this.__logger.warn(getErrorMessage(
438
+ "setUser should be an object and have up to 6 attributes",
439
+ user
440
+ ));
441
+ return void 0;
442
+ }
443
+ addMetadata(section, data) {
444
+ return addMetadata(this.__metadata, section, data);
445
+ }
446
+ getMetadata(section) {
447
+ return getMetadata(this.__metadata, section);
448
+ }
449
+ deleteMetadata(section) {
450
+ return deleteMetadata(this.__metadata, section);
451
+ }
452
+ };
453
+
454
+ // src/extension.ts
455
+ function defineExtension(extension) {
456
+ if (!extension)
457
+ return {};
458
+ return extension;
459
+ }
460
+
461
+ // src/types.ts
462
+ var EventTypes = /* @__PURE__ */ ((EventTypes2) => {
463
+ EventTypes2["UNCAUGHT_ERROR"] = "uncaughtError";
464
+ EventTypes2["RESOURCE_ERROR"] = "resourceError";
465
+ EventTypes2["UNHANDLEDREJECTION_ERROR"] = "unhandledrejectionError";
466
+ EventTypes2["AJAX_ERROR"] = "ajaxError";
467
+ EventTypes2["FETCH_ERROR"] = "fetchError";
468
+ EventTypes2["WEBSOCKET_ERROR"] = "websocketError";
469
+ EventTypes2["UNKNOWN_ERROR"] = "unknownError";
470
+ EventTypes2["MESSAGE"] = "message";
471
+ EventTypes2["FEEDBACK"] = "feedback";
472
+ EventTypes2["VIEW"] = "view";
473
+ EventTypes2["REACT"] = "react";
474
+ EventTypes2["VUE"] = "vue";
475
+ EventTypes2["ANGULAR"] = "angular";
476
+ EventTypes2["MINIAPP_ERROR"] = "miniappError";
477
+ EventTypes2["MINIAPP_UNHANDLEDREJECTION_ERROR"] = "miniappUnhandledrejectionError";
478
+ EventTypes2["MINIAPP_PAGENOTFOUND_ERROR"] = "miniappPagenotfoundError";
479
+ EventTypes2["MINIAPP_MEMORYWARNING_ERROR"] = "miniappMemorywarningError";
480
+ return EventTypes2;
481
+ })(EventTypes || {});
482
+ export {
483
+ Client,
484
+ EventTypes,
485
+ defineExtension,
486
+ isEvent
487
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ohbug/core",
3
- "version": "2.1.0",
3
+ "version": "2.1.1",
4
4
  "description": "Responsible for managing Ohbug's workflow",
5
5
  "license": "Apache-2.0",
6
6
  "author": "chenyueban <jasonchan0527@gmail.com>",
@@ -30,12 +30,11 @@
30
30
  "access": "public"
31
31
  },
32
32
  "dependencies": {
33
- "@ohbug/types": "2.1.0",
34
- "@ohbug/utils": "2.0.5"
33
+ "@ohbug/types": "2.1.1",
34
+ "@ohbug/utils": "2.0.6"
35
35
  },
36
36
  "scripts": {
37
37
  "build": "tsup",
38
38
  "dev": "tsup --watch"
39
- },
40
- "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"
39
+ }
41
40
  }