@yysports/tools 1.0.7 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. package/dist/Track/core/auth.d.ts +16 -0
  2. package/dist/Track/core/auth.js +83 -0
  3. package/dist/Track/core/constants.d.ts +14 -0
  4. package/dist/Track/core/constants.js +25 -0
  5. package/dist/Track/core/envelope.d.ts +9 -0
  6. package/dist/Track/core/envelope.js +54 -0
  7. package/dist/Track/core/instance.d.ts +108 -0
  8. package/dist/Track/core/instance.js +365 -0
  9. package/dist/Track/core/lifecycle.d.ts +18 -0
  10. package/dist/Track/core/lifecycle.js +59 -0
  11. package/dist/Track/core/profile.d.ts +5 -0
  12. package/dist/Track/core/profile.js +30 -0
  13. package/dist/Track/core/queue.d.ts +13 -0
  14. package/dist/Track/core/queue.js +36 -0
  15. package/dist/Track/core/replay.d.ts +17 -0
  16. package/dist/Track/core/replay.js +117 -0
  17. package/dist/Track/core/router.d.ts +6 -0
  18. package/dist/Track/core/router.js +15 -0
  19. package/dist/Track/core/system.d.ts +14 -0
  20. package/dist/Track/core/system.js +36 -0
  21. package/dist/Track/core/transport.d.ts +18 -0
  22. package/dist/Track/core/transport.js +151 -0
  23. package/dist/Track/index.d.ts +31 -28
  24. package/dist/Track/index.js +212 -392
  25. package/dist/Utils/common.js +5 -6
  26. package/dist/Utils/route.js +1 -6
  27. package/dist/Utils/url.js +1 -3
  28. package/dist/Utils/wx.js +2 -7
  29. package/dist/interfaces/track.interface.d.ts +14 -4
  30. package/lib/Track/core/auth.d.ts +16 -0
  31. package/lib/Track/core/auth.js +67 -0
  32. package/lib/Track/core/constants.d.ts +14 -0
  33. package/lib/Track/core/constants.js +56 -0
  34. package/lib/Track/core/envelope.d.ts +9 -0
  35. package/lib/Track/core/envelope.js +61 -0
  36. package/lib/Track/core/instance.d.ts +108 -0
  37. package/lib/Track/core/instance.js +257 -0
  38. package/lib/Track/core/lifecycle.d.ts +18 -0
  39. package/lib/Track/core/lifecycle.js +74 -0
  40. package/lib/Track/core/profile.d.ts +5 -0
  41. package/lib/Track/core/profile.js +47 -0
  42. package/lib/Track/core/queue.d.ts +13 -0
  43. package/lib/Track/core/queue.js +49 -0
  44. package/lib/Track/core/replay.d.ts +17 -0
  45. package/lib/Track/core/replay.js +131 -0
  46. package/lib/Track/core/router.d.ts +6 -0
  47. package/lib/Track/core/router.js +39 -0
  48. package/lib/Track/core/system.d.ts +14 -0
  49. package/lib/Track/core/system.js +53 -0
  50. package/lib/Track/core/transport.d.ts +18 -0
  51. package/lib/Track/core/transport.js +115 -0
  52. package/lib/Track/index.d.ts +31 -28
  53. package/lib/Track/index.js +126 -312
  54. package/lib/Utils/common.js +4 -6
  55. package/lib/interfaces/track.interface.d.ts +14 -4
  56. package/package.json +11 -10
