35 lines
834 B
TypeScript
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);
|
|
}
|
|
}
|