npm package: @pritam_dev/api-sentry-mode
Description: Sentry mode middleware for Express APIs. Detect slow, spammy, and error-prone requests with real-time dashboard.
Author: Pritam Kumar
Latest version: 1.0.0

# 🚨 API Sentry Mode – @pritam_dev/api-sentry-mode

![npm version](https://img.shields.io/npm/v/@pritam_dev/api-sentry-mode)
![MIT License](https://img.shields.io/npm/l/@pritam_dev/api-sentry-mode)
![Node.js](https://img.shields.io/badge/node-%3E%3D14-green)
![Made with ❤️ by Pritam](https://img.shields.io/badge/built%20by-Pritam%20Kumar-orange)

A **plug-and-play sentry-style logger** for Express.js that detects slow endpoints, spammy traffic, error spikes, and suspicious IPs — with a built-in **real-time dashboard** powered by `socket.io`.

---

## 🚀 Features

- 📊 **Real-time dashboard** at `http://localhost:5050`
- 🚨 **Spam/IP rate monitoring** (configurable)
- 🐢 **Slow endpoint detection**
- ❌ **500+ Error spike alerting** (Discord optional)
- 🔌 **WebSocket dashboard client**
- 🛠️ **Extendable and developer-friendly**

---

## 📦 Installation

```bash
npm install @pritam_dev/api-sentry-mode
```

## 🛠 Usage

```javascript
const express = require('express');
const {
  sentryMiddleware,
  sentryEmitter,
} = require('@pritam_dev/api-sentry-mode');
const startDashboard = require('@pritam_dev/api-sentry-mode/lib/dashboard');

const app = express();

// Add API Sentry middleware
app.use(
  sentryMiddleware({
    threshold: 500, // Alert on requests > 500ms
    spamLimit: 30, // Alert on > 30 requests/min per IP
    alertOn500: true, // Alert on 5xx errors
  })
);

// Start real-time dashboard
startDashboard(sentryEmitter, 5050);

// Listen to events
sentryEmitter.on('error', (data) => {
  console.error('API Error →', data);
});

app.get('/', (req, res) => res.send('API Running'));
app.listen(3000);
```

## 🌐 Live Dashboard

Visit **http://localhost:5050** to monitor:

- Live request logs with color-coded status
- Real-time statistics (total, slow, errors, spam)
- IP monitoring and spam detection
- Beautiful dark theme with neon accents

## 📡 Discord Integration

```javascript
const sendDiscordAlert = require('@pritam_dev/api-sentry-mode/lib/alert');

sentryEmitter.on('error', (data) => {
  sendDiscordAlert(
    process.env.DISCORD_WEBHOOK,
    `❌ Error: ${data.method} ${data.url} (${data.status})`
  );
});
```

## ⚙️ Configuration Options

| Option         | Default | Description                         |
| -------------- | ------- | ----------------------------------- |
| `threshold`    | 500     | Response time threshold (ms)        |
| `spamLimit`    | 100     | Max requests per minute per IP      |
| `timeWindow`   | 60000   | Time window for spam detection (ms) |
| `alertOn500`   | true    | Alert on 5xx errors                 |
| `logToConsole` | true    | Log requests to console             |

## 🧪 Sample Console Output

```bash
🚨 SENTRY MODE: HIGH TRAFFIC FROM IP 192.168.0.4 (120 hits in 60s)
⚠️  [SLOW] GET /api/products → 812ms
✅  [OK] POST /api/auth/login → 201 Created | 82ms
❌  [ERROR] POST /api/order → 500 Internal Server Error
```

## 📺 Dashboard Screenshot

```
+--------------------------------------+
|         🛰️ API Sentry Dashboard       |
+--------------------------------------+
| Total Requests | 1,250               |
| Slow Requests  | 23                  |
| Server Errors  | 5                   |
| Spam Alerts    | 2                   |
+--------------------------------------+
| 🟢 GET /api/users     → 200 (85ms)    |
| 🔴 POST /api/order    → 500 (150ms)   |
| 🟡 GET /api/products  → 200 (650ms)   |
+--------------------------------------+
```

## 🚦 Getting Started

1. **Clone the example:**

   ```bash
   git clone https://github.com/pritam-dev/api-sentry-mode.git
   cd api-sentry-mode
   npm install
   npm start
   ```

2. **Visit the dashboard:**
   Open http://localhost:5050

3. **Test endpoints:**
   ```bash
   curl http://localhost:3000/api/slow
   curl http://localhost:3000/api/error
   ```

## 🤝 Contributing

Feel free to open issues and pull requests!

## 📄 License

MIT © Pritam Kumar

---

⭐ **Star this repo** if you found it helpful!