Skip to content

Commit

Permalink
feat: add send event method customization
Browse files Browse the repository at this point in the history
  • Loading branch information
wa0x6e committed Jul 15, 2023
1 parent 9790211 commit 611802c
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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));
Expand Down
1 change: 1 addition & 0 deletions src/helpers/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 611802c

Please sign in to comment.