Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add send event method customization #68

Merged
merged 3 commits into from
Jul 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,14 @@ 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}`);
const url = to.replace('[PROPOSAL-ID]', event.id.split('/')[1]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel this URL format is only useful for our case, others may not use it or we need to find a way to send event type

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True. We can and should add support to inject all the other parameters send in the post body as a query params placeholder. And we should also support head request

try {
const res = await fetch(to, {
method: 'POST',
const res = await fetch(url, {
method,
headers: {
'Content-Type': 'application/json',
Authentication: headerSecret
Expand All @@ -99,7 +100,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 => capture(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
Loading