This commit is contained in:
33
src/util/webhook.ts
Normal file
33
src/util/webhook.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import { sleepSeconds } from './sleep.ts';
|
||||
import { WEBHOOK_ENDPOINT } from 'astro:env/client';
|
||||
|
||||
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, {
|
||||
body: JSON.stringify(data),
|
||||
headers: {
|
||||
'x-webhook-action': action
|
||||
},
|
||||
keepalive: false
|
||||
});
|
||||
|
||||
if (response.status === 200) return;
|
||||
} catch (_) {}
|
||||
|
||||
await sleepSeconds(60);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user