diff --git a/src/hooks/afterOperationWebhook.ts b/src/hooks/afterOperationWebhook.ts index ab10d8d..e95274e 100644 --- a/src/hooks/afterOperationWebhook.ts +++ b/src/hooks/afterOperationWebhook.ts @@ -67,14 +67,30 @@ export const afterDeleteWebhook: AfterDeleteHook = async ({ collection, doc }) = return doc; }; +if (!process.env.WEB_SERVER_HOOK_URL) throw new Error("Missing WEB_SERVER_HOOK_URL"); +if (!process.env.WEB_SERVER_HOOK_TOKEN) throw new Error("Missing WEB_SERVER_HOOK_TOKEN"); +if (!process.env.MEILISEARCH_HOOK_URL) throw new Error("Missing MEILISEARCH_HOOK_URL"); +if (!process.env.MEILISEARCH_HOOK_TOKEN) throw new Error("Missing MEILISEARCH_HOOK_TOKEN"); + +const webhookTargets: { url: string; token: string }[] = [ + { + url: process.env.WEB_SERVER_HOOK_URL, + token: process.env.WEB_SERVER_HOOK_TOKEN, + }, + { + url: process.env.MEILISEARCH_HOOK_URL, + token: process.env.MEILISEARCH_HOOK_TOKEN, + }, +]; + const sendWebhookMessage = async (message: AfterOperationWebHookMessage) => { try { await Promise.all( - [process.env.WEB_SERVER_HOOK_URL, process.env.MEILISEARCH_HOOK_URL].flatMap((url) => { + webhookTargets.flatMap(({ url, token }) => { if (!url) return; return fetch(url, { headers: { - Authorization: `Bearer ${process.env.WEB_SERVER_HOOK_TOKEN}`, + Authorization: `Bearer ${token}`, "Content-Type": "application/json", }, body: JSON.stringify(message),