usetraceforge 0.1.4
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 +67 -0
- package/dist/index.d.ts +33 -0
- package/dist/index.js +111 -0
- package/package.json +52 -0
package/README.md
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
# traceforge
|
|
2
|
+
|
|
3
|
+
TraceForge JavaScript SDK for sending errors to your TraceForge backend.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install traceforge
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Local pack test
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
cd packages/sdk
|
|
15
|
+
npm pack
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
Then in any local app:
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
npm install /path/to/traceforge-0.1.0.tgz
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## Usage
|
|
25
|
+
```ts
|
|
26
|
+
import TraceForge from "traceforge";
|
|
27
|
+
|
|
28
|
+
TraceForge.init({
|
|
29
|
+
apiKey: "YOUR_PROJECT_API_KEY",
|
|
30
|
+
endpoint: "http://localhost:3001/ingest",
|
|
31
|
+
autoCapture: true,
|
|
32
|
+
environment: "development",
|
|
33
|
+
release: "web@1.0.0"
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
try {
|
|
37
|
+
throw new Error("Something broke");
|
|
38
|
+
} catch (error) {
|
|
39
|
+
TraceForge.captureException(error, {
|
|
40
|
+
payload: { route: "/signup" }
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## Options
|
|
46
|
+
- `autoCapture`: Listen to `window.onerror` and `window.onunhandledrejection`.
|
|
47
|
+
- `ignoreErrors`: Array of strings or regex to skip noisy errors.
|
|
48
|
+
- `beforeSend`: Hook to modify/drop events before sending.
|
|
49
|
+
- `release`: Stable release tag like `web@1.0.0` or `api@2.3.1`.
|
|
50
|
+
|
|
51
|
+
## Publish
|
|
52
|
+
|
|
53
|
+
```bash
|
|
54
|
+
cd packages/sdk
|
|
55
|
+
npm login
|
|
56
|
+
npm run build
|
|
57
|
+
npm version patch
|
|
58
|
+
npm publish --access public
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
After publish, consumers can install it with:
|
|
62
|
+
|
|
63
|
+
```bash
|
|
64
|
+
npm install traceforge
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
If the unscoped npm name is unavailable, publish under a scoped package name and update the install line to match.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
type TraceForgeConfig = {
|
|
2
|
+
apiKey: string;
|
|
3
|
+
endpoint?: string;
|
|
4
|
+
autoCapture?: boolean;
|
|
5
|
+
environment?: string;
|
|
6
|
+
release?: string;
|
|
7
|
+
tags?: Record<string, string>;
|
|
8
|
+
ignoreErrors?: Array<string | RegExp>;
|
|
9
|
+
beforeSend?: (event: TraceForgeEvent) => TraceForgeEvent | null;
|
|
10
|
+
};
|
|
11
|
+
type CapturePayload = {
|
|
12
|
+
environment?: string;
|
|
13
|
+
release?: string;
|
|
14
|
+
payload?: Record<string, unknown>;
|
|
15
|
+
tags?: Record<string, string>;
|
|
16
|
+
};
|
|
17
|
+
type TraceForgeEvent = {
|
|
18
|
+
message: string;
|
|
19
|
+
stackTrace: string;
|
|
20
|
+
environment?: string;
|
|
21
|
+
release?: string;
|
|
22
|
+
payload?: Record<string, unknown>;
|
|
23
|
+
tags?: Record<string, string>;
|
|
24
|
+
};
|
|
25
|
+
declare const TraceForge: {
|
|
26
|
+
init: (options: TraceForgeConfig) => void;
|
|
27
|
+
captureException: (error: unknown, extras?: CapturePayload) => Promise<void>;
|
|
28
|
+
};
|
|
29
|
+
declare const init: (options: TraceForgeConfig) => void;
|
|
30
|
+
declare const captureException: (error: unknown, extras?: CapturePayload) => Promise<void>;
|
|
31
|
+
export default TraceForge;
|
|
32
|
+
export { init, captureException };
|
|
33
|
+
export type { TraceForgeConfig, CapturePayload, TraceForgeEvent };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
let config = null;
|
|
2
|
+
let autoCaptureInitialized = false;
|
|
3
|
+
const defaultEndpoint = "http://localhost:3001/ingest";
|
|
4
|
+
const ensureConfig = () => {
|
|
5
|
+
if (!config) {
|
|
6
|
+
throw new Error("TraceForge not initialized. Call TraceForge.init({ apiKey }) first.");
|
|
7
|
+
}
|
|
8
|
+
};
|
|
9
|
+
const normalizeError = (error) => {
|
|
10
|
+
if (error instanceof Error) {
|
|
11
|
+
return {
|
|
12
|
+
message: error.message,
|
|
13
|
+
stackTrace: error.stack || ""
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
if (typeof error === "string") {
|
|
17
|
+
return {
|
|
18
|
+
message: error,
|
|
19
|
+
stackTrace: ""
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
return {
|
|
23
|
+
message: "Unknown error",
|
|
24
|
+
stackTrace: JSON.stringify(error)
|
|
25
|
+
};
|
|
26
|
+
};
|
|
27
|
+
const shouldIgnore = (message) => {
|
|
28
|
+
const ignoreList = config?.ignoreErrors || [];
|
|
29
|
+
return ignoreList.some((entry) => {
|
|
30
|
+
if (typeof entry === "string") {
|
|
31
|
+
return message.includes(entry);
|
|
32
|
+
}
|
|
33
|
+
return entry.test(message);
|
|
34
|
+
});
|
|
35
|
+
};
|
|
36
|
+
const buildEvent = (error, extras) => {
|
|
37
|
+
const { message, stackTrace } = normalizeError(error);
|
|
38
|
+
return {
|
|
39
|
+
message,
|
|
40
|
+
stackTrace,
|
|
41
|
+
environment: extras?.environment || config?.environment,
|
|
42
|
+
release: extras?.release || config?.release,
|
|
43
|
+
payload: extras?.payload,
|
|
44
|
+
tags: { ...config?.tags, ...extras?.tags }
|
|
45
|
+
};
|
|
46
|
+
};
|
|
47
|
+
const sendEvent = async (event) => {
|
|
48
|
+
if (shouldIgnore(event.message)) {
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
const processed = config?.beforeSend ? config.beforeSend(event) : event;
|
|
52
|
+
if (!processed) {
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
await fetch(config.endpoint || defaultEndpoint, {
|
|
56
|
+
method: "POST",
|
|
57
|
+
headers: {
|
|
58
|
+
"Content-Type": "application/json",
|
|
59
|
+
"X-Traceforge-Key": config.apiKey
|
|
60
|
+
},
|
|
61
|
+
body: JSON.stringify(processed)
|
|
62
|
+
});
|
|
63
|
+
};
|
|
64
|
+
const capture = async (error, extras) => {
|
|
65
|
+
ensureConfig();
|
|
66
|
+
const event = buildEvent(error, extras);
|
|
67
|
+
await sendEvent(event);
|
|
68
|
+
};
|
|
69
|
+
const setupAutoCapture = () => {
|
|
70
|
+
if (autoCaptureInitialized)
|
|
71
|
+
return;
|
|
72
|
+
if (typeof window === "undefined")
|
|
73
|
+
return;
|
|
74
|
+
window.addEventListener("error", (event) => {
|
|
75
|
+
if (event.error) {
|
|
76
|
+
capture(event.error, { environment: "browser" }).catch(() => undefined);
|
|
77
|
+
}
|
|
78
|
+
else if (event.message) {
|
|
79
|
+
capture(new Error(event.message), { environment: "browser" }).catch(() => undefined);
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
window.addEventListener("unhandledrejection", (event) => {
|
|
83
|
+
capture(event.reason ?? new Error("Unhandled promise rejection"), {
|
|
84
|
+
environment: "browser"
|
|
85
|
+
}).catch(() => undefined);
|
|
86
|
+
});
|
|
87
|
+
autoCaptureInitialized = true;
|
|
88
|
+
};
|
|
89
|
+
const TraceForge = {
|
|
90
|
+
init: (options) => {
|
|
91
|
+
config = {
|
|
92
|
+
endpoint: defaultEndpoint,
|
|
93
|
+
autoCapture: false,
|
|
94
|
+
...options
|
|
95
|
+
};
|
|
96
|
+
if (config.autoCapture) {
|
|
97
|
+
setupAutoCapture();
|
|
98
|
+
}
|
|
99
|
+
},
|
|
100
|
+
captureException: async (error, extras) => {
|
|
101
|
+
await capture(error, extras);
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
const init = (options) => {
|
|
105
|
+
TraceForge.init(options);
|
|
106
|
+
};
|
|
107
|
+
const captureException = async (error, extras) => {
|
|
108
|
+
await TraceForge.captureException(error, extras);
|
|
109
|
+
};
|
|
110
|
+
export default TraceForge;
|
|
111
|
+
export { init, captureException };
|
package/package.json
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "usetraceforge",
|
|
3
|
+
"version": "0.1.4",
|
|
4
|
+
"private": false,
|
|
5
|
+
"description": "TraceForge JavaScript SDK for sending errors to a TraceForge ingest endpoint.",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"main": "dist/index.js",
|
|
8
|
+
"module": "dist/index.js",
|
|
9
|
+
"types": "dist/index.d.ts",
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "https://github.com/khushalp2004/TraceForge.git",
|
|
13
|
+
"directory": "packages/sdk"
|
|
14
|
+
},
|
|
15
|
+
"homepage": "https://usetraceforge.com/docs",
|
|
16
|
+
"bugs": {
|
|
17
|
+
"url": "https://github.com/khushalp2004/TraceForge/issues"
|
|
18
|
+
},
|
|
19
|
+
"keywords": [
|
|
20
|
+
"traceforge",
|
|
21
|
+
"monitoring",
|
|
22
|
+
"error-tracking",
|
|
23
|
+
"observability",
|
|
24
|
+
"javascript",
|
|
25
|
+
"sdk"
|
|
26
|
+
],
|
|
27
|
+
"exports": {
|
|
28
|
+
".": {
|
|
29
|
+
"types": "./dist/index.d.ts",
|
|
30
|
+
"import": "./dist/index.js"
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
"files": [
|
|
34
|
+
"dist"
|
|
35
|
+
],
|
|
36
|
+
"scripts": {
|
|
37
|
+
"build": "npx tsc -p tsconfig.json",
|
|
38
|
+
"dev": "tsx src/index.ts",
|
|
39
|
+
"prepack": "npm run build",
|
|
40
|
+
"publish:public": "npm publish --access public"
|
|
41
|
+
},
|
|
42
|
+
"license": "MIT",
|
|
43
|
+
"sideEffects": false,
|
|
44
|
+
"publishConfig": {
|
|
45
|
+
"access": "public"
|
|
46
|
+
},
|
|
47
|
+
"dependencies": {},
|
|
48
|
+
"devDependencies": {
|
|
49
|
+
"typescript": "^5.5.4",
|
|
50
|
+
"tsx": "^4.16.2"
|
|
51
|
+
}
|
|
52
|
+
}
|