forked from FlowwStar/FlowStar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuse-webhooks.ts
More file actions
158 lines (140 loc) · 4.32 KB
/
Copy pathuse-webhooks.ts
File metadata and controls
158 lines (140 loc) · 4.32 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
'use client'
import { useState, useCallback, useEffect } from 'react'
export type WebhookEventType =
| 'stream.created'
| 'stream.withdrawal'
| 'stream.cancelled'
| 'stream.completed'
| 'stream.topped_up'
| 'stream.transferred'
export interface WebhookConfig {
id: string
url: string
events: WebhookEventType[]
enabled: boolean
createdAt: number
}
export interface WebhookDelivery {
webhookId: string
eventType: WebhookEventType
statusCode: number | null
deliveredAt: number
success: boolean
}
const STORAGE_KEY = 'flowstar_webhooks'
const HISTORY_KEY = 'flowstar_webhook_history'
const MAX_HISTORY = 50
function loadWebhooks(): WebhookConfig[] {
if (typeof window === 'undefined') return []
try {
return JSON.parse(localStorage.getItem(STORAGE_KEY) ?? '[]')
} catch {
return []
}
}
function saveWebhooks(hooks: WebhookConfig[]) {
localStorage.setItem(STORAGE_KEY, JSON.stringify(hooks))
}
function loadHistory(): WebhookDelivery[] {
if (typeof window === 'undefined') return []
try {
return JSON.parse(localStorage.getItem(HISTORY_KEY) ?? '[]')
} catch {
return []
}
}
function saveHistory(history: WebhookDelivery[]) {
localStorage.setItem(HISTORY_KEY, JSON.stringify(history.slice(0, MAX_HISTORY)))
}
async function deliverWithRetry(
url: string,
payload: object,
retries = 3
): Promise<{ statusCode: number | null; success: boolean }> {
for (let attempt = 0; attempt < retries; attempt++) {
try {
const res = await fetch(url, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(payload),
})
if (res.ok) return { statusCode: res.status, success: true }
if (attempt < retries - 1) await new Promise((r) => setTimeout(r, 1000 * 2 ** attempt))
if (attempt === retries - 1) return { statusCode: res.status, success: false }
} catch {
if (attempt === retries - 1) return { statusCode: null, success: false }
await new Promise((r) => setTimeout(r, 1000 * 2 ** attempt))
}
}
return { statusCode: null, success: false }
}
export function useWebhooks() {
const [webhooks, setWebhooks] = useState<WebhookConfig[]>([])
const [history, setHistory] = useState<WebhookDelivery[]>([])
useEffect(() => {
setWebhooks(loadWebhooks())
setHistory(loadHistory())
}, [])
const addWebhook = useCallback((url: string, events: WebhookEventType[]) => {
const hook: WebhookConfig = {
id: crypto.randomUUID(),
url,
events,
enabled: true,
createdAt: Date.now(),
}
setWebhooks((prev) => {
const next = [...prev, hook]
saveWebhooks(next)
return next
})
}, [])
const removeWebhook = useCallback((id: string) => {
setWebhooks((prev) => {
const next = prev.filter((h) => h.id !== id)
saveWebhooks(next)
return next
})
}, [])
const toggleWebhook = useCallback((id: string) => {
setWebhooks((prev) => {
const next = prev.map((h) => (h.id === id ? { ...h, enabled: !h.enabled } : h))
saveWebhooks(next)
return next
})
}, [])
const fireEvent = useCallback(
async (eventType: WebhookEventType, data: object) => {
const active = webhooks.filter((h) => h.enabled && h.events.includes(eventType))
for (const hook of active) {
const payload = { event: eventType, timestamp: new Date().toISOString(), data }
const result = await deliverWithRetry(hook.url, payload)
const delivery: WebhookDelivery = {
webhookId: hook.id,
eventType,
statusCode: result.statusCode,
deliveredAt: Date.now(),
success: result.success,
}
setHistory((prev) => {
const next = [delivery, ...prev]
saveHistory(next)
return next
})
}
},
[webhooks]
)
const testWebhook = useCallback(async (id: string): Promise<boolean> => {
const hook = webhooks.find((h) => h.id === id)
if (!hook) return false
const payload = {
event: 'stream.created',
timestamp: new Date().toISOString(),
data: { stream_id: 0, note: 'FlowStar webhook test' },
}
const result = await deliverWithRetry(hook.url, payload, 1)
return result.success
}, [webhooks])
return { webhooks, history, addWebhook, removeWebhook, toggleWebhook, fireEvent, testWebhook }
}