link-stack/apps/bridge-worker/tasks/common/notify-webhooks.ts

32 lines
739 B
TypeScript
Raw Normal View History

2024-06-05 08:52:41 +02:00
import { db } from "@link-stack/bridge-common";
2024-04-30 13:13:49 +02:00
export interface NotifyWebhooksOptions {
backendId: string;
payload: any;
}
const notifyWebhooksTask = async (
options: NotifyWebhooksOptions,
): Promise<void> => {
const { backendId, payload } = options;
const webhooks = await db
.selectFrom("Webhook")
.selectAll()
.where("backendId", "=", backendId)
.execute();
for (const webhook of webhooks) {
const { endpointUrl, httpMethod, headers } = webhook;
const finalHeaders = { "Content-Type": "application/json", ...headers };
await fetch(endpointUrl, {
method: httpMethod,
headers: finalHeaders,
body: JSON.stringify(payload),
});
}
};
export default notifyWebhooksTask;