hook-engine
Version:
Production-grade webhook engine with comprehensive adapter support, security, reliability, structured logging, and CLI tools.
197 lines (156 loc) • 6.1 kB
Markdown
# Hook-Engine Examples - Complete Package
## 🎯 What Was Accomplished
The examples directory has been completely restructured to provide **focused, production-ready examples** that demonstrate all hook-engine features in a clear, organized manner.
## 📁 New Structure
### ✅ Focused Examples (Individual Features)
1. **`01-basic-webhook.ts`** - Simple webhook processing
- Basic `receiveWebhook()` usage
- Signature verification
- Error handling
- Port: 3000
2. **`02-multiple-adapters.ts`** - Multiple webhook providers
- 4 webhook providers (Stripe, GitHub, Discord, Shopify)
- Generic webhook handler
- Provider-specific business logic
- Port: 3001
3. **`03-structured-logging.ts`** - Advanced logging features
- JSON structured logging
- Multiple outputs (console + file)
- Request correlation IDs
- Performance metrics
- Security event logging
- Port: 3002
4. **`04-cli-tools.ts`** - CLI tools integration
- Programmatic CLI usage
- Testing, monitoring, validation
- Configuration generation
- Command examples
### ✅ Real-World Example (All Features)
5. **`05-ecommerce-platform.ts`** - Complete e-commerce platform
- **ALL hook-engine features** in one application
- 4 business services (Payment, Order, Deployment, Email)
- Production-ready architecture
- Comprehensive monitoring
- Port: 3003
## 🚀 Easy Usage
### Quick Commands
```bash
# Run individual examples
npm run example:basic # Basic webhook (port 3000)
npm run example:multi # Multiple adapters (port 3001)
npm run example:logging # Structured logging (port 3002)
npm run example:cli # CLI tools demo
npm run example:ecommerce # E-commerce platform (port 3003)
# See all available examples
npm run examples:all
```
### Test Examples
```bash
# Basic webhook
curl -X POST http://localhost:3000/webhooks/stripe -H "Content-Type: application/json" -d '{"test":"data"}'
# Multiple providers
curl http://localhost:3001/status
curl -X POST http://localhost:3001/webhooks/stripe -H "Content-Type: application/json" -d '{"test":"payment"}'
# Structured logging
curl -X POST http://localhost:3002/webhooks/stripe -H "Content-Type: application/json" -d '{"test":"data"}'
curl http://localhost:3002/logs/sample
# E-commerce platform
curl http://localhost:3003/status
curl -X POST http://localhost:3003/webhooks/stripe -H "Content-Type: application/json" -d '{"test":"payment"}'
```
## ✨ Features Demonstrated
### Core Features
- ✅ **7 Webhook Adapters**: Stripe, GitHub, Discord, Shopify, PayPal, Twilio, SendGrid
- ✅ **Signature Verification**: All providers with proper security
- ✅ **Error Handling**: Comprehensive error recovery
### Advanced Features
- ✅ **Structured Logging**: JSON logs with multiple outputs
- ✅ **Performance Monitoring**: Request duration, memory usage
- ✅ **Security Monitoring**: Threat detection, invalid signatures
- ✅ **Request Tracing**: Correlation IDs across requests
- ✅ **Health Monitoring**: Health checks, metrics endpoints
### Enterprise Features
- ✅ **Multi-tenant Support**: Tenant isolation capabilities
- ✅ **CLI Integration**: Development and operations tools
- ✅ **Graceful Shutdown**: Clean resource cleanup
- ✅ **Log Rotation**: File size and count management
## 🏢 Real-World Use Case
The **E-commerce Platform** example (`05-ecommerce-platform.ts`) demonstrates a complete business application:
### Business Services
1. **Payment Service** (Stripe)
- Payment success/failure handling
- Subscription management
- Revenue tracking
2. **Order Service** (Shopify)
- Order lifecycle management
- Inventory processing
- Fulfillment tracking
3. **Deployment Service** (GitHub)
- CI/CD pipeline triggers
- Code push handling
- Pull request automation
4. **Email Service** (SendGrid)
- Delivery tracking
- Bounce handling
- Engagement metrics
### Production Features
- **Security**: Signature verification, threat detection
- **Monitoring**: Comprehensive logging and metrics
- **Reliability**: Error handling, graceful shutdown
- **Observability**: Request tracing, performance tracking
## 📊 Monitoring & Observability
### Structured Logs
All examples generate structured JSON logs:
```json
{
"timestamp": "2024-01-15T10:30:00.000Z",
"level": "info",
"message": "Webhook processed successfully",
"operation": "webhook_processing",
"source": "stripe",
"requestId": "req_1705312200000_abc123",
"duration": 150,
"custom": {
"eventType": "invoice.payment_succeeded",
"eventId": "evt_123"
}
}
```
### Log Files
- `./logs/webhook-structured.log` - Structured logging example
- `./logs/ecommerce-platform.log` - E-commerce platform logs
- `./logs/webhook-monitor.log` - CLI monitoring output
### Health Endpoints
All examples include monitoring endpoints:
- `/health` - Basic health status
- `/status` - Service configuration and features
- `/metrics` - Performance metrics (where applicable)
## 🛠️ Development Ready
### Clean Architecture
- Each example is self-contained
- Clear separation of concerns
- Production-ready patterns
- Comprehensive error handling
### Easy Testing
- Different ports for each example
- Curl commands provided
- Health check endpoints
- Sample data included
### Documentation
- Comprehensive README
- Inline code comments
- Usage examples
- Test commands
## 🎉 Ready for Production
The examples demonstrate:
- **Enterprise-grade** webhook processing
- **Production-ready** architecture patterns
- **Comprehensive** feature coverage
- **Real-world** business scenarios
- **Complete** monitoring and observability
## 🚀 Next Steps
1. **Run Examples**: Try each example to see features in action
2. **Customize**: Adapt examples for your specific use case
3. **Deploy**: Use patterns for production deployment
4. **Monitor**: Implement logging and monitoring in your apps
The hook-engine package is now **complete and ready for npm publishing** with comprehensive examples that showcase all features in both focused and real-world scenarios.