Files
varo-website/src/util/webhook.ts
bytedream d7b05deff2
All checks were successful
deploy / build-and-deploy (/testvaro, /opt/website-test, website-test) (push) Successful in 28s
deploy / build-and-deploy (/varo, /opt/website, website) (push) Successful in 13s
edit reported team in admin ui
2025-06-22 00:11:10 +02:00

35 lines
834 B
TypeScript

import { sleepSeconds } from './sleep.ts';
import { WEBHOOK_ENDPOINT } from 'astro:env/server';
export enum WebhookAction {
Strike = 'strike'
}
export type WebhookActionType<T extends WebhookAction> = {
[WebhookAction.Strike]: {
users: string[];
totalWeight: number;
};
}[T];
export async function sendWebhook<T extends WebhookAction>(action: T, data: WebhookActionType<T>) {
if (!WEBHOOK_ENDPOINT || !/^https?:\/\/.+$/.test(WEBHOOK_ENDPOINT)) return;
while (true) {
try {
const response = await fetch(WEBHOOK_ENDPOINT, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'x-webhook-action': action
},
body: JSON.stringify(data)
});
if (response.status === 200) return;
} catch (_) {}
await sleepSeconds(60);
}
}