@ohbug/core 2.0.0 → 2.0.3
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.d.ts +1 -9
- package/dist/index.js +5 -495
- package/dist/index.mjs +5 -471
- package/package.json +4 -5
- package/src/action.ts +0 -23
- package/src/client.ts +0 -210
- package/src/config.ts +0 -75
- package/src/event.ts +0 -276
- package/src/extension.ts +0 -8
- package/src/index.ts +0 -5
- package/src/lib/getErrorMessage.ts +0 -17
- package/src/lib/metaData.ts +0 -21
- package/src/lib/verifyConfig.ts +0 -37
- package/src/notify.ts +0 -38
- package/src/types.ts +0 -27
package/dist/index.mjs
CHANGED
|
@@ -1,471 +1,5 @@
|
|
|
1
|
-
var
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
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
|
-
};
|
|
1
|
+
var $=Object.defineProperty;var A=Object.getOwnPropertySymbols;var B=Object.prototype.hasOwnProperty,H=Object.prototype.propertyIsEnumerable;var S=(e,t,n)=>t in e?$(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n,b=(e,t)=>{for(var n in t||(t={}))B.call(t,n)&&S(e,n,t[n]);if(A)for(var n of A(t))H.call(t,n)&&S(e,n,t[n]);return e};import{isObject as G,isString as F}from"@ohbug/utils";import{isFunction as U,isNumber as q,isObject as v,isString as p,logger as X}from"@ohbug/utils";var N={apiKey:{defaultValue:void 0,message:"is required",validate:e=>Boolean(e)&&p(e)},appVersion:{defaultValue:void 0,message:"should be a string",validate:e=>e===void 0||p(e)},appType:{defaultValue:void 0,message:"should be a string",validate:e=>e===void 0||p(e)},releaseStage:{defaultValue:"production",message:"should be a string",validate:e=>e===void 0||p(e)},endpoint:{defaultValue:"http://localhost:6660",message:"should be a string",validate:e=>e===void 0||p(e)},maxActions:{defaultValue:30,message:"should be a number between 0 and 100",validate:e=>e===void 0||q(e)&&e>=1&&e<=100},onEvent:{defaultValue:e=>e,message:"should be a function",validate:e=>e===void 0||U(e)},onNotify:{defaultValue:()=>{},message:"should be a function",validate:e=>e===void 0||U(e)},logger:{defaultValue:X,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||v(e)&&Object.keys(e).length<=6},metadata:{defaultValue:void 0,message:"should be an object",validate:e=>e===void 0||v(e)}};import{isFunction as W,isObject as j,isString as w}from"@ohbug/utils";var O=class{type;timestamp;message;data;constructor(t,n,r,i){this.type=r,this.timestamp=i||new Date().toISOString(),this.message=t,this.data=n}};function V(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 m(e,t){return new Error(`Invalid data
|
|
5
|
+
- ${e}, got ${JSON.stringify(t)}`)}function _(e,t,n){!t||(e[t]=n)}function y(e,t){if(e[t])return e[t]}function E(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:i,appType:o,releaseStage:a,timestamp:u,category:g,type:h,sdk:d,detail:c,device:C,user:M,actions:J,metadata:R}=t;this.apiKey=r,this.appVersion=i,this.appType=o,this.releaseStage=a,this.timestamp=u,this.category=g,this.type=h,this.sdk=d,this.detail=c,this.device=C,this.user=M,this.actions=J,this.metadata=R!=null?R:{},this.__client=n}get __isOhbugEvent(){return!0}addAction(t,n,r,i){var d,c;let o=this.actions,a=w(t)?t:"",u=n||{},g=w(r)?r:"",h=new O(a,u,g,i);o.length>=((c=(d=this.__client)==null?void 0:d.__config.maxActions)!=null?c:30)&&o.shift(),o.push(h)}getUser(){return this.user}setUser(t){var n;if(j(t)&&Object.keys(t).length<=6)return this.user=b(b({},this.user),t),this.getUser();(n=this.__client)==null||n.__logger.error(m("setUser should be an object and have up to 6 attributes",t))}addMetadata(t,n){return _(this.metadata,t,n)}getMetadata(t){return y(this.metadata,t)}deleteMetadata(t){return E(this.metadata,t)}toJSON(){let{apiKey:t,appVersion:n,appType:r,timestamp:i,category:o,type:a,sdk:u,device:g,detail:h,user:d,actions:c,metadata:C,releaseStage:M}=this;return{apiKey:t,appVersion:n,appType:r,timestamp:i,category:o,type:a,sdk:u,device:g,detail:h,user:d,actions:c,metadata:C,releaseStage:M}}};function I(e,t){let{apiKey:n,appVersion:r,appType:i,releaseStage:o}=t.__config,a=new Date().toISOString(),u=t.__device(t),g,h,d;return j(e)&&Object.prototype.hasOwnProperty.call(e,"type")&&Object.prototype.hasOwnProperty.call(e,"detail")?(g=e.category||"error",h=e.type,d=e.detail):(g="error",h="unknownError",d=e),new D({apiKey:n,appVersion:r,appType:i,timestamp:a,category:g,type:h,sdk:t.__sdk,device:u,user:t.__user,detail:d,actions:t.__actions,metadata:t.__metadata,releaseStage:o},t)}function k(e,t){let n=[t.__config.onEvent,...t.__extensions.map(({onEvent:r})=>r)].filter(r=>W(r));return n.length===0?e:n.reduce((r,i)=>r&&W(i)?i(r,t):null,e)}function x(e){return Boolean(e==null?void 0:e.__isOhbugEvent)}import{isFunction as Y}from"@ohbug/utils";function z(e,t){[t.__config.onNotify,...t.__extensions.filter(({onNotify:r})=>Y(r)).map(({onNotify:r})=>r)].forEach(r=>r==null?void 0:r(e,t))}async function P(e,t){try{let n=null;return e&&(n=await t.__notifier(e),z(e,t)),n}catch(n){t.__logger.error(n)}}function K(e,t){return Object.keys(t).reduce((r,i)=>{let o=e[i],{defaultValue:a,message:u,validate:g}=t[i];return o!==void 0?g(o)?r.config[i]=o:(r.config[i]=a,r.errors[i]=u):r.config[i]=a,r},{config:{},errors:{}})}var _t=class{__sdk;__config;__logger;__device;__notifier;__extensions;__actions;__user;__metadata;constructor({sdk:t,config:n,schema:r=N,device:i,notifier:o}){let{config:a,errors:u}=K(n,r);this.__sdk=t,this.__config=a,this.__logger=a.logger,this.__device=i,this.__notifier=o,this.__extensions=[],this.__actions=[],this.__user=a.user,this.__metadata={},G(a.metadata)&&Object.keys(a.metadata).forEach(g=>{this.addMetadata(g,a.metadata[g])}),Object.keys(u).length&&this.__logger.warn(V(u,n))}use(t){var n;return this.__extensions.push(t),(n=t.setup)==null||n.call(t,this),this}createEvent(t){let n=I(t,this);return k(n,this)}notify(t,n){let r;return Boolean(t)&&!x(t)?r=this.createEvent(t):r=t,n&&(r=n(r)),P(r,this)}addAction(t,n,r,i){let o=this.__actions,a=F(t)?t:"",u=n||{},g=F(r)?r:"",h=new O(a,u,g,i);o.length>=this.__config.maxActions&&o.shift(),o.push(h)}getUser(){return this.__user}setUser(t){if(G(t)&&Object.keys(t).length<=6)return this.__user=b(b({},this.__user),t),this.getUser();this.__logger.warn(m("setUser should be an object and have up to 6 attributes",t))}addMetadata(t,n){return _(this.__metadata,t,n)}getMetadata(t){return y(this.__metadata,t)}deleteMetadata(t){return E(this.__metadata,t)}};function Ct(e){return e}function Mt(e){return e}var Q=(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))(Q||{});export{_t as Client,Q as EventTypes,Mt as createExtensionUI,Ct as defineExtension,x as isEvent};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ohbug/core",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.3",
|
|
4
4
|
"description": "Responsible for managing Ohbug's workflow",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"author": "chenyueban <jasonchan0527@gmail.com>",
|
|
@@ -23,16 +23,15 @@
|
|
|
23
23
|
}
|
|
24
24
|
},
|
|
25
25
|
"files": [
|
|
26
|
-
"dist"
|
|
27
|
-
"src"
|
|
26
|
+
"dist"
|
|
28
27
|
],
|
|
29
28
|
"sideEffects": false,
|
|
30
29
|
"publishConfig": {
|
|
31
30
|
"access": "public"
|
|
32
31
|
},
|
|
33
32
|
"dependencies": {
|
|
34
|
-
"@ohbug/types": "2.0.
|
|
35
|
-
"@ohbug/utils": "2.0.
|
|
33
|
+
"@ohbug/types": "2.0.3",
|
|
34
|
+
"@ohbug/utils": "2.0.3"
|
|
36
35
|
},
|
|
37
36
|
"scripts": {
|
|
38
37
|
"build": "tsup",
|
package/src/action.ts
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
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
|
-
}
|