@yysports/tools 1.1.0 → 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,53 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+
19
+ // src/Track/core/system.ts
20
+ var system_exports = {};
21
+ __export(system_exports, {
22
+ ensureIMEI: () => ensureIMEI,
23
+ getSystemProfile: () => getSystemProfile
24
+ });
25
+ module.exports = __toCommonJS(system_exports);
26
+ var import_Utils = require("../../Utils");
27
+ var IMEI_KEY = "__IMEI__";
28
+ function ensureIMEI() {
29
+ try {
30
+ const val = localStorage.getItem(IMEI_KEY);
31
+ if (val)
32
+ return val;
33
+ const imei = (0, import_Utils.generateRandomString)(16);
34
+ localStorage.setItem(IMEI_KEY, imei);
35
+ return imei;
36
+ } catch {
37
+ return (0, import_Utils.generateRandomString)(16);
38
+ }
39
+ }
40
+ function getSystemProfile() {
41
+ return {
42
+ innerHeight: typeof window !== "undefined" ? window.innerHeight : 0,
43
+ innerWidth: typeof window !== "undefined" ? window.innerWidth : 0,
44
+ href: typeof location !== "undefined" ? location.href : "",
45
+ systemTime: /* @__PURE__ */ new Date(),
46
+ IMEI: ensureIMEI()
47
+ };
48
+ }
49
+ // Annotate the CommonJS export names for ESM import in node:
50
+ 0 && (module.exports = {
51
+ ensureIMEI,
52
+ getSystemProfile
53
+ });
@@ -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,115 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+
19
+ // src/Track/core/transport.ts
20
+ var transport_exports = {};
21
+ __export(transport_exports, {
22
+ beaconSend: () => beaconSend,
23
+ reportApiLog: () => reportApiLog,
24
+ reportAppVersion: () => reportAppVersion,
25
+ reportBatch: () => reportBatch,
26
+ reportError: () => reportError
27
+ });
28
+ module.exports = __toCommonJS(transport_exports);
29
+ var import_package = require("../../../package.json");
30
+ var import_constants = require("./constants");
31
+ async function fetchWrapper(url, options) {
32
+ return new Promise((resolve, reject) => {
33
+ fetch(url, options).then((response) => {
34
+ if (!response.ok) {
35
+ throw new Error(
36
+ `Network response was not ok: ${response.statusText}`
37
+ );
38
+ }
39
+ return response.json();
40
+ }).then((data) => resolve(data)).catch((error) => reject(error));
41
+ });
42
+ }
43
+ function beaconSend(url, payload) {
44
+ try {
45
+ if (!Array.isArray(payload) && (typeof payload !== "object" || payload === null)) {
46
+ throw new Error("Payload must be an object or array");
47
+ }
48
+ const body = new Blob([JSON.stringify(payload)], {
49
+ type: "application/json"
50
+ });
51
+ navigator.sendBeacon(url, body);
52
+ } catch (err) {
53
+ console.warn("[track:debug] beacon failed", err, url, payload);
54
+ }
55
+ }
56
+ function reportAppVersion(config) {
57
+ const { appId, domain } = config;
58
+ const storageKey = `__Track__${appId}`;
59
+ try {
60
+ const cached = localStorage.getItem(storageKey);
61
+ if (cached) {
62
+ try {
63
+ const { v } = JSON.parse(cached);
64
+ if (v === import_package.version) {
65
+ console.debug("[track:debug] skip version report (same)", import_package.version);
66
+ return;
67
+ }
68
+ } catch {
69
+ }
70
+ }
71
+ localStorage.setItem(
72
+ storageKey,
73
+ JSON.stringify({ v: import_package.version, t: (/* @__PURE__ */ new Date()).toLocaleString() })
74
+ );
75
+ } catch {
76
+ }
77
+ beaconSend(`${domain}${import_constants.APP_VERSION_ENDPOINT}`, { appId, version: import_package.version });
78
+ }
79
+ function reportBatch(config, batch) {
80
+ if (!batch.length)
81
+ return;
82
+ const { domain } = config;
83
+ beaconSend(`${domain}${import_constants.TRACK_ENDPOINT}`, batch);
84
+ }
85
+ function reportApiLog(config, apiLog, envelope) {
86
+ const { domain } = config;
87
+ beaconSend(`${domain}${import_constants.TRACK_ENDPOINT}`, [envelope]);
88
+ }
89
+ async function reportError(config, envelope, replayPayload) {
90
+ const { domain } = config;
91
+ const { data: id } = await fetchWrapper(`${domain}${import_constants.ERROR_TRACK_ENDPOINT}`, {
92
+ method: "POST",
93
+ headers: {
94
+ "Content-Type": "application/json"
95
+ },
96
+ body: JSON.stringify(envelope)
97
+ });
98
+ if (id && replayPayload) {
99
+ fetchWrapper(`${domain}${import_constants.ERROR_REPLAY_ENDPOINT}${id}`, {
100
+ method: "POST",
101
+ headers: {
102
+ "Content-Type": "text/plain"
103
+ },
104
+ body: JSON.stringify(replayPayload)
105
+ });
106
+ }
107
+ }
108
+ // Annotate the CommonJS export names for ESM import in node:
109
+ 0 && (module.exports = {
110
+ beaconSend,
111
+ reportApiLog,
112
+ reportAppVersion,
113
+ reportBatch,
114
+ reportError
115
+ });
@@ -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;