api-health-middleware 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.
package/README.md ADDED
File without changes
package/package.json ADDED
@@ -0,0 +1,23 @@
1
+ {
2
+ "name": "api-health-middleware",
3
+ "version": "1.0.0",
4
+ "description": "Express middleware to report per-request API health to a monitoring backend",
5
+ "main": "src/index.js",
6
+ "scripts": {
7
+ "start" : "node index.js"
8
+ },
9
+ "keywords": ["api", "health", "monitoring", "express", "middleware"],
10
+ "author": "Tarun Thakur",
11
+ "license": "MIT",
12
+ "type": "module",
13
+ "dependencies": {
14
+ "axios": "^1.13.4"
15
+ },
16
+ "peerDependencies": {
17
+ "express": ">=4"
18
+ }
19
+
20
+ }
21
+
22
+
23
+
package/src/index.js ADDED
@@ -0,0 +1 @@
1
+ export { apiHealth } from "./middleware.js";
@@ -0,0 +1,40 @@
1
+ import axios from "axios";
2
+
3
+ const INGEST_ENDPOINT = "https://devops-lite-backend.onrender.com/logs/health";
4
+
5
+
6
+ export function apiHealth({ apiKey }) {
7
+ if (!apiKey) throw new Error("apiKey is required");
8
+
9
+ return function apiHealthMiddleware(req, res, next) {
10
+ const start = process.hrtime.bigint();
11
+
12
+ res.on("finish", () => {
13
+ const durationMs =
14
+ Number(process.hrtime.bigint() - start) / 1_000_000;
15
+
16
+ const payload = {
17
+ Method: req.method,
18
+ api_End_Point: req.route?.path || req.path,
19
+ api_Status_Code: res.statusCode,
20
+ responseTime: durationMs,
21
+ api_Error: res.statusCode >= 400 ? "Error occurred" : null,
22
+ timestamp: Date.now()
23
+ };
24
+
25
+ axios
26
+ .post(INGEST_ENDPOINT, payload, {
27
+ headers: {
28
+ "x-api-key": `${apiKey}`,
29
+ "Content-Type": "application/json"
30
+ },
31
+ timeout: 2000
32
+ })
33
+ .catch((err) => {
34
+ console.log(err);
35
+ });
36
+ });
37
+
38
+ next();
39
+ };
40
+ }