heimdall-sdk 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 +8 -0
- package/dist/index.js +12 -0
- package/dist/interceptor.d.ts +7 -0
- package/dist/interceptor.js +92 -0
- package/package.json +20 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { type ReactNode } from 'react';
|
|
2
|
+
import { type HeimdallConfig } from './interceptor.js';
|
|
3
|
+
interface HeimdallProviderProps extends HeimdallConfig {
|
|
4
|
+
children: ReactNode;
|
|
5
|
+
enabled?: boolean;
|
|
6
|
+
}
|
|
7
|
+
export declare function HeimdallProvider({ children, collectorUrl, project, ignore, enabled, }: HeimdallProviderProps): import("react/jsx-runtime").JSX.Element;
|
|
8
|
+
export type { HeimdallConfig };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Fragment as _Fragment, jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { useEffect } from 'react';
|
|
3
|
+
import { patchFetch, unpatchFetch } from './interceptor.js';
|
|
4
|
+
export function HeimdallProvider({ children, collectorUrl, project = 'default', ignore = [], enabled = true, }) {
|
|
5
|
+
useEffect(() => {
|
|
6
|
+
if (!enabled)
|
|
7
|
+
return;
|
|
8
|
+
patchFetch({ collectorUrl, project, ignore });
|
|
9
|
+
return () => unpatchFetch();
|
|
10
|
+
}, [collectorUrl, project, enabled]); // eslint-disable-line react-hooks/exhaustive-deps
|
|
11
|
+
return _jsx(_Fragment, { children: children });
|
|
12
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
function shouldIgnore(url, patterns) {
|
|
2
|
+
return patterns.some(p => typeof p === 'string' ? url.includes(p) : p.test(url));
|
|
3
|
+
}
|
|
4
|
+
function sendLog(collectorUrl, payload) {
|
|
5
|
+
// Use the original fetch before we patch it
|
|
6
|
+
const f = window.__heimdall_fetch;
|
|
7
|
+
f(`${collectorUrl}/collect`, {
|
|
8
|
+
method: 'POST',
|
|
9
|
+
headers: { 'Content-Type': 'application/json' },
|
|
10
|
+
body: JSON.stringify(payload),
|
|
11
|
+
}).catch(() => {
|
|
12
|
+
// Silently fail — never break the app
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
export function patchFetch(config) {
|
|
16
|
+
const original = window.fetch.bind(window);
|
|
17
|
+
window.__heimdall_fetch = original;
|
|
18
|
+
window.fetch = async (input, init) => {
|
|
19
|
+
const url = typeof input === 'string'
|
|
20
|
+
? input
|
|
21
|
+
: input instanceof URL
|
|
22
|
+
? input.href
|
|
23
|
+
: input.url;
|
|
24
|
+
// Never intercept our own collector calls
|
|
25
|
+
if (url.startsWith(config.collectorUrl))
|
|
26
|
+
return original(input, init);
|
|
27
|
+
if (config.ignore && shouldIgnore(url, config.ignore))
|
|
28
|
+
return original(input, init);
|
|
29
|
+
const method = init?.method?.toUpperCase() ?? (input instanceof Request ? input.method : 'GET');
|
|
30
|
+
const requestBody = init?.body;
|
|
31
|
+
let requestBodyParsed = null;
|
|
32
|
+
if (requestBody) {
|
|
33
|
+
try {
|
|
34
|
+
requestBodyParsed = JSON.parse(requestBody);
|
|
35
|
+
}
|
|
36
|
+
catch {
|
|
37
|
+
requestBodyParsed = String(requestBody);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
const requestHeaders = Object.fromEntries(new Headers(init?.headers ?? (input instanceof Request ? input.headers : {})).entries());
|
|
41
|
+
const start = performance.now();
|
|
42
|
+
let response;
|
|
43
|
+
try {
|
|
44
|
+
response = await original(input, init);
|
|
45
|
+
}
|
|
46
|
+
catch (err) {
|
|
47
|
+
sendLog(config.collectorUrl, {
|
|
48
|
+
method,
|
|
49
|
+
url,
|
|
50
|
+
error: String(err),
|
|
51
|
+
duration_ms: Math.round(performance.now() - start),
|
|
52
|
+
request_headers: requestHeaders,
|
|
53
|
+
request_body: requestBodyParsed,
|
|
54
|
+
project: config.project ?? 'default',
|
|
55
|
+
timestamp: new Date().toISOString(),
|
|
56
|
+
});
|
|
57
|
+
throw err;
|
|
58
|
+
}
|
|
59
|
+
const duration = Math.round(performance.now() - start);
|
|
60
|
+
const cloned = response.clone();
|
|
61
|
+
let responseBody = null;
|
|
62
|
+
try {
|
|
63
|
+
const text = await cloned.text();
|
|
64
|
+
try {
|
|
65
|
+
responseBody = JSON.parse(text);
|
|
66
|
+
}
|
|
67
|
+
catch {
|
|
68
|
+
responseBody = text;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
catch { /* ignore */ }
|
|
72
|
+
const responseHeaders = Object.fromEntries(response.headers.entries());
|
|
73
|
+
sendLog(config.collectorUrl, {
|
|
74
|
+
method,
|
|
75
|
+
url,
|
|
76
|
+
status_code: response.status,
|
|
77
|
+
duration_ms: duration,
|
|
78
|
+
request_headers: requestHeaders,
|
|
79
|
+
request_body: requestBodyParsed,
|
|
80
|
+
response_headers: responseHeaders,
|
|
81
|
+
response_body: responseBody,
|
|
82
|
+
project: config.project ?? 'default',
|
|
83
|
+
timestamp: new Date().toISOString(),
|
|
84
|
+
});
|
|
85
|
+
return response;
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
export function unpatchFetch() {
|
|
89
|
+
const original = window.__heimdall_fetch;
|
|
90
|
+
if (original)
|
|
91
|
+
window.fetch = original;
|
|
92
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "heimdall-sdk",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Heimdall SDK — zero-config API logger for React",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"files": ["dist"],
|
|
9
|
+
"scripts": {
|
|
10
|
+
"build": "tsc",
|
|
11
|
+
"dev": "tsc --watch"
|
|
12
|
+
},
|
|
13
|
+
"peerDependencies": {
|
|
14
|
+
"react": ">=17"
|
|
15
|
+
},
|
|
16
|
+
"devDependencies": {
|
|
17
|
+
"@types/react": "^19.1.2",
|
|
18
|
+
"typescript": "^5.8.3"
|
|
19
|
+
}
|
|
20
|
+
}
|