Skip to content

Commit

Permalink
feat: data publisher utility to post data to external endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
AdarshRon committed Oct 12, 2023
1 parent 3fe07f3 commit ed270cf
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions nightfall-client/src/utils/dataPublisher.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import axios from 'axios';
import crypto from 'crypto';

export default class DataPublisher {
constructor(options) {
this.options = options || [];
}

async publish(data) {
const promises = this.options.map(async destination => {
switch (destination.type) {
case 'webhook': {
await this.publishWebhook(destination, data);
break;
}

default:
throw new Error('Unknown destination type');
}
});

await Promise.all(promises);
}

publishWebhook = async (destination, data) => {
const headers = {};

if (destination.signingkey) {
const signature = await this.signWebhookPayload(data, destination.signingkey);
headers['X-Signature'] = signature;
}

const response = await axios.post(destination.url, data, { headers });

if (response.status !== 200) {
throw new Error('Webhook failed with status: ', response.status);
}
};

signWebhookPayload = (data, signingKey) => {
const hmac = crypto.createHmac('sha256', signingKey);
hmac.update(JSON.stringify(data));
return hmac.digest('hex');
};
}

0 comments on commit ed270cf

Please sign in to comment.