kmind-apm-web-react 0.1.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/dist/index.d.ts +22 -0
- package/dist/index.js +37 -0
- package/package.json +12 -0
- package/src/index.tsx +16 -0
- package/tsconfig.json +1 -0
- package/tsup.config.ts +2 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { Component, ReactNode, ErrorInfo } from 'react';
|
|
2
|
+
import { KmindConfig } from 'kmind-apm-web';
|
|
3
|
+
|
|
4
|
+
declare function initKmindReact(config?: Partial<KmindConfig>): void;
|
|
5
|
+
declare function useKmindRoute(route: string): void;
|
|
6
|
+
declare class KmindErrorBoundary extends Component<{
|
|
7
|
+
children: ReactNode;
|
|
8
|
+
fallback?: ReactNode;
|
|
9
|
+
}, {
|
|
10
|
+
failed: boolean;
|
|
11
|
+
}> {
|
|
12
|
+
state: {
|
|
13
|
+
failed: boolean;
|
|
14
|
+
};
|
|
15
|
+
static getDerivedStateFromError(): {
|
|
16
|
+
failed: boolean;
|
|
17
|
+
};
|
|
18
|
+
componentDidCatch(error: Error, info: ErrorInfo): void;
|
|
19
|
+
render(): ReactNode;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export { KmindErrorBoundary, initKmindReact, useKmindRoute };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
// src/index.tsx
|
|
2
|
+
import { Component, useEffect, useRef } from "react";
|
|
3
|
+
import { init, logger, recordMetric } from "kmind-apm-web";
|
|
4
|
+
function viteEnv() {
|
|
5
|
+
return import.meta.env ?? {};
|
|
6
|
+
}
|
|
7
|
+
function initKmindReact(config = {}) {
|
|
8
|
+
const env = viteEnv();
|
|
9
|
+
init({ serviceName: config.serviceName ?? env.VITE_KMIND_SERVICE_NAME ?? "react-app", clientKey: config.clientKey ?? env.VITE_KMIND_CLIENT_KEY ?? "", endpoint: config.endpoint ?? env.VITE_KMIND_ENDPOINT, statusEndpoint: config.statusEndpoint ?? env.VITE_KMIND_STATUS_ENDPOINT, ...config });
|
|
10
|
+
}
|
|
11
|
+
function useKmindRoute(route) {
|
|
12
|
+
const previous = useRef(void 0);
|
|
13
|
+
useEffect(() => {
|
|
14
|
+
if (previous.current !== void 0 && previous.current !== route) recordMetric("navigation.route_change", 1, { route });
|
|
15
|
+
previous.current = route;
|
|
16
|
+
}, [route]);
|
|
17
|
+
}
|
|
18
|
+
var KmindErrorBoundary = class extends Component {
|
|
19
|
+
constructor() {
|
|
20
|
+
super(...arguments);
|
|
21
|
+
this.state = { failed: false };
|
|
22
|
+
}
|
|
23
|
+
static getDerivedStateFromError() {
|
|
24
|
+
return { failed: true };
|
|
25
|
+
}
|
|
26
|
+
componentDidCatch(error, info) {
|
|
27
|
+
logger.error(error.message, { "exception.stacktrace": error.stack ?? "", "react.component_stack": info.componentStack });
|
|
28
|
+
}
|
|
29
|
+
render() {
|
|
30
|
+
return this.state.failed ? this.props.fallback ?? null : this.props.children;
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
export {
|
|
34
|
+
KmindErrorBoundary,
|
|
35
|
+
initKmindReact,
|
|
36
|
+
useKmindRoute
|
|
37
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "kmind-apm-web-react",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"exports": { ".": { "types": "./dist/index.d.ts", "import": "./dist/index.js" } },
|
|
8
|
+
"scripts": { "build": "tsup" },
|
|
9
|
+
"dependencies": { "kmind-apm-web": "*" },
|
|
10
|
+
"peerDependencies": { "react": ">=18" },
|
|
11
|
+
"devDependencies": { "@types/react": "^19.1.2", "react": "^19.1.0", "tsup": "^8.5.0", "typescript": "^5.8.3" }
|
|
12
|
+
}
|
package/src/index.tsx
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { Component, type ErrorInfo, type ReactNode, useEffect, useRef } from "react";
|
|
2
|
+
import { init, logger, recordMetric, type KmindConfig } from "kmind-apm-web";
|
|
3
|
+
|
|
4
|
+
type PublicEnv = Record<string, string | undefined>;
|
|
5
|
+
function viteEnv(): PublicEnv { return (import.meta as ImportMeta & { env?: PublicEnv }).env ?? {}; }
|
|
6
|
+
export function initKmindReact(config: Partial<KmindConfig> = {}): void {
|
|
7
|
+
const env = viteEnv();
|
|
8
|
+
init({ serviceName: config.serviceName ?? env.VITE_KMIND_SERVICE_NAME ?? "react-app", clientKey: config.clientKey ?? env.VITE_KMIND_CLIENT_KEY ?? "", endpoint: config.endpoint ?? env.VITE_KMIND_ENDPOINT, statusEndpoint: config.statusEndpoint ?? env.VITE_KMIND_STATUS_ENDPOINT, ...config });
|
|
9
|
+
}
|
|
10
|
+
export function useKmindRoute(route: string): void { const previous = useRef<string | undefined>(undefined); useEffect(() => { if (previous.current !== undefined && previous.current !== route) recordMetric("navigation.route_change", 1, { route }); previous.current = route; }, [route]); }
|
|
11
|
+
export class KmindErrorBoundary extends Component<{ children: ReactNode; fallback?: ReactNode }, { failed: boolean }> {
|
|
12
|
+
state = { failed: false };
|
|
13
|
+
static getDerivedStateFromError() { return { failed: true }; }
|
|
14
|
+
componentDidCatch(error: Error, info: ErrorInfo) { logger.error(error.message, { "exception.stacktrace": error.stack ?? "", "react.component_stack": info.componentStack }); }
|
|
15
|
+
render() { return this.state.failed ? this.props.fallback ?? null : this.props.children; }
|
|
16
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{ "compilerOptions": { "target": "ES2020", "module": "ESNext", "moduleResolution": "Bundler", "strict": true, "jsx": "react-jsx", "declaration": true, "lib": ["DOM", "ES2020"], "skipLibCheck": true }, "include": ["src"] }
|
package/tsup.config.ts
ADDED