π wizinsight

β‘ A lightweight performance monitoring & health tracking library with Discord alerts and smart request optimization. Track your API requests, monitor health, and get instant alerts when issues arise.
β¨ Features
- π Request Metrics - Track performance of individual API requests
- π₯ Health Monitoring - Monitor multiple API endpoints with automatic alerts
- π Discord Integration - Get instant notifications when APIs go down
- β‘ Zero Configuration - Works out of the box with sensible defaults
- π‘οΈ TypeScript Support - Full type safety and IntelliSense
π Quick Start
π¦ Installation
npm install wizinsight
π» Basic Usage
import { createClient } from 'your-http-client'
import { initMetricsInterceptor } from 'wizinsight'
const client = createClient('https://api.example.com')
initMetricsInterceptor(client)
const users = await client.get('/users')
π Request Metrics
π Track the performance of your API requests with detailed timing and error information.
β What You Get Automatically
Every API request now logs:
- β
Request method and URL
- β±οΈ Response time (duration)
- π HTTP status code
- π¦ Request/response sizes (optional)
- β Detailed error information
π Basic Monitoring
import { createClient } from 'your-http-client'
import { initMetricsInterceptor } from 'wizinsight'
const client = createClient('https://api.example.com')
initMetricsInterceptor(client)
π¨ Custom Logging
initMetricsInterceptor(client, {
onRequestEnd: (metrics) => {
console.log(`π ${metrics.method} ${metrics.url} took ${metrics.duration}ms`)
},
onRequestError: (metrics) => {
console.error(`π₯ ${metrics.method} ${metrics.url} failed: ${metrics.errorMessage}`)
}
})
β οΈ Performance Alerts
initMetricsInterceptor(client, {
onRequestEnd: (metrics) => {
if (metrics.duration > 1000) {
console.warn(`π Slow request: ${metrics.method} ${metrics.url} took ${metrics.duration}ms`)
}
if (metrics.status >= 400) {
console.error(`β Error: ${metrics.method} ${metrics.url} returned ${metrics.status}`)
}
}
})
π Collect All Metrics
initMetricsInterceptor(client, {
collectRequestSize: true,
collectResponseSize: true,
onRequestEnd: (metrics) => {
console.log(`π ${metrics.method} ${metrics.url}`)
console.log(` β±οΈ Duration: ${metrics.duration}ms`)
console.log(` π Status: ${metrics.status}`)
console.log(` π¦ Request: ${metrics.requestSize} bytes`)
console.log(` π¦ Response: ${metrics.responseSize} bytes`)
}
})
π Custom Analytics Dashboard
const performanceData = {
totalRequests: 0,
averageResponseTime: 0,
errorCount: 0
}
initMetricsInterceptor(client, {
onRequestEnd: (metrics) => {
performanceData.totalRequests++
performanceData.averageResponseTime =
(performanceData.averageResponseTime + metrics.duration) / 2
if (metrics.status >= 400) {
performanceData.errorCount++
}
console.table(performanceData)
}
})
π₯ Health Monitoring
Monitor the health of multiple API endpoints with automatic alerts and status tracking.
Why Health Monitoring?
- π Proactive Monitoring - Catch issues before users do
- π’ Instant Alerts - Get notified immediately when APIs go down
- π Status Tracking - Keep track of API health over time
- π‘οΈ Prevent Downtime - Identify and fix issues quickly
Basic Health Monitoring
import { initHealthMonitor, getHealthStatus } from 'wizinsight'
initHealthMonitor({
targets: [
{ name: 'User API', url: 'https://api.example.com/users' },
{ name: 'Auth API', url: 'https://auth.example.com/health' },
{ name: 'Database', url: 'https://db.example.com/ping' }
]
})
const health = getHealthStatus()
console.log(health)
Simple Request Examples
{ name: 'API', url: 'https://api.example.com/health' }
{
name: 'GraphQL',
url: 'https://api.example.com/graphql',
method: 'POST',
body: { query: '{ __typename }' }
}
{
name: 'Auth',
url: 'https://api.example.com/auth',
method: 'POST',
body: { token: 'test' },
headers: { 'Authorization': 'Bearer test' }
}
{
name: 'Slow API',
url: 'https://api.example.com/slow',
timeout: 5000
}
Real API Testing Examples
{
name: 'Login API',
url: 'https://api.example.com/login',
method: 'POST',
body: { username: 'realuser', password: 'realpass' },
expectedStatus: 200
}
{
name: 'Register API',
url: 'https://api.example.com/register',
method: 'POST',
body: {
email: 'test@example.com',
password: 'testpass',
name: 'Test User'
},
expectedStatus: 201
}
{
name: 'User Profile',
url: 'https://api.example.com/user/profile',
method: 'GET',
headers: { 'Authorization': 'Bearer your-token-here' },
expectedStatus: 200
}
Example Output:
{
"https://api.example.com/users": {
"lastChecked": 1703123456789,
"lastStatus": 200,
"isHealthy": true
},
"https://auth.example.com/health": {
"lastChecked": 1703123456789,
"lastStatus": 503,
"isHealthy": false,
"lastError": "Expected 200, got 503"
}
}
Health Monitoring with Discord Alerts
initHealthMonitor({
targets: [
{ name: 'Production API', url: 'https://api.production.com/health' },
{ name: 'Staging API', url: 'https://api.staging.com/health' }
],
interval: 30000,
discordWebhook: 'https://discord.com/api/webhooks/your-webhook-url',
alertCooldown: 600000
})
Discord Alert Example:
π¨ Health Check Failed: Production API
API endpoint is not responding as expected
π URL: https://api.production.com/health
π Status: 503
β±οΈ Response Time: 245ms
π― Expected: 200
β Error: Expected 200, got 503
Advanced Configuration
initHealthMonitor({
targets: [
{ name: 'Health Check', url: 'https://api.example.com/health' },
{
name: 'GraphQL API',
url: 'https://api.example.com/graphql',
method: 'POST',
body: { query: '{ __typename }' }
},
{
name: 'Auth API',
url: 'https://api.example.com/auth/verify',
method: 'POST',
body: { token: 'test-token' },
headers: { 'Authorization': 'Bearer test-token' }
},
{
name: 'File Upload',
url: 'https://api.example.com/upload',
method: 'POST',
body: 'test-data',
headers: { 'Content-Type': 'text/plain' }
},
{
name: 'Slow API',
url: 'https://api.example.com/slow',
timeout: 5000
}
],
interval: 60000,
discordWebhook: process.env.DISCORD_WEBHOOK_URL,
alertCooldown: 900000
})
Programmatic Health Checks
const health = getHealthStatus()
const unhealthyServices = Object.entries(health).filter(([url, status]) => !status.isHealthy)
if (unhealthyServices.length > 0) {
console.log('β Unhealthy services detected:')
unhealthyServices.forEach(([url, status]) => {
console.log(` - ${url}: ${status.lastError}`)
})
} else {
console.log('β
All services are healthy!')
}
Stop Health Monitoring
import { stopHealthMonitor } from 'wizinsight'
stopHealthMonitor()
βοΈ Configuration Options
Metrics Interceptor Options
onRequestStart | Called when request starts | - |
onRequestEnd | Called when request completes | - |
onRequestError | Called when request fails | - |
collectRequestSize | Track request payload size | false |
collectResponseSize | Track response payload size | false |
Health Monitor Options
targets | Array of API endpoints to monitor | Required |
interval | Health check interval in milliseconds | 60000 (60s) |
discordWebhook | Discord webhook URL for alerts | - |
alertCooldown | Time between alerts in milliseconds | 900000 (15m) |
Health Target Configuration
name | Display name for the API | Required |
url | API endpoint URL | Required |
method | HTTP method for health check | GET |
body | Request body (any type) | - |
headers | Request headers | - |
expectedStatus | Expected HTTP status code | 200 |
timeout | Request timeout in milliseconds | - |
π οΈ Development
npm install
npm run build
npm run dev
npm run type-check
π Examples
Real-World Usage
import { createClient } from 'hyperwiz'
import { initMetricsInterceptor, initHealthMonitor } from 'wizinsight'
const client = createClient('https://api.yourcompany.com')
initMetricsInterceptor(client, {
onRequestEnd: (metrics) => {
if (metrics.duration > 1000) {
console.warn(`Slow API call: ${metrics.url}`)
}
}
})
initHealthMonitor({
targets: [
{ name: 'Main API', url: 'https://api.yourcompany.com/health' },
{
name: 'Login Service',
url: 'https://api.yourcompany.com/login',
method: 'POST',
body: { username: 'healthuser', password: 'healthpass' },
expectedStatus: 200
},
{
name: 'Registration',
url: 'https://api.yourcompany.com/register',
method: 'POST',
body: {
email: 'health@test.com',
password: 'testpass',
name: 'Health Test User'
},
expectedStatus: 201
},
{
name: 'User Profile',
url: 'https://api.yourcompany.com/user/profile',
method: 'GET',
headers: { 'Authorization': 'Bearer your-token-here' },
expectedStatus: 200
}
],
interval: 30000,
discordWebhook: process.env.DISCORD_WEBHOOK_URL
})
π€ Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
π License
MIT License - see the LICENSE file for details.