api-health-middleware 1.0.2 → 1.0.3
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/package.json +9 -10
- package/src/middleware.js +13 -10
package/package.json
CHANGED
|
@@ -1,23 +1,22 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "api-health-middleware",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.3",
|
|
4
4
|
"description": "Express middleware to report per-request API health to a monitoring backend",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"scripts": {
|
|
7
|
-
"start"
|
|
7
|
+
"start": "node index.js"
|
|
8
8
|
},
|
|
9
|
-
"keywords": [
|
|
9
|
+
"keywords": [
|
|
10
|
+
"api",
|
|
11
|
+
"health",
|
|
12
|
+
"monitoring",
|
|
13
|
+
"express",
|
|
14
|
+
"middleware"
|
|
15
|
+
],
|
|
10
16
|
"author": "Tarun Thakur",
|
|
11
17
|
"license": "MIT",
|
|
12
18
|
"type": "module",
|
|
13
|
-
"dependencies": {
|
|
14
|
-
"axios": "^1.13.4"
|
|
15
|
-
},
|
|
16
19
|
"peerDependencies": {
|
|
17
20
|
"express": ">=4"
|
|
18
21
|
}
|
|
19
|
-
|
|
20
22
|
}
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
package/src/middleware.js
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
import axios from "axios";
|
|
2
|
-
|
|
3
1
|
const INGEST_ENDPOINT = "http://localhost:5000/logs/health";
|
|
4
2
|
|
|
5
3
|
export function apiHealth({ apiKey }) {
|
|
@@ -18,7 +16,7 @@ export function apiHealth({ apiKey }) {
|
|
|
18
16
|
|
|
19
17
|
const payload = {
|
|
20
18
|
timestamp: new Date().toISOString(),
|
|
21
|
-
|
|
19
|
+
method: req.method,
|
|
22
20
|
endpoint: req.route?.path || req.originalUrl,
|
|
23
21
|
fullUrl: req.originalUrl,
|
|
24
22
|
statusCode: res.statusCode,
|
|
@@ -32,18 +30,23 @@ export function apiHealth({ apiKey }) {
|
|
|
32
30
|
userAgent: req.headers["user-agent"] || null,
|
|
33
31
|
};
|
|
34
32
|
|
|
35
|
-
//
|
|
36
|
-
|
|
33
|
+
// Timeout handling using AbortController
|
|
34
|
+
const controller = new AbortController();
|
|
35
|
+
const timeout = setTimeout(() => controller.abort(), 2000);
|
|
36
|
+
|
|
37
|
+
fetch(INGEST_ENDPOINT, {
|
|
38
|
+
method: "POST",
|
|
37
39
|
headers: {
|
|
38
40
|
"x-api-key": apiKey,
|
|
39
41
|
"Content-Type": "application/json",
|
|
40
42
|
},
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
43
|
+
body: JSON.stringify(payload),
|
|
44
|
+
signal: controller.signal,
|
|
45
|
+
})
|
|
46
|
+
.catch(() => {}) // Silent fail
|
|
47
|
+
.finally(() => clearTimeout(timeout));
|
|
45
48
|
|
|
46
|
-
} catch
|
|
49
|
+
} catch {
|
|
47
50
|
// Never break main request
|
|
48
51
|
}
|
|
49
52
|
});
|