From 8fe41b3989466a4b6dafaa783d720892ce5930e4 Mon Sep 17 00:00:00 2001 From: DrMint <29893320+DrMint@users.noreply.github.com> Date: Sun, 14 Jul 2024 18:09:58 +0200 Subject: [PATCH] Handle multiple urls (and token) for webhooks --- src/hooks/afterOperationWebhook.ts | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) 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),