From 611802cbc31f21f3cd2b3e1f267a96950264ae44 Mon Sep 17 00:00:00 2001 From: Wan Qi Chen <495709+wa0x6e@users.noreply.github.com> Date: Sat, 15 Jul 2023 14:32:44 +0900 Subject: [PATCH 1/2] feat: add send event method customization --- src/events.ts | 6 +++--- src/helpers/schema.sql | 1 + 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/events.ts b/src/events.ts index b2604ac..7cf6b33 100644 --- a/src/events.ts +++ b/src/events.ts @@ -68,13 +68,13 @@ export const handleDeletedEvent = async event => { return db.queryAsync(query, [event.id, event]); }; -export async function sendEvent(event, to) { +export async function sendEvent(event, to, method = 'POST') { event.token = sha256(`${to}${serviceEventsSalt}`); event.secret = sha256(`${to}${serviceEventsSalt}`); const headerSecret = sha256(`${to}${process.env.SERVICE_EVENTS_SALT}`); try { const res = await fetch(to, { - method: 'POST', + method, headers: { 'Content-Type': 'application/json', Authentication: headerSecret @@ -92,7 +92,7 @@ const sendEventToWebhookSubscribers = (event, subscribers) => { Promise.allSettled( subscribers .filter(subscriber => [event.space, '*'].includes(subscriber.space)) - .map(subscriber => sendEvent(event, subscriber.url)) + .map(subscriber => sendEvent(event, subscriber.url, subscriber.method)) ) .then(() => console.log('[events] Process event done')) .catch(e => console.log('[events] Process event failed', e)); diff --git a/src/helpers/schema.sql b/src/helpers/schema.sql index 0e10b90..3e75e8c 100644 --- a/src/helpers/schema.sql +++ b/src/helpers/schema.sql @@ -30,6 +30,7 @@ CREATE TABLE subscribers ( id INT NOT NULL AUTO_INCREMENT, owner VARCHAR(256) NOT NULL, url TEXT NOT NULL, + method VARCHAR(5) NOT NULL DEFAULT 'POST', space VARCHAR(256) NOT NULL, active INT(11) NOT NULL, created INT(11) NOT NULL, From c3c62b1bda3245df6c6b167ece2eb932e7ffd4b2 Mon Sep 17 00:00:00 2001 From: Wan Qi Chen <495709+wa0x6e@users.noreply.github.com> Date: Sat, 15 Jul 2023 14:53:22 +0900 Subject: [PATCH 2/2] feat: allow injection of dynamic proposal ID in webhook url --- src/events.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/events.ts b/src/events.ts index 7cf6b33..c9c24bb 100644 --- a/src/events.ts +++ b/src/events.ts @@ -72,8 +72,9 @@ export async function sendEvent(event, to, method = 'POST') { event.token = sha256(`${to}${serviceEventsSalt}`); event.secret = sha256(`${to}${serviceEventsSalt}`); const headerSecret = sha256(`${to}${process.env.SERVICE_EVENTS_SALT}`); + const url = to.replace('[PROPOSAL-ID]', event.id.split('/')[1]); try { - const res = await fetch(to, { + const res = await fetch(url, { method, headers: { 'Content-Type': 'application/json', @@ -83,7 +84,7 @@ export async function sendEvent(event, to, method = 'POST') { }); return res.text(); } catch (error) { - console.log('[events] Error sending event data to webhook', to, JSON.stringify(error)); + console.log('[events] Error sending event data to webhook', url, JSON.stringify(error)); return; } }