@whnz/frontend-experience-core 1.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.
@@ -0,0 +1,33 @@
1
+ type Env = 'prod' | 'staging' | 'dev';
2
+ type ExperienceEventType = 'fcp' | 'lcp' | 'cls' | 'inp' | 'longtask' | 'route' | 'react-render' | 'js-error' | 'promise-error' | 'react-error' | 'white-screen' | 'css-visibility-error' | 'css-covered' | string;
3
+ interface ExperienceEvent {
4
+ type: ExperienceEventType;
5
+ timestamp: number;
6
+ value?: number;
7
+ duration?: number;
8
+ extra?: Record<string, any>;
9
+ }
10
+ interface ExperienceContext {
11
+ app: string;
12
+ version: string;
13
+ env: Env;
14
+ route: string;
15
+ userId?: string;
16
+ }
17
+ interface InitCoreOptions {
18
+ app: string;
19
+ version: string;
20
+ env: Env;
21
+ reporter: (event: ExperienceEvent & ExperienceContext) => void;
22
+ sampleRate?: number;
23
+ }
24
+
25
+ declare function initCore(options: InitCoreOptions): void;
26
+
27
+ declare function report(event: ExperienceEvent): void;
28
+
29
+ declare function setRoute(route: string): void;
30
+ declare function setUserId(userId: string): void;
31
+ declare function getContext(): ExperienceContext;
32
+
33
+ export { type Env, type ExperienceContext, type ExperienceEvent, type ExperienceEventType, type InitCoreOptions, getContext, initCore, report, setRoute, setUserId };
package/dist/index.mjs ADDED
@@ -0,0 +1,68 @@
1
+ // src/context.ts
2
+ var context = {
3
+ app: "",
4
+ version: "",
5
+ env: "prod",
6
+ route: "/"
7
+ };
8
+ function initContext(partial) {
9
+ Object.assign(context, partial);
10
+ }
11
+ function setRoute(route) {
12
+ context.route = route;
13
+ }
14
+ function setUserId(userId) {
15
+ context.userId = userId;
16
+ }
17
+ function getContext() {
18
+ return { ...context };
19
+ }
20
+
21
+ // src/sampler.ts
22
+ var sampleRate = 1;
23
+ function initSampler(rate = 1) {
24
+ sampleRate = rate;
25
+ }
26
+ function shouldSample() {
27
+ if (sampleRate >= 1) return true;
28
+ return Math.random() < sampleRate;
29
+ }
30
+
31
+ // src/reporter.ts
32
+ var reporterFn = null;
33
+ function initReporter(fn) {
34
+ reporterFn = fn;
35
+ }
36
+ function report(event) {
37
+ if (!reporterFn) return;
38
+ if (!shouldSample()) return;
39
+ const payload = {
40
+ ...event,
41
+ ...getContext(),
42
+ timestamp: event.timestamp || Date.now()
43
+ };
44
+ try {
45
+ reporterFn(payload);
46
+ } catch (e) {
47
+ }
48
+ }
49
+
50
+ // src/init.ts
51
+ var inited = false;
52
+ function initCore(options) {
53
+ var _a;
54
+ if (inited) return;
55
+ inited = true;
56
+ initContext({
57
+ app: options.app,
58
+ version: options.version,
59
+ env: options.env,
60
+ route: location.pathname
61
+ });
62
+ initReporter(options.reporter);
63
+ initSampler((_a = options.sampleRate) != null ? _a : 1);
64
+ }
65
+
66
+ export { getContext, initCore, report, setRoute, setUserId };
67
+ //# sourceMappingURL=index.mjs.map
68
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/context.ts","../src/sampler.ts","../src/reporter.ts","../src/init.ts"],"names":[],"mappings":";AACA,IAAM,OAAA,GAA6B;AAAA,EACjC,GAAA,EAAK,EAAA;AAAA,EACL,OAAA,EAAS,EAAA;AAAA,EACT,GAAA,EAAK,MAAA;AAAA,EACL,KAAA,EAAO;AACT,CAAA;AACO,SAAS,YAAY,OAAA,EAAqC;AAC/D,EAAA,MAAA,CAAO,MAAA,CAAO,SAAS,OAAO,CAAA;AAChC;AACO,SAAS,SAAS,KAAA,EAAe;AACtC,EAAA,OAAA,CAAQ,KAAA,GAAQ,KAAA;AAClB;AACO,SAAS,UAAU,MAAA,EAAgB;AACxC,EAAA,OAAA,CAAQ,MAAA,GAAS,MAAA;AACnB;AACO,SAAS,UAAA,GAAgC;AAC9C,EAAA,OAAO,EAAE,GAAG,OAAA,EAAQ;AACtB;;;AClBA,IAAI,UAAA,GAAa,CAAA;AACV,SAAS,WAAA,CAAY,OAAO,CAAA,EAAG;AACpC,EAAA,UAAA,GAAa,IAAA;AACf;AACO,SAAS,YAAA,GAAwB;AACtC,EAAA,IAAI,UAAA,IAAc,GAAG,OAAO,IAAA;AAC5B,EAAA,OAAO,IAAA,CAAK,QAAO,GAAI,UAAA;AACzB;;;ACJA,IAAI,UAAA,GAA4C,IAAA;AACzC,SAAS,aAAa,EAAA,EAA0B;AACrD,EAAA,UAAA,GAAa,EAAA;AACf;AACO,SAAS,OAAO,KAAA,EAAwB;AAC7C,EAAA,IAAI,CAAC,UAAA,EAAY;AACjB,EAAA,IAAI,CAAC,cAAa,EAAG;AACrB,EAAA,MAAM,OAAA,GAAU;AAAA,IACd,GAAG,KAAA;AAAA,IACH,GAAG,UAAA,EAAW;AAAA,IACd,SAAA,EAAW,KAAA,CAAM,SAAA,IAAa,IAAA,CAAK,GAAA;AAAI,GACzC;AACA,EAAA,IAAI;AACF,IAAA,UAAA,CAAW,OAAO,CAAA;AAAA,EACpB,SAAS,CAAA,EAAG;AAAA,EAEZ;AACF;;;AChBA,IAAI,MAAA,GAAS,KAAA;AACN,SAAS,SAAS,OAAA,EAA0B;AALnD,EAAA,IAAA,EAAA;AAME,EAAA,IAAI,MAAA,EAAQ;AACZ,EAAA,MAAA,GAAS,IAAA;AACT,EAAA,WAAA,CAAY;AAAA,IACV,KAAK,OAAA,CAAQ,GAAA;AAAA,IACb,SAAS,OAAA,CAAQ,OAAA;AAAA,IACjB,KAAK,OAAA,CAAQ,GAAA;AAAA,IACb,OAAO,QAAA,CAAS;AAAA,GACjB,CAAA;AACD,EAAA,YAAA,CAAa,QAAQ,QAAQ,CAAA;AAC7B,EAAA,WAAA,CAAA,CAAY,EAAA,GAAA,OAAA,CAAQ,UAAA,KAAR,IAAA,GAAA,EAAA,GAAsB,CAAC,CAAA;AACrC","file":"index.mjs","sourcesContent":["import { ExperienceContext } from './types';\nconst context: ExperienceContext = {\n app: '',\n version: '',\n env: 'prod',\n route: '/',\n};\nexport function initContext(partial: Partial<ExperienceContext>) {\n Object.assign(context, partial);\n}\nexport function setRoute(route: string) {\n context.route = route;\n}\nexport function setUserId(userId: string) {\n context.userId = userId;\n}\nexport function getContext(): ExperienceContext {\n return { ...context };\n}\n","let sampleRate = 1;\nexport function initSampler(rate = 1) {\n sampleRate = rate;\n}\nexport function shouldSample(): boolean {\n if (sampleRate >= 1) return true;\n return Math.random() < sampleRate;\n}\n","import { ExperienceEvent } from './types';\nimport { getContext } from './context';\nimport { shouldSample } from './sampler';\nlet reporterFn: ((event: any) => void) | null = null;\nexport function initReporter(fn: (event: any) => void) {\n reporterFn = fn;\n}\nexport function report(event: ExperienceEvent) {\n if (!reporterFn) return;\n if (!shouldSample()) return;\n const payload = {\n ...event,\n ...getContext(),\n timestamp: event.timestamp || Date.now(),\n };\n try {\n reporterFn(payload);\n } catch (e) {\n // core 永远不能抛异常\n }\n}\n","import { InitCoreOptions } from './types';\nimport { initContext } from './context';\nimport { initReporter } from './reporter';\nimport { initSampler } from './sampler';\nlet inited = false;\nexport function initCore(options: InitCoreOptions) {\n if (inited) return;\n inited = true;\n initContext({\n app: options.app,\n version: options.version,\n env: options.env,\n route: location.pathname,\n });\n initReporter(options.reporter);\n initSampler(options.sampleRate ?? 1);\n}\n"]}
package/package.json ADDED
@@ -0,0 +1,13 @@
1
+ {
2
+ "name": "@whnz/frontend-experience-core",
3
+ "version": "1.0.0",
4
+ "main": "dist/index.js",
5
+ "types": "dist/index.d.ts",
6
+ "sideEffects": false,
7
+ "files": [
8
+ "dist"
9
+ ],
10
+ "scripts": {
11
+ "build": "tsup"
12
+ }
13
+ }