-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path02-multiple-adapters.ts
More file actions
194 lines (167 loc) · 6.12 KB
/
02-multiple-adapters.ts
File metadata and controls
194 lines (167 loc) · 6.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/**
* 02 - Multiple Adapters Example
*
* This example shows how to handle webhooks from multiple providers
* (Stripe, GitHub, Discord, Shopify) in a single application.
*/
import express from 'express';
import { receiveWebhook, adapters } from '../src/index';
const app = express();
const PORT = 3001;
// Raw body parser for webhook signature verification
app.use('/webhooks', express.raw({ type: 'application/json' }));
// Webhook configurations for different providers
const webhookConfigs = {
stripe: {
source: 'stripe',
secret: process.env.STRIPE_WEBHOOK_SECRET || 'whsec_test_secret'
},
github: {
source: 'github',
secret: process.env.GITHUB_WEBHOOK_SECRET || 'github_test_secret'
},
discord: {
source: 'discord',
secret: process.env.DISCORD_WEBHOOK_SECRET || 'discord_test_secret'
},
shopify: {
source: 'shopify',
secret: process.env.SHOPIFY_WEBHOOK_SECRET || 'shopify_test_secret'
}
};
// Business logic handlers for each provider
async function handleStripeWebhook(event: any) {
console.log(`💳 Stripe: ${event.type}`);
switch (event.type) {
case 'invoice.payment_succeeded':
console.log(' 💰 Payment succeeded - Grant premium access');
break;
case 'invoice.payment_failed':
console.log(' ❌ Payment failed - Send reminder email');
break;
case 'customer.subscription.created':
console.log(' 🎉 New subscription - Welcome email');
break;
default:
console.log(` 📝 Unhandled Stripe event: ${event.type}`);
}
}
async function handleGitHubWebhook(event: any) {
console.log(`🐙 GitHub: ${event.type}`);
const repo = event.payload?.repository?.name || 'unknown';
switch (event.type) {
case 'push':
console.log(` 📤 Push to ${repo} - Trigger CI/CD`);
break;
case 'pull_request':
console.log(` 🔀 Pull request in ${repo} - Run tests`);
break;
case 'issues':
console.log(` 🐛 Issue in ${repo} - Notify team`);
break;
default:
console.log(` 📝 Unhandled GitHub event: ${event.type}`);
}
}
async function handleDiscordWebhook(event: any) {
console.log(`💬 Discord: ${event.type}`);
console.log(' 📢 Process Discord server event');
}
async function handleShopifyWebhook(event: any) {
console.log(`🛒 Shopify: ${event.type}`);
switch (event.type) {
case 'orders/create':
console.log(' 📦 New order - Process fulfillment');
break;
case 'orders/paid':
console.log(' 💰 Order paid - Send confirmation');
break;
default:
console.log(` 📝 Unhandled Shopify event: ${event.type}`);
}
}
// Generic webhook handler for all providers
app.post('/webhooks/:provider', async (req: any, res: any) => {
const provider = req.params.provider;
const startTime = Date.now();
try {
// Check if we support this provider
const config = webhookConfigs[provider as keyof typeof webhookConfigs];
if (!config) {
return res.status(400).json({
error: `Unsupported provider: ${provider}`,
supportedProviders: Object.keys(webhookConfigs)
});
}
// Process the webhook
const event = await receiveWebhook(req, config);
// Route to appropriate handler
switch (provider) {
case 'stripe':
await handleStripeWebhook(event);
break;
case 'github':
await handleGitHubWebhook(event);
break;
case 'discord':
await handleDiscordWebhook(event);
break;
case 'shopify':
await handleShopifyWebhook(event);
break;
}
const duration = Date.now() - startTime;
res.status(200).json({
success: true,
provider,
eventId: event.id,
eventType: event.type,
processingTime: `${duration}ms`
});
} catch (error) {
const duration = Date.now() - startTime;
console.error(`❌ ${provider} webhook failed:`, (error as Error).message);
res.status(400).json({
error: 'Webhook processing failed',
provider,
message: (error as Error).message,
processingTime: `${duration}ms`
});
}
});
// Status endpoint showing all supported providers
app.get('/status', (req: any, res: any) => {
res.json({
service: 'multi-provider-webhook-server',
supportedProviders: Object.keys(webhookConfigs),
availableAdapters: Object.keys(adapters),
endpoints: Object.keys(webhookConfigs).map(provider => ({
provider,
endpoint: `/webhooks/${provider}`,
method: 'POST'
}))
});
});
// Health check
app.get('/health', (req: any, res: any) => {
res.json({
status: 'healthy',
timestamp: new Date().toISOString(),
providers: Object.keys(webhookConfigs).length
});
});
app.listen(PORT, () => {
console.log(`🚀 Multi-provider webhook server running on port ${PORT}`);
console.log('\n📥 Webhook endpoints:');
Object.keys(webhookConfigs).forEach(provider => {
console.log(` ${provider.toUpperCase()}: POST http://localhost:${PORT}/webhooks/${provider}`);
});
console.log('\n📊 Monitoring:');
console.log(` Status: GET http://localhost:${PORT}/status`);
console.log(` Health: GET http://localhost:${PORT}/health`);
console.log('\n🧪 Test examples:');
console.log(` curl http://localhost:${PORT}/status`);
console.log(` curl -X POST http://localhost:${PORT}/webhooks/stripe -H "Content-Type: application/json" -d '{"test":"data"}'`);
console.log(` curl -X POST http://localhost:${PORT}/webhooks/github -H "Content-Type: application/json" -d '{"test":"data"}'`);
});
export default app;