@@ -0,0 +1,18 @@
1
+ /**
2
+ * LifecycleManager - 浏览器生命周期与错误监听的封装
3
+ */
4
+ import { ErrorProfile } from '../../interfaces/track.interface';
5
+ type Options = {
6
+ onHidden?: () => void;
7
+ onError?: (error: ErrorProfile) => void;
8
+ };
9
+ export declare class LifecycleManager {
10
+ private opts;
11
+ constructor(opts: Options);
12
+ private onVisibilityChange;
13
+ private onWindowError;
14
+ private onUnhandledRejection;
15
+ mount(): void;
16
+ unmount(): void;
17
+ }
18
+ export {};
@@ -0,0 +1,59 @@
1
+ import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
2
+ import _createClass from "@babel/runtime/helpers/esm/createClass";
3
+ import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
4
+ /**
5
+ * LifecycleManager - 浏览器生命周期与错误监听的封装
6
+ */
7
+
8
+ export var LifecycleManager = /*#__PURE__*/function () {
9
+ function LifecycleManager(opts) {
10
+ var _this = this;
11
+ _classCallCheck(this, LifecycleManager);
12
+ _defineProperty(this, "opts", void 0);
13
+ _defineProperty(this, "onVisibilityChange", function () {
14
+ try {
15
+ var _this$opts$onHidden, _this$opts;
16
+ if (typeof document === 'undefined') return;
17
+ if (document.visibilityState === 'hidden') (_this$opts$onHidden = (_this$opts = _this.opts).onHidden) === null || _this$opts$onHidden === void 0 || _this$opts$onHidden.call(_this$opts);
18
+ } catch (_unused) {
19
+ /* ignore */
20
+ }
21
+ });
22
+ _defineProperty(this, "onWindowError", function (e) {
23
+ var _this$opts$onError, _this$opts2;
24
+ var err = {
25
+ message: e.message,
26
+ fileName: e.filename
27
+ };
28
+ (_this$opts$onError = (_this$opts2 = _this.opts).onError) === null || _this$opts$onError === void 0 || _this$opts$onError.call(_this$opts2, err);
29
+ });
30
+ _defineProperty(this, "onUnhandledRejection", function (e) {
31
+ var _reason$toString, _this$opts$onError2, _this$opts3;
32
+ var reason = e.reason;
33
+ var message = reason && (reason.message || ((_reason$toString = reason.toString) === null || _reason$toString === void 0 ? void 0 : _reason$toString.call(reason))) || 'unhandledrejection';
34
+ (_this$opts$onError2 = (_this$opts3 = _this.opts).onError) === null || _this$opts$onError2 === void 0 || _this$opts$onError2.call(_this$opts3, {
35
+ message: message,
36
+ fileName: 'unhandledrejection'
37
+ });
38
+ });
39
+ this.opts = opts;
40
+ }
41
+ _createClass(LifecycleManager, [{
42
+ key: "mount",
43
+ value: function mount() {
44
+ if (typeof addEventListener === 'undefined') return;
45
+ addEventListener('visibilitychange', this.onVisibilityChange);
46
+ addEventListener('error', this.onWindowError);
47
+ addEventListener('unhandledrejection', this.onUnhandledRejection);
48
+ }
49
+ }, {
50
+ key: "unmount",
51
+ value: function unmount() {
52
+ if (typeof removeEventListener === 'undefined') return;
53
+ removeEventListener('visibilitychange', this.onVisibilityChange);
54
+ removeEventListener('error', this.onWindowError);
55
+ removeEventListener('unhandledrejection', this.onUnhandledRejection);
56
+ }
57
+ }]);
58
+ return LifecycleManager;
59
+ }();
@@ -0,0 +1,5 @@
1
+ /**
2
+ * 用户画像合并:优先使用权限中心信息,并叠加外部传入的 patch
3
+ */
4
+ import { AuthCenterUserProfile, UserProfile } from '../../interfaces/track.interface';
5
+ export declare function mergeUserProfile(authCenter: AuthCenterUserProfile, external?: Partial<UserProfile>): UserProfile;
@@ -0,0 +1,30 @@
1
+ import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
2
+ /**
3
+ * 用户画像合并:优先使用权限中心信息,并叠加外部传入的 patch
4
+ */
5
+
6
+ export function mergeUserProfile(authCenter, external) {
7
+ var _authCenter$dataScope, _authCenter$dataScope2;
8
+ // 权限中心未返回 userName,则仅使用外部提供(保持与原逻辑一致)
9
+ if (!(authCenter !== null && authCenter !== void 0 && authCenter.userName)) {
10
+ return _objectSpread({
11
+ userId: '',
12
+ userName: ''
13
+ }, external || {});
14
+ }
15
+ var base = {
16
+ userId: authCenter.userName,
17
+ userName: authCenter.nickName,
18
+ orgCode: authCenter.orgCode,
19
+ orgTitle: authCenter.orgTitle,
20
+ dataScopeRoles: (_authCenter$dataScope = authCenter.dataScopeRoles) === null || _authCenter$dataScope === void 0 ? void 0 : _authCenter$dataScope.map(function (r) {
21
+ return r.name;
22
+ }).join(','),
23
+ dataScopeZonePermissions: (_authCenter$dataScope2 = authCenter.dataScopeZonePermissions) === null || _authCenter$dataScope2 === void 0 || (_authCenter$dataScope2 = _authCenter$dataScope2.list) === null || _authCenter$dataScope2 === void 0 ? void 0 : _authCenter$dataScope2.map(function (g) {
24
+ return g.list.map(function (z) {
25
+ return z.zoneId;
26
+ }).join(',');
27
+ }).join(',')
28
+ };
29
+ return _objectSpread(_objectSpread({}, base), external || {});
30
+ }
@@ -0,0 +1,13 @@
1
+ /**
2
+ * TrackQueue - 轻量埋点队列实现
3
+ * 提供 push / size / drain 三个最小操作,避免引入额外依赖。
4
+ */
5
+ export declare class TrackQueue<T> {
6
+ private items;
7
+ /** 入队一个元素 */
8
+ push(item: T): void;
9
+ /** 当前元素数量 */
10
+ size(): number;
11
+ /** 取出并清空所有元素(返回快照) */
12
+ drain(): T[];
13
+ }
@@ -0,0 +1,36 @@
1
+ import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
2
+ import _createClass from "@babel/runtime/helpers/esm/createClass";
3
+ import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
4
+ /**
5
+ * TrackQueue - 轻量埋点队列实现
6
+ * 提供 push / size / drain 三个最小操作,避免引入额外依赖。
7
+ */
8
+ export var TrackQueue = /*#__PURE__*/function () {
9
+ function TrackQueue() {
10
+ _classCallCheck(this, TrackQueue);
11
+ _defineProperty(this, "items", []);
12
+ }
13
+ _createClass(TrackQueue, [{
14
+ key: "push",
15
+ value: /** 入队一个元素 */
16
+ function push(item) {
17
+ this.items.push(item);
18
+ }
19
+ /** 当前元素数量 */
20
+ }, {
21
+ key: "size",
22
+ value: function size() {
23
+ return this.items.length;
24
+ }
25
+ /** 取出并清空所有元素(返回快照) */
26
+ }, {
27
+ key: "drain",
28
+ value: function drain() {
29
+ if (!this.items.length) return [];
30
+ var copy = this.items.slice();
31
+ this.items.length = 0;
32
+ return copy;
33
+ }
34
+ }]);
35
+ return TrackQueue;
36
+ }();
@@ -0,0 +1,17 @@
1
+ import { ErrorProfile, TrackConfig } from '../../interfaces/track.interface';
2
+ export declare class ReplayManager {
3
+ private config;
4
+ private stopRecord?;
5
+ private segments;
6
+ constructor(config: TrackConfig);
7
+ start(): void;
8
+ stop(): void;
9
+ /**
10
+ * 构建随错包携带的回放事件,受条数与字节限制
11
+ */
12
+ buildAttachment(): any[] | undefined;
13
+ /**
14
+ * 在回放流中注入一条“错误”标记事件,便于后续定位
15
+ */
16
+ markError(error: ErrorProfile): void;
17
+ }
@@ -0,0 +1,117 @@
1
+ import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
2
+ import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
3
+ import _createClass from "@babel/runtime/helpers/esm/createClass";
4
+ import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
5
+ /**
6
+ * ReplayManager - 封装 rrweb 录制 & 最近片段管理
7
+ */
8
+ import { record as rrwebRecord } from '@rrweb/record';
9
+ import { getRecordConsolePlugin } from '@rrweb/rrweb-plugin-console-record';
10
+ import { DEFAULT_REPLAY_CHECKOUT_NMS, DEFAULT_REPLAY_MAX_BYTES, DEFAULT_REPLAY_MAX_EVENTS } from "./constants";
11
+ export var ReplayManager = /*#__PURE__*/function () {
12
+ // 仅保留最近两段
13
+
14
+ function ReplayManager(config) {
15
+ _classCallCheck(this, ReplayManager);
16
+ _defineProperty(this, "config", void 0);
17
+ _defineProperty(this, "stopRecord", void 0);
18
+ _defineProperty(this, "segments", [[]]);
19
+ this.config = config;
20
+ }
21
+ _createClass(ReplayManager, [{
22
+ key: "start",
23
+ value: function start() {
24
+ var _this = this;
25
+ if (!this.config.enableReplay) return;
26
+ if (typeof window === 'undefined' || typeof document === 'undefined') return;
27
+ try {
28
+ var maxEvents = DEFAULT_REPLAY_MAX_EVENTS;
29
+ var rrwebOptions = {
30
+ checkoutEveryNms: DEFAULT_REPLAY_CHECKOUT_NMS
31
+ };
32
+ this.stopRecord = rrwebRecord(_objectSpread({
33
+ emit: function emit(event, isCheckout) {
34
+ if (isCheckout) _this.segments.push([]);
35
+ var last = _this.segments[_this.segments.length - 1];
36
+ last.push(event);
37
+ if (_this.segments.length > 2) _this.segments.shift();
38
+ if (last.length > maxEvents) last.splice(0, last.length - maxEvents);
39
+ },
40
+ plugins: [getRecordConsolePlugin()]
41
+ }, rrwebOptions));
42
+ } catch (err) {
43
+ console.warn('[track:debug] replay start failed', err);
44
+ }
45
+ }
46
+ }, {
47
+ key: "stop",
48
+ value: function stop() {
49
+ try {
50
+ this.stopRecord && this.stopRecord();
51
+ this.stopRecord = undefined;
52
+ } catch (_unused) {
53
+ /* ignore */
54
+ }
55
+ }
56
+
57
+ /**
58
+ * 构建随错包携带的回放事件,受条数与字节限制
59
+ */
60
+ }, {
61
+ key: "buildAttachment",
62
+ value: function buildAttachment() {
63
+ if (!this.config.enableReplay) return undefined;
64
+ if (!this.segments.length) return undefined;
65
+ var joined = (this.segments[this.segments.length - 2] || []).concat(this.segments[this.segments.length - 1] || []);
66
+ if (!joined.length) return undefined;
67
+ var maxEvents = DEFAULT_REPLAY_MAX_EVENTS;
68
+ var sliced = joined.slice(-maxEvents);
69
+ var maxBytes = DEFAULT_REPLAY_MAX_BYTES;
70
+ var json = JSON.stringify(sliced);
71
+ if (json.length > maxBytes) {
72
+ var step = Math.max(50, Math.floor(sliced.length * 0.1));
73
+ while (json.length > maxBytes && sliced.length > step) {
74
+ sliced = sliced.slice(step);
75
+ json = JSON.stringify(sliced);
76
+ }
77
+ }
78
+ return sliced;
79
+ }
80
+
81
+ /**
82
+ * 在回放流中注入一条“错误”标记事件,便于后续定位
83
+ */
84
+ }, {
85
+ key: "markError",
86
+ value: function markError(error) {
87
+ if (!this.config.enableReplay) return;
88
+ try {
89
+ var _location, _navigator;
90
+ var event = {
91
+ // rrweb 自定义事件类型:5(EventType.Custom)。避免直接依赖枚举,使用字面量。
92
+ type: 5,
93
+ timestamp: Date.now(),
94
+ data: {
95
+ tag: 'error',
96
+ payload: {
97
+ message: error.message,
98
+ fileName: error.fileName,
99
+ href: typeof location !== 'undefined' && (_location = location) !== null && _location !== void 0 && _location.href ? location.href : '',
100
+ userAgent: typeof navigator !== 'undefined' && (_navigator = navigator) !== null && _navigator !== void 0 && _navigator.userAgent ? navigator.userAgent : ''
101
+ }
102
+ }
103
+ };
104
+ if (!this.segments.length) this.segments.push([]);
105
+ var last = this.segments[this.segments.length - 1];
106
+ last.push(event);
107
+
108
+ // 与 start() 中保持一致的长度限制
109
+ var maxEvents = DEFAULT_REPLAY_MAX_EVENTS;
110
+ if (last.length > maxEvents) last.splice(0, last.length - maxEvents);
111
+ } catch (e) {
112
+ console.warn('[track:debug] markError failed', e);
113
+ }
114
+ }
115
+ }]);
116
+ return ReplayManager;
117
+ }();
@@ -0,0 +1,6 @@
1
+ /**
2
+ * 路由辅助:基于 base 前缀规范化路径,并从 routesMap 提取模块层级
3
+ */
4
+ import { RouteMapValueType, TrackConfig } from '../../interfaces/track.interface';
5
+ export declare function getFormattedPath(config: TrackConfig): string;
6
+ export declare function getPageModules(config: TrackConfig, routesMap: Map<string, RouteMapValueType>): string[];
@@ -0,0 +1,15 @@
1
+ /**
2
+ * 路由辅助:基于 base 前缀规范化路径,并从 routesMap 提取模块层级
3
+ */
4
+
5
+ export function getFormattedPath(config) {
6
+ var _config$base = config.base,
7
+ base = _config$base === void 0 ? '/' : _config$base;
8
+ var pathname = typeof location !== 'undefined' ? location.pathname : '';
9
+ // 确保只剥离一次 base 前缀
10
+ return pathname.replace(base, '/');
11
+ }
12
+ export function getPageModules(config, routesMap) {
13
+ var ri = routesMap.get(getFormattedPath(config));
14
+ return (ri === null || ri === void 0 ? void 0 : ri.module) || [];
15
+ }
@@ -0,0 +1,14 @@
1
+ /**
2
+ * 系统/运行环境信息采集:IMEI 生成 + 视口 + 当前 URL 等。
3
+ */
4
+ import { SystemProfile } from '../../interfaces/track.interface';
5
+ /**
6
+ * 确保存在一个持久化 IMEI(匿名设备标识)
7
+ * - 优先 localStorage
8
+ * - 不可用时降级随机字符串
9
+ */
10
+ export declare function ensureIMEI(): string;
11
+ /**
12
+ * 收集系统(会话)级别指标:窗口尺寸 / URL / 时间戳 / IMEI
13
+ */
14
+ export declare function getSystemProfile(): SystemProfile;
@@ -0,0 +1,36 @@
1
+ /**
2
+ * 系统/运行环境信息采集:IMEI 生成 + 视口 + 当前 URL 等。
3
+ */
4
+
5
+ import { generateRandomString } from "../../Utils";
6
+ var IMEI_KEY = '__IMEI__';
7
+
8
+ /**
9
+ * 确保存在一个持久化 IMEI(匿名设备标识)
10
+ * - 优先 localStorage
11
+ * - 不可用时降级随机字符串
12
+ */
13
+ export function ensureIMEI() {
14
+ try {
15
+ var val = localStorage.getItem(IMEI_KEY);
16
+ if (val) return val;
17
+ var imei = generateRandomString(16);
18
+ localStorage.setItem(IMEI_KEY, imei);
19
+ return imei;
20
+ } catch (_unused) {
21
+ return generateRandomString(16);
22
+ }
23
+ }
24
+
25
+ /**
26
+ * 收集系统(会话)级别指标:窗口尺寸 / URL / 时间戳 / IMEI
27
+ */
28
+ export function getSystemProfile() {
29
+ return {
30
+ innerHeight: typeof window !== 'undefined' ? window.innerHeight : 0,
31
+ innerWidth: typeof window !== 'undefined' ? window.innerWidth : 0,
32
+ href: typeof location !== 'undefined' ? location.href : '',
33
+ systemTime: new Date(),
34
+ IMEI: ensureIMEI()
35
+ };
36
+ }
@@ -0,0 +1,18 @@
1
+ import { TrackConfig } from '../../interfaces/track.interface';
2
+ /**
3
+ * sendBeacon 包装:
4
+ * - 统一 JSON 序列化
5
+ * - debug 输出
6
+ * - 失败仅打印(不抛错,避免影响业务)
7
+ */
8
+ export declare function beaconSend(url: string, payload: object | any[]): void;
9
+ /**
10
+ * 上报当前应用版本:避免重复上报,通过 localStorage 绑定 appId 做缓存
11
+ */
12
+ export declare function reportAppVersion(config: TrackConfig): void;
13
+ /** 批量上报日志事件数组 */
14
+ export declare function reportBatch(config: TrackConfig, batch: any[]): void;
15
+ /** 上报单条 API 日志(独立通道) */
16
+ export declare function reportApiLog(config: TrackConfig, apiLog: any, envelope: any): void;
17
+ /** 上报单条错误日志到专用端点 */
18
+ export declare function reportError(config: TrackConfig, envelope: any, replayPayload?: any): Promise<void>;
@@ -0,0 +1,151 @@
1
+ import _regeneratorRuntime from "@babel/runtime/helpers/esm/regeneratorRuntime";
2
+ import _typeof from "@babel/runtime/helpers/esm/typeof";
3
+ import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
4
+ /**
5
+ * 传输层:统一封装 sendBeacon 调用与批量 / 版本上报逻辑。
6
+ */
7
+ import { version } from "../../../package.json";
8
+ import { APP_VERSION_ENDPOINT, ERROR_REPLAY_ENDPOINT, ERROR_TRACK_ENDPOINT, TRACK_ENDPOINT } from "./constants";
9
+
10
+ /**
11
+ * 包装fetch,方便async/await调用
12
+ * @param url
13
+ * @param options
14
+ * @returns
15
+ */
16
+ function fetchWrapper(_x, _x2) {
17
+ return _fetchWrapper.apply(this, arguments);
18
+ }
19
+ /**
20
+ * sendBeacon 包装:
21
+ * - 统一 JSON 序列化
22
+ * - debug 输出
23
+ * - 失败仅打印(不抛错,避免影响业务)
24
+ */
25
+ function _fetchWrapper() {
26
+ _fetchWrapper = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(url, options) {
27
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
28
+ while (1) switch (_context.prev = _context.next) {
29
+ case 0:
30
+ return _context.abrupt("return", new Promise(function (resolve, reject) {
31
+ fetch(url, options).then(function (response) {
32
+ if (!response.ok) {
33
+ throw new Error("Network response was not ok: ".concat(response.statusText));
34
+ }
35
+ return response.json();
36
+ }).then(function (data) {
37
+ return resolve(data);
38
+ }).catch(function (error) {
39
+ return reject(error);
40
+ });
41
+ }));
42
+ case 1:
43
+ case "end":
44
+ return _context.stop();
45
+ }
46
+ }, _callee);
47
+ }));
48
+ return _fetchWrapper.apply(this, arguments);
49
+ }
50
+ export function beaconSend(url, payload) {
51
+ try {
52
+ // 只允许对象或数组
53
+ if (!Array.isArray(payload) && (_typeof(payload) !== 'object' || payload === null)) {
54
+ throw new Error('Payload must be an object or array');
55
+ }
56
+ var body = new Blob([JSON.stringify(payload)], {
57
+ type: 'application/json'
58
+ });
59
+ navigator.sendBeacon(url, body);
60
+ } catch (err) {
61
+ console.warn('[track:debug] beacon failed', err, url, payload);
62
+ }
63
+ }
64
+
65
+ /**
66
+ * 上报当前应用版本:避免重复上报,通过 localStorage 绑定 appId 做缓存
67
+ */
68
+ export function reportAppVersion(config) {
69
+ var appId = config.appId,
70
+ domain = config.domain;
71
+ var storageKey = "__Track__".concat(appId);
72
+ try {
73
+ var cached = localStorage.getItem(storageKey);
74
+ if (cached) {
75
+ try {
76
+ var _ref = JSON.parse(cached),
77
+ v = _ref.v;
78
+ if (v === version) {
79
+ console.debug('[track:debug] skip version report (same)', version);
80
+ return;
81
+ }
82
+ } catch (_unused) {
83
+ /* ignore parse error */
84
+ }
85
+ }
86
+ localStorage.setItem(storageKey, JSON.stringify({
87
+ v: version,
88
+ t: new Date().toLocaleString()
89
+ }));
90
+ } catch (_unused2) {
91
+ /* storage not available */
92
+ }
93
+ beaconSend("".concat(domain).concat(APP_VERSION_ENDPOINT), {
94
+ appId: appId,
95
+ version: version
96
+ });
97
+ }
98
+
99
+ /** 批量上报日志事件数组 */
100
+ export function reportBatch(config, batch) {
101
+ if (!batch.length) return;
102
+ var domain = config.domain;
103
+ beaconSend("".concat(domain).concat(TRACK_ENDPOINT), batch);
104
+ }
105
+
106
+ /** 上报单条 API 日志(独立通道) */
107
+ export function reportApiLog(config, apiLog, envelope) {
108
+ var domain = config.domain;
109
+ beaconSend("".concat(domain).concat(TRACK_ENDPOINT), [envelope]);
110
+ }
111
+
112
+ /** 上报单条错误日志到专用端点 */
113
+ export function reportError(_x3, _x4, _x5) {
114
+ return _reportError.apply(this, arguments);
115
+ }
116
+ function _reportError() {
117
+ _reportError = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(config, envelope, replayPayload) {
118
+ var domain, _yield$fetchWrapper, id;
119
+ return _regeneratorRuntime().wrap(function _callee2$(_context2) {
120
+ while (1) switch (_context2.prev = _context2.next) {
121
+ case 0:
122
+ domain = config.domain;
123
+ _context2.next = 3;
124
+ return fetchWrapper("".concat(domain).concat(ERROR_TRACK_ENDPOINT), {
125
+ method: 'POST',
126
+ headers: {
127
+ 'Content-Type': 'application/json'
128
+ },
129
+ body: JSON.stringify(envelope)
130
+ });
131
+ case 3:
132
+ _yield$fetchWrapper = _context2.sent;
133
+ id = _yield$fetchWrapper.data;
134
+ if (id && replayPayload) {
135
+ // 将replayPayload以文本方式上传,不经过序列化
136
+ fetchWrapper("".concat(domain).concat(ERROR_REPLAY_ENDPOINT).concat(id), {
137
+ method: 'POST',
138
+ headers: {
139
+ 'Content-Type': 'text/plain'
140
+ },
141
+ body: JSON.stringify(replayPayload)
142
+ });
143
+ }
144
+ case 6:
145
+ case "end":
146
+ return _context2.stop();
147
+ }
148
+ }, _callee2);
149
+ }));
150
+ return _reportError.apply(this, arguments);
151
+ }
@@ -1,29 +1,32 @@
1
- import { AuthCenterUserProfile, CustomizeProfile, ErrorProfile, RouteMapValueType, SystemProfile, TrackConfig, TrackType, UserProfile } from '../interfaces/track.interface';
2
- declare class Track {
3
- static config: TrackConfig;
4
- static userProfile: UserProfile;
5
- static authCenterUserProfile: AuthCenterUserProfile;
6
- static routesMap: Map<string, RouteMapValueType>;
7
- static authMap: Map<string, string>;
8
- static trackQueue: any[];
9
- static checkConfig(config?: TrackConfig): void;
10
- static send(): Promise<void>;
11
- static mountAutoTask(): void;
12
- static getSystemProfile(): SystemProfile;
13
- static getUserProfile(): UserProfile;
14
- static setUserProfile(userProfile: UserProfile): void;
15
- static initAuthCenterData({ getAuth, menuBuild, getDataScopeZonePermissions, menuNameMap, routes, }: Pick<TrackConfig, 'getAuth' | 'menuBuild' | 'menuNameMap' | 'routes' | 'getDataScopeZonePermissions'>): Promise<AuthCenterUserProfile>;
16
- static init({ userProfile, menuNameMap, routes, getAuth, menuBuild, getDataScopeZonePermissions, ...restConfig }: TrackConfig): Promise<AuthCenterUserProfile>;
17
- static registerAppInfo(): void;
18
- static pushQueue({ type, log, }: {
19
- type: TrackType;
20
- log: ErrorProfile | CustomizeProfile;
21
- }): void;
22
- static getFormatBasePath(): string;
23
- static getPageModuleName(): string[];
24
- static errorLog(errorLog: ErrorProfile): void;
25
- static eventLog(eventLog: CustomizeProfile | string): void;
26
- static visitLog(visitLog?: CustomizeProfile): void;
27
- static apiLog(apiLog: any): void;
1
+ /**
2
+ * 新版多实例埋点入口
3
+ * 提供统一 createTracker(config, { lazy }) API
4
+ */
5
+ import { TrackConfig } from '../interfaces/track.interface';
6
+ import { TrackerInstance } from './core/instance';
7
+ export type { TrackerInstance };
8
+ interface CreateTrackerOptions {
9
+ lazy?: boolean;
28
10
  }
29
- export default Track;
11
+ /**
12
+ * 创建埋点实例
13
+ * @param config TrackConfig
14
+ * @param options { lazy } 是否延迟 init(默认 false)
15
+ * - lazy=false: 返回 Promise<TrackerInstance>(已完成 init)
16
+ * - lazy=true: 返回未初始化实例 (TrackerInstance),需外部手动调用 init()
17
+ */
18
+ export declare function createTracker(config: TrackConfig, options?: CreateTrackerOptions): Promise<TrackerInstance> | TrackerInstance;
19
+ declare const _default: {
20
+ init: (config: TrackConfig) => Promise<TrackerInstance>;
21
+ isReady: () => boolean;
22
+ getInstance: () => TrackerInstance;
23
+ eventLog: TrackerInstance['eventLog'];
24
+ visitLog: TrackerInstance['visitLog'];
25
+ errorLog: TrackerInstance['errorLog'];
26
+ apiLog: TrackerInstance['apiLog'];
27
+ flush: () => void;
28
+ destroy: () => void;
29
+ setUserProfile: (profile: Parameters<TrackerInstance['setUserProfile']>[0]) => void;
30
+ getConfig: () => TrackConfig | undefined;
31
+ };
32
+ export default _default;