@ohbug/core 2.2.1 → 2.2.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.mts +30 -24
- package/dist/index.mjs +456 -582
- package/dist/index.mjs.map +1 -0
- package/package.json +18 -19
- package/dist/index.d.ts +0 -29
- package/dist/index.js +0 -639
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
|
-
|
|
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
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
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
|
-
|
|
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
|
-
|
|
113
|
-
${
|
|
114
|
-
|
|
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
|
-
|
|
120
|
-
- ${message}, got ${JSON.stringify(data)}`);
|
|
93
|
+
return /* @__PURE__ */ new Error(`Invalid data\n- ${message}, got ${JSON.stringify(data)}`);
|
|
121
94
|
}
|
|
122
|
-
|
|
123
|
-
|
|
95
|
+
//#endregion
|
|
96
|
+
//#region src/lib/metadata.ts
|
|
124
97
|
function addMetadata(map, section, data) {
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
map[section] = data;
|
|
98
|
+
if (!section) return;
|
|
99
|
+
map[section] = data;
|
|
128
100
|
}
|
|
129
101
|
function getMetadata(map, section) {
|
|
130
|
-
|
|
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
|
-
|
|
137
|
-
return delete map[section];
|
|
138
|
-
}
|
|
139
|
-
return void 0;
|
|
105
|
+
if (map[section]) return delete map[section];
|
|
140
106
|
}
|
|
141
|
-
|
|
142
|
-
|
|
107
|
+
//#endregion
|
|
108
|
+
//#region src/event.ts
|
|
143
109
|
var Event = class {
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
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
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
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
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
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
|
-
|
|
273
|
+
return Boolean(eventLike?.__isOhbugEvent);
|
|
354
274
|
}
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
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
|
-
|
|
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
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
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
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
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
|
-
|
|
456
|
+
//#endregion
|
|
457
|
+
//#region src/extension.ts
|
|
580
458
|
function defineExtension(extension) {
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
return extension;
|
|
459
|
+
if (!extension) return {};
|
|
460
|
+
return extension;
|
|
584
461
|
}
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
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
|