Handle multiple urls (and token) for webhooks

This commit is contained in:
DrMint 2024-07-14 18:09:58 +02:00
parent e2cd4a9388
commit 8fe41b3989
1 changed files with 18 additions and 2 deletions

View File

@ -67,14 +67,30 @@ export const afterDeleteWebhook: AfterDeleteHook = async ({ collection, doc }) =
return 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) => { const sendWebhookMessage = async (message: AfterOperationWebHookMessage) => {
try { try {
await Promise.all( await Promise.all(
[process.env.WEB_SERVER_HOOK_URL, process.env.MEILISEARCH_HOOK_URL].flatMap((url) => { webhookTargets.flatMap(({ url, token }) => {
if (!url) return; if (!url) return;
return fetch(url, { return fetch(url, {
headers: { headers: {
Authorization: `Bearer ${process.env.WEB_SERVER_HOOK_TOKEN}`, Authorization: `Bearer ${token}`,
"Content-Type": "application/json", "Content-Type": "application/json",
}, },
body: JSON.stringify(message), body: JSON.stringify(message),