@fazetitans/fscopy 1.1.1 → 1.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,128 @@
1
+ import type { Stats } from '../types.js';
2
+ import type { Logger } from '../utils/logger.js';
3
+
4
+ export interface WebhookPayload {
5
+ source: string;
6
+ destination: string;
7
+ collections: string[];
8
+ stats: Stats;
9
+ duration: number;
10
+ dryRun: boolean;
11
+ success: boolean;
12
+ error?: string;
13
+ }
14
+
15
+ export function detectWebhookType(url: string): 'slack' | 'discord' | 'custom' {
16
+ if (url.includes('hooks.slack.com')) {
17
+ return 'slack';
18
+ }
19
+ if (url.includes('discord.com/api/webhooks')) {
20
+ return 'discord';
21
+ }
22
+ return 'custom';
23
+ }
24
+
25
+ export function formatSlackPayload(payload: WebhookPayload): Record<string, unknown> {
26
+ const status = payload.success ? ':white_check_mark: Success' : ':x: Failed';
27
+ const mode = payload.dryRun ? ' (DRY RUN)' : '';
28
+
29
+ const fields = [
30
+ { title: 'Source', value: payload.source, short: true },
31
+ { title: 'Destination', value: payload.destination, short: true },
32
+ { title: 'Collections', value: payload.collections.join(', '), short: false },
33
+ { title: 'Transferred', value: String(payload.stats.documentsTransferred), short: true },
34
+ { title: 'Deleted', value: String(payload.stats.documentsDeleted), short: true },
35
+ { title: 'Errors', value: String(payload.stats.errors), short: true },
36
+ { title: 'Duration', value: `${payload.duration}s`, short: true },
37
+ ];
38
+
39
+ if (payload.error) {
40
+ fields.push({ title: 'Error', value: payload.error, short: false });
41
+ }
42
+
43
+ return {
44
+ attachments: [
45
+ {
46
+ color: payload.success ? '#36a64f' : '#ff0000',
47
+ title: `fscopy Transfer${mode}`,
48
+ text: status,
49
+ fields,
50
+ footer: 'fscopy',
51
+ ts: Math.floor(Date.now() / 1000),
52
+ },
53
+ ],
54
+ };
55
+ }
56
+
57
+ export function formatDiscordPayload(payload: WebhookPayload): Record<string, unknown> {
58
+ const status = payload.success ? '✅ Success' : '❌ Failed';
59
+ const mode = payload.dryRun ? ' (DRY RUN)' : '';
60
+ const color = payload.success ? 0x36a64f : 0xff0000;
61
+
62
+ const fields = [
63
+ { name: 'Source', value: payload.source, inline: true },
64
+ { name: 'Destination', value: payload.destination, inline: true },
65
+ { name: 'Collections', value: payload.collections.join(', '), inline: false },
66
+ { name: 'Transferred', value: String(payload.stats.documentsTransferred), inline: true },
67
+ { name: 'Deleted', value: String(payload.stats.documentsDeleted), inline: true },
68
+ { name: 'Errors', value: String(payload.stats.errors), inline: true },
69
+ { name: 'Duration', value: `${payload.duration}s`, inline: true },
70
+ ];
71
+
72
+ if (payload.error) {
73
+ fields.push({ name: 'Error', value: payload.error, inline: false });
74
+ }
75
+
76
+ return {
77
+ embeds: [
78
+ {
79
+ title: `fscopy Transfer${mode}`,
80
+ description: status,
81
+ color,
82
+ fields,
83
+ footer: { text: 'fscopy' },
84
+ timestamp: new Date().toISOString(),
85
+ },
86
+ ],
87
+ };
88
+ }
89
+
90
+ export async function sendWebhook(
91
+ webhookUrl: string,
92
+ payload: WebhookPayload,
93
+ logger: Logger
94
+ ): Promise<void> {
95
+ const webhookType = detectWebhookType(webhookUrl);
96
+
97
+ let body: Record<string, unknown>;
98
+ switch (webhookType) {
99
+ case 'slack':
100
+ body = formatSlackPayload(payload);
101
+ break;
102
+ case 'discord':
103
+ body = formatDiscordPayload(payload);
104
+ break;
105
+ default:
106
+ body = payload as unknown as Record<string, unknown>;
107
+ }
108
+
109
+ try {
110
+ const response = await fetch(webhookUrl, {
111
+ method: 'POST',
112
+ headers: { 'Content-Type': 'application/json' },
113
+ body: JSON.stringify(body),
114
+ });
115
+
116
+ if (!response.ok) {
117
+ const errorText = await response.text();
118
+ throw new Error(`HTTP ${response.status}: ${errorText}`);
119
+ }
120
+
121
+ logger.info(`Webhook sent successfully (${webhookType})`, { url: webhookUrl });
122
+ console.log(`📤 Webhook notification sent (${webhookType})`);
123
+ } catch (error) {
124
+ const message = error instanceof Error ? error.message : String(error);
125
+ logger.error(`Failed to send webhook: ${message}`, { url: webhookUrl });
126
+ console.error(`⚠️ Failed to send webhook: ${message}`);
127
+ }
128
+ }