@ohbug/core 1.1.5 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -2
- package/dist/index.d.ts +38 -4
- package/dist/index.js +495 -0
- package/dist/index.mjs +471 -0
- package/package.json +22 -20
- package/src/action.ts +23 -0
- package/src/client.ts +210 -0
- package/src/config.ts +75 -0
- package/src/event.ts +276 -0
- package/src/extension.ts +8 -0
- package/src/index.ts +5 -0
- package/src/lib/getErrorMessage.ts +17 -0
- package/src/lib/metaData.ts +21 -0
- package/src/lib/verifyConfig.ts +37 -0
- package/src/notify.ts +38 -0
- package/src/types.ts +27 -0
- package/dist/action.d.ts +0 -9
- package/dist/action.d.ts.map +0 -1
- package/dist/client.d.ts +0 -3
- package/dist/client.d.ts.map +0 -1
- package/dist/config.d.ts +0 -3
- package/dist/config.d.ts.map +0 -1
- package/dist/event.d.ts +0 -81
- package/dist/event.d.ts.map +0 -1
- package/dist/extension.d.ts +0 -12
- package/dist/extension.d.ts.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/lib/getErrorMessage.d.ts +0 -4
- package/dist/lib/getErrorMessage.d.ts.map +0 -1
- package/dist/lib/metaData.d.ts +0 -5
- package/dist/lib/metaData.d.ts.map +0 -1
- package/dist/lib/verifyConfig.d.ts +0 -8
- package/dist/lib/verifyConfig.d.ts.map +0 -1
- package/dist/notify.d.ts +0 -9
- package/dist/notify.d.ts.map +0 -1
- package/dist/ohbug-core.cjs.js +0 -687
- package/dist/ohbug-core.esm.js +0 -680
- package/dist/ohbug-core.esm.min.js +0 -29
- package/dist/ohbug-core.umd.js +0 -693
- package/dist/ohbug-core.umd.min.js +0 -29
package/dist/ohbug-core.cjs.js
DELETED
|
@@ -1,687 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
-
|
|
5
|
-
/*! *****************************************************************************
|
|
6
|
-
Copyright (c) Microsoft Corporation.
|
|
7
|
-
|
|
8
|
-
Permission to use, copy, modify, and/or distribute this software for any
|
|
9
|
-
purpose with or without fee is hereby granted.
|
|
10
|
-
|
|
11
|
-
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
12
|
-
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
13
|
-
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
14
|
-
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
15
|
-
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
16
|
-
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
17
|
-
PERFORMANCE OF THIS SOFTWARE.
|
|
18
|
-
***************************************************************************** */
|
|
19
|
-
|
|
20
|
-
var __assign = function() {
|
|
21
|
-
__assign = Object.assign || function __assign(t) {
|
|
22
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
23
|
-
s = arguments[i];
|
|
24
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
|
|
25
|
-
}
|
|
26
|
-
return t;
|
|
27
|
-
};
|
|
28
|
-
return __assign.apply(this, arguments);
|
|
29
|
-
};
|
|
30
|
-
|
|
31
|
-
function __awaiter(thisArg, _arguments, P, generator) {
|
|
32
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
33
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
34
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
35
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
36
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
37
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
38
|
-
});
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
function __generator(thisArg, body) {
|
|
42
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
43
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
44
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
45
|
-
function step(op) {
|
|
46
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
47
|
-
while (_) try {
|
|
48
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
49
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
50
|
-
switch (op[0]) {
|
|
51
|
-
case 0: case 1: t = op; break;
|
|
52
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
53
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
54
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
55
|
-
default:
|
|
56
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
57
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
58
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
59
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
60
|
-
if (t[2]) _.ops.pop();
|
|
61
|
-
_.trys.pop(); continue;
|
|
62
|
-
}
|
|
63
|
-
op = body.call(thisArg, _);
|
|
64
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
65
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
function __spreadArray$1(to, from) {
|
|
70
|
-
for (var i = 0, il = from.length, j = to.length; i < il; i++, j++)
|
|
71
|
-
to[j] = from[i];
|
|
72
|
-
return to;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
/*! *****************************************************************************
|
|
76
|
-
Copyright (c) Microsoft Corporation.
|
|
77
|
-
|
|
78
|
-
Permission to use, copy, modify, and/or distribute this software for any
|
|
79
|
-
purpose with or without fee is hereby granted.
|
|
80
|
-
|
|
81
|
-
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
82
|
-
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
83
|
-
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
84
|
-
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
85
|
-
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
86
|
-
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
87
|
-
PERFORMANCE OF THIS SOFTWARE.
|
|
88
|
-
***************************************************************************** */
|
|
89
|
-
|
|
90
|
-
function __spreadArray(to, from) {
|
|
91
|
-
for (var i = 0, il = from.length, j = to.length; i < il; i++, j++)
|
|
92
|
-
to[j] = from[i];
|
|
93
|
-
return to;
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
var prefix = 'Ohbug';
|
|
97
|
-
var logger = {
|
|
98
|
-
log: function () {
|
|
99
|
-
var args = [];
|
|
100
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
|
101
|
-
args[_i] = arguments[_i];
|
|
102
|
-
}
|
|
103
|
-
// eslint-disable-next-line no-console
|
|
104
|
-
console.log.apply(console, __spreadArray([prefix], args));
|
|
105
|
-
},
|
|
106
|
-
info: function () {
|
|
107
|
-
var args = [];
|
|
108
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
|
109
|
-
args[_i] = arguments[_i];
|
|
110
|
-
}
|
|
111
|
-
// eslint-disable-next-line no-console
|
|
112
|
-
console.info.apply(console, __spreadArray([prefix], args));
|
|
113
|
-
},
|
|
114
|
-
warn: function () {
|
|
115
|
-
var args = [];
|
|
116
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
|
117
|
-
args[_i] = arguments[_i];
|
|
118
|
-
}
|
|
119
|
-
console.warn.apply(console, __spreadArray([prefix], args));
|
|
120
|
-
},
|
|
121
|
-
error: function () {
|
|
122
|
-
var args = [];
|
|
123
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
|
124
|
-
args[_i] = arguments[_i];
|
|
125
|
-
}
|
|
126
|
-
console.error.apply(console, __spreadArray([prefix], args));
|
|
127
|
-
},
|
|
128
|
-
};
|
|
129
|
-
|
|
130
|
-
function isString(value) {
|
|
131
|
-
return typeof value === 'string';
|
|
132
|
-
}
|
|
133
|
-
function isNumber(value) {
|
|
134
|
-
return typeof value === 'number' && parseInt("" + value, 10) === value;
|
|
135
|
-
}
|
|
136
|
-
function isFunction(value) {
|
|
137
|
-
return typeof value === 'function';
|
|
138
|
-
}
|
|
139
|
-
function isObject(value) {
|
|
140
|
-
return Object.prototype.toString.call(value) === '[object Object]';
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
var schema = {
|
|
144
|
-
// base
|
|
145
|
-
apiKey: {
|
|
146
|
-
defaultValue: undefined,
|
|
147
|
-
message: 'is required',
|
|
148
|
-
validate: function (value) { return Boolean(value) && isString(value); },
|
|
149
|
-
},
|
|
150
|
-
appVersion: {
|
|
151
|
-
defaultValue: undefined,
|
|
152
|
-
message: 'should be a string',
|
|
153
|
-
validate: function (value) { return value === undefined || isString(value); },
|
|
154
|
-
},
|
|
155
|
-
appType: {
|
|
156
|
-
defaultValue: undefined,
|
|
157
|
-
message: 'should be a string',
|
|
158
|
-
validate: function (value) { return value === undefined || isString(value); },
|
|
159
|
-
},
|
|
160
|
-
releaseStage: {
|
|
161
|
-
defaultValue: 'production',
|
|
162
|
-
message: 'should be a string',
|
|
163
|
-
validate: function (value) { return value === undefined || isString(value); },
|
|
164
|
-
},
|
|
165
|
-
endpoint: {
|
|
166
|
-
defaultValue: 'http://localhost:6660/report',
|
|
167
|
-
message: 'should be a string',
|
|
168
|
-
validate: function (value) { return value === undefined || isString(value); },
|
|
169
|
-
},
|
|
170
|
-
maxActions: {
|
|
171
|
-
defaultValue: 30,
|
|
172
|
-
message: 'should be a number between 0 and 100',
|
|
173
|
-
validate: function (value) {
|
|
174
|
-
return value === undefined || (isNumber(value) && value >= 1 && value <= 100);
|
|
175
|
-
},
|
|
176
|
-
},
|
|
177
|
-
// hooks
|
|
178
|
-
created: {
|
|
179
|
-
defaultValue: function (event) { return event; },
|
|
180
|
-
message: 'should be a function',
|
|
181
|
-
validate: function (value) { return value === undefined || isFunction(value); },
|
|
182
|
-
},
|
|
183
|
-
notified: {
|
|
184
|
-
defaultValue: function () { },
|
|
185
|
-
message: 'should be a function',
|
|
186
|
-
validate: function (value) { return value === undefined || isFunction(value); },
|
|
187
|
-
},
|
|
188
|
-
// utils
|
|
189
|
-
logger: {
|
|
190
|
-
defaultValue: logger,
|
|
191
|
-
message: 'should be null or an object with methods { log, info, warn, error }',
|
|
192
|
-
validate: function (value) {
|
|
193
|
-
return value === undefined ||
|
|
194
|
-
(value &&
|
|
195
|
-
['log', 'info', 'warn', 'error'].reduce(function (accumulator, method) {
|
|
196
|
-
return accumulator && typeof value[method] === 'function';
|
|
197
|
-
}, true));
|
|
198
|
-
},
|
|
199
|
-
},
|
|
200
|
-
// data
|
|
201
|
-
user: {
|
|
202
|
-
defaultValue: undefined,
|
|
203
|
-
message: 'should be an object and have up to 6 attributes',
|
|
204
|
-
validate: function (value) {
|
|
205
|
-
return value === undefined ||
|
|
206
|
-
(isObject(value) && Object.keys(value).length <= 6);
|
|
207
|
-
},
|
|
208
|
-
},
|
|
209
|
-
metaData: {
|
|
210
|
-
defaultValue: undefined,
|
|
211
|
-
message: 'should be an object',
|
|
212
|
-
validate: function (value) { return value === undefined || isObject(value); },
|
|
213
|
-
},
|
|
214
|
-
};
|
|
215
|
-
|
|
216
|
-
function createExtension(extension) {
|
|
217
|
-
return extension;
|
|
218
|
-
}
|
|
219
|
-
function createExtensionUI(extensionUI) {
|
|
220
|
-
return extensionUI;
|
|
221
|
-
}
|
|
222
|
-
/**
|
|
223
|
-
* Extension loading and initialization
|
|
224
|
-
*
|
|
225
|
-
* @param extension
|
|
226
|
-
* @param client
|
|
227
|
-
* @param args
|
|
228
|
-
*/
|
|
229
|
-
function loadExtension(extension, client) {
|
|
230
|
-
var _a;
|
|
231
|
-
var args = [];
|
|
232
|
-
for (var _i = 2; _i < arguments.length; _i++) {
|
|
233
|
-
args[_i - 2] = arguments[_i];
|
|
234
|
-
}
|
|
235
|
-
var result = (_a = extension.init) === null || _a === void 0 ? void 0 : _a.call.apply(_a, __spreadArray$1([extension, client], args));
|
|
236
|
-
client._extensions.push(extension);
|
|
237
|
-
// @ts-ignore
|
|
238
|
-
// eslint-disable-next-line no-param-reassign
|
|
239
|
-
client._hooks.created = function (_event, _client) {
|
|
240
|
-
var _a, _b;
|
|
241
|
-
var funcs = __spreadArray$1([
|
|
242
|
-
_client._config.created
|
|
243
|
-
], _client._extensions
|
|
244
|
-
.filter(function (_a) {
|
|
245
|
-
var created = _a.created;
|
|
246
|
-
return isFunction(created);
|
|
247
|
-
})
|
|
248
|
-
.map(function (_a) {
|
|
249
|
-
var created = _a.created;
|
|
250
|
-
return created;
|
|
251
|
-
}));
|
|
252
|
-
if (funcs.length === 0)
|
|
253
|
-
return (function () {
|
|
254
|
-
var arg = [];
|
|
255
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
|
256
|
-
arg[_i] = arguments[_i];
|
|
257
|
-
}
|
|
258
|
-
return arg;
|
|
259
|
-
})(_event, _client);
|
|
260
|
-
if (funcs.length === 1)
|
|
261
|
-
return (_a = funcs[0]) === null || _a === void 0 ? void 0 : _a.call(funcs, _event, _client);
|
|
262
|
-
// @ts-ignore
|
|
263
|
-
return (_b = funcs.reduce(function (a, b) { return function (event) { return b(a(event, _client), _client); }; })) === null || _b === void 0 ? void 0 : _b(_event, _client);
|
|
264
|
-
};
|
|
265
|
-
// eslint-disable-next-line no-param-reassign
|
|
266
|
-
client._hooks.notified = function (_event, _client) {
|
|
267
|
-
return __spreadArray$1([
|
|
268
|
-
_client._config.notified
|
|
269
|
-
], _client._extensions
|
|
270
|
-
.filter(function (_a) {
|
|
271
|
-
var notified = _a.notified;
|
|
272
|
-
return isFunction(notified);
|
|
273
|
-
})
|
|
274
|
-
.map(function (_a) {
|
|
275
|
-
var notified = _a.notified;
|
|
276
|
-
return notified;
|
|
277
|
-
})).forEach(function (func) { return func === null || func === void 0 ? void 0 : func(_event, _client); });
|
|
278
|
-
};
|
|
279
|
-
return result || client;
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
var Action = /** @class */ (function () {
|
|
283
|
-
function Action(message, data, type, timestamp) {
|
|
284
|
-
this.type = type;
|
|
285
|
-
this.timestamp = timestamp || new Date().toISOString();
|
|
286
|
-
this.message = message;
|
|
287
|
-
this.data = data;
|
|
288
|
-
}
|
|
289
|
-
return Action;
|
|
290
|
-
}());
|
|
291
|
-
|
|
292
|
-
function getConfigErrorMessage(errors, config) {
|
|
293
|
-
return new Error("Invalid configuration\n" + Object.keys(errors)
|
|
294
|
-
.map(
|
|
295
|
-
// @ts-ignore
|
|
296
|
-
function (key) { return "- " + key + " " + errors[key] + ", got " + JSON.stringify(config[key]); })
|
|
297
|
-
.join('\n') + "\n ");
|
|
298
|
-
}
|
|
299
|
-
function getErrorMessage(message, data) {
|
|
300
|
-
return new Error("Invalid data\n- " + message + ", got " + JSON.stringify(data));
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
function addMetaData(map, section, data) {
|
|
304
|
-
if (!section)
|
|
305
|
-
return;
|
|
306
|
-
map[section] = data;
|
|
307
|
-
}
|
|
308
|
-
function getMetaData(map, section) {
|
|
309
|
-
if (map[section]) {
|
|
310
|
-
return map[section];
|
|
311
|
-
}
|
|
312
|
-
return undefined;
|
|
313
|
-
}
|
|
314
|
-
function deleteMetaData(map, section) {
|
|
315
|
-
if (map[section]) {
|
|
316
|
-
return delete map[section];
|
|
317
|
-
}
|
|
318
|
-
return undefined;
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
var Event = /** @class */ (function () {
|
|
322
|
-
function Event(values, client) {
|
|
323
|
-
var apiKey = values.apiKey, appVersion = values.appVersion, appType = values.appType, releaseStage = values.releaseStage, timestamp = values.timestamp, category = values.category, type = values.type, sdk = values.sdk, detail = values.detail, device = values.device, user = values.user, actions = values.actions, metaData = values.metaData;
|
|
324
|
-
this.apiKey = apiKey;
|
|
325
|
-
this.appVersion = appVersion;
|
|
326
|
-
this.appType = appType;
|
|
327
|
-
this.releaseStage = releaseStage;
|
|
328
|
-
this.timestamp = timestamp;
|
|
329
|
-
this.category = category;
|
|
330
|
-
this.type = type;
|
|
331
|
-
this.sdk = sdk;
|
|
332
|
-
this.detail = detail;
|
|
333
|
-
this.device = device;
|
|
334
|
-
this.user = user;
|
|
335
|
-
this.actions = actions;
|
|
336
|
-
this.metaData = metaData;
|
|
337
|
-
this._client = client;
|
|
338
|
-
}
|
|
339
|
-
Object.defineProperty(Event.prototype, "_isOhbugEvent", {
|
|
340
|
-
get: function () {
|
|
341
|
-
return true;
|
|
342
|
-
},
|
|
343
|
-
enumerable: false,
|
|
344
|
-
configurable: true
|
|
345
|
-
});
|
|
346
|
-
/**
|
|
347
|
-
* Add an action.
|
|
348
|
-
* Once the threshold is reached, the oldest breadcrumbs will be deleted.
|
|
349
|
-
* 新增一个动作。
|
|
350
|
-
* 一旦达到阈值,最老的 Action 将被删除。
|
|
351
|
-
*
|
|
352
|
-
* @param message
|
|
353
|
-
* @param data
|
|
354
|
-
* @param type
|
|
355
|
-
* @param timestamp
|
|
356
|
-
*/
|
|
357
|
-
Event.prototype.addAction = function (message, data, type, timestamp) {
|
|
358
|
-
var _a, _b;
|
|
359
|
-
var actions = this.actions;
|
|
360
|
-
var targetMessage = isString(message) ? message : '';
|
|
361
|
-
var targetData = data || {};
|
|
362
|
-
var targetType = isString(type) ? type : '';
|
|
363
|
-
var action = new Action(targetMessage, targetData, targetType, timestamp);
|
|
364
|
-
if (actions.length >= ((_b = (_a = this._client) === null || _a === void 0 ? void 0 : _a._config.maxActions) !== null && _b !== void 0 ? _b : 30)) {
|
|
365
|
-
actions.shift();
|
|
366
|
-
}
|
|
367
|
-
actions.push(action);
|
|
368
|
-
};
|
|
369
|
-
/**
|
|
370
|
-
* Get current user information
|
|
371
|
-
* 获取当前的用户信息
|
|
372
|
-
*/
|
|
373
|
-
Event.prototype.getUser = function () {
|
|
374
|
-
return this.user;
|
|
375
|
-
};
|
|
376
|
-
/**
|
|
377
|
-
* Set current user information
|
|
378
|
-
* 设置当前的用户信息
|
|
379
|
-
*/
|
|
380
|
-
Event.prototype.setUser = function (user) {
|
|
381
|
-
var _a;
|
|
382
|
-
if (isObject(user) && Object.keys(user).length <= 6) {
|
|
383
|
-
this.user = __assign(__assign({}, this.user), user);
|
|
384
|
-
return this.getUser();
|
|
385
|
-
}
|
|
386
|
-
(_a = this._client) === null || _a === void 0 ? void 0 : _a._logger.warn(getErrorMessage('setUser should be an object and have up to 6 attributes', user));
|
|
387
|
-
return undefined;
|
|
388
|
-
};
|
|
389
|
-
/**
|
|
390
|
-
* Add metaData
|
|
391
|
-
* 新增 metaData
|
|
392
|
-
*
|
|
393
|
-
* @param section
|
|
394
|
-
* @param data
|
|
395
|
-
*/
|
|
396
|
-
Event.prototype.addMetaData = function (section, data) {
|
|
397
|
-
return addMetaData(this.metaData, section, data);
|
|
398
|
-
};
|
|
399
|
-
/**
|
|
400
|
-
* Get metaData
|
|
401
|
-
* 获取 metaData
|
|
402
|
-
*
|
|
403
|
-
* @param section
|
|
404
|
-
*/
|
|
405
|
-
Event.prototype.getMetaData = function (section) {
|
|
406
|
-
return getMetaData(this.metaData, section);
|
|
407
|
-
};
|
|
408
|
-
/**
|
|
409
|
-
* Delete metaData
|
|
410
|
-
* 删除 metaData
|
|
411
|
-
*
|
|
412
|
-
* @param section
|
|
413
|
-
*/
|
|
414
|
-
Event.prototype.deleteMetaData = function (section) {
|
|
415
|
-
return deleteMetaData(this.metaData, section);
|
|
416
|
-
};
|
|
417
|
-
Event.prototype.toJSON = function () {
|
|
418
|
-
var _a = this, apiKey = _a.apiKey, appVersion = _a.appVersion, appType = _a.appType, timestamp = _a.timestamp, category = _a.category, type = _a.type, sdk = _a.sdk, device = _a.device, detail = _a.detail, user = _a.user, actions = _a.actions, metaData = _a.metaData, releaseStage = _a.releaseStage;
|
|
419
|
-
return {
|
|
420
|
-
apiKey: apiKey,
|
|
421
|
-
appVersion: appVersion,
|
|
422
|
-
appType: appType,
|
|
423
|
-
timestamp: timestamp,
|
|
424
|
-
category: category,
|
|
425
|
-
type: type,
|
|
426
|
-
sdk: sdk,
|
|
427
|
-
device: device,
|
|
428
|
-
detail: detail,
|
|
429
|
-
user: user,
|
|
430
|
-
actions: actions,
|
|
431
|
-
metaData: metaData,
|
|
432
|
-
releaseStage: releaseStage,
|
|
433
|
-
};
|
|
434
|
-
};
|
|
435
|
-
return Event;
|
|
436
|
-
}());
|
|
437
|
-
function createEvent(values, client) {
|
|
438
|
-
var _a = client._config, apiKey = _a.apiKey, appVersion = _a.appVersion, appType = _a.appType, releaseStage = _a.releaseStage;
|
|
439
|
-
var timestamp = new Date().toISOString();
|
|
440
|
-
var device = client._device(client);
|
|
441
|
-
var category;
|
|
442
|
-
var type;
|
|
443
|
-
var detail;
|
|
444
|
-
if (isObject(values) &&
|
|
445
|
-
Object.prototype.hasOwnProperty.call(values, 'type') &&
|
|
446
|
-
Object.prototype.hasOwnProperty.call(values, 'detail')) {
|
|
447
|
-
category = values.category || 'error';
|
|
448
|
-
type = values.type;
|
|
449
|
-
detail = values.detail;
|
|
450
|
-
}
|
|
451
|
-
else {
|
|
452
|
-
category = 'error';
|
|
453
|
-
type = 'unknownError';
|
|
454
|
-
detail = values;
|
|
455
|
-
}
|
|
456
|
-
return new Event({
|
|
457
|
-
apiKey: apiKey,
|
|
458
|
-
appVersion: appVersion,
|
|
459
|
-
appType: appType,
|
|
460
|
-
timestamp: timestamp,
|
|
461
|
-
category: category,
|
|
462
|
-
type: type,
|
|
463
|
-
sdk: client._sdk,
|
|
464
|
-
device: device,
|
|
465
|
-
user: client._user,
|
|
466
|
-
detail: detail,
|
|
467
|
-
actions: client._actions,
|
|
468
|
-
metaData: client._metaData,
|
|
469
|
-
releaseStage: releaseStage,
|
|
470
|
-
}, client);
|
|
471
|
-
}
|
|
472
|
-
function isEvent(eventLike) {
|
|
473
|
-
return Boolean(eventLike === null || eventLike === void 0 ? void 0 : eventLike._isOhbugEvent);
|
|
474
|
-
}
|
|
475
|
-
|
|
476
|
-
/**
|
|
477
|
-
* Used to control the timing of reporting events and the related life cycle.
|
|
478
|
-
*
|
|
479
|
-
* @param event
|
|
480
|
-
* @param client
|
|
481
|
-
*/
|
|
482
|
-
function notify(event, client) {
|
|
483
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
484
|
-
var result, e_1;
|
|
485
|
-
return __generator(this, function (_a) {
|
|
486
|
-
switch (_a.label) {
|
|
487
|
-
case 0:
|
|
488
|
-
_a.trys.push([0, 3, , 4]);
|
|
489
|
-
result = null;
|
|
490
|
-
if (!event) return [3 /*break*/, 2];
|
|
491
|
-
return [4 /*yield*/, client._notifier(event)];
|
|
492
|
-
case 1:
|
|
493
|
-
result = _a.sent();
|
|
494
|
-
_a.label = 2;
|
|
495
|
-
case 2:
|
|
496
|
-
if (isFunction(client._hooks.notified)) {
|
|
497
|
-
client._hooks.notified(event, client);
|
|
498
|
-
}
|
|
499
|
-
return [2 /*return*/, result];
|
|
500
|
-
case 3:
|
|
501
|
-
e_1 = _a.sent();
|
|
502
|
-
client._logger.error(e_1);
|
|
503
|
-
return [3 /*break*/, 4];
|
|
504
|
-
case 4: return [2 /*return*/];
|
|
505
|
-
}
|
|
506
|
-
});
|
|
507
|
-
});
|
|
508
|
-
}
|
|
509
|
-
|
|
510
|
-
function verifyConfig(config, schema) {
|
|
511
|
-
var keys = Object.keys(schema);
|
|
512
|
-
return keys.reduce(function (accumulator, key) {
|
|
513
|
-
var configValue = config[key];
|
|
514
|
-
var _a = schema[key], defaultValue = _a.defaultValue, message = _a.message, validate = _a.validate;
|
|
515
|
-
if (configValue !== undefined) {
|
|
516
|
-
var valid = validate(configValue);
|
|
517
|
-
if (valid) {
|
|
518
|
-
accumulator.config[key] = configValue;
|
|
519
|
-
}
|
|
520
|
-
else {
|
|
521
|
-
accumulator.config[key] = defaultValue;
|
|
522
|
-
accumulator.errors[key] = message;
|
|
523
|
-
}
|
|
524
|
-
}
|
|
525
|
-
else {
|
|
526
|
-
// if there is no corresponding configuration, use the default value
|
|
527
|
-
// 如果没有传入相应配置 使用默认值
|
|
528
|
-
accumulator.config[key] = defaultValue;
|
|
529
|
-
}
|
|
530
|
-
return accumulator;
|
|
531
|
-
}, {
|
|
532
|
-
config: {},
|
|
533
|
-
errors: {},
|
|
534
|
-
});
|
|
535
|
-
}
|
|
536
|
-
|
|
537
|
-
var Client = /** @class */ (function () {
|
|
538
|
-
function Client(_a) {
|
|
539
|
-
var _this = this;
|
|
540
|
-
var sdk = _a.sdk, baseConfig = _a.config, _b = _a.schema, schema$1 = _b === void 0 ? schema : _b, device = _a.device, notifier = _a.notifier;
|
|
541
|
-
var _c = verifyConfig(baseConfig, schema$1), config = _c.config, errors = _c.errors;
|
|
542
|
-
// initialization
|
|
543
|
-
this._sdk = sdk;
|
|
544
|
-
this._config = config;
|
|
545
|
-
this._logger = config.logger;
|
|
546
|
-
this._device = device;
|
|
547
|
-
this._notifier = notifier;
|
|
548
|
-
this._extensions = [];
|
|
549
|
-
this._hooks = {
|
|
550
|
-
created: config.created,
|
|
551
|
-
notified: config.notified,
|
|
552
|
-
};
|
|
553
|
-
this._actions = [];
|
|
554
|
-
this._user = config.user;
|
|
555
|
-
this._metaData = {};
|
|
556
|
-
if (isObject(config.metaData)) {
|
|
557
|
-
Object.keys(config.metaData).forEach(function (key) {
|
|
558
|
-
_this.addMetaData(key, config.metaData[key]);
|
|
559
|
-
});
|
|
560
|
-
}
|
|
561
|
-
if (Object.keys(errors).length) {
|
|
562
|
-
this._logger.warn(getConfigErrorMessage(errors, baseConfig));
|
|
563
|
-
}
|
|
564
|
-
}
|
|
565
|
-
/**
|
|
566
|
-
* Load extension
|
|
567
|
-
* 加载扩展
|
|
568
|
-
*
|
|
569
|
-
* @param extension
|
|
570
|
-
* @param args
|
|
571
|
-
*/
|
|
572
|
-
Client.prototype.use = function (extension) {
|
|
573
|
-
var args = [];
|
|
574
|
-
for (var _i = 1; _i < arguments.length; _i++) {
|
|
575
|
-
args[_i - 1] = arguments[_i];
|
|
576
|
-
}
|
|
577
|
-
return loadExtension.apply(void 0, __spreadArray$1([extension, this], args));
|
|
578
|
-
};
|
|
579
|
-
/**
|
|
580
|
-
* Create an event, you will get a data body containing device actions and other information
|
|
581
|
-
* 创建事件,将会得到一个含有 device actions 等信息的数据体
|
|
582
|
-
*
|
|
583
|
-
* @param value
|
|
584
|
-
*/
|
|
585
|
-
Client.prototype.createEvent = function (value) {
|
|
586
|
-
var event = createEvent(value, this);
|
|
587
|
-
if (isFunction(this._hooks.created)) {
|
|
588
|
-
// trigger created hook
|
|
589
|
-
return this._hooks.created(event, this);
|
|
590
|
-
}
|
|
591
|
-
return event;
|
|
592
|
-
};
|
|
593
|
-
/**
|
|
594
|
-
* Used to trigger the reporting interface
|
|
595
|
-
* 用于触发上报接口
|
|
596
|
-
*
|
|
597
|
-
* @param eventLike
|
|
598
|
-
* @param beforeNotify
|
|
599
|
-
*/
|
|
600
|
-
Client.prototype.notify = function (eventLike, beforeNotify) {
|
|
601
|
-
var event;
|
|
602
|
-
if (Boolean(eventLike) && !isEvent(eventLike)) {
|
|
603
|
-
event = this.createEvent(eventLike);
|
|
604
|
-
}
|
|
605
|
-
else {
|
|
606
|
-
event = eventLike;
|
|
607
|
-
}
|
|
608
|
-
if (beforeNotify)
|
|
609
|
-
event = beforeNotify(event);
|
|
610
|
-
return notify(event, this);
|
|
611
|
-
};
|
|
612
|
-
/**
|
|
613
|
-
* Add an action.
|
|
614
|
-
* Once the threshold is reached, the oldest breadcrumbs will be deleted.
|
|
615
|
-
* 新增一个动作。
|
|
616
|
-
* 一旦达到阈值,最老的 Action 将被删除。
|
|
617
|
-
*
|
|
618
|
-
* @param message
|
|
619
|
-
* @param data
|
|
620
|
-
* @param type
|
|
621
|
-
* @param timestamp
|
|
622
|
-
*/
|
|
623
|
-
Client.prototype.addAction = function (message, data, type, timestamp) {
|
|
624
|
-
var actions = this._actions;
|
|
625
|
-
var targetMessage = isString(message) ? message : '';
|
|
626
|
-
var targetData = data || {};
|
|
627
|
-
var targetType = isString(type) ? type : '';
|
|
628
|
-
var action = new Action(targetMessage, targetData, targetType, timestamp);
|
|
629
|
-
if (actions.length >= this._config.maxActions) {
|
|
630
|
-
actions.shift();
|
|
631
|
-
}
|
|
632
|
-
actions.push(action);
|
|
633
|
-
};
|
|
634
|
-
/**
|
|
635
|
-
* Get current user information
|
|
636
|
-
* 获取当前的用户信息
|
|
637
|
-
*/
|
|
638
|
-
Client.prototype.getUser = function () {
|
|
639
|
-
return this._user;
|
|
640
|
-
};
|
|
641
|
-
/**
|
|
642
|
-
* Set current user information
|
|
643
|
-
* 设置当前的用户信息
|
|
644
|
-
*/
|
|
645
|
-
Client.prototype.setUser = function (user) {
|
|
646
|
-
if (isObject(user) && Object.keys(user).length <= 6) {
|
|
647
|
-
this._user = __assign(__assign({}, this._user), user);
|
|
648
|
-
return this.getUser();
|
|
649
|
-
}
|
|
650
|
-
this._logger.warn(getErrorMessage('setUser should be an object and have up to 6 attributes', user));
|
|
651
|
-
return undefined;
|
|
652
|
-
};
|
|
653
|
-
/**
|
|
654
|
-
* Add metaData
|
|
655
|
-
* 新增 metaData
|
|
656
|
-
*
|
|
657
|
-
* @param section
|
|
658
|
-
* @param data
|
|
659
|
-
*/
|
|
660
|
-
Client.prototype.addMetaData = function (section, data) {
|
|
661
|
-
return addMetaData(this._metaData, section, data);
|
|
662
|
-
};
|
|
663
|
-
/**
|
|
664
|
-
* Get metaData
|
|
665
|
-
* 获取 metaData
|
|
666
|
-
*
|
|
667
|
-
* @param section
|
|
668
|
-
*/
|
|
669
|
-
Client.prototype.getMetaData = function (section) {
|
|
670
|
-
return getMetaData(this._metaData, section);
|
|
671
|
-
};
|
|
672
|
-
/**
|
|
673
|
-
* Delete metaData
|
|
674
|
-
* 删除 metaData
|
|
675
|
-
*
|
|
676
|
-
* @param section
|
|
677
|
-
*/
|
|
678
|
-
Client.prototype.deleteMetaData = function (section) {
|
|
679
|
-
return deleteMetaData(this._metaData, section);
|
|
680
|
-
};
|
|
681
|
-
return Client;
|
|
682
|
-
}());
|
|
683
|
-
|
|
684
|
-
exports.Client = Client;
|
|
685
|
-
exports.createExtension = createExtension;
|
|
686
|
-
exports.createExtensionUI = createExtensionUI;
|
|
687
|
-
exports.isEvent = isEvent;
|